66

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

}
81

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}
  • 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
52

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

10

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}
  • 3
    This won't break across pages whereas the mdframed solution will. – Seamus Nov 29 '11 at 16:30
8

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

  • 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
5

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.

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

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

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy