表中有空白处/自身命令中的控制序列未定义

表中有空白处/自身命令中的控制序列未定义

我编写了一个宏,将数字拆分成各个数字并将它们写入表格中,对此我有两个问题。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) 查看结果时,发现第一列有很多空白。这是为什么?

2)我还尝试编写一个宏,使用两个数字作为参数,并使用上面的宏将它们写入表中:

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

然后\test{123}{234}出现错误“未定义控制序列”。为什么会发生这种情况?

答案1

  • 确保宏定义不会引入意外的空格。

  • 执行时先定义\i再使用\test

  • 正如 David 在评论中指出的那样,是有缺陷的,因为只要扩展为以两个相同数字开头的数字,\if它就会产生 true 。例如,将其替换为。\i\ifnum\i=1\relax

这是您的更正后的代码。

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

在此处输入图片描述

答案2

多余的空格是由于行尾未受保护而造成的。

以下是构建表格的另一种实现方式

\digittable{<comma separated list of numbers>}

有一个可选参数用于设置显示的最小位数(用空格填充),请参阅示例代码。您还可以输入整数运算,宏将自行计算结果。

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

在此处输入图片描述

相关内容