2

I want to create links inside a pdf and actually I use \hypertarget{label}{target caption} and \hyperlink{label}{link caption} with the package hyperref. Perhaps it's possible to use something like |name| to create \hypertarget{label_name}{name} and ||name|| (or §name§) to create \hyperlink{label_name}{name}. Is it possible ? Perhaps do you know a package to do this ?

Alain Matthes
  • 95,075

1 Answers1

5

Yes, it's possible.

\documentclass{article}
\usepackage{hyperref}

\makeatletter
\catcode`|=\active
\protected\def|{\ifmmode\vert\else\expandafter\alain@vert\fi}
\def\alain@vert{\@ifnextchar|{\alain@link}{\alain@target}}
\def\alain@link|#1||{\hyperlink{label_#1}{#1}}
\def\alain@target#1|{\hypertarget{label_#1}{#1}}
\makeatother

\begin{document}

Hypertarget: |name|

$|x|$

\newpage

Hyperlink: ||name||

\end{document}
egreg
  • 1,121,712
  • Thanks I need to study your code tomorrow but it' certainly the good answer – Alain Matthes Mar 22 '16 at 23:23
  • Thanks egreg it's fine! What's the best way to manage this with \AtBeginDocument{\MakeShortVerb{\|}} With shortvrb package I have \DeleteShortVerb because sometimes the symbol | appears in my doc as mark of angles. So I need to change the active character but what other precautions should I take? – Alain Matthes Mar 23 '16 at 07:36
  • @AlainMatthes You can't have | doing a double role: either you use it for hyperlinks or for verbatim. – egreg Mar 23 '16 at 07:57
  • yes I understand that ! – Alain Matthes Mar 23 '16 at 08:34
  • Perhaps it's a good idea to create a macro like in your answer http://tex.stackexchange.com/questions/184438/how-do-you-define-macros-to-toggle-a-character-active-and-define-a-macro-for-it to redefine the correct catcode. – Alain Matthes Mar 23 '16 at 08:42