[texhax] getting to global scope from within a group (latex/ifthen)

bnb at ams.org bnb at ams.org
Sat Nov 15 18:51:23 CET 2014


> I'm trying to define a macro that behaves differently the first time its
> used compared to later uses.  But I tend to use the macro inside of
> blocks, and so the changes I make don't last until the next time I
> invoke the macro.
> 
> How can I get around this?
> 
> I know \outer works for plain TeX definitions, but I'm updating some
> symbol buried inside latex macros, in this case the \setboolean macro is
> resetting something.
> 
> Here's a little example.  Because the first \testit is inside a group,
> the second \testit acts as if it is being invoked for the first time.
> My actual useage is buried in several layers of macros and environments.
> 
> \documentclass{article}
> \usepackage{ifthen}
> 
> \newcommand{\testit}{Random text.
> \ifthenelse{\boolean{blahblah}}{I have already spoken.}{I speak at
> length.\setboolean{blahblah}{true}}}
> 
> \begin{document}
> \newboolean{blahblah}
> \begingroup
> \testit
> \endgroup
> 
> \testit
> 
> \testit
> \end{document}
> 
> Thanks.
> Ross Boylan
> 
> P.S. I'm using \begingroup so I can issue \centering to center a table
> within a float.  Is there a better way to center the table?  The
> beginning and end of the table are produced by different macros.

i checked with david carlisle about
whether he would consider adding a
global version of \newbookean to the
ifthen package.  here is his response:

   not keen there isn't even a global version of \newif
   all assignments in latex are local except counters which are always global:
   there are no (user level) commands that can do either for the same kind of
   thing.

   That said

   \newboolean{foo}
   \renewcommand\foofalse{\global\let \iffoo \iffalse}
   \renewcommand\footrue{\global\let \iffoo \iftrue}

   will work.

this is "latex-y" only in that it
uses \newboolean.  otherwise, it's
expressed in terms of tex primitives.
but it should solve the problem as
described.
 						-- bb


More information about the texhax mailing list