I'm trying to build a macro to generate a whole changelog table dynamically (template for professional document - prototype). I've found several useful answers to achieve it, but I'm stuck on a macro reset inside another.
Warning : I've detailled everything so that the path I took is clear, instead of posting the code not working and expect an answer...
My goal :
Have a macro to build dynamically a changelog table in the document :
Declare the version in the preamble :
\newrow{A}{H. Potter,R. Weasley}{First release for Snape} \newrow{B}{H. Granger}{Fixed tons of typos}
=> Comma-separated list of authors and/or modifications on sepearate table lines.
Build the table in the core (with the macro
\modifications
being the point of this question) :\setlength\extrarowheight{4.5pt} \begin{tabularx}{\textwidth}{|c|c|X|} \hline \multicolumn{3}{|c|}{\textbf{LOG OF CHANGES}}\\[2.25pt] \hline Version & \centering{Author} & {Modification} \\[2.25pt] \hline \modifications % <-- TABLE BUILD HERE DEPENDING ON HOW MANY ROWS THEY ARE... \end{tabularx}
Currently, this table is achieved with this code. Nothing fancy except the \specialcell
that allows line breaks.
% Command to brake line in a cell.
\newcommand{\specialcell}[2][c]{
\begin{tabular}{@{}#1@{}}#2\end{tabular}
}
% Construction of the tabular
\setlength\extrarowheight{4.5pt}
\begin{tabularx}{\textwidth}{|c|c|X|}
\hline
\multicolumn{3}{|c|}{\textbf{LOG OF CHANGES}}\\[2.25pt]
\hline
Version & \centering{Author} & {Modification} \\[2.25pt] \hline
A & \specialcell{H. Potter\\R. Weasley} & \specialcell[l]{First release for Snape\\Additions to Fred's old version} \\ \hline
B & \specialcell{H. Granger} & Fixed tons of typos ! \\ \hline
\end{tabularx}
Problem 0 : Break the line when a comma-separated list of authors and/or modification is entered. This has been solved with this answer.
Problem 1 : I don't know beforehand how much version are they going to be created !
I found this answer to build a table "dynamically", in function of the number of commands \addtotable{}
passed. Seems the right way! I tried thus to combine that with my case, and it worked.
Here's the code :
\documentclass{article}
\RequirePackage{tabularx}
\usepackage[top=3cm,bottom=4.5cm,left=2.5cm,right=2.5cm]{geometry}
\makeatletter
% Command to brake line in a cell.
\newcommand{\specialcell}[2][c]{
\begin{tabular}{@{}#1@{}}#2\end{tabular}
}
\newcommand\modifications{} % macro containing whole code to build all the lines of the table
\newcommand\authorsList{} % macro containing the code for the authors list
\newcommand\modifList{} % macro containing the code for the modifications list
\newcommand{\newrow}[3]{
% Version cell of the table
\g@addto@macro\modifications{#1 &}
% build \specialcell{Element1\\Element2} and put it the second cell
\g@addto@macro\authorsList{\@gobble}
\@for\tmp:=#2\do{%
\expandafter\g@addto@macro\expandafter\authorsList
\expandafter{\expandafter\\\tmp}
}%
\g@addto@macro\modifications{\specialcell{\authorsList}&}
% build \specialcell{Element1\\Element2} and put it the third cell
\g@addto@macro\modifList{\@gobble}
\@for\tmp:=#3\do{%
\expandafter\g@addto@macro\expandafter\modifList
\expandafter{\expandafter\\\tmp}
}%
\g@addto@macro\modifications{\specialcell[l]{\modifList}}
% end table
\g@addto@macro\modifications{\\\hline}
}
\makeatother
% Version definitions
\newrow{A}{H. Potter,R. Weasley}{First release for Snape, Additions to Fred's old version}
%\newrow{B}{H. Granger}{Fixed tons of typos}
\begin{document}
\setlength\extrarowheight{4.5pt}
\begin{tabularx}{\textwidth}{|c|c|X|}
\hline
\multicolumn{3}{|c|}{\textbf{LOG OF CHANGES}}\\[2.25pt]
\hline
Version & \centering{Author} & {Modification} \\[2.25pt] \hline
\modifications % <-- build of the table
\end{tabularx}
\end{document}
It work pretty well with 1 line in the table.Then I ran into another problem when I added
Main problem
The macros modifList
and authorsList
are not reset and then it meesses everything up. I tired forceing them to reset in the begin with \let\authorsList\@empty
and with \let\modifList\@empty
, but then it only keeps the last value.
At some point, the values that goes into the \specialcell
are not the current ones, but all from the beginning...
Here's the current output :
and the code :
\documentclass{article}
\usepackage{tabularx}
\usepackage[top=3cm,bottom=4.5cm,left=2.5cm,right=2.5cm]{geometry}
\makeatletter
% Command to brake line in a cell.
\newcommand{\specialcell}[2][c]{
\begin{tabular}{@{}#1@{}}#2\end{tabular}
}
\newcommand\modifications{} % macro containing whole code to build all the lines of the table
\newcommand\authorsList{} % macro containing the code for the authors list
\newcommand\modifList{} % macro containing the code for the modifications list
\newcommand{\newrow}[3]{
% Version cell of the table
\g@addto@macro\modifications{#1 &}
% build \specialcell{Element1\\Element2} and put it the second cell
\g@addto@macro\authorsList{\@gobble}
\@for\tmp:=#2\do{%
\expandafter\g@addto@macro\expandafter\authorsList
\expandafter{\expandafter\\\tmp}
}%
\g@addto@macro\modifications{\specialcell{\authorsList}&}
% build \specialcell{Element1\\Element2} and put it the third cell
\g@addto@macro\modifList{\@gobble}
\@for\tmp:=#3\do{%
\expandafter\g@addto@macro\expandafter\modifList
\expandafter{\expandafter\\\tmp}
}%
\g@addto@macro\modifications{\specialcell[l]{\modifList}}
% end table
\g@addto@macro\modifications{\\\hline}
}
\makeatother
% Version definitions
\newrow{A}{H. Potter,R. Weasley}{First release for Snape, Additions to Fred's old version}
\newrow{B}{H. Granger}{Fixed tons of typos}
\begin{document}
\setlength\extrarowheight{4.5pt}
\begin{tabularx}{\textwidth}{|c|c|X|}
\hline
\multicolumn{3}{|c|}{\textbf{LOG OF CHANGES}}\\[2.25pt]
\hline
Version & \centering{Author} & {Modification} \\[2.25pt] \hline
\modifications % <-- build of the table
\end{tabularx}
\end{document}
Any ideas on how to make this work with multiple versions ?