1

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{}
\newcounter{subtask}
\setcounter{subtask}{1}
\renewcommand{\thesubtask}{\alph{subtask})}
\newcommand{\task}[1][]{%
    \ifthenelse{\isempty{#1}}{%
        \thesubtask%
        \stepcounter{subtask}%
    }{%
        \thesubtask%
        \gappto\credits{(#1)}%
        \stepcounter{subtask}%
    }%
}

\begin{document}
\begin{tabularx}{\linewidth}[l]{l@{\hspace*{1ex}}X}
    \task[1]    &   TEXT1\\
    \task       &   TEXT2\\
    \task[3]    &   TEXT3
\end{tabularx}
\credits
\end{document}

enter image description here


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{}
\newcounter{subtask}
\setcounter{subtask}{1}
\renewcommand{\thesubtask}{\alph{subtask})}
\makeatletter
\newcommand{\task}[1][]{%
    \ifx\hfuzz\@tempdima%
%   % tabularx trial do nothing
    \else%
        \ifthenelse{\isempty{#1}}{%
            \thesubtask%
            \stepcounter{subtask}%
        }{%
            \thesubtask%
                \gappto\credits{(#1 BE)\\}%
            \stepcounter{subtask}%
        }%
    \fi
}
\makeatother


\begin{document}

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

\end{document}

enter image description here

  • 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
3

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{}
\newcounter{subtask}
\setcounter{subtask}{1}
\renewcommand{\thesubtask}{\alph{subtask})}
\makeatletter
\newcommand{\task}[1][]{%
    \ifx\hfuzz\@tempdima 
     \thesubtask
     \else
    \ifthenelse{\isempty{#1}}{%
        \thesubtask%
        \stepcounter{subtask}%
    }{%
        \thesubtask%
        \gappto\credits{(#1)}%
        \stepcounter{subtask}%
    }%
  \fi
}
\makeatother
\begin{document}
\begin{tabularx}{\linewidth}[l]{l@{\hspace*{1ex}}X}
    \task[1]    &   TEXT1\\
    \task       &   TEXT2\\
    \task[3]    &   TEXT3
\end{tabularx}
\credits
\end{document}

enter image description here

| improve this answer | |
  • 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
  • 1
    @rsa-krypto answer updated so it sets the task number in trial runs – David Carlisle Oct 10 '19 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.