I am writing a document which will contain many small (approximately a page) snippets of text, potentially organized in sections or subsections. To each such section, I would like to attach several "tags" from a predefined list. At a different place in the document, I would like to make a list of all tags and the sections which are associated with each.

I have an idea how to make a very primitive tagging system if the tags list occurs in the end of the document (e.g. I can successively redefine a command adding text to it; later I can place this command to list the contents). Does anyone know a better solution? Ideally I would like to organize my tags in the beginning of the document, after the table of contents. I feel there might be a package which does this but I haven't been able to find one.

  • Hi, Nasko! Welcome to TeX.SX. – Ryan Reich Dec 12 '11 at 0:34
  • Isn't this same as an index? – Aditya Dec 13 '11 at 13:15
  • @Aditya: I sort of agree, but I went with a ToC because I forget how to make indices :) Also, it seemed to me that the main job of indexing tools is to sort and collate the many index entries, and here, the entries (that is, tagged sections) should appear in document order but with repetitions that are not collated. Maybe another perspective: ToC's are "many to one" maps, combining topics under broad headings; indices are "one to many" maps, giving all the topics appearing under a heading (really, on a page). He wants a "many to many" map. I thought it closer to a ToC than an index. – Ryan Reich Dec 14 '11 at 3:42
  • Related tex.stackexchange.com/questions/250024/… – Andrew Aug 22 '18 at 6:26

You can do this by placing your tags into a "table of contents". I have adopted egreg's brilliant suggestion to put every tag in one ToC file and simply reread it repeatedly, deactivating the "wrong" tags each time. This mandates that I \input the ToC file manually, which circumvents the usual mechanism \listofX (here, X = tags) that actually creates this file (or overwrites it, which is why I can't use it more than once), so I have included the fairly superfluous feature that a plain list of all tags is then printed at the bottom (below the lists of their references) using this macro, so that everything is properly set up.

I recently read an answer (I believe by Ahmed Musa) that used the {filecontents} environment to build an inline package, and since this is obviously the right way of presenting such an example, I am using it here too.


% Here's the package file

% Declare the master TOC.  This will contain:
% * A list of tags
% * A sequence of entries where tags are referenced
% and it will be used multiple times to generate lists of tag usage
\newlistof{tags}{tags}{All tags}
\renewcommand*\cfttagstitlefont{\Large\bfseries}% For example

% Use this in the preamble to make a new tag.
 \addtocontents{tags}{(#1) }%
% This is only true when printing the list of tags
% This makes sure that the list of tags is not printed most of the time...
% ...because it wraps the entire TOC from the preamble

% This is a rather inefficient way to selectively print particular tags.
% Presumably, I should just pop each tag from the list as I go,
% but etoolbox doesn't seem to handle stacks.  Probably I'm missing something.
 \expandafter\let\csname l@tag#1\endcsname=\@gobbletwo
 \expandafter\let\csname l@tag#1\endcsname=\l@section

% These are hooks for the user
% This prints all the references to a particular tag.
% Effectively, it's a partial ToC for that tag.
% This prints the snippets, placed (with repeats) beneath their tags.
% It also prints a list of defined tags, more or less as an excuse
% to have \listoftags handle the .tags auxiliary ToC file.

% This is how to proclaim a snippet, which is what gets tagged.
\newcommand*\snippettitle{\thesnippet. \snippetname}

% In case of no hyperref
% This is how you place a tag beneath a snippet.
 \phantomsection% In case of hyperref


\renewcommand*\listoftagstitle{Snippets tagged with \currenttag}
% Preferred way to declare tags.  They will be printed in this order.


 \snippet{First snippet}


 \snippet{Second snippet}
 \placetag{A} \placetag{B}
  • in case you don't get time to avoid using too many writes, you can use my morewrites package, whose aim is to silently do all the non-synchronous work inthe background. --- just realized my version number is completely wrong... I haven't edited the documentation 2000 times yet :). – Bruno Le Floch Dec 12 '11 at 0:08
  • You could use only one "list-of" which is then read in multiple times, by redefining all the \l@tag... commands to no-ops except for the relevant one. Or even read in once, accumulating information and then spitting it out. – egreg Dec 12 '11 at 0:11
  • @Ryan Reich -- nice solution, a bit different that what I would do (if I get time later on I will post an alternative). filecontents is very useful, see usage tex.stackexchange.com/questions/33894/…. You can also use an input immediately after it to avoid getting an error on the first run. – Yiannis Lazarides Dec 12 '11 at 9:48
  • Would it be possible to somehow use this approach to tag sections instead of snippets? I cannot make it work for normal LaTeX sections.. – Steven Thiel Feb 11 '18 at 13:17

Just to present an alternative way to Ryan's fine solution.

This is the sort of problem, where TeX beats Python hands down. TeX provides a method to create control sequences automatically by using \csname...\endcsname. We leverage this to create a macro for each individual tag that will hold the list of sections that each tag contains.

I have assumed that you referring to tags in a similar fashion to those used in blogs on the web. We hold the tags in a list:


Each tag will have its own list, which will hold the section numbers, tagged with this particular tag,


We define macros to tag a section in a similar fashion to \index by calling it as \tag{tag name}.

The MWE is shown below:

\def\tags{Physics,Computer Science,Chemistry,}
%% sorts the tags for later on
%% we automatically create macros for each tag
%% Make macros to hold the lists for each tag

%% we now print the tags or save them to a file
  \section*{The Tags}  
     \ifx\next\@empty\else\next: See Section(s) \@nameuse{\next}\par\fi
% add some tags
% test to see everything ok and list is sorted   
% example text    
   \section{D E Knuth}
      \tag{Computer Science}\tag{Art}
      \tag{Computer Science}
%% prints the tags with the sections

We define a command to print the tags (which we keep sorted) as \thetags. The lists are kept in memory and printed at the end. If we want to print them at the beginning of the document then we need to store them in a file, which we import. For brevity I omitted this as is trivial, and wanted to keep the code short and simple to read. It produces output as:

enter image description here

  • Nice solution. How can I expand this to also show the subsection number. – fsociety Sep 4 '15 at 17:46

I don't know what egreg will make of the 16 hash characters in one of the \do's in this solution, but the aim was to use and as the last conjunction of section numbers.

            \tagnameformat{####1}: %
            See section\ifnum\reservedc=\@ne\else s\fi~%
                \iflastindris\@space and\@space\else,\@space\fi
    \@latex@error{Wrong location of '\string\printtags'}\@ehc

\tags{Physics, Mathematics, Physics}

\section{D E Knuth}
\tags{Computer Science, Art, Physics}

\tags{Mathematics, Art}

\tag{Computer Science}

\tags{Art, Physics}

\tags{Chemistry, Literature, Art}


enter image description here

  • 1
    +1 for the eight # :) – egreg Dec 12 '11 at 23:26
  • @egreg: Thanks. That was just to avoid another auxiliary command. It looks neater that way, at least for people like you. – Ahmed Musa Dec 13 '11 at 19:27
  • How can I expand this to also show the subsection number. – fsociety Sep 4 '15 at 19:38

Is hyperref's bookmark or the bookmark package of any help?

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.