# How to put a framed box around text + math environment

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

}


You can also use tcolorbox:

## 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}

• 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 '15 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. – Peter Grill Dec 11 '15 at 7:42
• NOTE: The blank lines before \begin{align} and _after \end{align} should be eliminated. – Peter Grill Mar 9 '17 at 0:24
• How to change the shade of box – user716881 Sep 8 at 18:35
• @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. – Peter Grill Sep 9 at 3:43

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]
$$f(x) = \sin(x)$$
\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]
$$f(x) = \sin(x)$$
\lipsum[2]
\end{mdframed}
\end{document}


• How do I control the width? At the moment, it's taking the entire horizontal space of the page. – hola Sep 8 at 5:35
• @xxx--- userdefinedwidth=10cm – Torbjørn T. Sep 8 at 5:57

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.
$$\frac{Lorem}{Ipsum}=Do^2lr+\int_i^t\,d(amet)$$
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}

• This won't break across pages whereas the mdframed solution will. – Seamus Nov 29 '11 at 16:30

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:

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

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}{

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

}

}\\


• 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). – Torbjørn T. Nov 29 '11 at 15:37

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.

• 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. – Andrew Swann Feb 23 '15 at 13:58
    \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}