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

pmks
  • 341
  • 2
    Please, try and produce a minimal example. I can't reproduce the error. – egreg Apr 13 '12 at 12:40
  • The first argument of \hyperlink must be fully expandable and \rightmark seems not to be in this case. You can't simply use arbitrary material as a label or hyperlink name. See my answer to Nested macros and hyperlink problem for details. – Martin Scharrer Apr 13 '12 at 12:58
  • @Martin What is your advice to get \rightmark's output in a fully expandable form? How can I store \rightmark's output - not the whole macro - in a variable? (I tried \newcommand{\temp}{\rightmark} but it is all the same, maybe the whole macro is assigned to \temp? – pmks Apr 13 '12 at 14:14
  • 1
    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
  • 2
    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
  • @MartinScharrer: After some desperate testing I found this: everything works fine until package babel referenced (\usepackage[language]{babel}). If I comment it out, all problem disappears. Language does not matter. So I have to investigate package babel? Unfortunately I am quite new in LaTeX.:) – pmks Apr 13 '12 at 19:14
  • @pmks: babel might put some code into \rightmark which is not expandable. Just make a copy of your document and remove everything else which is not related to the issue and add it to the above question as a MWE. Then we can have a closer look at it. Anyway: Do you really need to use \rightmark as a \hyperlink label? The label is only used internally, so using mylabel-\thesection etc. would also fine and expandable while still section dependent. – Martin Scharrer Apr 13 '12 at 19:19

1 Answers1

1

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

Martin Scharrer
  • 262,582
  • \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 (http://tex.stackexchange.com/questions/51635/how-can-i-get-the-section-number-of-the-first-section-of-a-page) on this site.) – pmks Apr 14 '12 at 05:47
  • I think the original question "why" is answered satisfactorily so I accept it. Still I don't want to close the topic cause I like to find a way to solve my problem. For example: I use \renewcommand\thesection{\color{color1}\arabic{chapter}.\arabic{section}} for coloring section numbers, but now is clear that using this "not fully expandable" \thesection macro to produce a unique name for \hyperlink will raise the same error. I may define my own counter to avoid using \thesection but I surely can not avoid using babel. – pmks Apr 14 '12 at 08:14