# Correct spacing of indices after hyperref or glossaries commands

I get different spacing in math mode when using some sort of command on a variable and an index.

The text below gives what I think is correct spacing (between the variable 'M' and its index):

$M_1 M_1 M_1$


When using hyperref's \href command, the spacing gets larger (being wrong in my opinion):

$\href{http://tex.stackexchange.com/}{M}_1 \href{http://tex.stackexchange.com/}{M}_1 \href{http://tex.stackexchange.com/}{M}_1$


The same goes for glossaries' gls command:

$\gls{M}_1 \gls{M}_1 \gls{M}_1$


Note, that the index does not belong to the hyperlink or to the glossary entry logically, so it should neither be clickable nor colored. Instead, it should be possible to use an index, which itself is an hyperlink or a glossary link, i.e. \gls{M}_\gls{k}.

I have the feeling (but that might be wrong), that grouping is related to the problem. Putting the 'M' into a group gives correct spacing:

${M}_1 {M}_1 {M}_1$


Putting a group after the 'M' gives wrong spacing:

$M{}_1 M{}_1 M{}_1$


Please see the differences in the following table:

How can I get correct spacing when using commands like \href or \gls? If the solution depends on the command I would be primarily interested in the \gls command.

Just for completion, the code of the minimal (non-)working example (besides the coloring) is the following (thanks to Scott Prahl and egreg):

\documentclass{article}

\usepackage{glossaries}
\makeglossaries
\newglossaryentry{M}{name=M,description=M}

\begin{document}

\begin{tabular}{rl}
normal: & $M_1 M_1 M_1|$\\
href: & $\href{http://tex.stackexchange.com/}{M}_1 \href{http://tex.stackexchange.com/}{M}_1 \href{http://tex.stackexchange.com/}{M}_1|$\\
gls: & $\gls{M}_1 \gls{M}_1 \gls{M}_1|$\\
in group: & ${M}_1 {M}_1 {M}_1|$\\
group between: & $M{}_1 M{}_1 M{}_1|$
\end{tabular}

\end{document}

-

The picture shows what I get from the following minimal example: notice the optional argument to \gls:

\documentclass{article}

\usepackage{glossaries}
\makeglossaries
\newglossaryentry{M}{name=M,description=M}

\begin{document}

\begin{tabular}{rl}
normal: & $M_1 M_1 M_1|$\\
href: & $\href{http://tex.stackexchange.com/}{M_1} \href{http://tex.stackexchange.com/}{M_1} \href{http://tex.stackexchange.com/}{M_1}|$\\
gls: & $\gls{M}[_1] \gls{M}[_1] \gls{M}[_1]|$\\
in group: & ${M}_1 {M}_1 {M}_1|$\\
group between: & $M{}_1 M{}_1 M{}_1|$
\end{tabular}

\end{document}


As you can see the first four lines have exactly the same spacing.

-
Hmm. I see the same spacing you see for gls. The problem still remains for \href{url}{M}_1 though. –  Scott Prahl Mar 25 '12 at 23:21
@ScottPrahl \href{url}{M_1} as you suggested. –  egreg Mar 26 '12 at 5:32
Thanks for the complete minimal example, I am going to give one the next time as well. I have clarified the question as I need an index outside the hyperlink. Furthermore, if hyperref is loaded before glossaries, the indices are shifted as in the table given in the question. –  Patrick Häcker Mar 26 '12 at 19:54
@MMM See edited answer –  egreg Mar 26 '12 at 22:03

Just put the subscript inside the braces.

\documentclass[11pt]{article}
\begin{document}
\newcommand{\hk}[2]{\href{#1}{#2}\kern-1pt}
\begin{tabular}{rl}
normal: & $M_1 M_1 M_1|$\\
href surrounding: & $\href{http://tex.stackexchange.com/}{M_1} \href{http://tex.stackexchange.com/}{M_1} \href{http://tex.stackexchange.com/}{M_1}|$\\
hk: & $\hk{http://tex.stackexchange.com/}{M}_1 \hk{http://tex.stackexchange.com/}{M}_1 \hk{http://tex.stackexchange.com/}{M}_1|$\\
href: & $\href{http://tex.stackexchange.com/}{M}_1 \href{http://tex.stackexchange.com/}{M}_1 \href{http://tex.stackexchange.com/}{M}_1|$\\
\end{tabular}
\end{document}


With the following results

Getting the colors as you want them will be another hassle though.

-
Thanks for the answer. I should have been more specific. Putting the subscript insinde the braces does not generally work for me. If I define M as an vector and refer to its elements, e.g. M_1, via gls, I want to refer to M, so I cannot write \gls{M_1}, as that does not compile. This is because M_1 has never been defined as a glossaries symbol. I think it is now obvious, that I do not want to define multiple glossaries symbols for one vector. –  Patrick Häcker Mar 25 '12 at 20:55
Adding \kern-1pt before the underscore will not work either? Perhaps the kerning could be incorporated into a macro. –  Scott Prahl Mar 25 '12 at 21:35
Adding \kern-1pt gives better results, thanks, but it does not fit exactly. I would like to have something, which just does the right thing so I do not need to adjust every index manually. How can I corporate something into the underscore macro? I know how to do it in a normal macro (\let and \renew or patch), but how about the underscore macro? –  Patrick Häcker Mar 26 '12 at 20:16

The problem seems to be more complicated than I thought. I played around and came up with the following solution (I concentrate on \gls, but \href should work identically).

Thanks to Scott Prahl, without his answer I might have missed that the problem is related to kerning. Using the package lua-visual-debug, which shows the boxes and the spacing, highlights that the difference when using gls is the kerning between the variable and the index (small rectange, filled yellow).

A nice solution would be to remove that kerning. Unfortunately, I do not know how to achieve that, so I would be happy about comments. As long as the kerning cannot be removed, it must be compensated. Therefore instead of calling \gls directly, the newly defined \var is called. If there is an underscore following the variable, \gls is called, the kerning is compensated and the index can follow (thanks to egreg for reminding me of \gls's optional argument). Using the mighty xparse package, this results in.

\NewDocumentCommand{\var}{mt_}{%
\IfBooleanTF{#2}{%
\gls{#1}[\compensateKerning{#1}]_%
}{%
\gls{#1}%
}%
}


To compensate the kerning, whose length depends on the last character of the variable, it would be great to have a low-level function expecting a character sequence and returning the trailing kerning. Again, comments are very welcome. Until then, the idea is to render the content twice, one time with correct linking and one time with correct kerning, and saving both widths. The difference of both widths is the needed kerning compensation.

\newlength\withoutKerning
\newlength\withKerning
\newlength\kerningLength
\NewDocumentCommand{\compensateKerning}{m}{%
\settowidth{\withoutKerning}{\ensuremath{\gls{#1}_{}}}%
\settowidth{\withKerning}{\ensuremath{\gls{#1}[_{}]}}%
\deflength{\kerningLength}{\withKerning-\withoutKerning}%
\kern\kerningLength%
}


The results of \var using an index should be always correct, as the red rectangle (negative kerning) exactly matches the yellow rectangle.

The command \var also works, if no index is given, as this case is explicitly checked.

If instead of an index a power is used, theoretically it should not work, but as most if not all variables should be slanted to the right, the \compensateKerning command should return 0pt, which is quite the right thing, so practically it works, too.

Unfortunately, \var does not completely mimic the normal behaviour if an index and a power are both used.

To fix that, another command (or more advanced TeX skills are needed. Did I mention that comments are welcome?) is needed. By taking the index and the power as an argument, it can compensate the index only without affecting the power. It is defined as follows and its results are correct.

\NewDocumentCommand{\varIndexPower}{mmm}{%
\gls{#1}_{\compensateKerning{#1}#2}^{#3}%
}


At last, this is the code and the overall look.

\documentclass{article}
% \usepackage{lua-visual-debug}
\usepackage{xparse}

\usepackage{glossaries}
\makeglossaries
\newglossaryentry{M}{name=M,description=M}

\begin{document}

\newlength\withoutKerning
\newlength\withKerning
\newlength\kerningLength
\NewDocumentCommand{\compensateKerning}{m}{%
\settowidth{\withoutKerning}{\ensuremath{\gls{#1}_{}}}%
\settowidth{\withKerning}{\ensuremath{\gls{#1}[_{}]}}%
\deflength{\kerningLength}{\withKerning-\withoutKerning}%
\kern\kerningLength%
}

\NewDocumentCommand{\var}{mt_}{%
\IfBooleanTF{#2}{%
\gls{#1}[\compensateKerning{#1}]_%
}{%
\gls{#1}%
}%
}

\NewDocumentCommand{\varIndexPower}{mmm}{%
\gls{#1}_{\compensateKerning{#1}#2}^{#3}%
}

\begin{tabular}{rl}
normal: & $M_1 M_1 M_1|$\\
gls: & $\gls{M}_1 \gls{M}_1 \gls{M}_1|$\\
normal: & $M_1 M_1 M_1|$\\
var: & $\var{M}_1 \var{M}_1 \var{M}_1|$\\
no index normal: & $M M M|$\\
no index var: & $\var{M} \var{M} \var{M}|$\\
power normal: & $M^1 M^1 M^1|$\\
power var: & $\var{M}^1 \var{M}^1 \var{M}^1|$\\
power and index normal: & $M_1^1 M_1^1 M_1^1|$\\
power and index var: & $\var{M}_1^1 \var{M}_1^1 \var{M}_1^1|$\\
power and index normal: & $M_1^1 M_1^1 M_1^1|$\\
power and index var: & $\varIndexPower{M}{1}{1} \varIndexPower{M}{1}{1} \varIndexPower{M}{1}{1}|$\\
\end{tabular}
\end{document}


-
What's wrong with my solution, that doesn't produce the kern? Just checked with lua-visual-debug. –  egreg Mar 31 '12 at 21:57
@egreg Your solution is correct regarding the kerning and the wording of my original question, thanks a lot. Unfortunately, I was not able to clearly state my requirements, i.e. the index should not be part of the link. I updated the question accordingly. If your approach meets this new requirement, I did not fully understand it. It would be fair to mark your answer as the correct one, as you answered the original question. But if someone in the future has the same problem as I had, my answer might be more useful. I am rather new to stackexchange and seek advice what to accept here. –  Patrick Häcker Apr 1 '12 at 5:52
I understand your point, but I can't understand why you don't want the index to be part of the link. A "real world" application in the original question might have helped to begin with. –  egreg Apr 1 '12 at 9:31
I am sorry, you are absolutely correct. I try to do better next time. I use glossaries to automatically generate a list of variables, showing me where I used a variable. This way I can identify a variable's first use and explain its meaning there. This works if all variables are linked. Whenever I see a variable printed in black, I have forgotten linking. Thus the k in $\var{M}_k$ must be black to not intermix it with $\var{M}_{\var{k}}$. Besides, if k were linked to M (as optional argument in \gls) it were double-linked to different targets. I do not know what the result would be. –  Patrick Häcker Apr 1 '12 at 20:18