146

I want to put a long piece of text in a box, for example

The quick brown fox jumps right over the lazy dog. the quick brown fox 
jumps right over the lazy dog. the quick brown fox jumps right over the lazy 
dog. the quick brown fox jumps right over the lazy dog. the quick brown fox 
jumps right over the lazy dog. the quick brown fox jumps right over the lazy 
dog. the quick brown fox jumps right over the lazy dog. the quick brown fox 
jumps right over the lazy dog.

How can this be done? Any suggestion in brief.

1

6 Answers 6

146

You can use \fbox{...} to put a frame around a \parbox{...} containing your text. Here's an example document. Note the \noindent that prevents the box from being shifted to the right.

\documentclass{article}

\begin{document}

\noindent\fbox{%
    \parbox{\textwidth}{%
        The quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog.
    }%
}
\end{document}
5
  • 9
    Use \linewidth for two column layout Jan 20, 2018 at 2:20
  • 1
    If I was to add this to an enumerate, how could I make sure that the box is shifted to the left margin of the page?
    – gen
    Jan 31, 2018 at 0:14
  • 1
    What do the % symbols do in this? I know it's a comment symbol but it doesn't look like you're commenting anything.
    – Addem
    Jun 9, 2019 at 20:31
  • 1
    The % are added to prevent the creation of unwanted characters, e.g. linebreaks messing with the spacing around the box.
    – Rayne
    Jun 23, 2019 at 15:27
  • 2
    Use \linewidth - 2\fboxsep for two column layout and the box around text to align with text.
    – nj2237
    Dec 28, 2021 at 14:52
82

A minipage environment is a sufficient way to box or group something:

\documentclass{article}
\begin{document}
\begin{minipage}{15em}
  The quick brown fox jumps right over the lazy dog. the quick brown fox 
  jumps right over the lazy dog. the quick brown fox jumps right over the lazy 
  dog. the quick brown fox jumps right over the lazy dog. the quick brown fox 
  jumps right over the lazy dog. the quick brown fox jumps right over the lazy 
  dog. the quick brown fox jumps right over the lazy dog. the quick brown fox 
  jumps right over the lazy dog.
\end{minipage}
\end{document}

Minipage

If you want to frame it, you can put a \fbox command around it. That is,

\fbox{\begin{minipage}{15em}
...
\end{minipage}}

Minipage + fbox

43

The solutions with \fbox and minipage or \parbox all have the defect that the text will be an indivisible object which can't be split across pages. For a one shot application it might be the way to go. Otherwise there's the mdframed package that's highly customizable.

\documentclass{article}
\usepackage[]{mdframed}
\begin{document}

\begin{mdframed}
Test
\end{mdframed}

\end{document}

You can find more information in the mdframed documentation:
https://mirror.ibcp.fr/pub/CTAN/macros/latex/contrib/mdframed/mdframed.pdf

30

enter image description here

\documentclass{article}
\usepackage{xcolor}
\usepackage{lipsum}
\begin{document}
\lipsum[1]

\medskip

\noindent\fcolorbox{red}{yellow}{%
    \minipage[t]{\dimexpr0.48\linewidth-2\fboxsep-2\fboxrule\relax}
        \lipsum[2]
    \endminipage}\hfill
    \fcolorbox{red}{yellow}{%
    \minipage[t]{\dimexpr0.48\linewidth-2\fboxsep-2\fboxrule\relax}
        \lipsum[3]
    \endminipage}

\medskip

\lipsum[4]
\end{document}

Notes:

  • You can adjust the thickness of border and padding of \fcolorbox{<border-color>}{<background-color>}{<contents>} by setting \fboxrule=<value><unit> and \fboxsep=<value><unit>, respectively. Put the setting before invoking \fcolorbox{<border-color>}{<background-color>}{<contents>}. For example: \fboxrule=1pt and \fboxsep=5pt.
  • Use t, c and b options to align the base line of the most top row, the center row and the most bottom row with the surrounding baseline.
5

I think this is the best answer here https://tex.stackexchange.com/a/25926/120007, using mdframed.

But previously, improving upon other answers I've defined a new command which works in single column mode and two column mode:

\documentclass[twocolumn]{article}
\usepackage{lipsum} 
\usepackage[]{mdframed}


% draw a frame around given text
\newcommand{\framedtext}[1]{%
\par%
\noindent\fbox{%
    \parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{#1}%
}%
}

\begin{document}
\lipsum[1]
\section{This uses mdframed}
Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Ut purus elit, vestibulum ut, placerat ac, adi-
piscing vitae, felis.
\begin{mdframed}
A long sentence with multiple words and a sample equation $e^{i\pi}+1=0$.
\end{mdframed}

Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Ut purus elit, vestibulum ut, placerat ac, adi-
piscing vitae, felis.

\section{This uses the new command}
Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Ut purus elit, vestibulum ut, placerat ac, adi-
piscing vitae, felis.
\framedtext{A long sentence with multiple words and a sample equation $e^{i\pi}+1=0$.}

\lipsum[4-7]

\end{document}

The output of the latex above

See also:

3

If you would like to specify the dimensions of the box (width and height) that is supposed to contain the text, please check this question: Fit text into given box by adjusting the fontsize

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .