2

I'm trying to use \docsvlist from etoolbox to generate a table row. However, I've found a weird interaction when I try to use this trick to skip the first ampersand in the output. Here's an MWVE:

\documentclass{standalone}
\usepackage{etoolbox}

\begin{document}
\def\nextitem{\def\nextitem{&}}%
\renewcommand*{\do}[1]{\nextitem #1}%
\begin{tabular}{c|c|c|c|c}
  \docsvlist{a,b,c,d,e}
\end{tabular}
\end{document}

When I compile this file, the table it produces looks like this:

a|bc|de|

I'd expect it to look more like this:

a|b|c|d|e

Interestingly, if I modify the separator to be something other than &, like ,, it works as would be expected:

a,b,c,d,e|

And even more interestingly, if I change \def\nextitem{\def\nextitem{&}} to just \def\nextitem{&}, it works correctly, except that it has an extra blank cell at the beginning:

|a|b|c|d\ne|

To me this suggests that there is some very specific problem with redefining \nextitem to & while inside the tabular environment. How can I fix this issue?

3

Each cell within a tabular forms a group. So your redefinition of \nextitem within the cell doesn't survive to the next cell. If you make the redefinition global - via \gdef (say) - you achieve the desired output:

enter image description here

\documentclass{article}

\usepackage{etoolbox}

\begin{document}

% Local \nextitem redefinition
\def\nextitem{\def\nextitem{&}}% http://tex.stackexchange.com/a/89187/5764
\renewcommand*{\do}[1]{\nextitem #1}%
\begin{tabular}{c|c|c|c|c}
  \docsvlist{a,b,c,d,e}
\end{tabular}

% Global \nextitem redefinition
\def\nextitem{\gdef\nextitem{&}}% http://tex.stackexchange.com/a/89187/5764
\renewcommand*{\do}[1]{\nextitem #1}%
\begin{tabular}{c|c|c|c|c}
  \docsvlist{a,b,c,d,e}
\end{tabular}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.