espaçamento diferente em whiledo com tabular

espaçamento diferente em whiledo com tabular

Às vezes, observo diferentes espaços introduzidos quando uso whiledoem um ambiente tabular. Como posso me livrar deles?

Verifique o seguinte 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}

saída

Observe que as células não estão alinhadas.

Responder1

insira a descrição da imagem aqui

Você está adicionando espaço no final da linha:

\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}

Responder2

Como destaca David, há espaços que você não leva em consideração.

Existem maneiras muito melhores de realizar tarefas repetitivas do que \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}

insira a descrição da imagem aqui

informação relacionada