9

I'm working on a document that needs to have a cross-reference section at the start of the style:

  1. Evidence 1 found in Paragraph 1.1, 1.2, 1.3
  2. Evidence 2 found in Paragraph 1.2, 2.3, 3.4

etc

I have my document formatted well, and can refer to individual paragraphs exactly as I wish using \label and \ref.

However, I wish to be able to define a label multiple times so that when \ref is called it prints a list of all the labels - not just the last one (as is done currently).

I've looked at using index for this, but can't seem to get it to work in its simplist format, let alone with custom outputs.

Is there anyway to allow \label to be defined multiple times?

3
  • There is always a way:-) (the standard definition of \@newl@bel that makes that warning is only 7 lines, so you could just redefine it, but first why does not the intended use with multiple \ref to unique labels not work for you and do you need it to work with hyperref (so the references become linked) Mar 26, 2014 at 16:28
  • The intended use with multiple refs and unique labels isn't ideal due to the scale of the number of labels/references that would be required. Mar 26, 2014 at 16:32
  • 1
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format.
    – Ruben
    Mar 26, 2014 at 16:36

2 Answers 2

8

enter image description here

\documentclass{article}

\makeatletter
\def\@newl@bel#1#2#3{{%
  \@ifundefined{#1@#2}%
    {\def\tmp{#3}}%
    {%
\edef\tmp{%
  {\expandafter\expandafter\expandafter\@firstoftwo\csname#1@#2\endcsname,
   \@firstoftwo#3}%
  {\expandafter\expandafter\expandafter\@secondoftwo\csname#1@#2\endcsname,
   \@secondoftwo#3}%
}}%
    \expandafter\xdef\csname#1@#2\endcsname{\tmp}%
}}

\def\@testdef #1#2#3{}
\makeatother

\begin{document}


good stuff in section(s) \ref{a}

bad stuff in section(s) \ref{b}

\section{zz}\label{a}
zz
\section{zzz}\label{a}
zzz
\section{zzzz}\label{a}
zzzz
\section{aaa}\label{b}
aaa
\section{zzzzz}\label{a}
zzzzz

\end{document}
2
  • Thank you for your solution. It looks like it could do the job. I will have a look at this tomorrow and see if I can get to the bottom of how it works! Mar 26, 2014 at 17:19
  • @user1016815 hyperref? Mar 31, 2014 at 13:12
2

You don't necessarily have to allow multible labels. In addition to @DavidCarlisle cool low-level hack I provide this solution (essentially a custom cross-ref-macro called \eref{<evidence no>}{<label>}) that loops over a internal created list:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{lipsum}

\makeatletter
\newcounter{para}
\renewcommand{\thepara}{\thesection.\@arabic\c@para}
\let\svd@paragraph=\paragraph
\renewcommand{\paragraph}{\refstepcounter{para} \svd@paragraph}

\newcommand{\eref}[2]{%
  \ifcsundef{evlist#1}{\@namedef{evlist#1}{}}{}
  \def\process{\def\process{,}}%
  \def\do##1{\process\ref{##1}}%
  \expandafter\listadd\csname evlist#1\endcsname{#2}%
  Evidence #1 found at paragraph
  \expandafter\dolistloop\csname evlist#1\endcsname}
\makeatother

\begin{document}
\section{First Section}
\paragraph{One}\label{para:1}
\lipsum[1]
  \eref{1}{para:1}
\paragraph{Two}\label{para:2}
\lipsum[2]
  \eref{1}{para:2}
\paragraph{Three}\label{para:3}
\lipsum[3]
  \eref{2}{para:3}
\end{document}

(Note that you don't have use lines 6-9 if you don't use \paragraph in your document structure.)

You must log in to answer this question.

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