8

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 !

2
  • BTW, you do not need package calc. \setlength\lenbox{\widthof{#1}} can be replaced by \settowidth\lenbox{#1}. Commented Aug 26, 2013 at 20:51
  • I didn't know that ! Thank you !!
    – pitchounet
    Commented Aug 26, 2013 at 20:52

3 Answers 3

11

The varwidth package's similarly-named varwidth environment provides this functionality by default, setting a box of "natural width" if the width is smaller than what is specified:

enter image description here

\documentclass[10pt,a4paper]{article}
\usepackage{varwidth}% http://ctan.org/pkg/varwidth

%\encadrer:
\newcommand{\encadrer}[1]{{%
  \centering
  \fbox{%
    \begin{varwidth}{\dimexpr\linewidth-2\fboxsep-2\fboxrule}
      \centering #1%
    \end{varwidth}%
  }
  \par}%
}


\begin{document}

\encadrer{$1+1+1=3$}

\encadrer{$1+1+1=3$ \\ and \\ $1+1=2$}

\end{document}
1
  • Thank your very much ! I didn't know this package. This is exactly what I was looking for.
    – pitchounet
    Commented Aug 26, 2013 at 20:51
5

You can avoid all measurements:

\documentclass[10pt,a4paper]{article}
\usepackage{varwidth}

%\encadrer:
\newcommand{\encadrer}[1]{%
  \begin{center}
  \setlength{\fboxrule}{2pt}
  \fbox{%
    \begin{varwidth}{\textwidth}
    #1
    \end{varwidth}%
  }
  \end{center}
}


\begin{document}

\encadrer{$1+1+1=3$}

\encadrer{$1+1+1=3$ \\ and \\ $1+1=2$}

\end{document}

Note that the parameter for the frame thickness is \fboxsep; setting it inside the center environment ensures it will revert to the previous value. However, 2pt is really too much, as you'll see in the image.

Note also that I protected some end-of-lines in order to avoid spurious spaces. It's not necessary to have % after \begin{varwidth}{\textwidth} and after #1 because it those positions the spaces will be ignored, but they aren't in \fbox, in general.

enter image description here

4

Your \encadrer command includes a lot of extra white space due to missing % at the ends of lines, apart from the problem with multiple line entries.

I think you want

\newcommand{\encadrer}[1]{\fbox{$\begin{array}{@{}c@{}}#1\end{array}$}}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .