Within my report I want do define several structural damages and I want to automatically generate a summary table at the beginning of the document. Here is an example of how far I have come so far and what I want to achieve.
\documentclass{article}
\usepackage{etoolbox}
\usepackage{pgffor}
\parindent=0pt
\newcounter{cntClassA}\newcounter{cntClassB}\newcounter{cntClassC}\newcounter{cntAddTmp}
\makeatletter
\newcommand\defineDamageKey[4] { \expandafter\edef\csname damage.#1.#2.#3\endcsname{#4} }
\newcommand{\addDamage}[3]{%
%
\ifstrequal{#1}{A}{\stepcounter{cntClassA} \setcounter{cntAddTmp}{\thecntClassA} }{}%
\ifstrequal{#1}{B}{\stepcounter{cntClassB} \setcounter{cntAddTmp}{\thecntClassB} }{}%
\ifstrequal{#1}{C}{\stepcounter{cntClassC} \setcounter{cntAddTmp}{\thecntClassC} }{}%
%
\defineDamageKey{#1}{\thecntAddTmp}{description}{#2}%
\defineDamageKey{#1}{\thecntAddTmp}{solution}{#3}%
\defineDamageKey{#1}{\thecntAddTmp}{ID}{#1-\thecntAddTmp}%
%
\printDamage{#1}{\thecntAddTmp}%
}
\newcommand{\printDamage}[2]{%
\textbf{ID:} @nameuse{damage.#1.#2.ID}\%
\textbf{Damage class:} #1\%
\textbf{Description:} @nameuse{damage.#1.#2.description}\%
\textbf{Solution:} @nameuse{damage.#1.#2.solution}\%
}
\newcommand{\damageSummary}[1]{%
\newcount\damageCountForClass
%
\ifstrequal{#1}{A}{ \damageCountForClass=\thecntClassA }{}%
\ifstrequal{#1}{B}{ \damageCountForClass=\thecntClassB }{}%
\ifstrequal{#1}{C}{ \damageCountForClass=\thecntClassC }{}%
%
\foreach \n in {1,...,\damageCountForClass}%
{%
\textbf{@nameuse{damage.#1.\n.ID}:} @nameuse{damage.#1.\n.description}\
}%
}
\makeatother
\begin{document}
\section{Building 1}
\addDamage{A}{Broken window}{Fix window}
\addDamage{B}{Wrong wall paint}{Repaint}
\section{Building 2}
\addDamage{C}{Leaky windows}{Seal windows}
\addDamage{A}{Defective electrics}{Fix electrics}
\section{Summary}
\damageSummary{A}
\damageSummary{B}
\damageSummary{C}
\end{document}
My basic idea was to define my data as key/value pairs with structured named macros:
\@namedef{damage.A.1.description}{description damage A-1}
\@namedef{damage.A.1.solution}{solution damage A-1}
\@namedef{damage.A.2.description}{description damage A-2}
But now I want to have the summary table at the beginning of the document! Am I right that this only works with AUX files?
I've just started with low level TeX / macro writing and tried to figure out how to do this by examining the todonotes package (as the \todo and \listoftodos would possible a similar mechanism). But I dont get it.
So is my current solution actual a good starting point or do I need a different approach? Could anyone give me a hint how to achieve this?




\begin{building}{Building 1} \addDamage{A}... \addDamage{B}... \end{building}\begin{building}{Building 2} \addDamage{C}... \addDamage{A}... \end{building} \printSummary \generateMainContents. As usual, having good data structures helps a lot. – frougon Jul 28 '21 at 15:30