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


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


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


I tried removing the underscore in the first argument with


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?


You have to do the substitution beforehand:


\usepackage{lipsum} % just for the example





  • 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.