動的テーブル: テーブルの後の列番号の設定

動的テーブル: テーブルの後の列番号の設定

\Nページの先頭に列を持つ動的なテーブルを作成したいのですが、列は\Nドキュメント全体のセクションの数なので、の値は次のように\N設定されます。テーブル。 の値を\Nテーブルの前に設定すると (この例では、行 26 を\N=\value{section}行 10 の後に移動します\section{section 1})、すべて正常ですが、 の値を\Nテーブルの後に設定すると (この例ではそうしています)、エラーが発生します。

これが私のコードです。表の後に set の値があります\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外部ファイルをドキュメントの前の部分に移動するだけです。ただし、テーブルを正しく更新するには、ドキュメントを 2 回コンパイルする必要があります。次に例を示します。

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

関連情報