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?
Best Answer
Edit: hyperref-support is added in case you wish hyperlinks from the summary to the place of the document where the damage is described.
If you don't mind having an auxiliary-file whose name is of pattern
⟨expansion of jobname⟩.Damageclass⟨name of damage-class⟩
for each damage-class, then you can probably do with LaTeX 2ε's\addtocontents
-\@starttoc
-meachanism. I useddescription
-environments for listing the items. How to tweak thedescription
-environment depends on the documentclass in use and may turn out tricky.The example needs to be compiled three times without having auxiliary-files deleted between runs until everything matches out correctly. (First run creates auxiliary files. Second run creates description-item-labes of summaries and stores measurements in the aux-file. Third run uses measurements from aux-file.)
The question "Use LaTeX to produce parts lists?" and the answers to it might be of interest to you— a similar problem is addressed there. In the answers you find a different approach which is based on the package datatool and a .csv-file.