The following MWE works as I expect but when I make certain changes I get errors:
- If I introduce say
\textbf{something}within a\solution{}say\solution{\textbf{xx}}. - If I change
\theenumito\textbf{\theenumi}
Probably both issues are the same problem.
The MWE below is based on the work from Write the value of enumerate to output file.
\documentclass{article}
\makeatletter
\newwrite\solutions@file
\newcommand{\collectSolutions}{\immediate\openout\solutions@file=\jobname.sol}
\newcommand{\solution}[1]{\immediate\write\solutions@file{\theenumi #1}}
\newcommand\printSolutions{\immediate\closeout\solutions@file{\input{\jobname.sol}}}
\makeatother
\begin{document}
\collectSolutions
\begin{enumerate}
\item hello \solution{xx}
\item hello \solution{xx}
\item hello \solution{xx}
\end{enumerate}
\printSolutions
\end{document}
\solution{\noexpand\textbf{xx}}– koleygr Aug 25 '19 at 09:37\solutionbut it didn't work. – Geoff Aug 25 '19 at 11:12\newcommand{\solution}[1]{\immediate\write\solutions@file{\theenumi \unexpanded{#1}}}– koleygr Aug 25 '19 at 11:24\noexpand\textbf\theenumi\unexpanded{#1}}and it solved both of my questions! – Geoff Aug 25 '19 at 11:29