How to draw a frame box around an arbitrary large piece of text/figures/whatever?

use package mdframed, it also supports a pagebreak

\documentclass{article}

\usepackage[linewidth=1pt]{mdframed}
\usepackage{lipsum}

\begin{document}
\lipsum[2]

\begin{mdframed}
\lipsum[2]
\end{mdframed}

\lipsum[2]

\begin{mdframed}[leftmargin=10pt,rightmargin=10pt]
\lipsum[2]
\end{mdframed}

\lipsum[2]

\begin{mdframed}[leftmargin=-10pt,rightmargin=-10pt]
\lipsum[2]
\end{mdframed}

\end{document}

Although it's an old question, none of the answers mention tcolorbox package which provides an environment for colored and framed text boxes with a heading line.

It declares an environment tcolorbox uses to build colored boxes which by default occupy the line width. These boxes can be completely customized, allow page breaks and can contain any kind of text (regular paragraphs, minipages, tabulars, math, ...). Some little examples look like:

\documentclass{article}
\usepackage{lipsum}
\usepackage{lmodern}
\usepackage{tcolorbox}

\begin{document}
\lipsum[2]

\begin{tcolorbox}
\lipsum[3]
\end{tcolorbox}

\begin{tcolorbox}[sharp corners, colback=green!30, colframe=green!80!blue, title=Another paragraph with title]
\lipsum[2]
\end{tcolorbox}

\begin{tcolorbox}[width=6cm]
I'm sure you know that 
\[
\sin^2 x + \cos^2 = 1
\]
Don't you?
\end{tcolorbox}

\end{document}

enter image description here

tcolorbox package also offers the command \tcbox which builds a box adjusted to the contents. This kind of boxes are not breakable.

\documentclass{article}
\usepackage{lipsum}
\usepackage{lmodern}
\usepackage{tcolorbox}

\begin{document}

\tcbox{This is a sentence}

\tcbox[colback=blue!30, colframe=blue!30!black]{\begin{tabular}{cc}A & B \\ C & d\end{tabular}}

\tcbox[sharp corners, colframe=red, colback=red!20!blue!30]{\includegraphics[width=4cm]{example-image-A}}

\end{document}

enter image description here

Information about tcolorbox and all of its bells and whistles can be found in CTAN


You need to use a \parbox or similar inside the \fbox for items that don't work For example

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\fbox{%
  \parbox{\textwidth}{%
    \begin{center}
      \lipsum[1-3]
    \end{center}
  }%
}
\end{document}

but for minipage situation things work just fine:

\documentclass{article}
\usepackage{lipsum}

\begin{document}
\fbox{%
  \begin{minipage}{\textwidth}
    \lipsum[1-3]
  \end{minipage}
}
\end{document}

(The lipsum package here is used purely to provide the filler text: it is not needed for the solution.)

Tags:

Framed