espaciado diferente en whiledo con tabular

espaciado diferente en whiledo con tabular

A veces, observo diferentes espacios introducidos cuando lo uso whiledoen un entorno tabular. ¿Cómo puedo deshacerme de ellos?

Verifique el siguiente 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}

producción

Tenga en cuenta que las celdas no están alineadas.

Respuesta1

ingrese la descripción de la imagen aquí

Estás agregando espacio desde los extremos de la línea:

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

Respuesta2

Como señala David, hay espacios que no se tienen en cuenta.

Hay formas mucho mejores de realizar tareas repetitivas 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}

ingrese la descripción de la imagen aquí

información relacionada