96

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

}
Karlo
  • 3,257
  • 1
    Welcome to TeX.sx! Your question was migrated here from [so]. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. – Torbjørn T. Nov 29 '11 at 15:03
  • possible duplicate of: http://tex.stackexchange.com/questions/11484/how-to-draw-a-frame-box-around-an-arbitrary-large-piece-of-text-figures-whatever – 0 _ Apr 13 '16 at 05:34

7 Answers7

128

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}

Peter Grill
  • 223,288
  • 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 07:42
  • NOTE: The blank lines before \begin{align} and _after \end{align} should be eliminated. – Peter Grill Mar 09 '17 at 00:24
  • How to change the shade of box – user716881 Sep 08 '20 at 18:35
  • 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. – Peter Grill Sep 09 '20 at 03: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 03 '20 at 11:38
  • @shevket: Yep, the package is growing. I strted of with mdframed and have slowly been dwithcing over to tcolorbox as it is a lot more flexible and has ongoing support. For simple cases, tcolrobox is pretty straight forward. Then, when you need to tweak somehting you can look for the required settings. I find that way of learnign works for me. – Peter Grill Dec 03 '20 at 17:26
  • When I do this around the first paragraph in en enumerate environment, the number ends aligned vertically with the middle of the paragraph instead of with the top of it. How can I change so that it aligns with the top? – StrawberryFieldsForever Feb 28 '21 at 13:44
  • @StrawberryFieldsForever: Best to post a new question. Please include a MWE including \documentclass and the appropriate packages that reproduces the problem. – Peter Grill Feb 28 '21 at 21:06
  • @PeterGrill Thanks. I ended up using this solution. – StrawberryFieldsForever Feb 28 '21 at 21:46
66

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

Torbjørn T.
  • 206,688
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}
Chel
  • 6,110
9

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

7

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

Troy
  • 13,741
Diegis
  • 1,107
  • 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). – Torbjørn T. Nov 29 '11 at 15:37
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.

Andrew Swann
  • 95,762
Leonid
  • 151
  • 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
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}