
저는 몇 주 전부터 LaTeX3를 배우기 시작했고 현재는 테이블을 가지고 놀고 있습니다. 내 코드는 다음과 같습니다.
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_protected:Npn \juhu_tablerow:n #1
{
\int_new:N \l_row_count_int
\int_set:Nn \l_row_count_int { 1 }
\prg_replicate:nn {#1}
{
\int_use:N \l_row_count_int .~row \\
\int_incr:N \l_row_count_int
}
}
\cs_new_eq:NN \tablerow \juhu_tablerow:n
\ExplSyntaxOff
\begin{document}
\begin{tabular}{c}
\tablerow{3}
\end{tabular}
\end{document}
이 코드의 출력은 다음과 같습니다.
1. row
1. row
1. row
원하는 출력은 다음과 같아야 합니다.
1. row
2. row
3. row
tabular
환경을 환경으로 바꾸면 center
예상되는 출력을 얻습니다.
\\
이제 in 을 \int_use:N \l_row_count_int .~row \\
로 바꾸면 환경 내부에서도 다시 작동하는 것처럼 보이며 출력은 다음과 같습니다.,~
\int_incr:N
tabular
1. row, 2. row, 3. row,
그래서 제 질문은 여러 행을 사용할 때 환경 내에서 정수를 늘리는 것이 작동하지 않는 이유 tabular
와 원하는 결과를 얻으려면 어떻게 해야 합니까? 입니다.
답변1
저의 겸손한 의견부터 답변까지.:)
당신은 해결책에 매우 가까워졌습니다! 여기서 범인은 범위입니다. 해당 할당은 로컬에서 발생합니다. 글로벌하게 만들자.
계속 진행하기 전에 정수 선언을 명령 정의 외부로 이동해야 합니다. 그렇지 않으면 에 대한 후속 호출에서 오류가 발생합니다 \tablerow
. 이제 약간의 코딩 규칙을 살펴보겠습니다.
\int_new:N \l_row_count_int
에게
\int_new:N \g_row_count_int
우리 카운터가 글로벌화 되었기 때문에. :)
이제 몇 가지 대체품이 있습니다( xparse
제안해 주신 egreg에게 감사드립니다!).
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\int_new:N \g_row_count_int
\cs_new_protected:Npn \juhu_tablerow:n #1
{
\int_gset:Nn \g_row_count_int { 1 }
\prg_replicate:nn {#1}
{
\int_use:N \g_row_count_int .~row \\
\int_gincr:N \g_row_count_int
}
}
\NewDocumentCommand{ \tablerow }{ m }{
\juhu_tablerow:n{#1}
}
\ExplSyntaxOff
\begin{document}
\begin{tabular}{c}
\tablerow{3}
\end{tabular}
\end{document}
그리고 모든 것이 잘 작동해야 합니다.:)
\g_tmpa_int
전역 할당을 위한 스크래치 정수를 사용하여 몇 달러를 절약할 수도 있습니다 .:)