2

I am curious to know why the following failed. A similar thread is on comp.text.tex unanswered. Of course, other manipulations of \whileexpr work, but why does this fail?

\documentclass{article}
\makeatletter
\def\whileexpr#1\do#2{%
  #1#2\expandafter\@iden\else\expandafter\@gobble\fi
  {\whileexpr#1\do{#2}}%
}
\newcount\colnum
\newcommand\generaterow[2]{%
  \colnum\z@
  \whileexpr\ifnum\colnum<#2\relax\do{%
    #1\advance\colnum\@ne\the\colnum\relax
  }%
}
\makeatother

\begin{document}
% Test (worked):
\generaterow{x}{3}% -> x1x2x3

% Failed:
\begin{tabular}{|*{4}{c|}}
  \hline
  x\generaterow{&}{3}\\\hline
\end{tabular}

\end{document}
Ahmed Musa
  • 11,742
  • 8
    Please take a look over the questions you've asked and see if you can 'accept' some of the answers. When there is an answer that 'works for you' it's a good idea to accept it. – Joseph Wright Dec 08 '11 at 22:10
  • @Joseph: Which questions? This is my first question here in 12 months. Or are you referring to comp.text.tex? I can't find any answer there. – Ahmed Musa Dec 08 '11 at 22:17
  • 2
    @AhmedMusa: See the questions you've asked on TeX.SX. They're found in your profile. – Werner Dec 08 '11 at 22:19
  • We'd like to keep answers separate from questions, so you should write a separate answer instead of editing your answer into the question. Self-answers are perfectly admissible, and a well-written answer may earn you additional reputation. – Werner Dec 09 '11 at 01:09
  • @AhmedMusa: My apologies if the following is obvious. Each answer to a question has a score. For questions that you have asked, you will see a grey tick (or check mark) "✓" below these answer scores. For each question that you've asked in the past, find the question that helped you the most and click on that grey tick. It will go green, indicating that you have accepted it as the answer to your question. – qubyte Dec 09 '11 at 02:15
  • For example, this question has a single answer, which you have provided. As you know that it's correct, look at the score to the left of the answer and you'll see the grey tick below it. Click on it to accept the answer. – qubyte Dec 09 '11 at 02:19
  • @MarkS.Everitt: In this case, Ahmed would have to wait for 2 days before he can accept his own answer. – Jake Dec 09 '11 at 02:30
  • @Jake: Right you are. Forgot about that rule. – qubyte Dec 09 '11 at 05:13
  • Possible duplicate: http://tex.stackexchange.com/q/7590/5763 – Martin Schröder Dec 09 '11 at 11:02

1 Answers1

3

I have seen the problem: conditionals can't be split across & in \halign. Each cell is executed in an alignment group. The following worked. It allows the conditional to complete execution before the material (including the alignment character &) is placed/executed.

\def\whileexpr#1\do#2{%
  #1\expandafter\@iden\else\expandafter\@gobble\fi
  {#2\whileexpr#1\do{#2}}%
}
Ahmed Musa
  • 11,742