Bảng động: đặt số cột sau bảng

Aug 30 2020

Tôi muốn tạo một bảng động với \Ncác cột ở đầu trang, trong đó \Nlà số phần trong toàn bộ tài liệu, vì vậy giá trị của \Nđược đặt sau bảng. Khi tôi đặt giá trị \Ntrước bảng (trong ví dụ của tôi, di chuyển dòng 26 \N=\value{section}sau dòng 10 \section{section 1}), mọi thứ đều ổn, nhưng khi tôi đặt giá trị \Nsau bảng (như ở đây trong ví dụ của tôi), điều này tạo ra lỗi .

Đây là mã của tôi, với giá trị \Nđược đặt sau bảng. Tôi cho rằng nó không quá khó, nhưng tôi không thể tìm ra giải pháp. Có ai có ý tưởng gì không?

\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}

Trả lời

2 AlanXiang Sep 01 2020 at 14:47

Thông thường, những vấn đề như thế này có thể được giải quyết bằng cách lưu bảng của bạn vào một tệp bên ngoài vào cuối tài liệu của bạn. Tất cả những gì bạn cần làm là vào \inputtệp bên ngoài trong phần trước của tài liệu. Tuy nhiên, điều này yêu cầu một người phải biên dịch tài liệu hai lần để bảng được cập nhật chính xác. Đây là một ví dụ:

\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}

Tôi quen thuộc hơn với LaTeX3. Tôi chắc chắn rằng bạn cũng có thể sử dụng mã LaTeX2e để đạt được mục tiêu tương tự.

Cập nhật: lưu trữ số phần trong một biến

Nếu tất cả những gì bạn muốn là lưu trữ số phần trong một biến, đây là một ví dụ đơn giản hơn. Nó ghi dòng \global\def\numsection{5}vào auxtệp, có thể được truy cập trong lần chạy tiếp theo.

\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}