11

How can I retrieve the name of a theorem?

I know how to refer to theorems using labels and take the responsibility of giving the title "Theorem" or "Lemma" myself. I also know that packages such as ntheorem can go further, and put the name "Theorem" or "Lemma" and then keep the consistency for me. However, I could not find a package that allows me to define a theorem and recover its title.

For example, take the following snippet:

\begin{theorem}[Rely-to-Atomic] For any predicate @{term "p"}, and relations @{term "r"}
and @{term "q"}, such that @{term "❙[p,q❙]"} tolerates interference @{term "r"},
\begin{IEEEeqnarray*}{c}
@{thm (concl) Law_7_4_Rely_To_Atomic_Ref}
\end{IEEEeqnarray*}\label{RTA_Ref}
\end{theorem}

After the system processing the funny quotations within @ blocks, this generates the corresponding latex and compiles to:

enter image description here

What I want is a way of retrieving the string "Rely-to-Atomic" from the label RTA_Ref. If I have that, I can reference theorems including the title for the convenience of my reader. For example, by typing:

Law~\ref{RTA_Ref}~(\nameofthm{RTA_Ref}) 

To obtain:

Law 3.95 (Rely-to-Atomic)

Since I know that some packages are able to provide a table of theorems, I believe that there should be a way of accessing the title of a theorem or then a package that fits this purpose.

6

The thmtools package allows to do that easily:

\documentclass{article}
\usepackage{hyperref}
\usepackage{thmtools}
\declaretheorem{Theorem}
\begin{document}

\begin{Theorem}[My Theorem]\label{thm}
  $1 + 3 = 4$
\end{Theorem}

As we saw in Theorem~\ref{thm} (\nameref{thm})

\end{document}

Referring to the Theorem by its name

You can obtain the same using amsthm:

\documentclass{article}
\usepackage{hyperref}
\usepackage{amsthm}
\newtheorem{thm}{Theorem}
\begin{document}

\begin{thm}[My Theorem]\label{thm}
  $1 + 3 = 4$
\end{thm}

As we saw in Theorem~\ref{thm} (\nameref{thm})

\end{document}

Finally, if you want to have a signle link instead of two, you can use the following:

\documentclass{article}
\usepackage{hyperref}
\usepackage{thmtools}
\declaretheorem{Theorem}

\begin{document}

\begin{Theorem}[My Theorem]\label{thm}
  $1 + 3 = 4$
\end{Theorem}

As we saw in \hyperref[thm]{Theorem~\ref*{thm} (\nameref*{thm})}.

\end{document}

The same, with a single link.

And if you don't want any link, just use \nameref*{thm}.

I guess you'll be able to adapt the code to print "Law" instead of "Theorem".

  • 1
    A shame that nobody upvoted this yet (... ;-)) Not even the O.P.! – user31729 Jan 18 '17 at 17:31
  • 1
    Hi Clement, thanks for the answer! I noticed that my preamble includes amsthm. I didn't know that this package was being used to make the magic to happen. Thanks as well for the complement about how to make a single link, instead of two, though I haven't bother much about that :-) – Diego Dias Jan 19 '17 at 18:29
4

Here is an additional solution that uses amsthm and is indipendent from hyperref. It mimics LaTeXs built in cross-ref-routine:

\documentclass{article}
\usepackage{amsthm}
\usepackage{amssymb}

\newtheorem{law}{Law}

\makeatletter
\let\sv@law\law
\let\sv@label\label
\renewcommand\law[1][]{\sv@law[#1]\def\curr@thmname{#1}}
\renewcommand\label[1]{%
  \immediate\write\@auxout{%
    \noexpand\global\noexpand\@namedef{#1-thmname}{\curr@thmname}}
  \sv@label{#1}
}
\newcommand\nameofthm[1]{\@nameuse{#1-thmname}}
\makeatother

\begin{document}
%Law~\ref{RTA_Ref}~(\nameofthm{RTA_Ref}) 
%
\begin{law}[Rely-to-Atomic]\label{RTA_Ref}
For any predicate $p$, and relations $r$ and $q$, such that $[p,q]$ tolerates interference $r$,
\[
  \mathbf{rely}\,r\cdot[p,q]\sqsubseteq\langle p,q\rangle
\]
\end{law}

...

Law~\ref{RTA_Ref}~(\nameofthm{RTA_Ref})
\end{document}

example_rendered

  • 2
    That's impressive. – Clément Jan 19 '17 at 20:47
  • @Clément -- thx. I think your thmtools answer is the prefered way of doing this, tho. +1 for your answer (actually it was earlier alredy :) – Ruben Jan 19 '17 at 20:51
  • This is a quite low level way of achieving that result, I am also impressed. Worth of being aware of, though I didn't fully understood the details of the solution. I also agree that Clément's answer shall continue as the preferred one, especially because people who may get to the same question would be more likely to be looking for the simplest solution. Thanks! – Diego Dias Jan 20 '17 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.