7

Please consider the following multipage MWE, which contains page number restarting; made to compile with pdflatex test.tex:

\documentclass[12pt]{article}

\usepackage{geometry}
\geometry{twoside,inner=50bp,outer=30bp,top=50bp,bottom=50bp}


% \usepackage{titlesec}
% \titleformat{\section}{\centering\normalsize}{\thesection}{1em}{}
% \titleformat{\subsection}{\centering\small}{\thesubsection}{1em}{}


\usepackage{layouts}
\usepackage{etoolbox}
\patchcmd{\drawpage}{\ifdrawparameters}{\iftrue}%
  {\typeout{^^J*******\string\drawpage fixed*******^^J}}%
  {\typeout{^^J*******\string\drawpage not fixed*******^^J}}

\usepackage{hyperref}
\usepackage{tikz,enumitem}
\usepackage{fix-cm}

\usepackage{lipsum}



\begin{document}


  \pagestyle{empty} % no page numbers on next few pages

% pg 1
    \begin{tikzpicture}[overlay,remember picture,yscale=-1, inner sep=0pt, outer sep=0pt,anchor=north west]

      \path (current page.center) node[anchor=center] (textX) {%

        Physical page 1 / Title

      };

    \end{tikzpicture}

  \clearpage

% pg 2
  Physical page 2 / Empty

  \clearpage

% pg 3
  Physical page 3 / TOC

  \tableofcontents

  \clearpage

% pg 4
  %Physical page 4 / Empty
  \ \\

  \clearpage

  \pagenumbering{arabic} % restart page numbering from 1
  \pagestyle{plain} % show page numbers

% pg 5
  Physical page 5

  \section*{Section One}
  \addcontentsline{toc}{section}{Section One}

  \subsection*{Subsection One}
  \addcontentsline{toc}{subsection}{Subsection One}

  \lipsum[1]

  \clearpage

% pg 6
  Physical page 6

  \subsection*{Subsection Two}
  \addcontentsline{toc}{subsection}{Subsection Two}

  \lipsum[2]

  \clearpage

% pg 7
  Physical page 7

  \section*{Section Two}
  \addcontentsline{toc}{section}{Section Two}

  \lipsum[3]

  \clearpage

% pg 8
  Physical page 8

  \subsection*{Subsection Two}
  \addcontentsline{toc}{subsection}{Subsection Two}

  \lipsum[4]

  \clearpage


\end{document}

When this is compiled (twice), there is absolutely no problems in the PDF - table of contents clickable links in PDF work as expected; note that for this correct case, we have test.toc:

\contentsline {section}{Section One}{1}{section*.2}
\contentsline {subsection}{Subsection One}{1}{section*.3}
\contentsline {subsection}{Subsection Two}{2}{section*.4}
\contentsline {section}{Section Two}{3}{section*.5}
\contentsline {subsection}{Subsection Two}{4}{section*.6}

and test.out:

\BOOKMARK [1][-]{section*.2}{Section One}{}% 1
\BOOKMARK [2][-]{section*.3}{Subsection One}{section*.2}% 2
\BOOKMARK [2][-]{section*.4}{Subsection Two}{section*.2}% 3
\BOOKMARK [1][-]{section*.5}{Section Two}{}% 4
\BOOKMARK [2][-]{section*.6}{Subsection Two}{section*.5}% 5

 

Now, uncomment the three lines around \usepackage{titlesec}, and recompile (deleting *.{out,toc,aux} previously if so desired). Eventually you can see pdflatex spitting:

Package hyperref Warning: The anchor of a bookmark and its parent's must not
(hyperref)                be the same. Added a new anchor on input line 72.

pdfTeX warning (ext4): destination with the same identifier (name{page.1}) has 
been already used, duplicate ignored

Now, basically the first link in the TOC (to "Section One") points to physical page 1 (start of document) - instead of physical page 5 (logical page 1, after restart), where it should be! In this case, test.toc is:

\contentsline {section}{Section One}{1}{Doc-Start}
\contentsline {subsection}{Subsection One}{1}{section*.1}
\contentsline {subsection}{Subsection Two}{2}{section*.1}
\contentsline {section}{Section Two}{3}{section*.1}
\contentsline {subsection}{Subsection Two}{4}{section*.2}

... and test.out is:

\BOOKMARK [1][-]{Doc-Start}{Section One}{}% 1
\BOOKMARK [2][-]{section*.1}{Subsection One}{Doc-Start}% 2
\BOOKMARK [2][-]{section*.1}{Subsection Two}{Doc-Start}% 3
\BOOKMARK [1][-]{section*.1}{Section Two}{}% 4
\BOOKMARK [2][-]{section*.2}{Subsection Two}{section*.1}% 5

 

My question is - can I somehow use the titlesec package in the above example, and yet still get correct hyperlinks for the table of contents in the PDF?

