1

I'm trying to create a table dynamically by adding rows to it.

The idea is that the table has to be used/created before the rows are appended using a macro. I modified the method described here In order to use an auxiliary file so I can use the append macro after the table is created.

The code I have so far is the following:

\documentclass[12pt]{article}

\usepackage[table]{xcolor}

\usepackage[english]{babel}

\usepackage{booktabs,tabularx}

\def\tmprevisionrows{} \def\revisionrows{}

\makeatletter \def\appendrevision#1{\g@addto@macro\tmprevisionrows{ #1}}

\newcommand\addrevision[4]{% \appendrevision{#1 & #2 & #3 & #4 \[10pt]} } \makeatother

\newcommand{\revisions}{ \begin{center} \setlength{\extrarowheight}{10pt} \begin{tabularx}{\textwidth}{| X | X | X | X |} \hline VERSION & MODIFICATION & DATE & AUTHOR \[10pt] \hline \revisionrows \hline \end{tabularx} \end{center} }

\begin{document}

\makeatletter @starttoc{xyz} \makeatother

\revisions

% Notice that we are appending after using the \revisions macro \addrevision{0.1}{Document Creation}{01/01/2020}{Author Name} \addrevision{0.2}{Document Modified}{02/01/2020}{Author Name}

\addtocontents{xyz}{\gdef\protect\revisionrows{\tmprevisionrows}}

\end{document}

The result is the following (As you can see no \hline on the table is added):

When I try to add an \hline anywhere I get a compilation error. How I can add an \hline (or alternative) to every row appended using my macro?

fr0zn
  • 51

1 Answers1

1

Change your macro \addrevision into

\newcommand\addrevision[4]{%
    \appendrevision{#1 & #2 & #3 & #4 \\[10pt] \protect\hline}
}

and remove the trailing \hline in the definition of \revisions

\newcommand{\revisions}{
  \begin{center}
  \setlength{\extrarowheight}{10pt}
  \begin{tabularx}{\textwidth}{| l | X | l | l |}
  \hline
  VERSION & MODIFICATION & DATE & AUTHOR \\[10pt]
  \hline
  \revisionrows
  \end{tabularx}
  \end{center}
}

I'd use a single X column, for the one with possible long data.

enter image description here

You can use the .aux file, instead of .xyz:

\documentclass[12pt]{article}

\usepackage[table]{xcolor}

\usepackage[english]{babel}

\usepackage{booktabs,tabularx}

\makeatletter \newcommand\revisionrows{} \newcounter{revisions} \newcommand\addrevision[4]{% \immediate\write@auxout{% \unexpanded{\add@revision{#1 & #2 & #3 & #4 \[10pt] \hline}}% }% \stepcounter{revisions}% } \newcommand{\add@revision}[1]{% \g@addto@macro\revisionrows{#1}% } \AtEndDocument{\refstepcounter{revisions}\label{count@revisions}} \makeatother

\newcommand{\revisions}{% \begin{center} \setlength{\extrarowheight}{10pt} \begin{tabularx}{\textwidth}{| l | X | l | l |} \hline VERSION & MODIFICATION & DATE & AUTHOR \[10pt] \hline \revisionrows \end{tabularx} \end{center} }

\begin{document}

\revisions

% Notice that we are appending after using the \revisions macro \addrevision{0.1}{Document Creation}{01/01/2020}{Author Name} \addrevision{0.2}{Document Modified}{02/01/2020}{Author Name}

\end{document}

With this code, you'll receive a Label(s) may have changed warning if the number of revisions is different from the one found from the previous run.

egreg
  • 1,121,712