Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Two questions please about this code example:

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}
share|improve this question
add comment

2 Answers

up vote 8 down vote accepted
  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!"

share|improve this answer
2  
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
add comment

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!.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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