1

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 block.

Ideally, it would be great if there exists some instruction that imitates the label used in the enumerate list, whether 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

3 Answers 3

2

Your task can be simply solved by TeX primitives:

\documentclass[12pt,a4paper]{report}

\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\def\eaddto#1#2{\expandafter\addto\expanded{\noexpand#1{#2}}}
\newcount\exenum
\def\excercises{\exenum=`A \def\solutions{\par {\bf Solutions:} }}
\def\exe#1#2{\par $\char\exenum=#1$
   \addto\solutions{\sol{#2}}\eaddto\solutions{{\the\exenum}}
   \global\advance\exenum by1
}
\def\sol#1#2{\ifnum#2=`A \else, \fi$\char#2=#1$}
\def\solutions{\par Solutions:}

\begin{document}

\excercises
\exe{3 + 5}{8}
\exe{4 \times 6}{24}
\exe{54 \div 9}{6}

% Print the contents of solutions

\solutions

\end{document}

The result is the same as shown in the egreg's answer.

1

You don't want enumerate, but itemize, your own counter and a simpler input syntax.

\documentclass[12pt,a4paper]{report}
\usepackage{enumitem}
\usepackage{amsmath}

\makeatletter
\newcommand{\addsol}[1]{%
  \ifdefined\solutions
    \xdef\solutions{%
      \unexpanded\expandafter{\solutions},
      \ $\Alph{exe}=#1$%
    }%
  \else
    \xdef\solutions{\unexpanded{\textbf{Solutions:} }$\Alph{exe}=#1$}%
  \fi
}

\newcounter{exe}
\newenvironment{exercises}
 {%
   \begin{itemize}[label=]
   \setcounter{exe}{0}%
   \global\let\solutions\undefined
 }
 {\end{itemize}}
\newcommand{\exe}[2]{% #1 = exercise, #2 = solution
  \item\stepcounter{exe}$\Alph{exe}=#1$\addsol{#2}%
}

\begin{document}

\begin{exercises}
\exe{3 + 5}{8}
\exe{4 \times 6}{24}
\exe{54 \div 9}{6}
\end{exercises}

% Print the contents of solutions

\solutions

\end{document}

enter image description here

An expl3 implementation

\documentclass[12pt,a4paper]{report}
\usepackage{enumitem}
\usepackage{amsmath}

\ExplSyntaxOn
\newcounter{exe}
\seq_new:N \g_ladi_solutions_seq
\NewDocumentEnvironment{exercises}{}
 {
  \begin{itemize}[label=]
  \setcounter{exe}{0}
  \seq_gclear:N \g_ladi_solutions_seq
 }
 {\end{itemize}}
\NewDocumentCommand{\exe}{mm}
 {
  \item\stepcounter{exe}$\Alph{exe}=#1$
  \seq_gput_right:Ne \g_ladi_solutions_seq { $\Alph{exe}=#2$ }
 }
\NewDocumentCommand{\solutions}{}
 {
  \par\textbf{Solutions:~}
  \seq_use:Nn \g_ladi_solutions_seq { ,~ }
 }
\ExplSyntaxOff

\begin{document}

\begin{exercises}
\exe{3 + 5}{8}
\exe{4 \times 6}{24}
\exe{54 \div 9}{6}
\end{exercises}

% Print the contents of solutions

\solutions

\end{document}
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.

2
  • 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
    Commented Jul 9, 2023 at 13:54
  • This solution could benefit from displaying the solution in-line, rather than a separate list. Also, what happens if you don't specify a solution to an exercise/problem? The solutions will print things incorrectly as it assumes you'll provide a solution for each exercise/problem.
    – Werner
    Commented Dec 6, 2023 at 20:42

You must log in to answer this question.

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