5

I want to define a set of conditionals using newif in a loop. The loop variable should become part of the conditional's name. However, I'm running into trouble when trying to set the conditional and when defining multiple conditionals in one loop.

When I try to define a second conditional using the same code structure, I end up getting the following error: Missing \endcsname inserted.. The same happens when trying to set the if conditional.

MWE:

\documentclass{article}

\makeatletter
\count@=0
\loop
  \advance\count@ 1
  \expandafter\newif\csname ifrst@onlyheight\@Alph\count@\endcsname%
  %\expandafter\newif\csname ifrst@onlywidth\@Alph\count@\endcsname%
  %\csname rst@onlyheight\@Alph\count@ true\endcsname
  \csname rst@onlyheightAtrue\endcsname
\ifnum\count@<6
\repeat
\makeatother

\begin{document}

More code here.

\end{document}

1 Answer 1

5

\count@ is set during the processing of \newif:

% latex.ltx, line 910:
\def\newif#1{%
  \count@\escapechar \escapechar\m@ne
    \let#1\iffalse
    \@if#1\iftrue
    \@if#1\iffalse
  \escapechar\count@}

Use \@tempcnta instead.

A different loop:

\documentclass{article}
\usepackage{expl3}

\ExplSyntaxOn
\int_step_inline:nnnn { 1 } { 1 } { 5 }
 {
  \exp_args:Nc \newif { ifrst@onlyheight \int_to_Alph:n { #1 } }
  \exp_args:Nc \newif { ifrst@onlywidth \int_to_Alph:n { #1 } }
  \use:c { rst@onlywidth \int_to_Alph:n { #1 } true }
 }
\ExplSyntaxOff

\begin{document}

\makeatletter\ttfamily\noindent
\meaning\ifrst@onlyheightA \\
\meaning\ifrst@onlyheightB \\
\meaning\ifrst@onlyheightC \\
\meaning\ifrst@onlyheightD \\
\meaning\ifrst@onlyheightE \\
\meaning\ifrst@onlywidthA \\
\meaning\ifrst@onlywidthB \\
\meaning\ifrst@onlywidthC \\
\meaning\ifrst@onlywidthD \\
\meaning\ifrst@onlywidthE
\makeatother
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .