TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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



%------- Chapter link ------------
\newcommand{\chapterlink}[1]{\addtocounter{chapter}{1}\hypertarget{\thechapter\ #1}{}\addtocounter{chapter}{-1}}

%------- Section link ------------
\newcommand{\sectionlink}[1]{\addtocounter{section}{1}\hypertarget{\thesection\ #1}{}\addtocounter{section}{-1}}

                \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}{\ }}}  





\Chapter{Chapter one}

chapter text chapter text chapter text chapter text chapter text chapter text 


\Section{Section one}

section text section text section text section text section text section text 


section text section text section text section text section text section text 


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

share|improve this question
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
up vote 1 down vote accepted

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:


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

share|improve this answer
\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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.