![動的テーブル: テーブルの後の列番号の設定](https://rvso.com/image/420037/%E5%8B%95%E7%9A%84%E3%83%86%E3%83%BC%E3%83%96%E3%83%AB%3A%20%E3%83%86%E3%83%BC%E3%83%96%E3%83%AB%E3%81%AE%E5%BE%8C%E3%81%AE%E5%88%97%E7%95%AA%E5%8F%B7%E3%81%AE%E8%A8%AD%E5%AE%9A.png)
\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}