4

I don't know if this is possible to do in Latex, but here's what I'd like to do. I want to cite a theorem using its label, but what should appear is both its theorem number and its name (the text in square brackets after \begin{theorem}).

For example, consider the following theorem definition:

\begin{theorem}[Triangle inequality]\label{thm:triangle}

......

\end{theorem}

When I cite this, I want to be able to write something like

This is easily proved using the \SomeCommand{thm:triangle}.

What I would like to appear is

This is easily proved using the Triangle inequality (Theorem 1).

Ideally, the text will also be hyperlinked to the theorem.

Can this be done?

1
  • 2
    Have you looked at the cleveref package?
    – jon
    Commented Mar 29, 2016 at 3:22

1 Answer 1

9

Here are two approaches. First, you can combine \ref and \nameref to format as desired. Second, you can use \mylabel to save preformatted text. The advantage of the latter is that it is implemented as a single hyperlink.

\documentclass{article}
\usepackage[colorlinks=true]{hyperref}

\newtheorem{theorem}{Theorem}

\makeatletter
\@ifpackageloaded{hyperref}%
  {\newcommand{\mylabel}[2]% #1=name, #2 = contents
    {\protected@write\@auxout{}{\string\newlabel{#1}{{#2}{\thepage}%
      {\@currentlabelname}{\@currentHref}{}}}}}%
  {\newcommand{\mylabel}[2]% #1=name, #2 = contents
    {\protected@write\@auxout{}{\string\newlabel{#1}{{#2}{\thepage}}}}}
\makeatother

\begin{document}
\begin{theorem}[Triangle inequality]\label{thm:triangle}
\mylabel{text:triangle}{Triangle inequality (Theorem \thetheorem)}
\end{theorem}
This is easily proved using the \nameref{thm:triangle} (Theorem \ref{thm:triangle}).

\medskip\noindent
This is easily proved using the \ref{text:triangle}.
\end{document} 

demo

1
  • I expanded the remark about the first solution at tex.stackexchange.com/a/347100/34551. I think it is much more readable and easier than your second solution, and can be easily adapted to print only a single link.
    – Clément
    Commented Jan 4, 2017 at 18:05

You must log in to answer this question.

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