I'm trying to accomplish something similar to autoref, but more customizable. In particular, I'm making a command which takes in a name for a label, and the text that should be output when that label is referenced. Then using the package hyperref, one can easily create a command that outputs the desired text:
\newcommand{\hlink}[1]{\hyperref[label text #1]{\csname label text #1\endcsname}}
All of that background isn't really the issue. The issue is that sometimes thinks like font choice muck this up. Really the issue is how to expand and save temporary info without expanding things like font choice. In particular, I can currently can assign the link text to the label text as follows: suppose \tempinfo
is some temporary information that I want stored.
\def\tempinfo{AAA}
\expandafter\label{label text \tempinfo}
\expandafter\xdef\csname label text \tempinfo\endcsname{blah link text \tempinfo}
Now \tempinfo
is saved in the command. If I change \tempinfo
, I still get the same output:
\csname label text AAA\endcsname\\
\def\tempinfo{BBB}
\csname label text AAA\endcsname
outputs
blah link text AAA
blah link text AAA
So far so good, but what if instead of blah link text
I want $\mathrm{blah}$
or \textit{italics}
? I can't use \xdef
(or \edef
) anymore without encountering errors. But if I just use \def
, \tempinfo
isn't saved. Running the same stuff as before but with "\xdef
" replaced by "\def
" yields
blah link text AAA
blah link text BBB
So I'm not sure if there's a way to get past this issue, or if there's a better solution available that I haven't found yet.
EDIT: Here's an example. Currently, \hlink
returns The Example Theorem (0.1)
without any formatting on "Example", and I'd like to be able to have it (for example) be italicized, or in \mathrm, or something of that sort.
\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\newtheorem{theorem}{Theorem}[section]
\newcommand{\labeldef}[2]{%
\def\templabelname{#1}%
\label{label text \templabelname}%
\expandafter\xdef\csname label text \templabelname\endcsname{#2}%
}
\newcommand{\hlink}[1]{%
\ifcsname label text #1\endcsname%
\hyperref[label text #1]{\csname label text #1\endcsname}%
\else%
[MISSING LINK FOR “#1”]%
\fi%
}
\begin{document}
\begin{theorem}[The \textit{Example} Theorem]
theorem text theorem text...
\end{theorem}
\labeldef{examplethm}{The Example Theorem (\arabic{section}.\arabic{theorem})}
\begin{theorem}
theorem text theorem text...
\end{theorem}
The ideal reference: \hyperref[label text examplethm]{The \textit{Example} Theorem (0.1)}
Referencing the above with the new command: \hlink{examplethm}
\end{document}
\expandafter
in\expandafter\label{label text \tempinfo}
does nothing. Nonetheless, could you provide us a complete working example to play with?def
instead ofxdef
the wrong theorem number is given.