2

I need to gather things that will be presented at the end of the document, as shown bellow

\documentclass{article}

\usepackage{collect}

\definecollection{somethings}

\newcommand{\collectthing}[1]{
    There is a #1 here!
    \begin{collect}{somethings}{}{}{}{} 
        \noindent #1 collected in section \arabic{section}
    \end{collect}
}

\begin{document}

\section{Section A}

\collectthing{ThingA}

\section{Section B}

\collectthing{ThingB}

\section*{Things collected}

\includecollection{somethings}

\end{document}

However, since the command \collectthing is expanded only when I call the command \includecollection, all sections are shown with the same value they share when they are expanded, as it can see in the result bellow

enter image description here

How do I fix it, so it shows the proper section number at the end?

3 Answers 3

4

You're storing \arabic{section}, not the current value.

Solution:

\documentclass{article}

\usepackage{collect}

\definecollection{somethings}

\newcommand{\collectthing}[1]{%
  There is a #1 here!%
  \expandandcollect{somethings}{\noindent #1 collected in section~\arabic{section}}%
}

\ExplSyntaxOn
\NewDocumentCommand{\expandandcollect}{mm}
 {
  \text_expand:n { \begin{collect}{#1}{}{}#2\end{collect} }
 }
\ExplSyntaxOff

\begin{document}

\section{Section A}

\collectthing{ThingA}

\section{Section B}

\collectthing{ThingB}

\section*{Things collected}

\includecollection{somethings}

\end{document}

Beware that this is not safe with fragile commands, so you'll need \protect in front of them. The created file will contain

\noindent ThingA collected in section~1

\noindent ThingB collected in section~2

enter image description here

Without expl3, but not as satisfying:

\makeatletter
\newcommand{\expandandcollect}[2]{%
  \begingroup\protected@edef\x{\endgroup
    \noexpand\begin{collect}{#1}{}{}#2\noexpand\end{collect}%
  }\x
}
\makeatother
1
  • Thanks! A simple and clear solution!
    – bmello
    May 27 at 10:38
2

If you don't mind a little expl3:

\documentclass{article}

\usepackage{xparse} % In case you are using LaTeX version older than 2021
\usepackage{collect}

\definecollection{somethings}

\ExplSyntaxOn

\newcommand{\collectthing}[1]{
    There ~ is ~ a ~ #1 ~ here!
    \exp_args:Nx \bmello_collectthing_internal:nn { \arabic{section} } { #1 }
}
\cs_new_protected:Nn \bmello_collectthing_internal:nn
  {
    \begin{collect}{somethings}{}{}{}{}
        \noindent #2 ~ collected ~ in ~ section ~ #1
    \end{collect}
  }

\ExplSyntaxOff

\begin{document}

\section{Section A}

\collectthing{ThingA}

\section{Section B}

\collectthing{ThingB}

\section*{Things collected}

\includecollection{somethings}

\end{document}

An internal command \bmello_collectthing_internal:nn is defined, and by using the x-type argument (\exp_args:Nx changes \bmello_collectthing_internal:nn to \bmello_collectthing_internal:xn), \arabic{section} is expanded before passing to this command.

enter image description here

1

If you don't wish the tokens forming #1 to be expanded, you can do something like this:

\documentclass{article}

\usepackage{collect}

\definecollection{somethings}

\newcommand\exchange[2]{#2#1}

\newcommand{\collectthing}[1]{%
  There is a #1 here!
  \expandafter\exchange\expandafter{%
    \number\arabic{section}%
  }{%
    \begin{collect}{somethings}{}{}{}{}%
    \noindent #1 collected in section~%
  }%
  \end{collect}%
}

\begin{document}

\section{Section A}

\collectthing{ThingA}

\section{Section B}

\collectthing{ThingB}

\section*{Things collected}

\includecollection{somethings}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.