0

When using twocolumn mode, it is a known issue that references to chapter headings point to the baseline rather than the chapter heading.

In a previous question, I asked how to fix this for the table of contents, but I realize that the solution does not solve the same issue for \ref and friends.

My attempt tried to patch \label in the same way as \addcontentsline, but this makes no difference to the \hypertarget of \*ref. How can I modify the target of chapter: labels? If it helps, bookmarks referencing the chapter are always prefixed by chapter:.

Reprex:

\documentclass{scrbook}
\usepackage[twocolumn]{geometry}
\usepackage{varioref}
\usepackage[hypertexnames=false]{hyperref}
\usepackage{cleveref}

\usepackage{etoolbox}

% Add extra hyper target for chapter: chapter..\thechapter
\renewcommand*{\chapterformat}{%
  \mbox{\raisebox{25pt}[0pt][0pt]{\hypertarget{.\theHchapter}{}}% Add 
    \chapappifchapterprefix{\nobreakspace}\thechapter\hspace{1em}}%
}

\makeatletter
% Update \addcontentsline to jump to new hyper target _only_ if \chapter is used
\patchcmd{\addcontentsline}% <cmd>
  {\Hy@writebookmark}% <search>
  {\ifnum\pdfstrcmp{chapter}{#2}=0 % Chapter mark
     \edef\@currentHref{.\theHchapter}%
   \fi
   \Hy@writebookmark}% <replace>
  {}{}% <success><failure>

\patchcmd{\label}% <cmd>
  {\Hy@writebookmark}% <search>
  {\edef\@currentHref{.\theHchapter}%
   \Hy@writebookmark}% <replace>
  {}{}% <success><failure>

\makeatother


\makeatletter
\@addtoreset{chapter}{part}
\makeatother

\begin{document}
\tableofcontents
\part{A}
\chapter{foo\label{chapter:foo}}
\chapter{bar}
\part{B}
\chapter{Baz}\label{chapter:Baz}
\chapter{Foobar}

\Vref{chapter:Baz}.

\Vref{chapter:foo}.

\end{document}
1

Hyperref automatically provides targets at the top of each page by the name page.\thepage.

\documentclass{scrbook}
\usepackage[twocolumn]{geometry}
\usepackage{varioref}
\usepackage{hyperref}
\usepackage{cleveref}

\newcommand{\topref}[1]% #1 = label
{\hyperlink{page.\getpagerefnumber{#1}}{\getrefnumber{#1}}}

\begin{document}

chapter \topref{chapter:foo}

\part{A}
\chapter{foo}\label{chapter:foo}
\chapter{bar}
\part{B}
\chapter{Baz}\label{chapter:Baz}
\chapter{Foobar}

\end{document}

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.