Espacio vacío en la tabla/Secuencia de control indefinida en el propio comando

Espacio vacío en la tabla/Secuencia de control indefinida en el propio comando

He escrito una macro que divide un número en sus dígitos y los escribe en una tabla y tengo 2 preguntas al respecto. MWE:

\documentclass{article}
\usepackage{pgffor, etoolbox}
\newcommand*\mytablecontents{}
\newcommand*\numtostr[1]{
\renewcommand*\mytablecontents{}
\pgfmathtruncatemacro{\laenge}{ln(#1)/ln(10)+1}
\pgfmathtruncatemacro{\aziffer}{#1}
\foreach \i [remember=\aziffer as \aziffer] in {\laenge,...,1} {%
    \pgfmathtruncatemacro{\ziffer}{\aziffer/(10^(\i-1))}
    \pgfmathtruncatemacro{\aziffer}{\aziffer-\ziffer*(10^(\i-1))}
    \xappto\mytablecontents{$\ziffer$}
    \if\i1
        \gappto\mytablecontents{\\}
    \else
        \gappto\mytablecontents{&}
    \fi
}%
\mytablecontents
}%

\begin{document}

\begin{tabular}{*{7}{|r}}
    \numtostr{6563}
    \numtostr{3475}
\end{tabular}

\end{document}

1) Al observar el resultado se ve que la primera columna contiene mucho espacio vacío. ¿Porqué es eso?

2) También intenté escribir una macro que use como argumentos dos números y los escriba en una tabla usando la macro de arriba:

\newcommand*{\test}[2]{%
\pgfmathtruncatemacro{\groesse}{ln(\i)/ln(10)+1}
\begin{tabular}{*{\groesse}{r}}
    \numtostr{#1}
    \numtostr{#2}
\end{tabular}
}%

Luego \test{123}{234}aparece el error "Secuencia de control no definida". ¿Por qué pasó esto?

Respuesta1

  • Asegúrese de que la definición de su macro no introduzca espacios no deseados.

  • Definir \iantes de usarlo al ejecutar \test.

  • Como señaló David en los comentarios, \iftiene errores, ya que resulta verdadero cada vez que \ise expande a un número que comienza con dos dígitos idénticos. Reemplácelo, por ejemplo, por \ifnum\i=1\relax.

Aquí está su código con las correcciones.

\documentclass{article}
\usepackage{pgffor, etoolbox}
\newcommand*\mytablecontents{}
\newcommand*\numtostr[1]{%
\renewcommand*\mytablecontents{}%
\pgfmathtruncatemacro{\laenge}{ln(#1)/ln(10)+1}%
\pgfmathtruncatemacro{\aziffer}{#1}%
\foreach \i [remember=\aziffer as \aziffer] in {\laenge,...,1} {%
    \pgfmathtruncatemacro{\ziffer}{\aziffer/(10^(\i-1))}%
    \pgfmathtruncatemacro{\aziffer}{\aziffer-\ziffer*(10^(\i-1))}%
    \xappto\mytablecontents{$\ziffer$}%
    \ifnum\i=1\relax
        \gappto\mytablecontents{\\}%
    \else
        \gappto\mytablecontents{&}%
    \fi
}%
\mytablecontents
}%

\newcommand*{\test}[2]{%
\pgfmathtruncatemacro{\groesse}{ln(\i)/ln(10)+1}
\begin{tabular}{*{\groesse}{r}}
    \numtostr{#1}
    \numtostr{#2}
\end{tabular}
}%

\begin{document}

\begin{tabular}{*{7}{|r}}
    \numtostr{6563}
    \numtostr{3475}
\end{tabular}

\renewcommand\i{100}
\test{123}{234}

\end{document}

ingrese la descripción de la imagen aquí

Respuesta2

Los espacios sobrantes se deben a finales de líneas desprotegidos.

Aquí hay una implementación diferente que construye una tabla a partir de

\digittable{<comma separated list of numbers>}

Hay un argumento opcional para establecer el número mínimo de dígitos mostrados (rellenados con espacios en blanco); consulte el código de ejemplo. También puede ingresar operaciones con números enteros; las macros calcularán el resultado por sí mismas.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\digittable}{O{0}m}
 {
  \martin_digittable:nn { #1 } { #2 }
 }

\seq_new:N \l__martin_digittable_numbers_seq
\seq_new:N \l__martin_digittable_entry_seq
\int_new:N \l__martin_digittable_length_int
\tl_new:N \l__martin_digittable_body_tl

\cs_new_protected:Nn \martin_digittable:nn
 {
  \seq_clear:N \l__martin_digittable_numbers_seq
  \clist_map_inline:nn { #2 }
   {
    \seq_put_right:Nx \l__martin_digittable_numbers_seq { \int_eval:n { ##1 } }
   }
  % compute the maximum length
  \int_zero:N \l__martin_digittable_length_int
  \seq_map_inline:Nn \l__martin_digittable_numbers_seq
   {
    \int_set:Nn \l__martin_digittable_length_int
     {
      \int_max:nn { \l__martin_digittable_length_int } { \tl_count:n { ##1 } }
     }
   }
  \int_set:Nn \l__martin_digittable_length_int
   {
    \int_max:nn { \l__martin_digittable_length_int } { #1 }
   }
  % build the table rows
  \tl_clear:N \l__martin_digittable_body_tl
  \seq_map_inline:Nn \l__martin_digittable_numbers_seq
   {% pad the current digit with \__martin_digittable_blank:
    % first split the current item at every token
    \seq_set_split:Nnn \l__martin_digittable_entry_seq { } { ##1 }
    % add to the left the needed amount of blanks
    \prg_replicate:nn { \l__martin_digittable_length_int - \tl_count:n { ##1 } }
     {
      \seq_put_left:Nn \l__martin_digittable_entry_seq { \__martin_digittable_blank: }
     }
    % form the next table row, by inserting & between items
    \tl_put_right:Nx \l__martin_digittable_body_tl
     {
      \seq_use:Nn \l__martin_digittable_entry_seq { & }
      \exp_not:N \\
     }
   }
  % produce the table
  \begin{tabular}{|*{\l__martin_digittable_length_int}{c|}}
  \hline
  \l__martin_digittable_body_tl
  \hline
  \end{tabular}
 }

\cs_new_protected:Nn \__martin_digittable_blank:
 {
  \phantom{0}
 }
\ExplSyntaxOff

\begin{document}

\digittable{3329+3234,3475,212,1}

\bigskip

\digittable[6]{6563,3475,212,1}

\end{document}

ingrese la descripción de la imagen aquí

información relacionada