4

[TeXstudio on Mac]

I'm using the glossaries package, and I'm wondering how best to handle recursion?

For example, with the following glossary terms defined (note the tcpip definition invokes the other two recursively:

\newglossaryentry{tcp}{name=TCP, description={Transmission Control Protocol}}
\newglossaryentry{ip}{name=IP, description={Internet Protocol}}
\newglossaryentry{tcpip}{name=TCP/IP, description={Internet protocol suite comprising of both the \gls{tcp} and \gls{ip} protocols}}

I include the term TCP/IP in my document using the \gls{tcpip} command. As a result of this command, the glossary now includes the entry:

TCP/IP Internet protocol suite comprising of both the TCP and IP protocols

However, in addition to the above entry, I was hoping that definitions for both TCP and IP would also now be included.

The glossary package is able to correctly substitute the nested \gls commands, but doesn't add the associated entries to the glossary. Thus the reader, in my example, is left wondering what TCP and IP mean.

Update after Nicola posted her answer

My present build sequence (using TeXstudio on Mac) is:

txs:///compile | txs:///makeglossaries | txs:///bibliography | txs:///compile| txs:///compile | txs:///makecopy | txs:///view

And I have one \makeglossaries in the preamble of my document, as you do in your example.

So, are you saying that I need to insert an additional txs:///makeglossaries into my build command after one of the succeeding compile invocations?

  • @TeXnician: Where would I put those commands? My understanding, so far (and I've just started using this package) is that terms appear in the glossary only when they are used in the text. – gone Apr 16 '18 at 16:13
  • Your use of \gls in the description is the method I recommend, but you need an extra LaTeX+makeglossaries in your build process. See also Nested Links. – Nicola Talbot Apr 16 '18 at 16:16
  • @NicolaTalbot: Thanks, I'm already doing that. @TeXnician, It worked when I used \glsadd in the definition of tcpip. Funny thing was that I only needed to apply that function to one of the recursively defined terms, tcp in my case, and both TCP and IP have shown up in the glossary. Cheers! – gone Apr 16 '18 at 16:20
  • @TeXnician: Hmm, I spoke kind of too early. I needed to two builds. However each build already invokes makeglossaries. So it appears that I need to run makeglossaries an equivalent number of times to the depth of the recursion? – gone Apr 16 '18 at 16:24
  • 1
    @gone It can be handled by bib2gls and glossaries-extra. I'll update my answer to include that. – Nicola Talbot Apr 16 '18 at 16:33
8

Consider this example:

\documentclass{report}

\usepackage{glossaries}

\makeglossaries

\newglossaryentry{tcp}{name=TCP, description={Transmission Control Protocol}}
\newglossaryentry{ip}{name=IP, description={Internet Protocol}}
\newglossaryentry{tcpip}{name=TCP/IP, description={Internet protocol suite 
comprising of both the \gls{tcp} and \gls{ip} protocols}}

\begin{document}
\chapter{Sample}

\gls{tcpip}.

\printglossaries

\end{document}

On the first LaTeX run, tcpip is indexed on page 1. The glossary doesn't exists so the description isn't typeset, which means that the \gls instances in the description aren't processed. So, if the document is called myDoc.tex, if the build process is:

pdflatex myDoc
makeglossaries myDoc
pdflatex myDoc

then the PDF only shows TCP/IP in the glossary:

Glossary TCP/IP Internet protocol suite comprising of both the TCP and IP protocols. 1

but if you look in the .glo file you'll find that the TCP and IP entries have been indexed:

\glossaryentry{TCP/IP?\glossentry{tcpip}|setentrycounter[]{page}\glsnumberformat}{1}
\glossaryentry{TCP?\glossentry{tcp}|setentrycounter[]{page}\glsnumberformat}{2}
\glossaryentry{IP?\glossentry{ip}|setentrycounter[]{page}\glsnumberformat}{2}

They were indexed on the second pdflatex when the glossary was being typeset. An extra makeglossaries and LaTeX call are required to update the glossary, so the complete build process requires:

pdflatex myDoc
makeglossaries myDoc
pdflatex myDoc
makeglossaries myDoc
pdflatex myDoc

Now IP and TCP show in the glossary (with the page number 2, which is where they were indexed in the tcpip description).

Glossary IP Internet Protocol. 2 TCP Transmission Control Protocol. 2 TCP/IP Internet protocol suite comprising of both the TCP and IP protocols. 1

If the dependent terms also reference a term in their description then that term can't be indexed until the dependent terms have their descriptions typeset in the glossary. For example:

\documentclass{report}

\usepackage{glossaries}

\makeglossaries

\newglossaryentry{transmission}{name={transmission},
  description={programme or signal that is broadcast or sent out}}
\newglossaryentry{tcp}{name=TCP,
  description={\Gls{transmission} Control Protocol}}
\newglossaryentry{ip}{name=IP, description={Internet Protocol}}
\newglossaryentry{tcpip}{name=TCP/IP, description={Internet protocol suite 
comprising of both the \gls{tcp} and \gls{ip} protocols}}

\begin{document}
\chapter{Sample}

\gls{tcpip}.

\printglossaries

\end{document}

This now requires:

pdflatex myDoc
makeglossaries myDoc
pdflatex myDoc
makeglossaries myDoc
pdflatex myDoc
makeglossaries myDoc
pdflatex myDoc

If you want an automated method that follows through all the dependencies then you need to switch to bib2gls and glossaries-extra. This requires that the entries are defined in a .bib file. For example, entries.bib:

% Encoding: UTF-8

@entry{transmission,
  name={transmission},
  description = {programme or signal that is broadcast or sent out}
}

@entry{tcp,
  name = {TCP},
  description = {\Gls{transmission} Control Protocol}
}

@entry{ip,
  name = {IP},
  description = {Internet Protocol}
}

@entry{tcpip,
  name = {TCP/IP},
  description = {Internet protocol suite 
   comprising of both the \gls{tcp} and \gls{ip} protocols}
}

The document (myDoc.tex) now looks like:

\documentclass{report}

\usepackage[record,% using bib2gls
 postpunc=dot]{glossaries-extra}

\GlsXtrLoadResources[% using bib2gls
  src = {entries}% terms defined in entries.bib
]

\begin{document}
\chapter{Sample}

\gls{tcpip}.

\printunsrtglossaries % <-- 'unsrt' variant needed with bib2gls

\end{document}

The document build process is now:

pdflatex myDoc
bib2gls myDoc
pdflatex myDoc

If you want a visible separation between letter groups, you need the --group (or -g) switch:

pdflatex myDoc
bib2gls -g myDoc
pdflatex myDoc

The glossary now includes all the dependent terms:

Glossary IP Internet Protocol. TCP Transmission Control Protocol. TCP/IP Internet protocol suite comprising of both the TCP and IP protocols. 1 transmission programme or signal that is broadcast or sent out.

but note the dependent terms don't include the page number. Those can only be obtained on a subsequent bib2gls + LaTeX call so the complete build process needs to be:

pdflatex myDoc
bib2gls -g myDoc
pdflatex myDoc
bib2gls -g myDoc
pdflatex myDoc

but at least this limits the maximum number of calls required to ensure the locations are up to date.

Glossary IP Internet Protocol. 2 TCP Transmission Control Protocol. 2 TCP/IP Internet protocol suite comprising of both the TCP and IP protocols. 1 transmission programme or signal that is broadcast or sent out. 2

You may prefer to omit the page numbers for the entries that only appear in the document and only have page numbers for the entries that are explicitly indexed in the document. In this case, you can change the default format (encap) to glsignore at the start of the glossary:

\documentclass{report}

\usepackage[record,% using bib2gls
 postpunc=dot]{glossaries-extra}

\GlsXtrLoadResources[% using bib2gls
  src = {entries}% terms defined in entries.bib
]

\begin{document}
\chapter{Sample}

\gls{tcpip}.

\GlsXtrSetDefaultNumberFormat{glsignore}
\printunsrtglossaries % <-- 'unsrt' variant needed with bib2gls

\end{document}

Now the document is up-to-date with just the sequence:

pdflatex myDoc
bib2gls --group myDoc
pdflatex myDoc

(Naturally, if you need to add a bibliography etc, then the build process becomes more complicated.) You may also need an extra bib2gls+LaTeX after deleting all the temporary files since \gls just produces ?? on the first LaTeX call. This can only be resolved once bib2gls has created the file input by \GlsXtrLoadResources and this may cause a shift in the page breaks once \gls produces the correct text. (This doesn't affect the appear of the entries in the glossary, but may alter the location lists slightly.)

  • Thanks. However I'm still a little confused as to where the additional makeglossaries should go. I have extended my OP to include my current build settings and document construction. – gone Apr 16 '18 at 16:45
  • @gone I think you'd need txs:///compile | txs:///makeglossaries | txs:///compile | txs:///makeglossaries | txs:///bibliography | txs:///compile| txs:///compile | txs:///makecopy | txs:///view but with bib2gls you'd just need txs:///compile | txs:///bib2gls | txs:///bibliography | txs:///compile| txs:///compile | txs:///makecopy | txs:///view (You'd need to configure TeXStudio to recognise bib2gls. I would imagine that you'd just need to follow the instructions for makeglossaries but replace makeglossaries with bib2gls, but I've never used TeXStudio.) – Nicola Talbot Apr 16 '18 at 16:58
  • Cheers! I'll stick with the makeglossaries approach for now as it satisfies my present need. The additional information will come in handy if I have a more complicated piece of work to do. – gone Apr 16 '18 at 17:18

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.