# A macro that behaves like a font-changing command (affects the rest of the current group)

Many LaTeX packages provide an interface to modify the typesetting of certain elements with the standard "parameterless" font commands, such as \bfseries, \sffamily or \color{red}. All of these commands are "parmeterless" in the sense, that they do not get a macro parameter of the content to format, but have an effect until the end of the current group. So:

Standard, but we {switch to \bfseries bold and \color{red} red until the end} of the group.


gives us:

However, what do do if the macro one would like to apply for a certain formatting is not "parameterless", but a "parameterless" macro is expected? The MWE demonstrates this problem with \fbox and listings:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listings,xcolor,beramono}

\lstset{language=C,basicstyle=\ttfamily,keywordstyle=\bfseries}

\lstset{moredelim=**[is][\color{red}]{<}{>}}  % works
\lstset{moredelim=**[is][\mystyle]{@}{@}}     % hmmm...

\def\mystyle{\fbox} % how to get the parameter to for \fbox?

\begin{document}
\begin{lstlisting}[gobble=4]
int main() {
if( printf ("Hello, <tex.se!") == 0 )
<return -1;> // error!
@return 0;@   // success
}
\end{lstlisting}
\end{document}


As you can see, a small \fbox appears at the begin of the affected line, but the goal is that it should be around the return 0;. Is it possible, to define \mystyle in a way that it retrieves everything until the end of the group and can then (robustly) pass this to \fbox?

-

LaTeX2e introduced lrbox an environment form of \sbox preciously to address this case. the usrguide.tex that comes with LaTeX has this example for making a framed minipage

\newsavebox{\fmbox}
\newenvironment{fmpage}[1]
{\begin{lrbox}{\fmbox}\begin{minipage}{#1}}
{\end{minipage}\end{lrbox}\fbox{\usebox{\fmbox}}}


Although of course for the particular example of putting frames around things there are many packages now with more elaborate variants of this.

I don't really know the listings package, there may be an easier way to get environment syntax used, but this appears to work:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listings,xcolor,beramono}

\lstset{language=C,basicstyle=\ttfamily,keywordstyle=\bfseries}

\lstset{moredelim=**[is][\color{red}]{<}{>}}  % works
\lstset{moredelim=**[is][\mystyle]{@}{@}}     % hmmm...

\newsavebox{\flrbx}
\newenvironment{flrbox}
{\begin{lrbox}{\flrbx}}
{\end{lrbox}\fbox{\usebox{\flrbx}}}

\def\mystyle{\begin{flrbox}\bgroup\aftergroup\emystyle}
\def\emystyle{\end{flrbox}\egroup}

\begin{document}

\begin{lstlisting}[gobble=4]
int main() {
if( printf ("Hello, <tex.se!") == 0 )
<return -1;> // error!
@return 0;@    // success
}
\end{lstlisting}
\end{document}

-
While I do know about lrbox, it remains open to me how this solves my problem. Maybe you could elaborate on this with the MWE? –  Daniel Mar 7 '12 at 12:02
well... the problem as posed isn't really soluble you can't make a macro grab to the end of the current group. If you need do do something like "start an fbox here.. and end it here" then an environment is the latex way. –  David Carlisle Mar 7 '12 at 12:12
example of environment use added –  David Carlisle Mar 7 '12 at 13:01
+1 and thanks a lot David! The combination of \aftergroup and an environment did the trick. I didn't imagine it to be that easy :-) –  Daniel Mar 7 '12 at 13:29
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listings,xcolor,beramono}

\lstset{language=C,basicstyle=\ttfamily,keywordstyle=\bfseries}

\lstset{moredelim=**[is][\color{red}]{<}{>}}  % works
\lstset{moredelim=**[is][\mystyle]{@}{@}}     % hmmm...

\def\mystyle{} % how to get the parameter to for \fbox?

\begin{document}
\begin{lstlisting}[gobble=4,escapechar=&]
int main() {
if( printf ("Hello, <tex.se!") == 0 )
<return -1;> // error!
@&\fbox{\bfseries return 0;}&@   // success
}
\end{lstlisting}
\end{document}

-
just to note I only edited the wiki markup, actually I only pressed the {} icon, I did not edit the code in this example. –  David Carlisle Mar 7 '12 at 12:13
Thanks S. Murugan, but honestly speaking that is messing around. My goal is not to get this particular box (it's just an MWE to illustrate the problem), but a general solution –  Daniel Mar 7 '12 at 12:32