2

I write a command to store the problem answer with the collect package. Mr @egreg help me expand the problem number in the aux file. Now I meet another trouble with the answer. When the answer contains a counter, it will not expand. Please help me correct the below code.

enter image description here

Moreover, please give some documentation of "expandafter" and ".aux" commands Thank you very much!

\documentclass{article}
\usepackage{collect}
\definecollection{btcol}
\newcounter{cbt} %the number problems
\newcounter{anschoice} %the answer key

\newenvironment{bt} {\refstepcounter{cbt}\par \noindent{\bfseries Problem \thecbt}.}{}

\makeatletter \newcommand{\loigiai}[1]{% @bsphack\expandafter\loigiai@aux\expandafter{\expanded{\thecbt}}{#1}@esphack } \newcommand{\loigiai@aux}[2]{% \begin{collect}{btcol}{\par\noindent {\bfseries AP #1}.\ignorespaces}{}{}{} #2% \end{collect} } \makeatother

\begin{document} \setcounter{anschoice}{3} \begin{bt} Content 1 \loigiai{Answer for Pro.1 $ \rightarrow $ Choice: \Alph{anschoice}} \end{bt}

\begin{bt} Content 2 \end{bt}

\setcounter{anschoice}{2} \begin{bt} Content 3 \loigiai{Answer for Pro.3 $ \rightarrow $ Choice: \Alph{anschoice}} \end{bt}

\includecollection{btcol} \end{document}

1 Answers1

2

I'm sure this could be better automated, but here, I expand the offending phrase with an \edef, before passing it to \loigiai.

EDITED to simplify input for the user. Instead of forcing an \edef upon the user, I create the macro \exloigiai which the user may alternatively use, which applies \expanded to the argument prior to passing it to \loigiai.

\documentclass{article}
\usepackage{collect}
\definecollection{btcol}
\newcounter{cbt} %the number problems
\newcounter{anschoice} %the answer key

\newenvironment{bt} {\refstepcounter{cbt}\par \noindent{\bfseries Problem \thecbt}.}{}

\makeatletter \newcommand{\loigiai}[1]{% @bsphack\expandafter\loigiai@aux\expandafter{\expanded{\thecbt}}{#1}@esphack } \newcommand{\loigiai@aux}[2]{% \begin{collect}{btcol}{\par\noindent {\bfseries AP #1}.\ignorespaces}{}{}{} #2% \end{collect} }

\newcommand\exloigiai[1]{\expandafter\loigiai\expandafter{\expanded{#1}}} \makeatother

\begin{document} \setcounter{anschoice}{3} \begin{bt} Content 1 \exloigiai{Answer for Pro.1 $ \rightarrow $ Choice: \Alph{anschoice}} \end{bt}

\begin{bt} Content 2 \end{bt}

\setcounter{anschoice}{2} \begin{bt} Content 3 \exloigiai{Answer for Pro.3 $ \rightarrow $ Choice: \Alph{anschoice}} \end{bt}

\includecollection{btcol} \end{document}

enter image description here