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.

I'm new at making macros and am in the process of building a package to produce course outlines. I want the user to input room/day/time information as below (where everything has been simplified to just using the room)

\newcounter{RoomCount}
\NewDocumentCommand\Room{R(){TBA}}{%
    \stepcounter{RoomCount}%
    \expandafter\newcommand\csname Room\theRoomCount\endcsname{#1}%
}

At this point I would like to feed this information into a tabular environment to format the room/day/time information. To do so, I would like a macro that spits out 1 row for each room/day/time combination that has been input. This macro would then be executed in a tabular environment to build the table.

\NewDocumentCommand\buildrows{}{%
\newbool{myBool}%
\booltrue{myBool}%
\newcounter{rownum}%
\whileboolexpr{myBool}{%
    \stepcounter{rownum}%
    \ifcsdef{Room\therownum}%
    {\csname Room\therownum\endcsname\\}%
    {\boolfalse{myBool}}%
}%
}

The intention of the loop is: check whether the user has input a second room, if so, then write a row for tabular, if not, then set myBool to false so that the loop exits. The following, however, produces many "package etoolbox error: invalid boolean expression" errors

\begin{document}
\Room(C244)

\begin{tabular}{l}
  \buildrows{}
\end{tabular}

\end{document}

At this point I don't have enough "tools in my toolbox" to figure out what I'm doing wrong so any help/suggestions would be greatly appreciated. Thanks! ps. The full example would be:

\documentclass{report}
\usepackage{xparse}
\usepackage{etoolbox}

\newcounter{RoomCount}
\NewDocumentCommand\Room{+R(){TBA}}{%
\stepcounter{RoomCount}%
\expandafter\newcommand\csname Room\theRoomCount\endcsname{#1}%
}

\NewDocumentCommand\buildrows{}{%
\newbool{myBool}%
\booltrue{myBool}%
\newcounter{rownum}%
\whileboolexpr{myBool}{%
    \stepcounter{rownum}%
    \ifcsdef{Room\therownum}%
    {\csname Room\therownum\endcsname}%
    {\boolfalse{myBool}}%
}%
}

\begin{document}
\Room(C244)

\begin{tabular}{l}
\buildrows{}
\end{tabular}

\end{document}
share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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}
share|improve this answer
    
Thank you very much for the prompt answer Marco. Thank you for the tips, and the answer to my problem...since it was tucked down a little lower, I had missed that part of the documentation! In editing my original example I missed an important part which now seems to be an issue. I think I will have to make another question :( –  Scott H. May 1 '12 at 0:17
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.