[Tex/LaTex] Placing glossary entries inside \section titles

glossariessectioningtable of contents

In my document, some glossary entries appear within the section titles. E.g.:

\documentclass{article}
    \usepackage{glossaries}
    \makeglossaries
    \begin{document}
        \tableofcontents
        \section{\newglossaryentry{1}{name=somename,description={Description.}}\gls{111}}
    \printglossaries
\end{document}

When compiling for a second time, it reports:

Package glossaries Error: Glossary entry `1' has already been defined.

I think maybe it appears in the table of contents, then in the page, then in the headings within the page, so it returns this error.

  • How can I place glossary entries inside \part's, \chapter's, and \section's while displaying a table of contents and page headings with the title of the current section?
  • The page number listed in the glossary should point to the first appearance of the entry within the title of the chapter as it appears within the page (i.e. not point to the location in the table of contents).

Best Answer

The "already defined" error comes from the fact that the \newglossaryentry makes its way into the ToC, which then executes it twice.

You should use the ToC-caption entry when performing such commands within section headings to avoid the duplication:

\section[<toc-entry>]{<heading>}

enter image description here

\documentclass{article}
\usepackage{glossaries}% http://ctan.org/pkg/glossaries
\makeglossaries
\begin{document}
\tableofcontents
\section[somename]{\newglossaryentry{1}{name=somename,description={Description.}}\gls{1}}
\printglossaries
\end{document}
Related Question