# convert a \newcommand to \newenvironment

I have defined a command as follows

\newsavebox{\textsix}

\newcommand{\bottomr}[1]{\sbox{\textsix}{\parbox{11cm}{\begin{flushright} #1
\end{flushright}}}}


which save the parameter (text) of the command \bottomr to a \sbox, That can be used with \usebox{\textsix}

I would like to convert it to an environment like

\newsavebox{\textsix}
\newenvironment{bottomr}{ }{ }


which capture the text inside bottomr environment and save it to a \sbox Approximate like this

\documentclass[a4paper{article}
\usepackage{lipsum}

\begin{document}
\newsavebox{\textsix}
\newenvironment{bottomr}{?}{?}
\begin{bottomr}
\lipsum[1]
\end{bottomr}

\usebox{\textsix}
\end{document}

• Seems that environ package should be able to do what you want. Jul 17 '14 at 17:45

The environment form of \sbox is environment lrbox. Environment minipage can be used instead of the command \parbox. Environment flushright is not necessary, because there is the command form \raggedleft.

\documentclass[a4paper]{article}
\usepackage{lipsum}

\begin{document}
\newsavebox{\textsix}

\newenvironment{bottomr}{%
\begin{lrbox}{\textsix}%
\begin{minipage}{11cm}%
\raggedleft
}{%
\end{minipage}%
\end{lrbox}%
% export box register setting outside the scope of the environment
\global\setbox\textsix=\copy\textsix
}
\begin{bottomr}
\lipsum[1]
\end{bottomr}

\usebox{\textsix}
\end{document}


Following some guidelines from How can I collect the contents of a LaTeX environment in a macro without the grouping?, here is an environ approach:

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

\newsavebox{\textsix}
\NewEnviron{bottomr}[1][11cm]{%
\expandafter\gdef\expandafter\envBODY\expandafter{\BODY}% Store body
\gdef\dolaterbottomr{\savebox\textsix{\parbox{#1}{\raggedleft\strut\envBODY\strut}}}
\aftergroup\dolaterbottomr%
}

\begin{document}
\begin{bottomr}
\lipsum*[2]
\end{bottomr}

\lipsum*[1]\strut

\usebox{\textsix}

\lipsum[3]
\end{document}


I've added an optional argument specifying the width of the \parbox (default is 11cm), as well as some \struts to manage baselines at the start/end of paragraph(s). I don't know whether this is necessary.