# How can I tag all occurrences of a keyword then reference all the pages containing the same tag?

I am writing a syllabus with specific curricular requirements. Activities are tagged with these requirements to demonstrate alignment. I want to use a labelling or indexing system to tag the instances of alignment and then refer to all of the pages were an instance of alignment occurs.

Is there a package that will reference all of the pages containing the same label, as in an index, but allow me to print the pages for that label at an arbitrary point in the document?

This need seems to have elements of \label - \ref and of \index.

I don't have a minimal working example but what I want would look something like this.

\documentclass{article}
\begin{document}
\begin{description}
\item[CR1] This is a description of a requirement. \\ See page <LIST OF PAGES LABELED WITH CR1>
\item[CR2] This is a description of another requirement. \\ See page <LIST OF PAGES LABELED WITH CR2>
\end{description}

\section{Section 1}
This is an activity aligned with CR1. (\label{CR1} or \index{CR1})

This is an activity aligned with CR2. (\label{CR2} or \index{CR2})
\section{Section 2}
This is another activity aligned with CR1. (\label{CR1} or \index{CR1})

\end{document}

• Welcome! For the uninitiated, you might wish to clarify that alignment does not here mean alignment in the TeX sense.
– cfr
Jan 29, 2017 at 3:48
• This seems to be exactly what \index is designed to do or wrappers around index such as glossaries Jan 29, 2017 at 11:11
• I used the index solution by @DavidCarlisle because I already had the requirements description file and it was easier to add tags and \listfor command. I look forward to trying glossary the next time I can start a project like this from scratch. Jan 29, 2017 at 19:56

I see Nicola just did a glossaries version but here's my barebones makeindex version.

You just want an index style that saves each page list in a definition rather than typesetting, say foo.ist:

preamble
"\n\\makeatletter{"
postamble
"}\n\\makeatother\n"

item_0 "}\n\\@namedef{"
delim_0 "}{"


Then

pdflatex file
makeindex -s foo.ist file
pdflatex file


should produce the above output from a document like

\documentclass{article}
\usepackage{makeidx}
\makeindex
\def\listfor#1{\csname #1\endcsname}
\begin{document}

\printindex

\begin{description}
\item[CR1] This is a description of a requirement. \\ See pages \listfor{CR1}
\item[CR2] This is a description of another requirement. \\ See pages \listfor{CR2}
\end{description}

\section{Section 1}
This is an activity aligned with CR1\index{CR1}.

aa
\clearpage

This is an activity aligned with CR2\index{CR2}.
\section{Section 2}
This is another activity aligned with CR1\index{CR1}.

\section{Section 3}
This is an activity aligned with CR1\index{CR1}.

aa
\clearpage

This is an activity aligned with CR2\index{CR2}.
\section{Section 4}
This is another activity aligned with CR1\index{CR1}.

aa
\clearpage

aa
\clearpage

This is an activity aligned with CR2\index{CR2}.
\section{Section 4}
This is another activity aligned with CR1\index{CR1}.

\end{document}


Here's an example that uses glossaries:

\documentclass{article}

\usepackage[colorlinks]{hyperref}% optional but if needed must come
% before glossaries.sty
\usepackage[nopostdot]{glossaries}

\makeglossaries

% syntax: \newglossaryentry{label}{options}
\newglossaryentry{CR1}{name={CR1},
description={This is a description of a requirement.}}

% Or
% syntax: \longnewglossaryentry{label}{options}{description}

\longnewglossaryentry{CR2}{name={CR2}}%
{This is a description of another requirement.

With a paragraph break.
}

\begin{document}
\printglossary[title={Requirements}]

\section{Section 1}
This is an activity aligned with CR1.\glsadd{CR1}% index only

This is an activity aligned with \gls{CR2}.% index and show name

\section{Section 2}

This is an activity aligned with \gls{CR1}.% index and show name

\end{document}


Build process (assuming file is called myDoc.tex):

pdflatex myDoc
makeglossaries myDoc
pdflatex myDoc


makeglossaries is a Perl script that calls makeindex with all the required switches set.

Alternatively if you don't have Perl installed, there's a lightweight Lua script:

pdflatex myDoc
makeglossaries-lite myDoc
pdflatex myDoc


Either build process produces:

The red text indicates a hyperlink. The 1 after the description is the page number where the entry was referenced.

With the extension package glossaries-extra you can suppress the automated indexing on an individual basis with the noindex option.

\documentclass{article}

\usepackage[colorlinks]{hyperref}% optional but if needed must come
% before glossaries.sty
\usepackage{glossaries-extra}

\makeglossaries

% syntax: \newglossaryentry{label}{options}
\newglossaryentry{CR1}{name={CR1},
description={This is a description of a requirement.}}

% Or
% syntax: \longnewglossaryentry{label}{options}{description}

\longnewglossaryentry{CR2}{name={CR2}}%
{This is a description of another requirement.

With a paragraph break.
}

\begin{document}
\printglossary[title={Requirements}]

\section{Section 1}
This is an activity aligned with CR1.\glsadd{CR1}% index only

This is an activity aligned with \gls{CR2}.% index and show name

\newpage

\section{Section 2}

This is an activity aligned with \gls{CR1}.% index and show name

Some minor reference to \gls[noindex]{CR2} that doesn't need
indexing.

\end{document}


The build process is the same.

You can change the style. For example:

\printglossary[title={Requirements},style=index]


There are plenty of predefined styles to choose.

For future reference, there will at some point soon be another approach using bib2gls instead of makeglossaries / makeglossaries-lite. I've added it here in case someone finds this question at a later date.

Create a .bib file called, say requirements.bib:

@entry{CR1,
name={CR1},
description={This is a description of a requirement.}
}

@entry{CR2,
name={CR2},
description={This is a description of another requirement.

With a paragraph break.}
}


The document myDoc.tex is now:

\documentclass{article}

\usepackage[colorlinks]{hyperref}% optional but if needed must come
% before glossaries.sty
\usepackage[record]{glossaries-extra}

\begin{document}
\printunsrtglossary[title={Requirements}]

\section{Section 1}
This is an activity aligned with CR1.\glsadd{CR1}% index only

This is an activity aligned with \gls{CR2}.% index and show name

\newpage

\section{Section 2}

This is an activity aligned with \gls{CR1}.% index and show name

Some minor reference to \gls[noindex]{CR2} that doesn't need
indexing.

\end{document}


The build process is:

pdflatex myDoc
bib2gls myDoc
pdflatex myDoc


There's no call to makeindex as bib2gls simultaneously fetches and sorts the entries from the .bib file and collects the locations from the .aux file.

The differences here are use of the record option and \GlsXtrLoadResources. The command \makeglossaries isn't required and the glossary is now printed with \printunsrtglossary instead of \printglossary.