Many thanks in advance for any answers,
Cheers!

3 Answers 3

6

EDIT: What worked for me is to avoid titlesec, and do Changing the Section Headings the old way - by wrapping the recommended \renewcommand in \makeatletter/\makeatother in the preamble as so:

\makeatletter
\renewcommand{\section}{\@startsection
{section}%                   % the name
{1}%                         % the level
{\z@}%                       % the indent / 0mm
{-\baselineskip}%            % the before skip / -3.5ex \@plus -1ex \@minus -.2ex
{0.5\baselineskip}%          % the after skip / 2.3ex \@plus .2ex
{\centering\normalsize}} % the style

Ah well, turns out it's a known thing, apparently:

http://www.tug.org/applications/hyperref/ftp/README

titlesec
--------
  "nameref" supports titlesec, but hyperref does not
  (unsolved is the anchor setting, missing with unnumbered
  section, perhaps problems with page breaks with numbered ones).

Well, if anyone knows a workaround, would be great to hear it :)

Cheers!

1
  • The linked page has moved and now apparently doesn't show this any more. Here's a working Archive link.
    – Pezo
    Aug 30, 2017 at 12:18
3

Your setup seems to suggest that you're not interested in numbering your sectional units. So, continue to use \section and \subsection but add

\setcounter{secnumdepth}{0}

to your document preamble. Subsequently, using titlesec provides correct hyperlinks to sectional units from the ToC, despite some compiler warnings.

Here is your preamble:

\documentclass[12pt]{article}

\usepackage{geometry}
\geometry{twoside,inner=50bp,outer=30bp,top=50bp,bottom=50bp}

\setcounter{secnumdepth}{0}

\usepackage{titlesec}
\titleformat{\section}{\centering\normalsize}{\thesection}{1em}{}
\titleformat{\subsection}{\centering\small}{\thesubsection}{1em}{}

\usepackage{layouts}
\usepackage{etoolbox}
\patchcmd{\drawpage}{\ifdrawparameters}{\iftrue}%
  {\typeout{^^J*******\string\drawpage fixed*******^^J}}%
  {\typeout{^^J*******\string\drawpage not fixed*******^^J}}

\usepackage{hyperref}
\usepackage{tikz,enumitem}
\usepackage{fix-cm}
\usepackage{lipsum}

\begin{document}
1
  • Many thanks for that, @Werner - indeed, I do not need numbering here; however, I'm trying to recompile with this preamble, and I still get the same warnings and problems as in the question. I've tried to redefine \section the old way (as described in the edit of my post), and that seems to work OK so far. Many thanks again - cheers!
    – sdaau
    May 16, 2012 at 6:10
3

I solved it differently: I used titletoc package to configure the table of contents (in a way that was quite similar to the one that latex defaults to) and I ended up being able to use hyperref and titlesec together.

Here is a snippet of my code:

\documentclass{book}

\usepackage{titlesec,titletoc,hyperref}

% Section heading format/spacing    
\titleformat{\chapter}[block]%
    {\flushright\huge\bfseries}{\thechapter.}{2ex}{}[\titlerule]
\titlespacing{\chapter}{0pt}{.45\textheight}{3em}
\titleformat{\section}{\normalfont\bfseries}{\thesection.}{5ex}{}
\titleformat{\subsection}{\normalfont}{\thesubsection.}{3ex}{}
\titleformat{\subsubsection}{\normalfont}{\thesubsubsection.}{3ex}{}

% Table of contents line format
\titlecontents{part}[0pt]{\vspace{2em}}%
    {\bfseries\Large\contentslabel{-2.3pc}}{\bfseries\Large\hspace*{-2pc}}%
    {\bfseries\Large\titlerule*{ }\contentspage}
\titlecontents{chapter}[2em]{}%
    {\bfseries\contentslabel{2.3pc}}{\bfseries\hspace*{-2pc}}%
    {\bfseries\titlerule*{ }\contentspage}
\titlecontents{section}[2em]{}%
    {\contentslabel{2.3pc}}{\hspace*{-2pc}}%
    {\titlerule*{.}\contentspage}
\titlecontents{subsection}[2em]{}%
    {\contentslabel{2.3pc}}{\hspace*{-2pc}}%
    {\titlerule*{.}\contentspage}
\titlecontents{subsubsection}[2em]{}%
    {\contentslabel{2.3pc}}{\hspace*{-2pc}}%
    {\titlerule*{.}\contentspage}

\begin{document}

\tableofcontents

\part{Part One}
\chapter{Chapter One}
\section{Section One}
\section{Section Two}

\chapter{Chapter Two}

\end{document}

Sorry if this post is discontinued, but I found an alternate solution which I found to be more confortable and thought I'd leave it here just in case someone else ends up here.

0

You must log in to answer this question.

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