Tabela dinâmica: definindo o número da coluna após a tabela

Tabela dinâmica: definindo o número da coluna após a tabela

Gostaria de criar uma tabela dinâmica com \Ncolunas no início da página, na qual \Nestá o número de seções de todo o documento, então o valor de \Né definidodepoisa mesa. Quando eu defino o valor de \Nbefore the table (no meu exemplo, movendo a linha 26 \N=\value{section}depois da linha 10 \section{section 1}), está tudo certo, mas quando eu defino o valor de \Nafter the table (como está aqui no meu exemplo), isso gera um erro .

Aqui está o meu código, com o valor \Ndefinido após a tabela. Suponho que não seja tão difícil, mas não consegui encontrar uma solução. Alguém tem alguma ideia?

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

Responder1

Normalmente, problemas como este podem ser resolvidos salvando sua tabela emum arquivo externoaté o final do seu documento. Tudo que você precisa fazer é acessar \inputo arquivo externo na parte anterior do documento. No entanto, isso exige que o documento seja compilado duas vezes para que a tabela seja atualizada corretamente. Aqui está um exemplo:

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

Estou mais familiarizado com o LaTeX3. Tenho certeza de que você também pode usar o código LaTeX2e para atingir o mesmo objetivo.

Atualização: armazenando o número de seções em uma variável

Se tudo o que você deseja é armazenar o número de seções em uma variável, aqui está um exemplo mais simples. Ele grava a linha \global\def\numsection{5}no auxarquivo, que pode ser acessado na próxima execução.

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

informação relacionada