# Expansion and formatting commands

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

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

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}%
}

\ifcsname label text #1\endcsname%
\hyperref[label text #1]{\csname label text #1\endcsname}%
\else%
\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}

• Note: the \expandafter in \expandafter\label{label text \tempinfo} does nothing. Nonetheless, could you provide us a complete working example to play with? – Steven B. Segletes May 22 '19 at 17:02
• I'm not sure how you intend to use this. Can you please be more specific with an example usage? – egreg May 22 '19 at 17:04
• The general idea is that some theorems have names (with math and sometimes formatting), and rather than reference them by location (e.g. "theorem II.25") I'd prefer to reference the name (e.g. "The Example Theorem (II.25)"). The theorem name would be set by the user, but the location would ideally be saved as well. I'll edit in a more complete example. – JunderscoreH May 22 '19 at 17:34
• Could you please extend your MWE to actually demonstrate the use of your temporary information in the newly defined command? In what way should \tempinfo´ be integrated into the command's replacement text? – siracusa May 23 '19 at 3:49
• @siracusa That would be the location information, e.g. \arabic{section}, and \arabic{theorem}. With def instead of xdef the wrong theorem number is given. – JunderscoreH May 23 '19 at 4:51

I believe you're asking an XY-question.

The mechanism is already there, namely \nameref.

\documentclass{article}

\usepackage{amsmath}
\usepackage{hyperref}

\newcommand{\nnref}[1]{\nameref{#1}~(\ref{#1})}

\newtheorem{theorem}{Theorem}[section]

\begin{document}

\begin{theorem}[The \textit{Example} Theorem]\label{tet}
theorem text theorem text...
\end{theorem}

\begin{theorem}
theorem text theorem text...
\end{theorem}

The ideal reference: \nnref{tet}.

\end{document}


An easy solution to fix your specific expansion problem is to split the location part into another argument and add it back into the replacement text at the right position:

\documentclass{article}

\usepackage{amsmath}
\usepackage{hyperref}
\newtheorem{theorem}{Theorem}[section]

\newcommand{\labeldef}[3]{%
\def\templabelname{#1}%
\label{label text \templabelname}%
\edef\temp{%
\gdef\csname label text \templabelname\endcsname{\unexpanded{#2} (#3)}%
}\temp
}

\ifcsname label text #1\endcsname%
\hyperref[label text #1]{\csname label text #1\endcsname}%
\else%
\fi%
}

\begin{document}
\begin{theorem}[The \textit{Example} Theorem]
theorem text theorem text...
\end{theorem}

\labeldef{examplethm}{The \textit{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}


The temporary command \temp` fully expands the new command name and location parameter, but leaves the theorem name unexpaned such that you can use all kind of macros in it.