4

I want to be able to apply the same label multiple times and have a referencing command, that lists all uses of that label. Minimal example:

\documentclass{article}

\newcounter{example}

\begin{document}

Warning: Examples
\myref{pessimistic} % 1, 4
are passimistic. Apart from that, only examples
\myref{beginner} % 1, 2
are suitable for beginners.

\refstepcounter{example} \mylabel{beginner} \mylabel{pessimistic}

\refstepcounter{example} \mylabel{beginner} \mylabel{optimistic}

\refstepcounter{example} \mylabel{intermediate} 

\refstepcounter{example} \mylabel{expert} \mylabel{pessimistic}

\end{document}

I'm looking for an implementation for pdflatex - avoiding makeindex if possible.

Is there already a package that does something similar - or how could I go about implementing that?

2
  • 1
    An index seems the natural way to do this- just out of curiosity, why do you want to avoid using one?
    – cmhughes
    Commented Feb 10, 2014 at 18:21
  • @cmhughes I agree and I'm kinda tempted to (ab)use the indexing mechanisms to do the above. This is for a book project, in which the first \part is introducing topics. At the end of each topic, I want to give a list of references, where this topic appears in the rest of the book. In that way, it's similar to the TeX's back-references.
    – arney
    Commented Feb 10, 2014 at 18:40

1 Answer 1

4

The following seems to be what you're after:

enter image description here

\documentclass{article}

\makeatletter
% Taken from latex.ltx
\def\@newl@bel#1#2#3{{%
  \@ifundefined{#1@#2}%
    {\expandafter\xdef\csname rr@#2\endcsname{\@firstoftwo #3}}
    {\gdef \@multiplelabels {%
       \@latex@warning@no@line{There were multiply-defined labels}}%
     \@latex@warning@no@line{Label `#2' multiply defined}
     \expandafter\xdef\csname rr@#2\endcsname{\csname rr@#2\endcsname, \@firstoftwo #3}}%
  \global\@namedef{#1@#2}{#3}}}
\makeatother
\newcounter{example}

\newcommand{\myref}[1]{\csname rr@#1\endcsname}
\let\mylabel\label
\begin{document}

Warning:

Examples
\myref{pessimistic} % 1, 4
are pessimistic.

Apart from that, only examples
\myref{beginner} % 1, 2
are suitable for beginners.

\refstepcounter{example} \mylabel{beginner} \mylabel{pessimistic}

\refstepcounter{example} \mylabel{beginner} \mylabel{optimistic}

\refstepcounter{example} \mylabel{intermediate} 

\refstepcounter{example} \mylabel{expert} \mylabel{pessimistic}

\end{document}

The definition of \@newl@bel is taken from latex.ltx and updated to create a macro \rr@<label> for every \label{<label>}. If \rr@<label> already exists, then it appends the new label, otherwise it just creates it. Referencing requires \myref, while \mylabel is similar to \label.

This will not work with hyperref.


Label can also be supplied as a comma-separated list using etoolbox and the following definition of \mylabel. See How to iterate over a comma separated list? for more options:

\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\newcommand{\mylabel}[1]{% \mylabel{<list of labels>}
  \renewcommand*{\do}[1]{\label{##1}}% How to process each item
  \docsvlist{#1}}% Process list of items
2
  • Awesome! Is there by any chance a way to extend this to parsing \mylabel{beginner, pessimistic} as \mylabel{beginner} \mylabel{pessimistic} ?
    – arney
    Commented Feb 10, 2014 at 19:02
  • 1
    @arney: I've update the definition of \mylabel to process it as such.
    – Werner
    Commented Feb 10, 2014 at 19:12

You must log in to answer this question.

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