Still soldiering on with trying to learn these macros. Can someone tell me why this isn't working?

The macro \mycline{n-n} is supposed to expand to \cmidrule{1-1} ... \cmidrule{n-n}, but in my code below I get multiple errors..


  \foreach \a in {#1,...,#2}{%
  \xdef\MC{\the\toks0 \temp}%

1&2&3&4\\ \mycline{1-3}
  • 2
    There are various things up here, but before tackling them what is wrong with simply \cmidrule{1-3}?
    – Joseph Wright
    Oct 31, 2012 at 14:06
  • hi @JosephWright as per egreg's answer I'm looking for a shortened \cmidrule. As a newbie I've been using \setlength{\extracolsep} to get the intercolumn gaps. Oct 31, 2012 at 14:24
  • 2
    Related Question: Wrapping \cmidrule in a macro. Oct 31, 2012 at 18:09

2 Answers 2


As Joseph comments, there's no point in saying


because the effect is the same as saying


If you want to use the "shortening" feature of \cmidrule and generate


then you can do the computations in a \noalign and then deliver the result:

  \foreach \a in {#1,...,#2}{%

% the macro
1&2&3&4\\ \mycline(1-3)

enter image description here

Notice some points:

  1. You have to say \noexpand\cmidrule in the \edef

  2. Using \g@addto@macro is a more economic way to add tokens to a parameterless macro

  3. Using a @-command such as \@mycline is better than \MC that could clobber some already existing macro

  4. Using \noalign ensures that the work is done "outside any alignment cell"


After \\ (which expands amongst other things to the primitive \cr that ends a table row, TeX looks ahead using expansion only to see if the table cell needs special treatment, specifically it looks for \noalign \span or omit. As soon as it finds a non-expandable token that is not one of these it stops and processes the cell normally.

so if you put \mycline at the start of a row it is expanded one level and then the next token that is seen is the \xdef which is not expandable so after that it is not possible to have any spanning entries (which are needed for \cline \cmidrule \multicolumn and other similar constructs).

If you look how these things are coded in table packages you will see that either they have to work purely by expansion (so no \xdef or foreach etc) or they have to start a \noalign, do all the computation in the local scope of \noalign and then globally insert the expandable commands to span the columns after the \noalign has finished.

You must log in to answer this question.

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