2

I use hyperrefs extensively in my document and I define them like this:

\hyperref[label]{some text \ref*{label}}

It's really annoying that I need to repeat the label twice. Is it possible to parameterize the content of {} with contents of []?

I'd like to achieve something like this:

\hyperref[label]{some text \ref*{#1}}

I guess it is possible if you define your own command, but the question is if it is possible to do that without defining any commands.

2
  • 3
    \newcommand{\myref}[2]{\hyperref{#1}{#2 \ref*{#1}} Aug 9, 2015 at 14:04
  • Yes, that's a possible solution but I pointed out that I don't want creating a new command. Aug 9, 2015 at 14:06

1 Answer 1

5

It is not possible in general. TeX does not provide such kind of access. Also this will not work for expandable macros. A definition of a macro, which contains the argument would destroy the expandability, because a definition is not expandable.

In this case, \hyperref is not expandable anyway, thus the latter argument does not apply. The label form with the optional argument \hyperref[<label>]{...} is implemented internally by macro \label@hyperref, which can be redefined to remember the label argument by a macro, e.g. \arglabel:

\documentclass{article}
\usepackage{hyperref}

\makeatletter
\let\org@label@hyperref\label@hyperref
\def\label@hyperref[#1]{%
  \def\arglabel{#1}%
  \org@label@hyperref[{#1}]% or \org@label@hyperref[\arglabel]%
}
\makeatother

\begin{document}
\section{Hello}
\label{sec:hello}

\hyperref[sec:hello]{Section~\ref*{\arglabel}}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .