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

I have some macros I use a lot, mostly just for my digital note keeping. Because of the nature of these macros, I could use a specific versions of these. For example I mention some file names throughout a document such as \workdocument which assigns them a specific url, and I would like to have a list of these files at the end of the document which I can just use to copy paste into some unix terminal tool. I could parse the file with a programming language to get these, but with multiple includes it isn't something I prefer.

The contents of these commands aren't references as one would semantically understand it, so I would like to avoid using bibtex.

If this can be accomplished with a feature that only exists in any of lualatex/xelatex/pdflatex, that is fine by me.

My tex file, in the simplest sense, looks like the following:

\documentclass[english]{scrartcl}

\newcommand*\thingofnote[1]{#1} % Somehow add the input to an 'array' to be printed at the end as well?
\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}

% Here, create a list of all the things of note previously included in the document.
% Even this output would be acceptable, as long as it is not done manually:
% xyz

\end{document}


In the real document there are more commands and multiple includes.

This produces,

I would like to produce a version of,

• See chapter 15 of the KOMA documentation which shows how to make new lists of things. The tocloft package can do this too. – Alan Munn May 29 at 17:53

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}


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}


• Just nitpicking: You don't need to use \long\gdef for \thing<n> (of course it doesn't hurt either :-) because the \par will be in the definition. Non-\long commands only trap \par when they appear in the argument. Also the \expandafter before \long can be dropped; TeX will remember prefixes until the first unexpandable token is found, so \expandafter is ok. – Phelype Oleinik May 29 at 20:05
• @PhelypeOleinik Thank you for that wisdom. I learned something new! – Steven B. Segletes May 29 at 20:14
• Thanks for the answers everyone. I am going to mark this as the answer since I have to choose one of them, but I probably will use similar code to both of these solutions in my documents over time. This seems easier to expand upon, although the etoolbox solution looks simpler for documents that I was already using the etoolbox package on. Thanks again, these help a lot. – Lacey May 29 at 23:35

Using etoolbox's \docsvlist:

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

\newcommand\mylistofstuff{}
\newcommand*\thingofnote[1]{%
#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}