테이블 형식 환경의 루프

테이블 형식 환경의 루프

자동으로 크기에 따라 테이블을 생성하려고 합니다. LaTeX에는 루프 명령이 급증하여 어떤 명령이 적합한지 파악하기가 어렵습니다. 일반 TeX 루프와 패키지의 루프를 시도했지만 forloop성공 pgfloop하지 못했습니다.

분명히 환경 내에서 루프를 실행하는 데 문제가 있습니다 tabular. 또한 먼저 루프를 실행하고 출력 문자열을 a에 token저장한 다음 tabular. 이것은 어떤 경우에는 작동합니다.

최소한의 예는 다음과 같습니다. 첫째, 제가 생산하고 싶은 것:

\begin{tabular}{|*{4}{c|}}
  1&2&3&4
\end{tabular}

TeX 루프를 사용하여 시도한 내용은 다음과 같습니다.

\newcounter{numofcol}\setcounter{numofcol}{4}
\newcounter{tmpcol}\setcounter{tmpcol}{1}

\begin{tabular}{|*{\thenumofcol}{c|}}
  \thetmpcol
  \loop
    \ifnum\value{tmpcol}<\value{numofcol}%
    \stepcounter{tmpcol}%
    &\thetmpcol
  \repeat
\end{tabular}

분명히 이 &기호는 루프에서 문제를 야기합니다. 패키지 forloop또는 pgfloop. 오류는 다음과 같습니다

ERROR: Forbidden control sequence found while scanning use of \loop.

--- TeX said ---
<inserted text> 
                \par 
l.140     &
           \thetmpcol
--- HELP ---
No help available

따라서 나는 행의 문자열을 tokenby 로 저장할 수 있다고 생각했습니다.

\newcounter{numofcol}\setcounter{numofcol}{4}
\newcounter{tmpcol}\setcounter{tmpcol}{1}
\newtoks\tmprowcontent

\tmprowcontent={x}
\loop%
\ifnum\value{tmpcol}<\value{numofcol}%
  \stepcounter{tmpcol}%
  \tmprowcontent=\expandafter{\the\expandafter\tmprowcontent & x}%
\repeat

\begin{tabular}{|*{\thenumofcol}{c|}}
  \the\tmprowcontent
\end{tabular}

에 해당 카운터를 포함하지 않으면 작동합니다 token. x현재 카운터 값으로 바꾸면 , 즉,

\newcounter{numofcol}\setcounter{numofcol}{4}
\newcounter{tmpcol}\setcounter{tmpcol}{1}
\newtoks\tmprowcontent

\tmprowcontent={\thetmpcol}
\loop%
\ifnum\value{tmpcol}<\value{numofcol}%
  \stepcounter{tmpcol}%
  \tmprowcontent=\expandafter{\the\tmprowcontent & \thetmpcol}%
\repeat

\begin{tabular}{|*{\thenumofcol}{c|}}
  \the\tmprowcontent
\end{tabular}

그러면 모든 항목은 카운터의 마지막 값이 됩니다. 즉, 4이 경우입니다. 이는 을 사용하기 때문입니다 \expandafter. 그러나 를 사용하지 않으면 \expandafterTeX는 메모리가 부족하다고 알려줍니다.

따라서 내 질문은 어떻게 하면 이 작업을 수행할 수 있는가입니다.

답변1

&너무 일찍 표시되지 않도록 숨기고 테스트 도중 테이블 셀을 종료 해야 할 수도 있지만 간단히 값을 반복할 수 있습니다.

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

\documentclass{article}

\begin{document}

\newcounter{numofcol}\setcounter{numofcol}{4}
\newcounter{tmpcol}\setcounter{tmpcol}{0}

\newcommand\foo{%
  \stepcounter{tmpcol}%
  \thetmpcol
  \ifnum\value{tmpcol}<\value{numofcol}\hiddenamp\expandafter\foo\fi}
\newcommand\hiddenamp{&}

\begin{tabular}{|*{\thenumofcol}{c|}}
  \foo
\end{tabular}
\end{document}

관련 정보