3

Sometimes, I observe different spaces introduced when I use whiledo in a tabular environment. How can I get rid of them?

Check the following MWE:

\documentclass{article}
\usepackage{ifthen}

\newcounter{qai} \def\myand{&}

\begin{document} \begin{tabular}{ |l|l|l|l|} \hline Q & Q & Q & Q\ \hline \setcounter{qai}{1} \whiledo{\value{qai}<3}{Q\myand\stepcounter{qai}} Q & Q \\hline \end{tabular} \end{document}

output

Note that the cells are not aligned.

David Carlisle
  • 757,742

2 Answers2

5

enter image description here

You are adding space from ends of line:

\documentclass{article}
\usepackage{ifthen}

\newcounter{qai} \def\myand{&}

\begin{document} \begin{tabular}{ |l|l|l|l|} \hline Q & Q & Q & Q\ \hline \setcounter{qai}{1}% \whiledo{\value{qai}<3}{Q\myand\stepcounter{qai}}% Q & Q \\hline \end{tabular} \end{document}

David Carlisle
  • 757,742
5

As David points out, there are spaces you don't take into account.

There are far better ways to do repetitive tasks than \whiledo.

\documentclass{article}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\Repeat}{m O{} +m} {% #1 = number of repetitions % #2 = what to put in between % #3 = thing to repeat \int_compare:nT { #1 > 0 } {% do nothing if #3 <= 0 #3 \prg_replicate:nn { #1 - 1 } { #2 #3 } } }

\ExplSyntaxOff

\begin{document}

\Repeat{10}{I must not drive the principal's car\par}

\bigskip

\begin{tabular}{|c|c|c|c|} \hline Q & Q & Q & Q \ \hline \Repeat{4}[&]{Q} \ \hline \end{tabular}

\end{document}

enter image description here

egreg
  • 1,121,712