테이블에 빈 공간 / 자체 명령에 정의되지 않은 제어 순서

테이블에 빈 공간 / 자체 명령에 정의되지 않은 제어 순서

숫자를 숫자로 나누어 표에 기록하는 매크로를 작성했는데 이에 대해 2가지 질문이 있습니다. 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}

여기에 이미지 설명을 입력하세요

관련 정보