\fancyhead[LO,RE]{\hyperlink{\rightmark}{\bfseries\rightmark}}
(\leftmark
is all the same) cause error:
TeX capacity exceeded, sorry [input stack size...
Why? (here, \rightmark
has double function: the name and text argument of hyperlink (actually it is the full name of a section (e.g. 1.2 mysection) by which a hypertarget was created before inside \section{...}
macro.)
Here is a siple working code. My idea is to have chapter and section names automatically hyperlinked in header. This code works but if you uncomment \usepackage[]{babel}
it produces the problematic error.
\documentclass[pdftex,a4paper,12pt,oneside]{book}%
%\usepackage[magyar,english]{babel}
\usepackage{fancyhdr}
\usepackage{xifthen}
\usepackage{xstring}
\usepackage{hyperref}
\hypersetup{colorlinks,
linktoc=all,
hypertexnames=false,
unicode=true,
bookmarksnumbered=false,
pdfmenubar=true,
pdftoolbar=true}
%------- Chapter link ------------
\newcommand{\chapterlink}[1]{\addtocounter{chapter}{1}\hypertarget{\thechapter\ #1}{}\addtocounter{chapter}{-1}}
\newcommand{\Chapter}[2][]{\chapterlink{#2}\ifthenelse{\equal{#1}{}}{\chapter{#2}}{\chapter{#1}{#2}}}
%------- Section link ------------
\newcommand{\sectionlink}[1]{\addtocounter{section}{1}\hypertarget{\thesection\ #1}{}\addtocounter{section}{-1}}
\newcommand{\Section}[2][]{\sectionlink{#2}\ifthenelse{\equal{#1}{}}{\section{#2}}{\section{#1}{#2}}}
\newcommand{\mainheader}
{
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\chaptermark}[1]{\markboth{\thechapter\ ##1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ ##1}}
\fancyhead[LO,RE]{\hyperlink{\leftmark}{\bfseries\StrBehind{\leftmark}{\ }}\ :\ \hyperlink{\rightmark}{\bfseries\StrBehind{\rightmark}{\ }}}
\fancyhead[LE,RO]{\bfseries\thepage}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{2.5pt}
\fancypagestyle{plain}{\fancyhead{}\renewcommand{\headrulewidth}{0pt}}
}
%-----------------------------------
\begin{document}
\mainheader
\Chapter{Chapter one}
chapter text chapter text chapter text chapter text chapter text chapter text
\newpage
\Section{Section one}
section text section text section text section text section text section text
\newpage
section text section text section text section text section text section text
\end{document}
Investigating the meaning of the macro \rightmark
I have found the follwing:
macro:->\expandafter \@rightmark \firstmark \@empty \@empty
So I had a look into \firstmark
without \usepackage[magyar]{babel}
:
{1:Chapter-One}{3:Section-One}
%1 and 3 are my private counter values to be unique id-s in hyperlink, text is chapter name and section name
with \usepackage[magyar]{babel}
:
{\protect \foreignlanguage
{magyar}{\protect \bbl@restore@actives 1:Chapter-One}}{\protect \foreignlanguage {magyar}{\protect
\bbl@restore@actives 3:Section-One}}
Surely not fully expandable :)
But this investigation gives me the clue to solve my problem. \meaning\firstmark can be parsed and split into substrings to get that part of the text that I wanted to use to be \hyperlink name and text.
(If anybody like to see the whole MWE I readyly show it.)
\rightmark
in your case.