Get the LaTeX chapter name and section name on the document header

Wow, this was a tough LaTeX problem, and it would really help if I knew a lot more about LaTeX.

My goal for a LaTeX PDF that I created recently was to get the header of my pages to look something like this:

Section Name (Chapter Name)

where “Section Name” and “Chapter Name” are left-aligned in the header of the page, and would be dynamically replaced by the actual values of the current section and chapter names, respectively.

I was driven by the fact that this was a very complicated technical document, and I wanted the users to be able to look at the header of their current page and easily identify the chapter and section they were currently in. This may seem like overkill, but I found it to be exceptionally useful, and the readers really liked it.

Solution

To enable this capability, all I had to do (hah!) was use the following code segment at the beginning of my LaTeX document. (Of course it took me 30 minutes to figure this out, and I was yelling and screaming the entire time.)

  \lhead{\nouppercase{\rightmark} (\nouppercase{\leftmark})}
  \chead{}
  \rhead{}
  \lfoot{\today}
  \cfoot{}
  \rfoot{\thepage}
  \renewcommand{\headrulewidth}{0.4pt}
  \renewcommand{\footrulewidth}{0.4pt}

  \renewcommand{\chaptermark}[1]{%
  \markboth{#1}{}}

As I wrote, I wish I knew more about LaTeX so I could explain this well, but alas, I do not. I can tell you that the important pieces are the \rightmark (section), \leftmark (chapter), and \renewcommand{\chaptermark} commands, and that I generated my LaTeX PDF document with the pdflatex command.

The \renewcommand{\chaptermark} portion of the code is especially important in getting the "Chapter" and chapter number out of the chapter text that appears in the header. If that doesn't make any sense, try commenting out these two lines of code, and then see what the resulting output looks like.

I forgot to mention it, but you can find out a lot more about this subject by reading the "fancyhdr" document, fancyhdr.pdf. It is available by searching at the tug.org web site.

 

Permalink

I found myself messing around with headers in LaTeX, and wanted to find the command for the chapter name. Using \leftmark did the trick, so thanks for that!

Permalink

WOW thx a lot man. I´ve searched so long for this hint and now i am finnaly done with my bachelor-thesis.

Thx man! :)

Permalink

Thanks a lot, i was really worried with my report as i couldn't figure out what to do, including the footer and header wasn't enough. thanks a lot for your help

Permalink

Just what I needed for my report. I had tried other things, but this (after a tiny change) was just like I wanted it to be.

Thanks again

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.