5

hyperref seems not to work with \_ in the first argument of either hypertarget or hyperlink:

 \hypertarget{foo\_}{foo\_}
 \hyperlink{foo\_}{foo\_}

When clicking on the hyperlink it always jumps to the beginning of the document. I get the correct behavior by removing \_ or, for example, writing

 \hypertarget{foo_}{foo\_}
 \hyperlink{foo_}{foo\_}

The problem is that the two functions are used as a part of custom commands which require both arguments to be the same.

\newcommand{\mytarget}[1]{\hypertarget{#1}{#1}}
\newcommand{\mylink}[1]{\hyperlink{#1}{#1}}

I tried removing the underscore in the first argument with

\newcommand{\mytarget}[1]{\hypertarget{\StrSubstitute{#1}{\_}{dummy}}{#1}}
\newcommand{\mylink}[1]{\hyperlink\StrSubstitute{#1}{\_}{dummy}}{#1}}

but it gives me a compilation error. Is it possible to renew the two hyperref commands so that some characters are replaced in the first argument?

2

You have to do the substitution beforehand:

\documentclass{article}

\usepackage{lipsum} % just for the example
\usepackage{xstring}
\usepackage{hyperref}

\newcommand{\makesub}[1]{%
  \saveexpandmode\noexpandarg
  \StrSubstitute{#1}{\_}{_}[\temp]%
  \restoreexpandmode
}
\newcommand{\mytarget}[1]{%
  \makesub{#1}%
  \hypertarget{\temp}{#1}%
}
\newcommand{\mylink}[1]{%
  \makesub{#1}%
  \hyperlink{\temp}{#1}%
}

\begin{document}

\mytarget{foo\_bar}
\lipsum

\mylink{foo\_bar}

\end{document}
  • Could you explain to me what is the function of saveexpandmode and noexpandarg? I'm not sure if they're required for my text. The relevant words only have the form foo1\_foo2\_.... – Raul Laasner Dec 12 '15 at 1:05
  • @RaulLaasner They're not so important if that's the only application of xstring in your document: in that case just \noexpandarg in the preamble is sufficient. Read in the manual about \fullexpandarg and friends. – egreg Dec 12 '15 at 10:52
  • @egreg It seems to me like this is an inconsistency in hyperref (hypertarget supports PDF Standard characters and does not expand while hyperlink fully expands?). I would classify this inconsistency as a bug. Maybe I am wrong. Could you explain the reasoning for this? – Jonathan Komar Apr 12 '16 at 8:19
  • @macmadness86 Underscores are supported in the second argument of \hyperref; it's the OP who wants to use just one argument both for typesetting and the link. – egreg Apr 12 '16 at 8:55
  • @egreg My issue is with expansion of tokens in arg1. If you have arg1 in both \hypertarget and \hyperlink set to foo\_, then I would expect the link to work. hyperref should interpret the target as 5 chars AND the link as 5 chars. What I see happening is the total chars for target=5 and total chars for link=4 (because \_ in link gets interpreted as one char, but as two chars in the target). – Jonathan Komar Apr 12 '16 at 9:09

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.