1

I use the collect package to store the answer code. When the Problem number is wrong when calling the \includecollection comment. Please help me correct the below code.enter image description here

\documentclass{article}
\usepackage{collect}
\definecollection{btcol}
\newcounter{cbt}
\newenvironment{bt}{\refstepcounter{cbt}\par\noindent {\bfseries Problem \thecbt}.}{\ignorespaces.}
\newcommand{\loigiai}[1]{
\begin{collect}{btcol}{\par\noindent {\bfseries AP\; \thecbt}.}{}{}{} 
    \begingroup
        #1
    \endgroup
\end{collect}
}

\begin{document}
\begin{bt}
Conntent 1
\loigiai{Answer for Pro.1}
\end{bt}
\begin{bt}
Conntent 2
\loigiai{Answer for Pro.2}
\end{bt}

\includecollection{btcol}
\end{document}

1 Answer 1

1

You need to expand \thecbt before calling the collect environment; this is most conveniently done with an auxiliary macro.

\documentclass{article}
\usepackage{collect}
\definecollection{btcol}
\newcounter{cbt}

\newenvironment{bt}
 {%
  \par
  \noindent\refstepcounter{cbt}{\bfseries Problem \thecbt}.%
 }
 {\unskip.}
\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}.}{}{}{} 
    #2%
  \end{collect}
}
\makeatother

\begin{document}

\begin{bt}
Content 1
\loigiai{Answer for Pro.1}
\end{bt}

\begin{bt}
Content 2
\loigiai{Answer for Pro.2}
\end{bt}

\includecollection{btcol}

\end{document}

I'd avoid adding an “automatic” period: the code I provide is not really fail safe, because you can't have a blank line before \loigiai.

enter image description here

2
  • Thank you very much! Jul 16, 2021 at 15:48
  • The \loigiai comment also contains another counter, key. Please show me how to expand it. Where can I find the document for these command, using aux file. Thank you very much! Jul 18, 2021 at 22:40

You must log in to answer this question.

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