3

I would like to know if it is possible in LaTeX to have automatically the current section name of a document reported in the page header or footer, with the possibility to jump back to the beginning of this current section by a click on this section name in the header or footer.

Same question for chapter.

Emile
  • 69
  • Good question. Try consulting the hyperref as well as the titlesec documentations, if you don't get an answer (although I doubt it). – thymaro Jan 20 '18 at 11:37
  • 1
    Please tell us more about your document setup. E.g., which document class do you employ? Do you use thefancyhdr package (or something similar)? – Mico Jan 20 '18 at 11:43
  • I use the Book KomaScript class and I am ready to use either fancyhdr or scrlayer-scrpage packages (for what I know). – Emile Jan 20 '18 at 12:07
  • I may be wrong but I have not seen the question adressed in hyperref or titlesec documentation. – Emile Jan 20 '18 at 12:37
  • I think it is a dublicate of https://tex.stackexchange.com/questions/122792/breadcrumb-hyperlink-header – sergiokapone Jan 20 '18 at 13:13

2 Answers2

1

Here is a solution using the scrbook class, the hyperref package, and the \automark option of scrlayer-scrpage. You can hide the links by adding the hidelinks option to hyperref. The command

\automark[section]{section}

updates the \headmark command to be the title of the current section for both even and odd pages. Then, the command

\ohead{\hyperlink{section.\arabic{chapter}.\arabic{section}}{\headmark}}

sets the outer header to be a hyperlink whose text is \headmark and whose target is the beginning of the current section. Note that the first page of a chapter does not have a header by default.

You can also produce links to chapters instead of sections by uncommenting the corresponding code below.

\documentclass{scrbook}
\usepackage{nameref}
\usepackage{lipsum}
\usepackage{scrlayer-scrpage}
\usepackage{hyperref}

% Use this to link to sections on both sides
\automark[section]{section}
\ohead{\hyperlink{section.\arabic{chapter}.\arabic{section}}{\headmark}}

% Use this to link to chapters on both sides
%\automark[chapter]{chapter}
%\ohead{\hyperlink{chapter.\arabic{chapter}}{\headmark}}

% Use this to link to chapters on even pages and sections on odd pages
%\automark[section]{chapter}
%\lehead{\hyperlink{chapter.\arabic{chapter}}{\headmark}}
%\rohead{\hyperlink{section.\arabic{chapter}.\arabic{section}}{\headmark}}

\pagestyle{scrheadings}

\begin{document}
    \chapter{Ch1}   
    \section{Ch1, S1}
    \lipsum[1-8]

    \section{Ch1, S2}
    \lipsum[1-8]

    \section{Ch1, S3}
    \lipsum[1-8]

    \chapter{Ch2}
    \section{Ch2, S1}
    \lipsum[1-8]
    \section{Ch2, S2}
    \lipsum[1-8]
    \section{Ch2, S3}
    \lipsum[1-8]
\end{document}

scrbook

wimi
  • 1,968
  • 8
  • 17
  • Same remark than for the first proposed solution, it works only for the numbered chapters and sections and not for the unnumbered ones of the scrbook class. – Emile Jan 20 '18 at 17:30
1

Possible solution for book class with defining own pagestyle:

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

\makeatletter

\def\ps@myheadings{
\def\chaptermark##1{%
      \markboth {\protect\hyperlink{\@currentHref}{\MakeUppercase{%
        \ifnum \c@secnumdepth >\m@ne
          \if@mainmatter
            \@chapapp\ \thechapter. \ %
          \fi
        \fi
        ##1}}}{}}%

\def\schaptermark##1{%
      \markboth {\protect\hyperlink{\@currentHref}{\MakeUppercase{%
        \ifnum \c@secnumdepth >\m@ne
        \fi
        ##1}}}{}}%

    \def\sectionmark##1{%
      \markright {\protect\hyperlink{\@currentHref}{\MakeUppercase{%
        \ifnum \c@secnumdepth >\z@
          \thesection. \ %
        \fi
        ##1}}}} 

    \def\ssectionmark##1{%
      \markright {\protect\hyperlink{\@currentHref}{\MakeUppercase{%
        ##1}}}} 

}

\def\schapter#1{
\chapter*{#1}
\addcontentsline{toc}{chapter}{#1}
\phantomsection
\schaptermark{#1}
}

\def\ssection#1{
\section*{#1}
\addcontentsline{toc}{section}{#1}
\phantomsection
\ssectionmark{#1}
}

\makeatother
\begin{document}
\pagestyle{myheadings}
\tableofcontents


\schapter{Intro}


\lipsum

\ssection{One}


\lipsum

\lipsum

\section{Two}

\lipsum

\chapter{title}

\lipsum

\section{One}

\lipsum

\end{document}

Solution for KOMA (based on answer of javi_gg1):

\documentclass{scrbook}
\usepackage{lipsum}
\usepackage[automark]{scrlayer-scrpage}
\usepackage{hyperref}


\makeatletter
\def\chaptermark#1{%
      \markboth {\protect\hyperlink{\@currentHref}{\MakeUppercase{%
        \ifnum \c@secnumdepth >\m@ne
          \if@mainmatter
            \@chapapp\ \thechapter. \ %
          \fi
        \fi
        #1}}}{}}%
    \def\sectionmark#1{%
      \markright {\protect\hyperlink{\@currentHref}{\MakeUppercase{%
        \ifnum \c@secnumdepth >\z@
          \thesection. \ %
        \fi
        #1}}}} 
\makeatother

\ohead{\leftmark}
\rohead{\rightmark}

\pagestyle{scrheadings}

\begin{document}
    \tableofcontents
    \addchap[Intro]{Intro}

    \lipsum

    \chapter{Ch1}  

    \section{Ch1, S1}
    \lipsum[1-8]

    \section{Ch1, S2}
    \lipsum[1-8]

    \section{Ch1, S3}
    \lipsum[1-8]

    \chapter{Ch2}
    \section{Ch2, S1}
    \lipsum[1-8]
    \section{Ch2, S2}
    \lipsum[1-8]
    \section{Ch2, S3}
    \lipsum[1-8]
\end{document}
sergiokapone
  • 5,578
  • 1
  • 16
  • 39
  • This is a very dirty hack given that KOMA has many options to create own page styles. – TeXnician Jan 20 '18 at 16:59
  • @TeXnicain Yes, I agree with you. I change answer for standart book class. – sergiokapone Jan 20 '18 at 17:05
  • I checked it for scrbook class. It works for numbered chapters and sections but it fails for unnumbered ones proposed by scrbook and obtained with \addchap and \addsec. – Emile Jan 20 '18 at 17:18
  • @Emile I had changed the code, but you need some additional complification for writing stared sectioning commands. – sergiokapone Jan 20 '18 at 19:05
  • @sergiokapone Is there a difficulty to handle the \addchap and \addsec ? They are unnumbered sectionning which appear in the table of content. – Emile Jan 20 '18 at 19:30
  • Ok, the solution for KOMA (based on answer of javi_gg1) works fine. Thanks to both of you ! – Emile Jan 20 '18 at 22:17