5

In two previous questions (i.e. Distinguishing \ref and \cref through different colors, and ''Proof of Theorem x'' header trough modification of \ref) I addressed some problems I had with hyperref and cleverref. Here there is a new problem I have, that can be exemplified by the following MWE:

\documentclass[12pt]{article}%

\usepackage{amsmath}%
\usepackage{amsthm}%
\usepackage{amsfonts}%
\usepackage{amssymb}%


\usepackage[usenames,dvipsnames]{xcolor}%


\theoremstyle{plain}
\newtheorem{theorem}{Theorem}

\newtheoremstyle{named}{}{}{\itshape}{}{\bfseries}{.}{.5em}{\thmnote{#3}}
\theoremstyle{named}
\newtheorem*{namedtheorem}{Theorem}



\usepackage{varioref}
\usepackage{hyperref}
\usepackage[noabbrev,nameinlink]{cleveref}

\hypersetup{colorlinks={true},linkcolor={red},citecolor={green}}

\newcommand{\aref}[2][blue]{%
\hypersetup{linkcolor=#1}%
\cref{#2}%
\hypersetup{linkcolor=red}%
}


\begin{document}

\section{Bla}
\label{section:bla}


\begin{theorem}[Bla]
\label{th:bla}
Bla.
\end{theorem}

\begin{namedtheorem}[Bla Bla]
\label{thm:blabla}
Bla bla
\end{namedtheorem}


\section{Bla Bla}

In section \ref{section:bla}, we get \aref{th:bla}, but we also get \aref{thm:blabla}.


\end{document}

Thus, the all problem is that when I use \aref (the modification of \cref) applied to namedtheorem, instead of giving me the name of the theorem (e.g. "Bla Bla" in blue), it gives me "section 1" in Blue.

Is there a way to fix it?

Thank you for your time.


Edit after comment:

A user as kindly provided an attempt to fix the problem. Namely, to substitute \newtheorem* with \newtheorem and then use \crefname{namedtheorem}{theorem}{theorems} after loading cleverref. By doing this, the hyperlink doesn't show up anymore as section, but still there are two problems:

  1. I don't get the name of the theorem in blue (e.g. Bla Blain blue), but rather Theorem 1in blue;

  2. If there is another theorem, no matter what is the label, when I called them, in both cases I get theorem 1.

  • maybe this helps? cleveref and amsthm — incorrect environment name – barbara beeton Jan 8 '16 at 18:02
  • Thanks for the feedback. I tried, but unfortunately it does not. – Kolmin Jan 8 '16 at 18:11
  • 1
    you need to replace \newtheorem* with \newtheorem and then use \crefname{namedtheorem}{theorem}{theorems} after loading cleverref. – touhami Apr 10 '16 at 22:01
  • 1
    You're setting up an unnumbered theorem environment called "named", and you're giving an instance of this theorem environment a \label (thm:blabla). However, since the "named" theorem style (by design!) does not increment a counter (via \refstepcounter), LaTeX associates the \label with a recently-incremented counter, which in the present case happens to be the section counter. Note that this problem is not specific to cleveref: You're somehow trying to generate a link, but there's no hook to link to via the \label-\ref mechanism. – Mico Apr 11 '16 at 17:39
  • 1
    @Kolmin - You cannot realistically stick with your current setting: it's not working, and it cannot work. In the meantime, I've posted an answer that shows (hopefully...) that it's not all that difficult to use the \hypertarget / \hyperlink mechanism to generate cross-references to unnumbered entities (e.g., unnumbered theorems) that are also hyperlinks. – Mico Apr 11 '16 at 19:16
8
+100

You have two types of theorems in your document -- numbered ones, of type plain, and unnumbered ones, of type named. As you've already discovered and pointed out, it's easy to assign a \label to a numbered theorem and to cross-reference it via \ref, \cref, or \aref.

Theorems of the "named" type, in contrast, do not have a counter associated with them. While it's possible to associate a \label with unnumbered theorems, doing so is not going to work (and, in fact, cannot work): LaTeX will merrily associate the \label with some counter that was incremented previously; in the case of your sample code, that happens to be the counter named section.

Note that this behavior is not specific to, or caused by, either the hyperref or the cleveref package.

How, then, to go about creating cross-references to unnumbered theorems? Fortunately, the hyperref package provides the \hypertarget/\hyperlink mechanism for just this purpose. The former macro serves to install a "hook" (and to give a name to the hook), and the latter serves to create hyperlinks elsewhere in the document to hooks created by \hypertarget. Observe that whereas \label and \ref/\cref take one argument, \hypertarget and \hyperlink take two arguments. The first argument corresponds, in design, to the lone argument of \label and \ref; the second argument can be an arbitrary text string. Thus, if an object is to be cross-referenced via \hypertarget and \hyperlink, the first arguments of the two instructions must be the same; the second arguments, in contrast, can but do not have to be the same. To wit, in the following example the second argument of \hypertarget is Mystery theorem whereas the second argument of \hyperlink is mystery theorem.

Incidentally, I've set up a helper macro named \bref that simplifies switching the color of the cross-reference from red to some other color (default: blue). (Plus, I couldn't resist creating an opportunity to write a sentence that contains \aref, \bref, and \cref...)

enter image description here

\documentclass[12pt]{article}

\usepackage{amsthm,xcolor}
\usepackage[colorlinks, linkcolor=red]{hyperref}
\usepackage[noabbrev,nameinlink]{cleveref}

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}

\newtheoremstyle{named}%
    {}{}{\itshape}{}{\bfseries}{.}{.5em}{\thmnote{#3}}
\theoremstyle{named}
\newtheorem*{namedtheorem}{Theorem}

\newcommand{\aref}[2][blue]{%
    \begingroup%
    \hypersetup{linkcolor=#1}%
    \cref{#2}%
    \endgroup}
\newcommand\bref[3][blue]{%
    \begingroup%
    \hypersetup{linkcolor=#1}%
    \hyperlink{#2}{#3}%
    \endgroup}

\begin{document}

\section{Uno} \label{section:uno}

\begin{theorem}[Bla]\label{thm:bla}
Bla.
\end{theorem}

\begin{namedtheorem}[\hypertarget{thm:mystery}{Mystery theorem}]
Bla bla.
\end{namedtheorem}

\section{Due}

In \cref{section:uno}, we have both \aref{thm:bla} and an unnumbered \bref{thm:mystery}{mystery theorem}.

\end{document}
  • 2
    Moving on from your last sentence, I cannot resist from saying that it is a marvellous answer, that incidentally also solved my problem. Also, you explained the whole problem and the solution in a beautiful way, with the kind of intuition that – I find – often lacking in answers. What I mean that often you have answers that give a fish, but do not really teach how to fish. Thanks a lot. – Kolmin Apr 11 '16 at 21:10
  • @Kolmin - Many thanks for the compliment -- and many thanks for the bounty!! – Mico Apr 11 '16 at 21:12
  • 1
    @Kolmin - \newcommand\bref[3][blue]{...} and \newcommand{\bref}[3][blue]{...} are equivalent. For sure, Leslie Lamport (the creator of LaTeX) used the second form in his book. As both forms work equally well, which form to choose becomes mostly a matter of personal preference. As for me, I like to go for an uncluttered look in my code, and I like to economize on the use of curly braces whenever their use isn't required. Both forms are OK, though: As you develop your own programming habits, just be sure to develop a consistent style. – Mico Apr 12 '16 at 21:41
  • 1
    Sorry for this late question, but I am trying to exploit your answer for hyperlinks to steps or claims in a proof (or whatever I want). The problem is that it modifies the overall look, and instead of "Step 1.", I get "Step 1 ()." (this is if I write [\hypertarget{step:1}{}] close to \begin{step}. Nothing changes if I drop the second couple of curly braces, but if I write something inside, then it obviously appears in the round braces in the text. Is there a way to use your system to get the link with the simple "Step 1."? Sorry to bother after all this time, and thanks a lot anyway. – Kolmin May 19 '16 at 3:38
  • 1
    You are absolutely right. Here there is the link to my new question: tex.stackexchange.com/questions/310486/… . As you will notice, the problem is essentially the one presented here. That's why I thought at the beginning it was possible to avoid a potential duplicate (or something pretty close to it) with the new question. – Kolmin May 19 '16 at 16:13

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.