# Glossaries: using name of (sub)sections instead of page numbers

How can I print the section instead the pagenumber in glossar/index like \nameref it does. Would be nice to have something like:

\documentclass{report}

\usepackage{glossaries}

% Syntax: \newperson{label}{forename}{surname}
\newcommand*{\newperson}[3]{%
\newglossaryentry{#1}%
{%
name={#2\space#3},
sort={#3},% sort on surname
description={#3},
text={#2\ #3},% title \space surname
user1={#2},% forname
user2={#3},% surname
}%
}

\let\Pforename\glsuseri
\let\Psurname\glsuserii

\newglossarystyle{person}%
{%
\glossarystyle{list}%
\glsnogroupskiptrue
\renewcommand*{\glossaryentryfield}[5]{%
\item[]\glsentryitem{##1}\textbf{\glstarget{##1}{##2}}
\\~##5}%
}%

\makeglossaries

\newperson{foobar}{Aardvark}{Foobar}
\newperson{foo}{Bar}{Foo}

\begin{document}

\section{DNS}
\subsection{stuff}
\gls{miller}
\section{DHCP}
\subsection{stuff}
\gls{foobar}
\section{FTP}
\subsection{stuff}
\gls{foo}
\clearpage
\section{SMTP}
\subsection{stuff}
\gls{foo}

\printglossary[title={Stuff},style=person]

\end{document}

-
get mananaged to print section numbers instead of pages with set of the counter value: \usepackage[counter=section]{glossaries} –  Megachip Nov 22 '12 at 13:41

The location has to be written to the external file as a number (formatted using standard counter commands such as \arabic, \roman or \alph) otherwise makeindex will reject the entry. Even if you use xindy, you'd have to define a location style and list the mappings between all the section numbers and their titles. The following example writes the section number to the external file as normal but defines a set of mappings from section number to section title and redefines \glsnumberformat to fetch the corresponding title given the section number. However, even this isn't straightforward as the argument to \glsnumberformat may contain a list separated by \delimN or \delimR (Edit: I've changed the example to have multiple glossaries):

\documentclass{report}

\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage[counter=section]{glossaries}

\newglossary{glossary2}{gls2}{glo2}{Glossary 2}[page]
\newglossary{glossary3}{gls3}{glo3}{Glossary 3}[section]

\makeglossaries

\makeatletter
\newcommand\@fourthoffive[5]{#4}

% adapt toc section command to get number -> title map

\let\org@l@section\l@section
\renewcommand{\l@section}[2]{%
\org@l@section{#1}{#2}%
\expandafter\@fetchsecnum\@fourthoffive#1\end@fetchsecnum
}

\def\@fetchsecnum#1#2#3\end@fetchsecnum{%
\ifdefequal\numberline{#1}%
{%
% numbered section, save title
\csgdef{sec@#2}{#3}%
}%
{%
% unnumbered section, do nothing
}%
}

% also need to take into account any glossary entries at the start
% of the chapter before the first section

\let\org@l@chapter\l@chapter
\renewcommand{\l@chapter}[2]{%
\org@l@chapter{#1}{#2}%
\expandafter\@fetchchapnum\@fourthoffive#1\end@fetchchapnum
}

\def\@fetchchapnum#1#2#3\end@fetchchapnum{%
\ifdefequal\numberline{#1}%
{%
% numbered chapter, save title
\csgdef{sec@#2.0}{#3}%
}%
{%
% unnumbered chapter, do nothing
}%
}

\makeatother

\newcommand*{\getsectitle}[1]{%
\ifcsdef{sec@#1}
{%
}%
{%
\hyperlink{section.#1}{#1}% undefined so just display section number
}%
}

\DeclareListParser*{\glsnumlistN}{\delimN}

\newcommand{\glsnumlistNhandler}[1]{%
\locNsep
\def\locRsep{}%
\glsnumlistR{\glsnumlistRhandler}{#1}%
\let\locNsep\delimN
}

\DeclareListParser*{\glsnumlistR}{\delimR}

\newcommand{\glsnumlistRhandler}[1]{%
\locRsep\getsectitle{#1}\let\locRsep\delimR
}

\newcommand*{\glstitlelocformat}[1]{%
\def\locNsep{}%
\glsnumlistN{\glsnumlistNhandler}{#1}%
}

\newglossaryentry{sample}{name=sample,description={an example}}

\newglossaryentry{sample2}{type=glossary2,
name=sample2,description={a second example}}

\newglossaryentry{sample3}{type=glossary3,
name=sample3,description={a third example}}

\begin{document}
\tableofcontents

\chapter{Sample Chapter}

\gls{sample}. \gls{sample2}.

\section{First section}

\gls{sample}.

\section{Second section}
\gls{sample}. \gls{sample2}.

\section{Third section}

\section{Fourth section}
\gls{sample}. \gls{sample3}.

\section{Fifth section}
\gls{sample}.

\lipsum

\gls{sample2}.

\let\glsnumberformat\glstitlelocformat
\printglossary

\renewcommand{\glsnumberformat}[1]{\glshypernumber{#1}}
\printglossary[type=glossary2]

\let\glsnumberformat\glstitlelocformat
\printglossary[type=glossary3]

\end{document}


Result:

Caveat:

You need to consider the possibility that the reader may want to print out the document and read the hardcopy, in which case this design makes it difficult to look up the reference, as you're forcing the reader to search through the table of contents to find the appropriate page number. At least with a section number the reader can make an intuitive guess as to whereabouts roughly the section is within the document.

Since I consider this poor document design style, I'm reluctant to add it as a feature, but the above should work if you really want to do it.

-
you made my day, thx a lot. I belive this solution did not work, if i will use multiple glossaries? As you already mentioned this would be poor design. The whole document will also use a normal glossary as glossary, but there would be also 2 glossaries which will build up as descriped in this question. shall i post a new question for this problem? –  Megachip Mar 16 '13 at 23:50
@Megachip You could redefine \glsnumberformat just before you display the glossary (using \printglossary) –  Nicola Talbot Mar 17 '13 at 0:13
ok, but with 'counter=section' i'll force all glossaries to that format, correct? –  Megachip Mar 17 '13 at 18:56
@Megachip no, you'll only get that format if \glsnumberformat has the definition as shown in my example when \printglossary occurs. In my example I defined it in the preamble so it's in effect through the whole document, but if you change it back using \renewcommand{\glsnumberformat}[1]{#1} the next \printglossary will display as normal. –  Nicola Talbot Mar 17 '13 at 18:59
Sorry, did not describe it proper. i have one main glossar where references are set to page numbers and than i need 2 glossaries which needs the described style. –  Megachip Mar 17 '13 at 19:49

The following did not work, but cause there seems no solution for this problem, i undeleted it. Possible it could help to found some solutions...

Get the following solution, which seems to work mostly: Add the following code to glossaries.sty


\let\Sectionmark\sectionmark
\def\sectionmark#1{\def\thesectionname{#1}\Sectionmark{#1}}

\ifthenelse{\equal{\glscounter}{sectionname}}%
{%
\ifcsundef{chapter}{}%
{%
\let\@gls@old@chapter\@chapter
\def\@chapter[#1]#2{\@gls@old@chapter[{#1}]{#2}%
\ifcsundef{hyperdef}{}{\hyperdef{section}{\thesectionname}{}}}%
}%
}%
{}


And activate it with:

\usepackage[counter=sectionname]{glossaries}


Didn't know why, but seems not working for refs on the first page :( Possible someone could post a solution without editing glossaries or Nicola could add such a feature to

-
This solution did not work correct, but hopefully it will help to find a working one. –  Megachip Nov 22 '12 at 23:14