4

I tried to use a \savebox around the \BODY macro of \NewEnviron. Can I keep the linebreaks somehow (they are gone, in the MWE)?

\documentclass{article}

\usepackage{environ}

\newsavebox{\mybox}

\NewEnviron{myenv}{%
    \savebox{\mybox}{\BODY}
    \usebox{\mybox}
}

\begin{document}

\begin{myenv}
A

B
\end{myenv}

\end{document}

The problem is probably independent from the environ package but that is what I'm trying to use.

Or: Is there a way to determine the vertical and horizontal size of the \BODY?

masu
  • 6,571

1 Answers1

3

There's no notion of “natural width” of a text, when paragraphs are being built: TeX will use what you specify or \linewidth in case you don't.

However there's the varwidth environment that can help:

\documentclass{article}
\usepackage{environ,varwidth}

\newsavebox{\mybox}

\NewEnviron{myenv}{%
  \sbox{\mybox}{\begin{varwidth}{\linewidth}\BODY\end{varwidth}}%
  The box is \texttt{\the\wd\mybox} wide
  and \texttt{\the\dimexpr\ht\mybox+\dp\mybox} high.
}

\begin{document}

\begin{myenv}
A

B
\end{myenv}

\end{document}

It depends on you what to do with those dimensions.

enter image description here

Using \NewEnviron is not necessary:

\documentclass{article}
\usepackage{varwidth}

\newsavebox{\mybox}

\newenvironment{myenv}
 {\begin{lrbox}{\mybox}
  \begin{varwidth}{\linewidth}}
 {\end{varwidth}%
  \end{lrbox}%
  The box is \texttt{\the\wd\mybox} wide and
  \texttt{\the\dimexpr\ht\mybox+\dp\mybox} high.
}

\begin{document}

\begin{myenv}
A

B
\end{myenv}

\end{document}
egreg
  • 1,121,712