Draw a box around text in LaTeX (fbox, tcolorbox)
\fbox{text} draws a frame around a short piece of text on one line. For a whole paragraph, put a \parbox inside the \fbox, or use the tcolorbox environment for coloured boxes with a title.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{xcolor}
\usepackage{tcolorbox}Frame a word or a short line
\fbox is as wide as its content. \framebox[4cm][l]{text} takes a fixed width and the position of the text in it: l, c or r.
\fbox{Important}
\framebox[4cm][l]{left}Frame a paragraph
Text inside \fbox does not wrap. A \parbox of a given width wraps it. \fboxsep is the gap between text and frame (3 pt by default) and \fboxrule the thickness of the line (0.4 pt).
\setlength{\fboxsep}{8pt}
\setlength{\fboxrule}{1pt}
\noindent\fbox{\parbox{0.8\linewidth}{This paragraph is wrapped
inside the frame. The box takes 80 percent of the line width,
plus the gap and the line on both sides.}}Coloured background
\colorbox{yellow}{text} fills the background. \fcolorbox{red}{white}{text} takes the colour of the frame first and the background second. Both come from xcolor.
\colorbox{yellow}{Marked}
\fcolorbox{red}{white}{Warning}A box with a title
A tcolorbox is as wide as the text line and wraps its content. colback is the background colour, colframe the colour of the frame and title bar, and title the text in the bar.
\begin{tcolorbox}[title=Note, colback=blue!5, colframe=blue!60!black]
The box takes the full line width and wraps the text.
\end{tcolorbox}Pitfalls
- A long text in
\fboxwithout\parboxruns out of the page and gives an overfull hbox warning. - A
tcolorboxstays on one page. To let it continue on the next page, add\tcbuselibrary{breakable}to the preamble and the optionbreakableto the box. - For a frame around a formula, use
\boxed, see Box around an equation in LaTeX.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
tcolorbox - Package
mdframed - Package
xcolor - How-to Box around an equation in LaTeX
- Error Overfull \hbox (263.664pt too wide) in paragraph at lines 3--6