1

I would like to create a dynamic table with \N columns at the beginning of the page, in which \N is the number of sections in the whole document, so the value of \N is set after the table. When I set the value of \N before the table (in my example, moving line 26 \N=\value{section} after line 10 \section{section 1}), everything is alright, but when I set the value of \N after the table (as it is here in my example), this generates an error.

Here is my code, with the value of \N set after the table. I suppose it's not so difficult, but I couldn't find a solution. Does anyone have an idea?

\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 Answer 1

2

Usually, problems like this can be resolved by saving your table to an external file by the end of your document. All you need to do is to \input the external file in earlier part of the document. However, this does require one to compile the document twice for the table to be updated correctly. Here is an example:

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

I am more familiar with LaTeX3. I am sure you can use LaTeX2e code to achieve the same goal as well.

Update: storing the number of sections in a variable

If all you want is to store the number of sections in a variable, here is a simpler example. It writes the line \global\def\numsection{5} into the aux file, which can be accessed in the next run.

\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}
3
  • Thanks a lot for your answer and example! However, I must say I was expecting something more simpler, like a counter or a variable for the number of sections, that would be set at the end of the document, and the document to be runned 2 times. Or maybe something like a \label{mylastpage}, and then \ref{mylastpage}? Or maybe like the lastpage package?
    – Garulfo
    Sep 4, 2020 at 19:16
  • @Garulfo If you only want to store the number of sections in a variable, there is definitely a simpler solution. Please refer to the updates.
    – Alan Xiang
    Sep 4, 2020 at 19:36
  • @ Alan Xiang Thank you so much!!! :-D This is exactly what I was expecting, end it solves definitely my problem! Thanks for your kind help! Robin (Garulfo)
    – Garulfo
    Sep 5, 2020 at 9:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .