Recording the inputs of a command and producing a list of them later on

For Dr. Seuss lovers, this approach will actually create macros \thing1 and \thing2! (in \csname form, of course)

\documentclass[english]{scrartcl}
\usepackage{pgffor}
\newcounter{things}
\newcommand*\thingofnote[1]{#1%
  \stepcounter{things}\expandafter\gdef\csname thing\thethings\endcsname{#1}}
\newcommand\listthings{%
  \begin{enumerate}
  \foreach\z in{1,...,\thethings}{\item \csname thing\z\endcsname}
  \end{enumerate}
}
\begin{document}

\section{Section A}

A thing of note is \thingofnote{x}, while another thing of note is \thingofnote{y}.

\section{Section B}

One must not forget about \thingofnote{z}.

\section{All The Things}

\listthings

\end{document}

enter image description here

The approach can be generalized to multi-paragraph things of note as follows:

\documentclass[english]{scrartcl}
\usepackage{pgffor}
\newcounter{things}
\newcommand\thingofnote[1]{#1%
  \stepcounter{things}\expandafter\gdef\csname thing\thethings\endcsname{#1}}
\newcommand\listthings{%
  \begin{enumerate}
  \foreach\z in{1,...,\thethings}{\item \csname thing\z\endcsname}
  \end{enumerate}
}
\begin{document}

\section{Section A}

A thing of note is \thingofnote{x

and new paragraph of more x}, while another thing of note is \thingofnote{y}.

\section{Section B}

One must not forget about \thingofnote{z}.

\section{All The Things}

\listthings

\end{document}

enter image description here


Using etoolbox's \docsvlist:

enter image description here

\documentclass[english]{scrartcl}
\usepackage{etoolbox}

\newcommand\mylistofstuff{}
\newcommand*\thingofnote[1]{%
  \gappto\mylistofstuff{,{#1}}% Add to list
  #1}% write on paper
\begin{document}

\section{Section A}

A thing of note is \thingofnote{x}, while another thing of note is \thingofnote{y}.

\section{Section B}

One must not forget about \thingofnote{z}.

\section{All The Things}

\begin{enumerate}
  \def\do#1{\item #1}
  \expandafter\docsvlist\expandafter{\mylistofstuff}
\end{enumerate}

\end{document}

Tags:

Macros