97

I am looking for something like \fbox but it seems that you cannot use it when a math environment is included. However its not only a math environment (then you could use \boxed I know) but also text, I need to be in a frame.

example:

\rdmframeboxaroundwhatever{

blablabla

\begin{align}
Formula of the universe
\end{align}


Hoaray

}
2

7 Answers 7

127

You can also use tcolorbox:

enter image description here

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tcolorbox}

\begin{document}
\begin{tcolorbox}

blablabla

\begin{align}
E &= mc^2 & \text{Formula of the universe}
\end{align}


Hoaray

\end{tcolorbox}

\end{document}
10
  • Can we change the color and embrossment of the box..I expect the box must me like our "add comment" box, and letter must be shown like that.. Can I do?@peter
    – David
    Dec 10, 2015 at 14:32
  • @David: Sure, pretty much everything can be customized. The tcolorbox manual should have the desired info. If you are unable to locate the desired info I would suggest you post a new question as your question is about customizing a tcolorbox which is independent of if it is around math or text. In the question describe or show exactly what you want and include a fully compilable MWE. Dec 11, 2015 at 7:42
  • NOTE: The blank lines before \begin{align} and _after \end{align} should be eliminated. Mar 9, 2017 at 0:24
  • 1
    @user716881: Not sure exactly what you want, but you can try using \begin{tcolorox][colback=yellow] for instance. There are numerous other options, so you should search for tcolorbox on this site, see the tcolorbox documentation or post a new question regarding a specfic style you want to see. Sep 9, 2020 at 3:43
  • 1
    Cannot believe that the tcolorbox documentation is 530pp long. I hoped to have a quick feel of the package by looking at the "examples" pdf, but that's 8pp of colored boxes... without the source code! I'll use the mdframed package instead, for its neat and simple documentation. I'm still so impressed by latex's complexity for doing such trivial things as style={background-color: #fba; border: 1px solid #432; padding: 20px}... :'(
    – shevket
    Dec 3, 2020 at 11:38
65

You could take a look at mdframed, which also allows pagebreaks, and allows you to customize the look of the frame. A simple example, which puts a box similar to \fbox around the content.

\documentclass{article}
\usepackage{mdframed}
\usepackage{lipsum} % for creating dummy text

\begin{document}
\begin{mdframed}
\lipsum[1]
\begin{equation}
 f(x) = \sin(x)
\end{equation}
\lipsum[2]
\end{mdframed}
\end{document}

A second example, showing some more of what the package can do. See the manual for the full story.

\documentclass{article}
\usepackage[framemethod=TikZ]{mdframed}
\usepackage{lipsum}
\mdfdefinestyle{MyFrame}{%
    linecolor=blue,
    outerlinewidth=2pt,
    roundcorner=20pt,
    innertopmargin=\baselineskip,
    innerbottommargin=\baselineskip,
    innerrightmargin=20pt,
    innerleftmargin=20pt,
    backgroundcolor=gray!50!white}

\begin{document}

\begin{mdframed}[style=MyFrame]
\lipsum[1]
\begin{equation}
 f(x) = \sin(x)
\end{equation}
\lipsum[2]
\end{mdframed}
\end{document}

enter image description here

4
  • How do I control the width? At the moment, it's taking the entire horizontal space of the page.
    – hola
    Sep 8, 2020 at 5:35
  • 2
    @xxx--- userdefinedwidth=10cm Sep 8, 2020 at 5:57
  • @TorbjørnT. where do I put that?
    – S P Sharan
    Jan 14, 2022 at 11:08
  • @SPSharan Along with the other settings in the definition of the MyFrame style. Jan 14, 2022 at 11:17
17

Wrap the math and text in a minipage, then put that in the fbox:

\documentclass{minimal}

\begin{document}
\fbox{%
\begin{minipage}{4 in}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed fringilla
purus bibendum odio ultricies non bibendum massa condimentum. Morbi
scelerisque pulvinar quam, sed porta ipsum blandit ac.
\begin{equation}
    \frac{Lorem}{Ipsum}=Do^2lr+\int_i^t\,d(amet)
\end{equation}
Sed felis arcu, pretium ut vulputate at, feugiat vitae orci. In hac
habitasse platea dictumst. Integer mollis vulputate dui. Fusce
eget sollicitudin purus.
\end{minipage}}
\end{document}
2
  • 3
    This won't break across pages whereas the mdframed solution will.
    – Seamus
    Nov 29, 2011 at 16:30
  • \fbox{ worked with Geogebra (linux) :)
    – khaz
    Jun 22 at 20:48
10

I'm actually a TeX noob, so use this with caution, but I saw an approach in this answer which makes the box tight, that is, it's exactly as wide and tall as the equation makes it be:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\pagestyle{empty}

\usepackage{collectbox}

\makeatletter
\newcommand{\mybox}{%
    \collectbox{%
        \setlength{\fboxsep}{1pt}%
        \fbox{\BOXCONTENT}%
    }%
}
\makeatother

\begin{document}
\mybox{$\left(\nabla_X Y\right)^k = X^i (\nabla_i Y)^k = X^i \left(\frac{\partial Y^k}{\partial x^i} + \Gamma^k{}_{im} Y^m\right)$}
\mybox{$\nabla_{\mathbf{u}_i}\mathbf{u}_j = \omega^k{}_{ij}\mathbf{u}_k$}
\end{document}

Output:

enter image description here

2
  • Do you happen to know how to change the border colors for the box?
    – M--
    Jun 13, 2018 at 21:27
  • @Masoud I'm afraid not. Jun 14, 2018 at 14:39
8

My combination of a frame box and math (trying to align the formula vertically as well as horizontally - give more space when the math gets really tall and allow more room horizontally for better visual effect):

\framebox{

\parbox[t][1.0cm]{4.50cm}{

\addvspace{0.2cm} \centering 

$ p \;=\; -\, \dfrac{17}{23}\;;\; \quad q \;=\; \dfrac{10}{23} $ 

} 

}\\

enter image description here

1
  • 1
    Tip: You can mark code sch as I did in my edit by either indenting it with four spaces, or selecting the block and clicking the button marked {} above the text field (keyboard shortcut, Ctrl + K). Nov 29, 2011 at 15:37
6

You can put the text in a \mbox and use \boxed. Example:

\begin{document}
$\boxed{\sin \theta \mbox{blablablablablabla}}$
\end{document}

This should produce a box around both sin theta and blablablablablabla. Hope this helps.

1
  • 2
    Welcome to TeX.SX! As it stands your answer is not compilable, \boxed is not a standard LaTeX command. Also this does not answer the question, which is about boxes around material including displayed mathematics. Feb 23, 2015 at 13:58
0
    \begin{document}
     \color{red}
      \begin{center}
        \framebox{


       \parbox[t][1.0cm]{4.50cm}{


       \color{blue}
       \begin{tabular}{c}
         \\
       $ E^2=p^2c^2+m_{0}c^4 $ 
       \end{tabular}

         } 

        }
       \end{center}

This does not use any packages and we have full freedom to play with it. \end{document}

You must log in to answer this question.