我編寫了一個宏,它將數字拆分為數字並將它們寫入表格中,我有兩個關於它的問題。微量元素:
\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
是有問題的,因為只要\i
擴展到以兩個相同數字開頭的數字,它就會產生 true。例如將其替換為\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}