Tx · LaTeX & TikZ

How to Add a Border or Frame Around a Box in LaTeX

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose the box type

    Use \fbox for short inline content and a framed minipage for text that needs line wrapping.

  2. 2. Set frame spacing

    Adjust \fboxsep for padding and \fboxrule for border thickness.

  3. 3. Account for the border

    Make the inner minipage narrower than the available line width so the frame fits.

  4. 4. Use a box package

    Choose framed or tcolorbox when the frame needs to span pages or have advanced styling.

LaTeX’s built-in \fbox command draws a rectangular frame around its contents. It is a good choice for short text or an existing box. For a paragraph, put a minipage inside the frame so the text can wrap. The example below defines a reusable environment and shows how to adjust the border and padding.

Use \fbox for short content

The basic syntax is:

\fbox{Content inside the frame}

For example, \fbox{$x^2+y^2=1$} frames a mathematical expression. The command creates a box in horizontal mode; it does not automatically wrap a long paragraph. If the content is too wide, the framed box may extend into the margin.

Two lengths control the appearance:

  • \fboxsep is the space between the content and the frame.
  • \fboxrule is the thickness of the frame.

Set them locally around a frame to avoid changing other boxes in the document:

{\setlength{\fboxsep}{6pt}%
 \setlength{\fboxrule}{0.8pt}%
 \fbox{A framed phrase}}

The braces group the settings, so they apply only to this material. The default values are usually fine for a quick border; increase \fboxsep for more breathing room or \fboxrule for a heavier line.

Frame a paragraph with a minipage

A minipage gives paragraph text a fixed width and lets it wrap inside the frame. The minipage must be narrower than the available line width: \fbox adds the rule and padding on both sides. The following complete example defines an environment for framed paragraphs.

\documentclass{article}

\newenvironment{framedtext}
  {\par\noindent
   \setlength{\fboxsep}{8pt}%
   \setlength{\fboxrule}{0.6pt}%
   \fbox\bgroup
   \begin{minipage}{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}}
  {\end{minipage}\egroup\par}

\begin{document}

\begin{framedtext}
This paragraph wraps within the frame. The minipage width is reduced
by the frame thickness and the padding on each side, so the outside of
its frame fits within the available line width.
\end{framedtext}

\end{document}

The environment opens an \fbox and places a minipage inside it. Its width is \linewidth minus twice the padding and twice the rule thickness. Using \linewidth, rather than \textwidth, lets the box adapt to the current text width—for instance, inside a list. The environment’s grouping keeps its length settings local.

You can change the values of \fboxsep and \fboxrule in the environment definition. For example, a larger \fboxsep adds space between the paragraph and its border. If the framed box should be narrower than the full line, subtract an additional amount from the minipage width.

When to use a framing package

A standard \fbox cannot split its contents across pages. For a long paragraph that may need to continue on the next page, use a package designed for framed text, such as tcolorbox or framed. For example, tcolorbox supports options for border color, background color, titles, and breakable boxes. With tcolorbox, a simple framed paragraph can be written as:

\usepackage[most]{tcolorbox}

\begin{tcolorbox}[colback=white,colframe=black,breakable]
A longer paragraph goes here. The breakable option allows the box to
continue across pages when the package's breakable library is loaded.
\end{tcolorbox}

Place the package declaration in the preamble. The [most] option loads the package’s commonly used libraries, including support for breakable boxes. For a small, unbroken box, adding a package is unnecessary; \fbox is simpler.

Common mistakes

  • Expecting \fbox to wrap a paragraph: It frames a box, not a paragraph layout. Use a minipage or a framing package for wrapped text.
  • Using the full line width for the minipage: The border and padding add width outside the minipage. Subtract 2\fboxsep + 2\fboxrule to fit the frame to \linewidth.
  • Changing frame lengths globally by accident: Group local settings with braces or put them inside an environment, as in the example.
  • Using \fbox for a multi-page box: A single \fbox cannot break across pages. Use a package with breakable-box support instead.

References

  1. The LaTeX2e unofficial reference manual
  2. The tcolorbox package documentation

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.