1

I have some problems related to the application of an answer that I received to a previous question of mine, that can be found here.

Below there is a MWE, where I used \clearpageextensively, because I noticed that when \hypertargetdoes not work properly, it links to the first page of the document.

Problem:
When I use [\hypertarget{...}{...}] along with \begin{proof} or \begin{namedtheorem}, no additional round braces appear in the text.

However, when I use it along with \begin{step}, \begin{claim}, or \begin{myclaim}, there two additional round braces appear.

Is there a way to get rid of those round braces?

\documentclass[12pt]{article}

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

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

\theoremstyle{remark}
\newtheorem{claim}{Claim}
\newtheorem*{myclaim}{Claim}
\newtheorem{step}{Step}

\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{One}

LaTeX Problems. 

\clearpage

\section{Two} \label{section:two}

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

\begin{proof}[\hypertarget{proof:1}{Proof.}]
Bla
\end{proof}

\begin{namedtheorem}[\hypertarget{thm:mystery}{Mystery theorem}]
A mysterious theorem
\end{namedtheorem}

\begin{proof}
\begin{step}[\hypertarget{step1}{}]
Here step 1.
\end{step}

\begin{step}
Here step 2.
\end{step}

\begin{claim}[\hypertarget{claim1}{}]
A numbered claim.
\end{claim}
\end{proof}

\clearpage

\begin{theorem}
Bla bla 
\end{theorem}

\begin{proof}
\begin{myclaim}[\hypertarget{myclaim}{}]
An unnumbered claim.
\end{myclaim}
\end{proof}

\clearpage

\section{Three}

In \cref{section:two}, we have both a \aref{thm:bla}, a \bref{proof:1}{proof}, \bref{step1}{step 1}, a numbered \bref{claim1}{claim 1}, and a single \bref{myclaim}{claim}.

\end{document}

Thank you for your time.

Kolmin
  • 559

2 Answers2

4

Numbered entities do not need an additional \hypertarget, because hyperref already sets an anchor at the place of \refstepcounter. A \label is enough to catch the anchor. \hyperref[<label>]{<text>} can be used to set arbitrary text pointing to the label.

Unnumbered entities are more tricky. Since there is usually no anchor, an anchor can be placed with \phantomsection. The place is the tricky part. The optional argument of the theorem environments are not the best choice because of the problems. The following example sets the anchor in the vertical list before the first paragraph with the theorem title via \vadjust pre{...}, a feature of the pdfTeX engine.

\documentclass[12pt]{article}

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

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

\theoremstyle{remark}
\newtheorem{claim}{Claim}
\newtheorem*{myclaim}{Claim}
\newtheorem{step}{Step}

\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}%
    \hyperref[{#2}]{#3}%
  \endgroup
}

\newcommand*{\vprelabel}[1]{%
  \leavevmode
  \vadjust pre{\phantomsection\label{#1}}%
  \ignorespaces
}

\begin{document}

\section{One}

\LaTeX\ problems are hopefully solved more or less.

\clearpage

\section{Two} \label{section:two}

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

\begin{proof}[Proof.]
\vprelabel{proof:1}
Bla
\end{proof}

\begin{namedtheorem}[Mystery theorem]
\label{thm:mystery}
A mysterious theorem
\end{namedtheorem}

\begin{proof}
\begin{step}
\label{step1}
Here step 1.
\end{step}

\begin{step}
Here step 2.
\end{step}

\begin{claim}
\label{claim1}
A numbered claim.
\end{claim}
\end{proof}

\clearpage

\begin{theorem}
Bla bla
\end{theorem}

\begin{proof}
\begin{myclaim}
\vprelabel{myclaim}
An unnumbered claim.
\end{myclaim}
\end{proof}

\clearpage

\section{Three}

In \cref{section:two}, we have both a \aref{thm:bla}, a
\bref{proof:1}{proof}, \bref{step1}{step 1}, a numbered \bref{claim1}{claim
1}, and a single \bref{myclaim}{claim}.

\end{document}
Heiko Oberdiek
  • 271,626
2

You wrote:

However, when I use it along with \begin{step}, \begin{claim}, or \begin{myclaim}, there two additional round parentheses appear.

Is there a way to get rid of those round braces?

Don't surround the corresponding \hypertarget{...}{} statements with square brackets ([ and ]). Otherwise, amsthm will interpret the material as indicating the optional "title" of the corresponding theorem-like environments. Expressed differently, only use square brackets if the second argument of \hypertarget is nonempty.

Another way to understand this better is to consider the two cases in your code where the second argument of \hypertarget is nonempty:

\begin{proof}[\hypertarget{proof:1}{Proof.}]

and

\begin{namedtheorem}[\hypertarget{thm:mystery}{Mystery theorem}]

In these cases, the square brackets indicate to LaTeX that "Proof." and "Mystery theorem", respectively, should be used as the titles of the corresponding environments. That's not the case with

\begin{step}\hypertarget{step1}{}
\begin{claim}\hypertarget{claim1}{}
\begin{myclaim}\hypertarget{myclaim}{}

and that's why you shouldn't provide square brackets.

You also wrote:

Below there is a MWE, where I used \clearpage extensively, because I noticed that when \hypertarget does not work properly, it links to the first page of the document.

When that happens, try inserting \phantomsection directives immediately ahead of the environments or other items being cross-referenced via \hypertarget and \hyperlink.

Mico
  • 506,678
  • Thanks a lot for the answer. Actually, by mistake, after implementing your simple advice, I noticed that if I write \hypertarget{label}without the second couple of curly braces, nothing really change. May I ask you why it is the case? Are there some side-effects in doing this? – Kolmin May 19 '16 at 20:10
  • @Kolmin - Hmmm, My hunch is that it's probably pure luck that's letting you get away with not providing the second argument explicitly. LaTeX is probably picking up some unneeded/unnoticed token after the first argument and treats it as the second argument of \hypertarget, and luckily for you that token isn't needed elsewhere. Could you give a specific example? – Mico May 19 '16 at 20:18
  • I see. The specific example is just this very same code. – Kolmin May 19 '16 at 23:47
  • @Kolmin - My hunch is that you're safe leaving off the blank {} element as long as the next token is an ordinary letter or punctuation mark. All bets are off if the next token is a macro, say \textbf or \begin{equation}. – Mico May 20 '16 at 03:56