35

The command fbox{...} can be used to draw a box around a piece of text, and apparently also a figure. However, I've not been able to use it if the "text" contains environments such as minipages and center.

So, how does one draw a framed box around an arbitrary text which contains more than just text, such as figures, tables, environments, etc.?

gablin
  • 17,006

4 Answers4

26

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

Ignasi
  • 136,588
24

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}
8

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.)

Seamus
  • 73,242
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
4

Here is one using eplain:

\input eplain
\vsize=4.3in
\boxit{\halign{&\tabskip1em#\hfil\cr Some stuff & in a table & just to show \cr
  \noalign{\smallskip\hrule\smallskip}
  the frame & around the table \cr}}
\smallskip
\boxit{\vbox{\hsize=2in% just to keep the image smaller
  Lorem ipsum dolor sit amet. This could go on and on and on until we hit the
  edge of the page at which point there should be automatically another line}}
\smallskip \boxitspace=2em
\boxit{\vbox{\hsize=2in\item{1.} A list \item{2.} With list items
  \item{3.} And 2em padding}}
\smallskip \boxitspace=5pt
\boxit{\XeTeXpicfile "test-pattern.jpg"}
\footline={\boxit{\hbox{\tenrm\folio}}\hss}
\bye
% the boxit macro from eplain for completeness
% modified for stand-alone use (ehrule to hrule; evrule to vrule)
\newdimen\boxitspace \boxitspace = 3pt
\long\def\boxit#1{%
  \vbox{%
    \hrule
    \hbox{%
      \vrule
      \kern\boxitspace
      \vbox{\kern\boxitspace \parindent = 0pt #1\kern\boxitspace}%
      \kern\boxitspace
      \vrule
    }%
    \hrule
  }%
}%

enter image description here

morbusg
  • 25,490
  • 4
  • 81
  • 162