# Framing “standalone” class output with “framed” and others

I've used framed package to conveniently enclose all of my equations (including multi-lined ones; which is something no other package I ran across could do; at least with such simplicity) into an outline frame. But, unfortunately, it doesn't seem to work with standalone class. So I would appreciate a replacement or a tweak that would make it work in this circumstances.

Here is a piece of code that I had used before standalone class:

\documentclass[fleqn]{article}
\usepackage{amsmath}
\usepackage{framed}
\begin{document}
\begin{framed}
\begin{aligned} 2^{1}&=\binom{1}{0}+\binom{1}{1}\\ 2^{2}&=\binom{2}{0}+\binom{2}{1}+\binom{2}{2} \end{aligned}
\end{framed}
\end{document}


If you replace

\documentclass[fleqn]{article}


with

\documentclass[fleqn]{standalone}


the typeset process will stop with the following message

(texlive/2016/texmf-dist/tex/latex/framed/framed.sty)
(./untitled-1.aux)
! Missing \endgroup inserted.
<inserted text>
\endgroup
l.5 \begin{framed}


If you remove \begin{framed} and \end{framed}, however, it will work.

I'm afraid there is no package on par with framed in terms of simplicity but I hope I'm wrong. Your help is appreciated.

Thank you.

• What do you mean by 'on par'? The package tcolorbox, e.g., is (in my opinion) way beyond 'par' compared to framed. If you replace 'framed' with 'tcolorbox' in each instance, your example will work. – jon Oct 15 '16 at 19:22
• Thank you for your response. I have just tried to use "tcolorbox" and it does look better in a sense that it works with "standalone" class without specifying the "varwidth" and it automatically adjusts spacing almost equally at all sides of the box. The only problem with "tcolorbox" is that its right margin has a little more spacing than the left. I wonder if there is something that interferes with the correct spacing. I have to say, if the right margin problem is taken care of and you post an answer (not a comment) with such example, I will have to mark it as the answer. – Ben Oct 15 '16 at 20:01

Use the \fbox command instead of framed.

\documentclass[fleqn]{standalone}
\usepackage{amsmath}
\begin{document}
\fbox{%
\begin{aligned} 2^{1}&=\binom{1}{0}+\binom{1}{1}\\ 2^{2}&=\binom{2}{0}+\binom{2}{1}+\binom{2}{2} \end{aligned}%
}
\end{document}


• Oh, this is perfect. Thank you very much. I think Jon's suggested "tcolorbox" can be tweaked to work just as well. So anyone who ran into the same problem as I might want to take a look at that option too. Thank you everyone. – Ben Oct 15 '16 at 20:25
• @Ben You can use most boxing commands if you omit the framed environment. As I said in my other comment, framed extends the real width to text width, so other boxes around it cannot determine the width of the equations anymore. In the beginning, your posting looked like keeping framed was a requirement. – gernot says Reinstate Monica Oct 15 '16 at 20:31

Use the varwidth option of the standalone class.

\documentclass[fleqn,varwidth]{standalone}
\usepackage{amsmath}
\usepackage{framed}
\begin{document}
\begin{framed}
\begin{aligned} 2^{1}&=\binom{1}{0}+\binom{1}{1}\\ 2^{2}&=\binom{2}{0}+\binom{2}{1}+\binom{2}{2} \end{aligned}
\end{framed}
\end{document}


If you want the text width to be smaller, you can specify also a width with the option, like varwidth=5cm.

• Thank you for your response. Looks like at least you have "framed" typeset successfully with "standalone" class (with no error messages popping-up). But the whole idea of me using "standalone" class was to be able to automatically clip all the unnecessary spacing around the equations. It does not seem like setting the width manually would be effective at all for this purpose. Can we have "framed" enclose just the amount of space that is displayed by "standalone" without it adding extra spacing? – Ben Oct 15 '16 at 19:40
• @Ben The problem is neither standalone nor tcolorbox nor varwidth, but the framed environment. It makes a box of \textwidth; after that anything that you wrap around it only sees the textwidth but no longer the actual width of aligned. – gernot says Reinstate Monica Oct 15 '16 at 20:16

Just for the alternative, here's a tcolorbox solution. Note that you can globally set options using \tcbset and/or locally for each colour box. The hbox option (a shortcut for the option, capture=hbox), for example, sets the size of the box to the dimensions of the content. (This is the default for the related command \tcbox.)

\documentclass[fleqn, 12pt]{standalone}

\usepackage{amsmath}

% Version: 3.95 (2016/10/21)
\usepackage{tcolorbox}[2016/10/21]

