etoolbox and tabularx — problem with appto

I'm trying to print worksheets. I have already implemented an automatic numbering for subtasks. I use tabularx for my subtasks (see the MWE below).

Yesterday, I tried to define a new macro called \task that has one optional argument which is the amount of points you can obtain for this particular question. I would like these optional arguments to be gathered with etoolbox's \appto command so that I can print them with my \credits command.

So far, this seems to be ind of struggling as tabularx is working on the tables several times. Every time tabularx calculates the width of X columns the points are appended to \credits which leads to an incorrect list of points.

Is there any way I can solve this problem? I'm currently stucked with this issue.

Plus, I don't know why my \task command does work. Why is the counter subtask not affected by tabularx's several trials?

\documentclass{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{etoolbox}
\usepackage{booktabs}
\usepackage{ltablex}
\usepackage{xifthen}

\def\credits{}
\ifthenelse{\isempty{#1}}{%
}{%
\gappto\credits{(#1)}%
}%
}

\begin{document}
\begin{tabularx}{\linewidth}[l]{l@{\hspace*{1ex}}X}
\end{tabularx}
\credits
\end{document}


The solution given by David Carlisle below causes a table that is too wide. How can this be undone?

\documentclass{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[a6paper, showframe=true]{geometry}

\usepackage{etoolbox}
\usepackage{booktabs}
\usepackage{ltablex}
\usepackage{xifthen}

\def\credits{}
\makeatletter
\ifx\hfuzz\@tempdima%
%   % tabularx trial do nothing
\else%
\ifthenelse{\isempty{#1}}{%
}{%
\gappto\credits{(#1 BE)\\}%
}%
\fi
}
\makeatother

\begin{document}

\begin{tabularx}{\linewidth}[l]{l@{\hspace*{1ex}}X}
\task[1]    &   Some longer text that exceedes the line width.\\
\end{tabularx}
\credits

\end{document}


• I will ask a new question for this. The original problem has been solved. – rsa-krypto Oct 10 '19 at 5:42
• I will ask a new question for this. The original problem has been solved. tex.stackexchange.com/q/511681/164672 – rsa-krypto Oct 10 '19 at 5:58
• I fixed the answer here sorry then voted to close that one as a duplicate (or you could delete it) – David Carlisle Oct 10 '19 at 7:01

To answer your final question first. tabularx takes special care not to over-increment latex counters, but it can not by default handle other kinds of side effects that may get executed during the trials. (LaTeX maintains a global structure for resetting counter values mainly for the \includeonly system so counters can be reset after omitted sections, and tabularx "borrows" that.

However tabularx does disable other things during trials so you can test for a trial run and do nothing, here I test for \hfuzz having a non standard definition only used during trials:

\documentclass{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{etoolbox}
\usepackage{booktabs}
\usepackage{ltablex}
\usepackage{xifthen}

\def\credits{}
\makeatletter
\ifx\hfuzz\@tempdima
\else
\ifthenelse{\isempty{#1}}{%
}{%
\gappto\credits{(#1)}%
}%
\fi
}
\makeatother
\begin{document}
\begin{tabularx}{\linewidth}[l]{l@{\hspace*{1ex}}X}

• I looked through the tabularx code to find some if commands that may be the solution, but I didn't try this one. Thank you for your quick response! – rsa-krypto Oct 7 '19 at 5:21