Page Layoutlinking page numbers to TOC

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
gghuffer
Posts: 2
Joined: Fri Sep 11, 2015 5:13 am

linking page numbers to TOC

Post by gghuffer »

Hi,

I just started to write my thesis and I managed to find a good way to get distracted (finding cool stuff to do with LaTeX).

I'd like to make my page numbers link back to the table of contents. I found a couple answers to this on stack exchange but they don't seem to work for me.

http://stackoverflow.com/questions/3393 ... s-in-latex

I made a hypertarget to the TOC with :

Code: Select all

\renewcommand*{\contentsname}{\hypertarget{TOC}{Table of Contents}}
which I know works because I can set up a link to it:

Code: Select all

\hyperlink{TOC}{back to TOC}
but then making the pagenumbers into links doesn't seem to work. I tried this:

Code: Select all

%\renewcommand{\thepage}{\hyperlink{TOC}{\arabic{page}}}
...and I get 56 errors.
my .toc file seems to be a complete mess. I'm using hyperref's linktocpage to get links from the TOC to the pages, by the way.

I guess it's trying to put the links to the TOC into the TOC where the pagenumber should be.

Code: Select all

\contentsline {chapter}{\numberline {1}Introduction}{\let \Hy@reserved@a \relax \let \reserved@d =[\def \def \def 2{1}\def 2{7-\relax }\edef \relax {2}\edef \relax {\relax }\edef 2{2}\global \c@lips@count 2\relax \global \advance \c@lips@count -1\relax {\par }\futurelet \@let@token \let }{TOC}{1}
\contentsline {section}{\numberline {1.1}Preamble}{\let \Hy@reserved@a \relax \let \reserved@d =[\def \def \def 2{1}\def 2{7-\relax }\edef \relax {2}\edef \relax {\relax }\edef 2{2}\global \c@lips@count 2\relax \global \advance \c@lips@count -1\relax {\par }\futurelet \@let@token \let }{TOC}{1}
\contentsline {section}{\numberline {1.2}More to Come}{\let \Hy@reserved@a \relax \let \reserved@d =[\def \def \def 2{1}\def 2{7-\relax }\edef \relax {2}\edef \relax {\relax }\edef 2{2}\global \c@lips@count 2\relax \global \advance \c@lips@count -1\relax {\par }\futurelet \@let@token \let }{TOC}{1}
Thanks in advance for wading into this mess with me.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

linking page numbers to TOC

Post by cgnieder »

Welcome to the LaTeX community!

Do you really want to make every page number anywhere in your document to be a link to the table of contents? This sounds rather strange to me…

Anyway: “hiding” the link in an engine-robust macro seems to work. For this I used the package etoolbox which provides \newrobustcmd. In order to avoid lots of

Code: Select all

Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
warnings I used \pdfstringdefDisableCommands.

Code: Select all

\documentclass{article}
\usepackage{etoolbox,hyperref}

\renewcommand*{\contentsname}{\hypertarget{TOC}{Table of Contents}}
\newrobustcmd*{\TOClink}[1]{\hyperlink{TOC}{#1}}
\renewcommand{\thepage}{\TOClink{\arabic{page}}}

\pdfstringdefDisableCommands{\def\TOClink#1{#1}}

\begin{document}
\tableofcontents

\section{foo}

\end{document}
Regards
site moderator & package author
gghuffer
Posts: 2
Joined: Fri Sep 11, 2015 5:13 am

linking page numbers to TOC

Post by gghuffer »

Thanks Clemens!
This works but I seem to have broken the links that hyperref makes in the process somehow...
When I comment out

Code: Select all

\renewcommand{\thepage}{\TOClink{\arabic{page}}}
then hyperref works fine.
the lines in the .TOC file looks like this

Code: Select all

\contentsline {chapter}{\numberline {1}Introduction}{1}{chapter.1}
or this:

Code: Select all

\contentsline {chapter}{\numberline {1}Introduction}{\TOClink {1}}{chapter.1}
Post Reply