# One glossary for different files

I'm trying to do a symbols list for different files. I have two files file1.tex and file2.tex, I want to have a symbols list containing the symbols of both files, but I don't know how to write it correctly in order to obtain the references to the different files. I have a symbols.tex with the symbols from both files in the format:

\newglossaryentry{id}{
.
.
.
}


and the output for a symbol s1 in file1.tex should be

s1 description, page reference (in file1.pdf)


and for file2.pdf I want to achieve something like

s1 description, 1-page reference (the number 1 being a symbol to be aware that this symbol occurs in file1)


to reference equations, figures, etc. between files I'm using xr package and latexmkrc as suggested in ShareLaTeX, but I cannot figure out how to do something similar with glossaries (I'm very new with glossaries).

• Do you mean something like the sample-suppl-main-hyp.pdf, which includes a cross-reference to another document? (In this case, the cross-reference is a section number, but it can be the page number.) – Nicola Talbot Jul 26 '18 at 17:00
• @NicolaTalbot Yes is exactly like that!, can you post it as an answer because a comment cannot be marked as an answer. Code would be appreciated, because I still have some doubts with the \theHsection and the \theHvalue – Luis Turcio Jul 26 '18 at 18:28

This can be done with the glossaries-extra extension package, but it's quite limited unless you use bib2gls. At least version 1.14 of glossaries-extra is required, although the latest version is best.

I'll start with the standard makeindex method. In this case only the internal references are automated. The cross-references are done manually, which is sub-optimal.

Here's the first document called test-supp.tex:

\documentclass{report}

\usepackage{glossaries-extra}

\makeglossaries

\newglossaryentry{sample1}{name={sample 1},
description={first example}}

\newglossaryentry{sample2}{name={sample 2},
description={second example}}

\begin{document}
\tableofcontents

\chapter{Sample}
\gls{sample1} and \gls{sample2}.

\printglossaries
\end{document}


The build process is:

pdflatex test-supp
makeglossaries test-supp
pdflatex test-supp


The glossary contains two entries indexed on page 2:

The second document is called test.tex and contains:

\documentclass{article}

\usepackage{glossaries-extra}

\makeglossaries

\glssetcategoryattribute{supplementary}{externallocation}{test-supp.pdf}

\newglossaryentry{sample1}{category={supplementary},
name={sample 1},
description={first example}}

\newglossaryentry{sample3}{name={sample 3},
description={third example}}

\begin{document}
\section{Sample}
In this document: \gls{sample1} and \gls{sample3}.
(Test link to check the PDF viewer:
\hyperref{test-supp.pdf}{}{page.2}{2}.)

\printglossaries
% manually index other document:
\end{document}


This requires knowing that sample1 was indexed on page 2 of the cross-referenced document. The document build process is:

pdflatex test
makeglossaries test
pdflatex test


The external links are in cyan as that's the default colour with hyperref's colorlinks option.

A more automated method can be achieved with bib2gls. The entries are now defined in the file sample-entries.bib, which contains:

% Encoding: UTF-8
@entry{sample1,
name = {sample 1},
description = {first example}
}

@entry{sample2,
name = {sample 2},
description = {second example}
}

@entry{sample3,
name = {sample 3},
description = {third example}
}


The test-supp.tex file needs to be modified to:

\documentclass{report}

\usepackage[record % requires bib2gls
]{glossaries-extra}

src={sample-entries}% entries defined in sample-entries.bib
]

\begin{document}
\tableofcontents

\chapter{Sample}
\gls{sample1} and \gls{sample2}.

\printunsrtglossaries %<-- changed
\end{document}


The document build process is now:

pdflatex test-supp
bib2gls test-supp
pdflatex test-supp


The result is the same as before.

The second document test.tex needs to be changed to:

\documentclass{article}

\usepackage[record % using bib2gls
]{glossaries-extra}

% separator between locations in this document and in the other
% document:
\newcommand{\bibglssupplementalsep}{; }

src=sample-entries,% data in sample-entries.bib
supplemental-locations=test-supp,% records in test-supp.aux
% set the category label for entries that have supplemental
% locations:
supplemental-category={supplemental}
]

\begin{document}
\section{Sample}
In this document: \gls{sample1} and \gls{sample3}.
(Test link to check the PDF viewer:
\hyperref{test-supp.pdf}{}{page.2}{2}.)

\printunsrtglossaries % <-- changed
\end{document}


The build process is now:

pdflatex test
bib2gls test
pdflatex test


