3

Feeding

\documentclass{article}
\pagestyle{empty}
\usepackage[makeindex,toc,nogroupskip,nomain]{glossaries-extra}
\newglossary[nlg]{notation}{not}{ntn}{Symbols}
%%% See http://tex.stackexchange.com/questions/399338/hyperref-glossaries-latex-bad-spacing-around-math-relations
\glssetcategoryattribute{mathrelation}{hyperoutside}{false}%% Using "false" gives good math-relation spacing in general but applying \mathord fails to kill the pre-spacing. Using "true" kills all spacing, unfortunately.
\glssetcategoryattribute{mathrelation}{textformat}{mathrel}
\newglossaryentry{not:prefixRelation}{type=notation, name={\(\sqsubseteq\)}, category=mathrelation, text=\sqsubseteq, sort=prefixRelation, description={A prefix relation. Given finite or infinite sequences \(s\) and \(s'\), the relation \(s \prefixRelationSymbol s'\) holds iff a sequence \(r\) exists that satisfies \(sr = s'\).}}
\begin{document}\noindent
\(x\gls{not:prefixRelation}z\)\\
\(x{\gls{not:prefixRelation}}z\)\\
\(x\mathord{\gls{not:prefixRelation}}z\)
\end{document}

to any of [pdf|xe|lua]latex from the current TeX Live 2022 (as of 2023-01-06) fails to kill visible horizontal space between ๐‘ฅ and โŠ‘:

๐‘ฅ โŠ‘๐‘ง

output with false hyperoutside

However, due to braces around the relation symbol or due to \mathord, all space in the second and third output lines should be killed, and this was indeed the case a few months ago.

Setting hyperoutside to true or commenting out the line setting hyperoutside kills all horizontal space:

๐‘ฅโŠ‘๐‘ง

output with hyperoutside being true

Of course, this is undesirable: we should see the usual math-relation horizontal space in the first output line.

Any help on typesetting a glossarized relation symbol properly? That is, without \mathord{โ€ฆ} around \gls{โ€ฆ}, we should have the expected math-relation space (e.g., 5 mu to the next ordinary symbol), and with \mathord{โ€ฆ} around \gls{โ€ฆ}, we should have no extra horizontal space around the relation symbol.

1 Answer 1

3

Commands like \gls internally use code that varies depending on whether or not hyperlinks are supported. The internal command takes two arguments: the label (for the hyperlink target) and the hyperlink text. If hyperlinks aren't supported, the first argument is ignored. Either way, the hyperoutside conditional determines whether or not the command that has the potential to form a hyperlink is outside or inside the encapsulating formatting command. So even though hyperref isn't loaded, the conditional still has an effect.

In order to protect style leakage in the event of nested \gls (that is, where \gls occurs inside a field value of another glossary entry), internal grouping is needed. This automatically occurs with \hyperlink. The glossaries-extra package redefines \glsdonohyperlink to ensure the grouping is added and also makes some adjustments to further mitigate against the effects of nested links. It's this internal grouping that is causing the problem with the spacing in math mode.

If you have hyperoutside=true, regardless of whether or not you use hyperref, then the encapsulating command will be in the link text argument, which means that it's inside the grouping, so you essentially have x{\mathrel{\sqsubseteq}}z, which removes the spacing.

If you have hyperoutside=false, then the encapsulating command (\mathrel in this case) has the internal code (that has the potential to create a hyperlink) within its argument, so you essentially have x\mathrel{{\sqsubseteq}}z.

Example:

\documentclass{article}
\usepackage{glossaries-extra}
\newglossaryentry{rel}{name={\sqsubseteq},description={...},category={mathrelation}}
\begin{document}
Test:

\(x\mathrel{\sqsubseteq} z\)

\(x\gls[textformat=mathrel,hyperoutside=false]{rel}z\)\par

\(x\gls[textformat=mathrel,hyperoutside=true]{rel}z\)\par

\glssetcategoryattribute{mathrelation}{textformat}{mathrel}
\glssetcategoryattribute{mathrelation}{hyperoutside}{false}

\(x\gls{rel}z\)\par
\end{document}

image of result

So that's why commenting out the line that sets hyperoutside to false removes the spacing, because the default setting is true.

The textformat option overrides the textformat attribute:

\documentclass{article}
\usepackage{glossaries-extra}

\glssetcategoryattribute{mathrelation}{textformat}{mathrel}
\glssetcategoryattribute{mathrelation}{hyperoutside}{false}

\newglossaryentry{rel}{name={\sqsubseteq},description={...},category={mathrelation}}
\begin{document}
Using attribute textformat=mathrel:

\(x\gls{rel}z\)

Using option textformat=mathord (mathrel not used):

\(x\gls[textformat=mathord]{rel}z\)
\end{document}

image of document: no space around second example

If you need to do this a lot but don't like the inconvenience of typing the optional argument, then you can define a command that works like \gls but automatically sets the required option:

\documentclass{article}
\usepackage{glossaries-extra}

\glssetcategoryattribute{mathrelation}{textformat}{mathrel}
\glssetcategoryattribute{mathrelation}{hyperoutside}{false}

\glsxtrnewgls[textformat=mathord]{}{\gmord}

\newglossaryentry{rel}{name={\sqsubseteq},description={...},category={mathrelation}}
\begin{document}
Using attribute textformat=mathrel:

\(x\gls{rel}z\)

Using option textformat=mathord (mathrel not used):

\(x\gls[textformat=mathord]{rel}z\)

Equivalently:

\(x\gmord{rel}z\)
\end{document}

Note that the base glossaries package doesn't include grouping if hyperref isn't used. This means there's an inconsistency between documents with hyperlinks enabled and those without. The glossaries manual advises against nested use of commands like \gls because this lack of grouping causes style leakage (that is, the placeholder commands used by the formatting styles are changed and not restored).

I've considered adding the grouping for the base glossaries package, which can be done by redefining \glsdonohyperlink to include grouping, however the base glossaries package doesn't have the hyperoutside option and this example demonstrates the problems that can be caused by that grouping. So, if you just use the base glossaries package, don't nest commands like \gls but if you have hyperlinks, the implicit grouping added by \hyperlink will cause spacing issues in math mode.

I think the reason why \mathord{\gls{rel}} introduces spacing is because of the grouped whatsit that performs the indexing. You essentially have:

\documentclass{article}
\makeindex
\begin{document}
Analogous to \verb|x\mathord{\gls[textformat=mathrel,hyperoutside=false]{rel}}z|:

\(x\mathord{{\index{rel}}\mathrel{{\sqsubseteq}}}z\)

Analogous to \verb|x\gls[textformat=mathord,hyperoutside=false]{rel}z|:

\(x{\index{rel}}\mathord{{\sqsubseteq}}z\)

\end{document}

image of result: the first example has extra spacing

The grouping around the whatsit was introduced to fix issue #189. That issue was triggered by the fix to issue #175, which again involved incorrect spacing caused by the whatsit. Basically, the indexing whatsit and the grouping that are required to make \gls work correctly interfere with math mode spacing. The only workaround is to use hyperoutside=false and textformat=mathord.

If you don't want the extra grouping you can remove it with the following:

\renewcommand*{\glsencapwrcontent}[1]{#1}
\renewcommand*{\glsdonohyperlink}[2]{#2}

The first line will lead you back to issue #175 and the second won't allow nested use of commands like \gls. If you want to encapsulate both the whatsit and the content with a single group you can achieve that with:

\renewcommand*{\glslinkwrcontent}[1]{{#1}}

but that will lead you to issue #189.

6
  • Thanks! Alright. If with hyperoutside=false we essentially have x\mathrel{{\sqsubseteq}}z, then putting \mathord{โ€ฆ} around \mathrel{โ€ฆ} should yield โ€œ๐‘ฅโŠ‘๐‘งโ€ without any spacing, shouldn't it? In my test, \(x\mathord{\mathrel{{\sqsubseteq}}}z\) indeed yields โ€œ๐‘ฅโŠ‘๐‘งโ€ without any spacing. However, both \(x\mathord{\gls{rel}}z\) and \(x\mathord{\gls[textformat=mathrel,hyperoutside=false]{rel}}z\) produce unequal spacing โ€œ๐‘ฅ โŠ‘๐‘งโ€. Or has the behaviour changed, and \mathord{โ€ฆ} around \gls{โ€ฆ} (with hyperoutside being false) no longer supposed to kill the math-relation spacing?
    – user282514
    Commented Jan 8, 2023 at 19:01
  • 1
    @AlbertNash You can counteract \mathrel with \(x\gls[textformat=mathord,hyperoutside=false]{rel}z\). The textformat option overrides the textformat category attribute. Commented Jan 8, 2023 at 19:21
  • Alright, got it, \(x\gls[textformat=mathord,hyperoutside=false]{rel}z\) indeed typesets โ€œ๐‘ฅโŠ‘๐‘งโ€, thank you! Please feel free to put your comment into your answer. In the long run, are \(x\mathord{\gls[hyperoutside=false]{rel}}z\) and \(x\mathord{\gls{rel}}z\) with hyperoutside previously set to false NOT supposed to work (i.e., they are not supposed to enforce no horizontal spacing around the relation symbol), or are they supposed to work (i.e., they are supposed to typeset an unspaced relation symbol) and are going to be repaired in a future version of glossaries(-extra)?
    – user282514
    Commented Jan 8, 2023 at 22:03
  • Wow, it's a long chain of connected issue reports. Thanks for the explanation! As for x{\index{rel}}\mathord{{\sqsubseteq}}z, being a demonstration of what x\gls[textformat=mathord,hyperoutside=false]{rel}z is equivalent to, I see lots of braces, perhaps, too many. IMHO, without hyperref, we should probably see something equivalent to x\mathord{\index{rel}\sqsubseteq}z, and with hyperref, probably something similar to x\mathord{\index{rel}\hyperlink{targetInAGlossary}{\sqsubseteq}}z or x\mathord{\hyperlink{targetInAGlossary}{\index{rel}\sqsubseteq}}z
    – user282514
    Commented Jan 10, 2023 at 0:40
  • @AlbertNash The braces are necessary around the content even without hyperref. I've spent years telling people not to nest \gls but they still do because it's convenient (and possibly because they don't read the manual) so I had to find a workaround to allow it. The braces around the whatsit are also necessary see tex.stackexchange.com/a/621504 I have tried braces that encapsulate both the whatsit and the content (fix for issue 175) but that led to issue 189. The only way to fix that was to have the braces around just the whatsit. Commented Jan 10, 2023 at 10:15

You must log in to answer this question.