동적 테이블 : 테이블 뒤에 열 번호 설정

동적 테이블 : 테이블 뒤에 열 번호 설정

\N페이지 시작 부분에 \N전체 문서의 섹션 수를 나타내는 열이 있는 동적 테이블을 만들고 싶습니다.\N 설정됩니다.~ 후에탁자. 테이블 앞 의 값을 설정하면 \N(예제에서는 26번 라인을 \N=\value{section}10번 라인 뒤로 이동 \section{section 1}) 모든 것이 정상이지만 테이블 뒤의 값을 설정하면 \N(예제에서와 같이) 오류가 발생합니다. .

\N다음은 테이블 뒤에 값이 설정된 내 코드입니다 . 그다지 어렵지는 않을 것 같은데 해결책을 찾지 못했습니다. 누구든지 아이디어가 있나요?

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{graphicx}
\newtoks\cols
\newcounter{i}
\newcount\N
\begin{document}
    \section{section 1}
\cols={}
\setcounter{i}{1}
\loop
\cols=\expandafter{\the\expandafter\cols\the\value{i}}
\ifnum\value{i}<\N
\cols=\expandafter{\the\cols &}
\stepcounter{i}
\repeat
\begin{tabular}{|*{\N}{c|}}
    \the\cols
\end{tabular}
\section{section 2}
\section{section 3}
\section{section 4}
\section{section 5}
\N=\value{section}
\end{document}

답변1

일반적으로 이와 같은 문제는 테이블을 다음 위치에 저장하면 해결될 수 있습니다.외부 파일문서가 끝날 때까지. \input문서의 이전 부분에 있는 외부 파일을 사용하기만 하면 됩니다 . 그러나 테이블을 올바르게 업데이트하려면 문서를 두 번 컴파일해야 합니다. 예는 다음과 같습니다.

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{graphicx}
\usepackage{expl3}

\begin{document}
\section{section 1}
% load the table, if it exists
\InputIfFileExists{\jobname.table}{}{}
\section{section 2}
\section{section 3}
\section{section 4}
\section{section 5}

\ExplSyntaxOn
% open external file for writing
\iow_open:Nn \g_tmpa_iow {\jobname.table}
% get number of sections
\int_set:Nn \l_tmpa_int {\value{section}}
% write begin environment 
\iow_now:Nx \g_tmpa_iow {\c_backslash_str begin{tabular}
    {|*{\int_use:N \l_tmpa_int}{c|}}}
% write columns into a sequence
\seq_clear:N \l_tmpa_seq
% loop over integers and fill the sequence
\int_step_inline:nn {\l_tmpa_int} {
    \seq_put_right:Nn \l_tmpa_seq {#1}
}
% write table content
\iow_now:Nx \g_tmpa_iow {
    \seq_use:Nn \l_tmpa_seq {~&~} % join the sequence with " & "
}
% write end environment
\iow_now:Nx \g_tmpa_iow {\c_backslash_str end{tabular}}
\iow_close:N \g_tmpa_iow
\ExplSyntaxOff
\end{document}

나는 LaTeX3에 더 익숙합니다. LaTeX2e 코드를 사용하여 동일한 목표를 달성할 수 있다고 확신합니다.

업데이트: 변수에 섹션 수 저장

원하는 것이 섹션 수를 변수에 저장하는 것이라면 다음은 더 간단한 예입니다. 다음 실행에서 액세스할 수 있는 파일 \global\def\numsection{5}에 해당 행을 기록합니다 .aux

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{graphicx}


\begin{document}
Number of sections: \numsection

\section{section 1}
\section{section 2}
\section{section 3}
\section{section 4}
\section{section 5}

\makeatletter
\immediate\write\@auxout{\string\global\string\def\string\numsection{\the\value{section}}}
\makeatother

\end{document}

관련 정보