When I want to put formulas into boxes, I like the box to adapt to the length of my formula. For example, I don't want to put $1+1=2$
in a very large box. To address this, I use the command \encadrer
(it requires the calc
package and the ifthen
package) which is included in the preamble of the following minimal example :
\documentclass[10pt,a4paper]{article}
\usepackage{ifthen}
\usepackage{calc}
%\encadrer:
\newlength\lenbox
\newcommand{\encadrer}[1]{
\linethickness{2pt}
\setlength\lenbox{\widthof{#1}}
\begin{center}
\fbox{
\begin{minipage}{\lenbox}
#1
\end{minipage}
}
\end{center}
}
\begin{document}
\encadrer{$1+1+1=3$}
\end{document}
But this command doesn't work for multiple lines. For example, if I put
\encadrer{$1+1+1=3$ \\ and \\ $1+1=2$}
in the previous example, the box becomes larger. In this example, I would like the length of the box to be max(\widthof{$1+1+1=3$}
, \widthof{$1+1=2$}
). I thought of something :
If the argument of \encadrer
is of the form : string1 \\ string2 \\ ... \\ stringN
, I can use the xstring
package to extract all the strings between \\
and use a loop to define something like that : (in pseudocode)
set lenbox to 0
for i from 1 to N
set lenbox to max(lenbox,\widthof{stringi})
end for
But it feels complicated and I wonder if there is another solution. If I on the right track, can someone help me convert this pseudocode to LaTeX code ? Thank you for your help !
calc
.\setlength\lenbox{\widthof{#1}}
can be replaced by\settowidth\lenbox{#1}
.