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}
Result
The compiled example

Open in the editor

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.}}
Result
The compiled example

Open in the editor

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}
Result
The compiled example

Open in the editor

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}
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages