# How do I set a variable inside of \rlap

1) Why does \rlap prevent me from setting a variable?
2) How do I set a variable from within an \rlap that is visible outside of \rlap?

I'm using MikTex 2.8. Thanks for any clue!

\documentclass{article}
\newif\ifmycheck
\begin{document}
\rlap{\mychecktrue}
% \mychecktrue
\ifmycheck
Check True!
\else
Check False!  % always takes this path!
\fi
\end{document}

-

1. Changes within a group stay inside that group, and \rlap puts its argument inside an \hbox, therefore within its own group. So if you did:

\rlap{\mychecktrue % group is still open!
% \mychecktrue
\ifmycheck
Check True!
\else
Check False!
\fi}


You will get

Check True!

So you see that you did set the variable inside \rlap.

2. You want the changes to persist outside the group. So use the prefix \global:

\rlap{\global\mychecktrue}
% \mychecktrue
\ifmycheck
Check True!
\else
Check False!
\fi


That will result again in "Check True!"

-
What you say is correct except that it implies that setting a conditional inside the argument of any macro is local; this is not the case. (Try \def\mymac#1{#1}, for example.) The braces around a macro argument do not form a group; in the case of \rlap, it is because the argument is placed inside an \hbox, and the contents of an \hbox (or \vbox, etc.) do form a group. – Ryan Reich Dec 24 '10 at 8:44
@Ryan: You are correct, good sir. Edited. – Matthew Leingang Dec 24 '10 at 12:17

To complement Matthew's answer, a solution other than setting the variable globally is to use aftergroup to delay the assignment until after the group (produced by \hbox) is finished. Your example becomes

\documentclass{article}
\newif\ifmycheck
\begin{document}
\rlap{\aftergroup\mychecktrue}
\ifmycheck
Check True!
\else
Check False!
\fi
\end{document}


and now typesets Check True!.

-