Some small mistakes you have done:
- If you set
\newbool{myBool}
and \newcounter{rownum}
inside the definition of the command \buildrows
, the commands will be defined every time you call the command \buildrows
. So the definition must be done before.
- The command
\whileboolexpr
doesn't accept a single bool. The first argument of whileboolexpr
evaluates the first argument. This is described in section 3.6.5 of the documentation.
- This is really a tip: Instead of using
\expandafter
you can use the command \csdef
provided by etoolbox
.
Here a complete MWE
\documentclass{report}
\usepackage{xparse}
\usepackage{etoolbox}
\newcounter{RoomCount}
\NewDocumentCommand\Room{+R(){TBA}}{%
\stepcounter{RoomCount}%
\csdef{Room\theRoomCount}{#1}%
}
\newbool{myBool}%
\newcounter{rownum}%
\setcounter{rownum}{0}
\NewDocumentCommand\buildrows{}{%
\booltrue{myBool}%
\whileboolexpr{ bool {myBool} }{%
\stepcounter{rownum}%
\ifcsdef{Room\therownum}%
{\csname Room\therownum\endcsname}%
{\boolfalse{myBool}}%
}%
}
\begin{document}
\Room(C244)
\Room(C247)
\begin{tabular}{l}
\buildrows{}
\end{tabular}
\buildrows{}
\end{document}
There are a few reasons why defining document commands has always been done using the control sequence for the command (\foo
) rather than the control sequence name (foo
). Of course, some of this comes down to 'you'd have to ask Leslie Lamport or Don Knuth', as it follows from the TeX basics. (For example, \newcommand\foo
mirrors \def\foo
, and \DeclareDocumentCommand\foo
mirrors \cs_new:Npn \foo
.)
First, there is an intrinsic consistency for requiring that the 'appearance' at the point of definition is the same as that at point of use.
Secondly, requiring that a token is construction avoids confusion about what can usually appear in a command name. For example, if you did
\declare{foo-bar}...
\foo-bar
the latter would not work (assuming standard LaTeX category codes) as TeX would 'see' the token \foo
followed by a hyphen and the letters b
, a
and r
. That looks like a good way to have problems.
Third, the current set up allows 'experts' to skip the {
... }
pair
\newcommand\foo{definition}
works in the same way as
\newcommand{\foo}{definition}
something that was important when TeX was 'smaller' (we save two tokens there), but also something that many people find easier to read. (Moreover, as most good editors highlight LaTeX tokens differently from 'running text', using a token-based approach works better with many editors.)
Related to that, you have to remember that the current interface was designed in the 1980s based on a system developed in the late 1970s. The additional work for both \csname
expansion and storage of tokens (separate letters rather than a control sequence) was much more important in the past than it is today.
Finally, if you look at the LaTeX3 stuff we want to avoid 'dynamic' definitions as far as possible at the document interface level. One of the lessons of LaTeX2.09/LaTeX2e is that a clearly defined interface is a good thing: encouraging the use of dynamic names does not help there.
Best Answer
The definition of
\appto
isThe first argument should be a parameterless macro. Why not using
\apptocmd
? Because it is much less efficient and it may even not succeed if the replacement text has “non standard” tokens (for instance, characters with a different category code than usual).Apart from this, the only other difference between
\appto
and\apptocmd
is that the latter preserves the status of a\long
(parameterless) macro, whereas\appto
will always change it to non-\long
.