0

When the hyperref package is used, it creates nice anchors that can be used as part of a URL to get to specific locations in the generated PDF file.

\documentclass {book}
\usepackage{hyperref}

\begin{document}

\tableofcontents*

\chapter {One}

\clearpage
\chapter {Two}

\clearpage
\chapter {Chapter Three}

\end{document}

When the resultant PDF is placed in a directory that a webserver can reach, the URL with the anchor #One leads to the beginning of Chapter One, but the URL with the anchor #Chapter%20Three leads to the beginning of the PDF file.

How can I make an external URL that is anchored to Chapter Three?

2 Answers 2

0

hyperref doesn't create anchors from the chapter titles. The internal destination names are chapter.1, chapter.2 and chapter.3, and so an url with ....test-utf8.pdf#chapter.3 works as expected.

Using the bookmark names works to some extend too, the pdf reader seems to skim the outline titles to find a destination, but as you discovered you will run into problems if the title contains someting that doesn't work well in a url, or if hyperref has replaced some content when creating the bookmark title, or if a title is repeated. A better option for named destination is imho the destlabel option, which will use a following label key:

\documentclass {book}
\usepackage[destlabel]{hyperref}

\begin{document}

\tableofcontents*

\chapter {One}\label{One}

\clearpage
\chapter {Two}\label{Two}

\clearpage
\chapter {Chapter Three}\label{ChapterThree}

\end{document}

With this #ChapterThree will work.

3
  • WIth my browser (Chrome on OS/X) #chapter.3 doesn't work at all. I'm not sure how to troubleshoot it. I had seen those internal tiles before, but because they didn't work I didn't pursue them. I had also tried the destlabel approach, and that failed on my browser as well. May 17, 2019 at 16:05
  • Perhaps the difference between your behavior and mine is that I am processing through XeLaTeX. May 17, 2019 at 16:31
  • I doubt it, but can't test now, but you can test it easily: compile my example with pdflatex. May 17, 2019 at 16:37
0

Further research has indicated that pdf destinations cannot contain spaces, so that we need to eliminate the spaces from the bookmark name in the pdf file.

This can be done through the use of \texorpdfstring in the chapter title.

Also, by using the stringstrings I can substitute all spaces in the title to hyphens, and can do so in my .sty file

    \documentclass {memoir}

\usepackage{hyperref}
\usepackage{stringstrings}

\newcommand{\mychapter}[1]{
  \convertchar[q]{#1}{ }{-} %
  \chapter [\texorpdfstring{#1}{\thestring}][#1]{#1}
}

\begin{document}

\tableofcontents

\mychapter{One}

Chapter one body

\clearpage
\mychapter{Chapter Two}

Chapter two body

\clearpage
\mychapter{This is a test}

This is a test body.
\end{document}

This provides me the functionality I am after. It works with my existing files with only minimal changes, and allows me to create easily-defined external URL links.

You must log in to answer this question.

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