\newtcolorbox{framed}{hbox, left=0mm, right=0mm}
\newtcbox{\fhbox}{left=0mm, right=0mm}

\begin{document}
\Huge% just for sake of example

\begin{framed}
\begin{aligned} 2^{1}&=\binom{1}{0}+\binom{1}{1}\\ 2^{2}&=\binom{2}{0}+\binom{2}{1}+\binom{2}{2}\\ \end{aligned}
\end{framed}

\fhbox{%
\begin{aligned} 2^{1}&=\binom{1}{0}+\binom{1}{1}\\ 2^{2}&=\binom{2}{0}+\binom{2}{1}+\binom{2}{2} \end{aligned}% <-- the % is needed because this is not an environment
}

\end{document}


For versions of tcolorbox < 3.95

The one problem seems to be that there can be an issue with the hbox option in the environment. However, it appears that one can avoid this through the addition of a % at the end of the equation.

\documentclass[fleqn]{standalone}

\usepackage{amsmath}
\usepackage{tcolorbox}

\newtcolorbox{framed}{hbox, left=0mm, right=0mm}
\newtcbox{\fhbox}{left=0mm, right=0mm}

\begin{document}

\begin{framed}
\begin{aligned} 2^{1}&=\binom{1}{0}+\binom{1}{1}\\ 2^{2}&=\binom{2}{0}+\binom{2}{1}+\binom{2}{2}\\ \end{aligned}% <-- Note the % character is needed
\end{framed}

\fhbox{%
\begin{aligned} 2^{1}&=\binom{1}{0}+\binom{1}{1}\\ 2^{2}&=\binom{2}{0}+\binom{2}{1}+\binom{2}{2} \end{aligned}%
}


In order to show why using a variant of \tcbox gives more balanced spacing, one can use eso-pic to help see the differences:

\documentclass[fleqn]{article}

\usepackage[grid, gridunit=pt,
gridcolor=red!20,
subgridcolor=blue!20]{eso-pic}

\usepackage{tcolorbox}
\newtcolorbox{framed}{hbox, left=0mm, right=0mm}
\newtcbox{\fhbox}{left=0mm, right=0mm}

\begin{document}
\parindent0pt

FBOX:\\
\fbox{\hbox{Dummy text to show text mode}}

\bigskip

\verb+\newtcbox{\fhbox}{left=0mm, right=0mm}+
\fhbox{Dummy text to show text mode}

\verb+\newtcolorbox{framed}{hbox, left=0mm, right=0mm}+
\begin{framed}
Dummy text to show text mode
\end{framed}

\verb+\tcbox+
\tcbox{Dummy text to show text mode}

\end{document}


• Thank you. May I ask you to please help me with setting the right margin of "tcolorbox" in your example equal to the left margin? There is too much space on the right. – Ben Oct 15 '16 at 20:06
• I have tried to use "\tcboxfit" to fit the contents (that is what I need) but it gave me an error message. I think I placed it incorrectly or the type of the equation is incompatible. I will have to look into the documentation more thoroughly. – Ben Oct 15 '16 at 20:18
• I've updated the answer. Hopefully this is more helpful. – jon Oct 15 '16 at 21:09
• I reported the issue a few days ago: github.com/T-F-S/tcolorbox/issues/13 – jon Oct 20 '16 at 2:21
• I've updated the answer because the package has been updated and the original issue is no longer a problem. – jon Oct 25 '16 at 16:34

You can try this but it looks self-defeating regarding page breaks (but standalone class is special with respect to page breaks too).

Anyway, the answer by @gernot is much better. But I just wanted to give framed its try too.

\documentclass[fleqn,varwidth]{standalone}
\usepackage{amsmath}
\usepackage{framed, color}%\setlength{\FrameSep}{0pt}
\setlength{\FrameRule}{5pt}
\begin{document}
\begin{framed}
\setbox0\hbox{\begin{aligned} 2^{1}&=\binom{1}{0}+\binom{1}{1}\\ 2^{2}&=\binom{2}{0}+\binom{2}{1}+\binom{2}{2} \end{aligned}}\hsize\wd0\box0
\end{framed}
\end{document}


• Thank you for your response. Looks like the old "framed" does a good job yet again. Although I would want to reduce the spacing a little. – Ben Oct 15 '16 at 20:37
• with \setlength{\FrameSep}{0pt} you get tightest possible fit (but same in @gernot answer with \fboxsep rather) – user4686 Oct 15 '16 at 20:39
• These are great answers. If only they allowed to mark more than one as solution I would definitely do so. Thanks everyone. – Ben Oct 15 '16 at 20:41