0

I have a macro called « \addsol » that appends solutions for given exercises to a fixed \solutions macro, in order to list the solution of the exercises at the end of the document.

I wish to add to it an automatic mechanism such that each solution is preceded by it’s label in the enumerate bloc.

Ideally, it would be great if there exists some instruction that imitates the label used in the enumerate list, wether it its A, B, C or 1) 2) 3) without having to reconfigure the command to each case. As this seems to me difficult to obtain, I tried to use the enumi counter, as shown in the MWE below, but the output doesn’t give the number of each question, instead, it gives the final value of enumi.

Any suggestions would be very appreciated

\documentclass[12pt,a4paper]{report}

\usepackage[french]{babel}
\usepackage{fontspec} 
\usepackage[shortlabels]{enumitem}
\usepackage{amsmath}

\newcommand{\equal}{=}

\newcommand{\addsol}[1]{%
  \ifdefined\solutions
    \expandafter\gdef\expandafter\solutions\expandafter{\solutions,\ $\Alph{enumi}\equal$ #1}%
  \else
    \gdef\solutions{\textbf{Solutions:} $\Alph{enumi}\equal$ #1}%
  \fi
}

\begin{document}

\begin{enumerate}[label=$\Alph*\equal$]
    \item $3 + 5$ \addsol{$8$}
    \item $4 \times 6$ \addsol{$24$}
    \item $54 \div 9$ \addsol{$6$}
\end{enumerate}

% Print the contents of solutions

\solutions

\end{document}

Here is the output of the preceding exemple :

enter image description here

1 Answer 1

0

I think this can be addressed by using LaTeX3. Here is my idea, by \addsol you can put data to a sequence and then pop the data by \solutions.

\documentclass{article}
\usepackage[shortlabels,inline]{enumitem}
\ExplSyntaxOn
\seq_new:N \l_solutions_seq
\tl_new:N \l_answer_tl
\NewDocumentCommand{ \addsol }{ m }{ \seq_gput_right:Nn \l_solutions_seq {#1} }

\NewDocumentCommand{\solutions}{ }
  { \textbf{Solutions:}\c_space_tl
    \begin{enumerate}[resume*,start=1]
      \bool_do_until:nn
        { \seq_if_empty_p:N \l_solutions_seq }
        { \seq_gpop_left:NN \l_solutions_seq \l_answer_tl \item $\l_answer_tl$ }
    \end{enumerate}
}
\ExplSyntaxOff

\newcommand{\equal}{=}
\begin{document}

\begin{enumerate}[label=$\Alph*\equal$]
  \item $3 + 5$ \addsol{8}
  \item $4 \times 6$ \addsol{24}
  \item $54 \div 9$ \addsol{6}
\end{enumerate}

\solutions

\end{document}

enter image description here However, the enumerate environment in the 10/14 lines can not be replaced by enumerate* since otherwise an error will be raised.

1
  • I’m afraid that I’m not familiar with the expl syntax, and was hoping for a solution with the usual plain Latex commands. But if there is no other solution for this, I will dig into it, so thank you for this proposition!
    – ladi
    Jul 9 at 13:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .