I want to store table lines with \addtocontents and generate a table afterwards from this entries with \@starttoc. Because \@starttoc uses \begingroup and \endgroup it makes problems with tables (! Extra }, or forgotten \endgroup). So I tried to define my own \starttoc command based on the definition of \@starttoc without the group macros, which now actually compiles:
\documentclass{article}
\makeatletter
\newcommand\addEntry[2]{#1\}
\newcommand\mystarttoc[1]{@input{\jobname.#1}\if@filesw\expandafter\newwrite\csname tf@#1\endcsname\immediate\openout\csname tf@#1\endcsname\jobname.#1\relax\fi}
\begin{document}
\begin{tabular}{|l|l|}
\addEntry{\textbf{ID}}{\textbf{Description}}
\mystarttoc{damageClassA} % summarize damages with class A
%\mystarttoc{damageClassA}\ % would not work cause damageClassA contains multiple table lines
\mystarttoc{damageClassB} % summarize damages with class B
\mystarttoc{damageClassC} % should not extend table cause there are no class C damages in this MWE
\end{tabular}
\addtocontents{damageClassA}{\addEntry{A-1}{damage1 with classification A}}
\addtocontents{damageClassA}{\addEntry{A-2}{damage2 with classification A}}
\addtocontents{damageClassA}{\addEntry{A-X}{damage...X with classification A}}
\addtocontents{damageClassB}{\addEntry{B-1}{damage1 with classification B}}
\makeatother
\end{document}
However, it seems that the if-clause of the starttoc macro introduces some superfluous spaces which results in a misaligned foo4 line and a superfluous empty table line:
Can this be prevented somehow or should I use a completely different approach? LaTeX2e syntax would be prefered over expl syntax.
UPDATE This is actually a follow up question from How to automatically generate data summary at the beginning of the document? I updated my MWE so that hopefully my intention is more clear.
I want to summarize 0..X damages with classification A in a file damageClassA. And the same for classes B and C. So there will be only 3 files at a time. But each file should be able to handle multiple table lines.
And the final table will actually be a bit more complex. So using something other than a table to get the current visual result will unfortunately not be a solution either.



\mystarttoc{test1}\\would result infoo2 & barfoo3 & bar \\. So the table line ending\\have to be handled by\addEntry. – Cryptkeeper Oct 04 '21 at 16:17\\correctly in the tabular environment will not work. – Cryptkeeper Oct 04 '21 at 21:20