The result is nearly the same as before except that the external location has been separated from the internal one with a semi-colon.

You can change the selection criteria so that it includes all the entries defined in the source bib file that were indexed in the cross-referenced file with supplemental-selection={all}:

\GlsXtrLoadResources[
src=sample-entries,% data in sample-entries.bib
supplemental-locations=test-supp,% records in test-supp.aux
% set the category label for entries that have supplemental
% locations:
supplemental-category={supplemental},
% Add all entries in the supplemental file that have been
% defined in sample-entries.bib:
supplemental-selection={all}
]


Or you can have a comma-separated list of entries that have been indexed in the other document. For example:

\GlsXtrLoadResources[
src=sample-entries,% data in sample-entries.bib
supplemental-locations=test-supp,% records in test-supp.aux
% set the category label for entries that have supplemental
% locations:
supplemental-category={supplemental},
supplemental-selection={sample2}
]


## Further Customization

The supplemental location list is encapsulated with \bibglssupplemental{n}{locations} where n is the number of locations and locations is the code to format the location list. In the above example, this is:

\bibglssupplemental{1}{\setentrycounter[]{page}\glsxtrsupphypernumber{2}}


There's only one location, so the first argument is 1. Each location first identifies the counter (in this case page) to allow the correct formation of the hyperlink reference. The default definition is provided in the .glstex file created by bib2gls:

\providecommand{\bibglssupplemental}[2]{#2}


which ignores the first argument.

The actual location is encapsulated with \glsxtrsupphypernumber{location}. This is defined as follows:

\newcommand*{\glsxtrsupphypernumber}[1]{%
{%
\glshasattribute{\glscurrententrylabel}{externallocation}%
{%
\def\glsxtrsupplocationurl{%
\glsgetattribute{\glscurrententrylabel}{externallocation}}%
}%
{%
\def\glsxtrsupplocationurl{}%
}%
\glshypernumber{#1}%
}%
}


This first checks if the entry's category has the externallocation attribute set. If it does, then it defines \glsxtrsupplocationurl to that value, which makes \glshypernumber an external link. Otherwise \glsxtrsupplocationurl is set to empty, in which case \glshypernumber is just a normal internal link.

So it's possible to make some adjustments to the way the supplementary locations are displayed. For example, to add a prefix to the entire list:

\newcommand{\bibglssupplemental}[2]{%
supplementary
\ifnum#1=1
reference:
\else
references:
\fi
#2%
}


(This needs to go before \GlsXtrLoadResources so that the \providecommand in the .glstex file does nothing.)

To prefix each supplementary location:

\renewcommand*{\glsxtrsupphypernumber}[1]{%
{%
\glshasattribute{\glscurrententrylabel}{externallocation}%
{%
\def\glsxtrsupplocationurl{%
\glsgetattribute{\glscurrententrylabel}{externallocation}}%
[file: \glsxtrsupplocationurl]
}%
{%
\def\glsxtrsupplocationurl{}%
}%
\glshypernumber{#1}%
}%
}


(This needs \renewcommand as it's defined by glossaries-extra.sty not in the .glstex file.) These two modifications produce:

If the supplementary locations are concatenated into a range (for example, 2--4) then the location list will be in the form:

\bibglssupplemental{3}{\setentrycounter[]{page}\glsxtrsupphypernumber{2}\delimR \setentrycounter[]{page}\glsxtrsupphypernumber{4}}


The range delimiter \delimR is the same as for ordinary internal locations (--) and is outside of the encapsulator, so the above modification to \glsxtrsupphypernumber will alter both the start and end of the range:

• just one more thing about this (I don't know if I have to post another question or a comment is enough) I already change my glossaries files to use bib2gls, which I believe is great, but I would like to do something with the page references of the glossaries, in your example prints: sample 1 first example 1; 2. But some times can happened something like blah foo 1; 1, which I believe is not nice. Is possible to achieve something like sample 1 first example 1; I-2? My first attempt was \newcommand{\bibglssupplementalsep}{; I-}but I actually have 3 files so this is not good enough – Luis Turcio Aug 8 '18 at 23:56
• @LuisTurcio I've updated my answer. It will be more complicated if there are multiple external files, so you may need to post a follow-up question for that. – Nicola Talbot Aug 9 '18 at 12:22
• sorry for this, but I'm not sure if you saw the follow up question and I can't figure out how to do the customization. For your comment "It will be more complicated if there are multiple external files" I can say that I'm not smart enough to achieve what I want – Luis Turcio Aug 17 '18 at 3:17