# Why \hyperlink{\rightmark}{} does not work in fancyhead (and inside some other macros)?

\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}
hypertexnames=false,
unicode=true,
bookmarksnumbered=false,
pdftoolbar=true}

{
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\chaptermark}[1]{\markboth{\thechapter\ ##1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ ##1}}

\renewcommand{\footrulewidth}{0pt}
}

%-----------------------------------

\begin{document}

\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.)

-
Maybe you can refer to comments of "Tex capacity exceeded." with beamer –  Claudio Fiandrino Apr 13 '12 at 12:33
Please, try and produce a minimal example. I can't reproduce the error. –  egreg Apr 13 '12 at 12:40
possible duplicate of nested macros and hyperlink problem –  Marco Daniel Apr 13 '12 at 12:43
As @egreg said, we would need a minimal working example (MWE) so we know the exact definition of \rightmark in your case. –  Martin Scharrer Apr 13 '12 at 14:22
Let's not downvote this any further. There's nothing to be gained from piling on; having a single net downvote is ample indication that the question needs to be improved. Also, those that have downvoted, please remember to undo your downvote once the question has been improved. –  Jake Apr 13 '12 at 17:41

As stated in my answer to your similar question Nested macros and hyperlink problem, the first argument of \hyperlink and the matching \hypertarget must be a fully expandable string. This string is only used as an internal name, similar to the name for \label and the matching \ref.

You should therefore try to use something else and not the section text as label. Something which is always expandable, but still different for every section. I would suggest using:

\fancyhead[LO,RE]{\hyperlink{section-\thesection}{\bfseries\rightmark}}


where \thesection expands to the section number. (If there is any issue with that use \arabic{section} instead, which is always expandable.)

-
\fancyhead[LO,RE]{\hyperlink{section-\thesection}{\bfseries\rightmark}} is not ok, because in \fancyhead \thesection results in the number of the last section of the page while \rightmark refers to the first. That is why I have to store the unique identifier in rightmark. –  pmks Apr 13 '12 at 19:38
@pmks: Please use backticks  to format inline code. –  Martin Scharrer Apr 13 '12 at 19:41
Yes, I see. Sorry I absolutely new here. –  pmks Apr 13 '12 at 19:47
@pmks: Sure, no problem! Have a look at Welcome to TeX.SX! which explains most of the basics. –  Martin Scharrer Apr 13 '12 at 19:49
Consider this: \leftmark contains the last \chapter issued before the end of the page, while \rightmark contains the first \section issued on the actual page. If none have been issued (on the actual page) the most recently defined one will be used. If there would be a macro like \thefirstsection wich would give the number of the first section of the actual page, there would be no need to use \rightmark` (see my other question (tex.stackexchange.com/questions/51635/…) on this site.) –  pmks Apr 14 '12 at 5:47