6

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, In simplest terms, the file I have

I would like to produce a version of, The file I would rather have

Thanks in advance.

  • 1
    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
6

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

  • 1
    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
  • 1
    @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
3

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}

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.