0

I have recently discovered, that I can't move from hyperref connected with number of chapter/section in table of contents to a page, where the chapter/section is - it look as it blocked... Actually, I can move from the number, which is present in the main text. What could I do to make it work here and here?

Current situation (1)

Current situation (2)

The code:

\documentclass[12pt, a4paper]{book}
\usepackage[left=2.5cm, right=2.5cm, bottom=2.5cm, headheight=15pt]{geometry} %marginesy
%\usepackage[utf8]{inputenc} %kodowanie polskich znaków
\usepackage[polish]{babel}
\babelprovide[transforms = oneletter.nobreak]{polish} %brak wiszących liter na końcu zdania
\selectlanguage{polish}

\usepackage{fancyhdr}
\usepackage{tocloft} %nowoczesny spis treści
\usepackage{xpatch}

\renewcommand{\cftpartpresnum}{\hfil}
\renewcommand{\cftpartleader}{\hfil}

\renewcommand{\cftchapaftersnum}{.} %kropki po numerze rozdziału
\renewcommand{\cftsecaftersnum}{.} %kropki po numerze sekcji
\renewcommand{\cftchappresnum}{\bfseries} %numer pogrubiony przy rozdziale
\renewcommand{\cftchapleader}{\normalfont\cftdotfill{\cftchapdotsep}} %kropki przy rozdziałach w spisie treści
\renewcommand\cftchappagefont{\bfseries\normalsize}
\setlength{\cftchapnumwidth}{1cm} %odstęp między kropką a nazwą rozdziału w spisie treści
\renewcommand{\cftchapfont}{\normalfont\large} %duża czcionka rozdziału
\renewcommand{\cftpartfont}{\bfseries\Large}

\usepackage{caption}
\usepackage{subcaption}
%\usepackage{secdot}
\usepackage{fontspec}
\usepackage{graphicx}

\usepackage[nottoc, notlof]{tocbibind}
\usepackage[bookmarksnumbered, pdfpagelayout=TwoPageRight, pagebackref, linktoc=all]{hyperref}

\usepackage{bookmark}
\usepackage{lipsum}
\usepackage[Bjornstrup]{fncychap} %nowoczesny wygląd rozdziału

\makeatletter
\patchcmd{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi}{}{}{}
\makeatother %zabezpieczenie przed tworzeniem nowej strony przed rozdziałem

%\renewcommand{\thesection}{\thechapter.\arabic{section}}
\renewcommand{\thechapter}{\hyperref[toc]{\arabic{chapter}}}
\renewcommand{\thesection}{\hyperref[toc]{\arabic{chapter}.\arabic{section}.}}

\makeatletter
\let\sv@endpart\@endpart
\def\@endpart{\thispagestyle{empty}\sv@endpart}
\makeatother

\usepackage{xparse}
\AtBeginDocument{
    \let\oldchapter\chapter
    \RenewDocumentCommand{\chapter}{s o m}{%
        \IfBooleanTF{#1}
        {\oldchapter*{\hyperref[toc]{#3}}}% \chapter*[..]{...}
        {\IfValueTF{#2}
            {\oldchapter[#2]{\hyperref[toc]{#3}}}% \chapter[..]{...}
            {\oldchapter[#3]{\hyperref[toc]{#3}}}% \chapter{...}
            %\label{chapter-\thechapter}% \label this chapter
        }%
    }
    
    \let\oldsection\section
    \RenewDocumentCommand{\section}{s o m}{%
        \IfBooleanTF{#1}
        {\oldsection*{\hyperref[toc]{#3}}}
        {\IfValueTF{#2}
            {\oldsection[#2]{\hyperref[toc]{#3}}}
            {\oldsection[#3]{\hyperref[toc]{#3}}}
            %\label{section-\thesection}
        }%
    }
    
    \let\oldpart\part
    \RenewDocumentCommand{\part}{s o m}{%
        \IfBooleanTF{#1}
        {\oldpart*{\hyperref[toc]{#3}}}
        {\IfValueTF{#2}
            {\oldpart[#2]{\hyperref[toc]{#3}}}
            {\oldpart[#3]{\hyperref[toc]{#3}}}
            \label{part-\thepart}
        }%
    }
}

\hbadness=99999
\hfuzz=100.002pt

\begin{document}
    \tableofcontents\label{toc}
    \addtocontents{toc}{\protect\thispagestyle{empty}}
    \newpage
    \thispagestyle{fancy}
    \fancyhead{}
    \fancyfoot{}
    \renewcommand{\headrulewidth}{0pt}
    
    \setcounter{part}{0}
    \addtocontents{toc}{\cftpagenumbersoff{part}}
    \part*{\texorpdfstring{\vspace*{2cm} Część I \\[-1.5cm] Część pierwsza}{Część pierwsza}}
    \addcontentsline{toc}{part}{\texorpdfstring{I. Część pierwsza}{Część pierwsza}}
    
    \setcounter{chapter}{0}
    \chapter{Rozdział 1}
    \pagenumbering{arabic}
    \setcounter{page}{5}
    \lipsum[1]
    \stepcounter{figure}
    \cftaddtitleline{lof}{figure}{\large Ryc. \textbf{\thefigure.} Napis}{\textbf{\thepage}}
    \begin{figure}[h!] 
        \centering
        \includegraphics[width=3cm]{example-image-a} 
        \caption*{Napis}
    \end{figure}
    \section{Sekcja 1}
    \lipsum[2]
    \stepcounter{figure}
    \cftaddtitleline{lof}{figure}{\large Ryc. \textbf{\thefigure.} Napis 2}{\textbf{\thepage}}
    \stepcounter{figure}
    \cftaddtitleline{lof}{figure}{\large Ryc. \textbf{\thefigure.} Napis 3}{\textbf{\thepage}}
   \newpage
  
    \bookmarksetup{startatroot}
\end{document}
5

1 Answer 1

1

Don't redefine \thechapter and \thesection to add links. Instead redefine the chapter number by redefining the command from fncychap:

\renewcommand{\DOCH}{%
    \settowidth{\py}{\CNoV\thechapter}
    \addtolength{\py}{-10pt}      % Amount of space by which the
%                                  % number is shifted right
    \fboxsep=0pt%
    \colorbox[gray]{.85}{\rule{0pt}{40pt}\parbox[b]{\textwidth}{\hfill}}%
    \kern-\py\raise20pt%
    \hbox{\color[gray]{.5}\CNoV\hyperref[toc]{\thechapter}}\\%<<<---
  }

And to add a link to section numbers redefine \@seccntformat:

\makeatletter
\renewcommand\@seccntformat[1]{\csname sec#1numberformat\endcsname{\csname the#1\endcsname}\quad}
\newcommand\secsectionnumberformat[1]{\hyperref[toc]{#1}}
\makeatother 

(I don't think that these links are useful. I don't like it if documents have too many active areas, as you then have to be careful not to click on or touch a screen as it can move you to unwanted places. If I want to go to the toc I use the bookmarks.).

1
  • Many thanks, this is exactly what I wanted! And sorry for making a trouble! Oct 19, 2022 at 11:00

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.