3

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?

  • 2
    Unrelated to the question: you should move \newsavebox{\mybox} outside of the environment's definition (try using the environment twice to see why) – clemens Nov 10 '13 at 16:02
  • The line breaks are lost because they're converted into spaces (or \par) as part of the tokenization stage. Moreover \savebox typesets its contents in horizontal mode, so you'll get a long line. Maybe you can expand your question telling what's your aim. – egreg Nov 10 '13 at 16:05
  • @egreg. I'm trying to determine the vertical and horizontal size of the output of \BODY. \savebox seemed to be the closest to this aim. – masu Nov 10 '13 at 16:13
  • There's no "natural" horizontal size: you have to specify what you want or it will be the current \linewidth. – egreg Nov 10 '13 at 16:15
  • 1
    The adjustbox package should help you here. You need to use the varwidth key to get a variable width. An alternative is to use the lrbox environment with a varwidth environment inside it (needs the varwidth package). – Martin Scharrer Nov 10 '13 at 16:16
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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.