32

Actually I am using the fancyhdr package, and trying to put the current part name in the right header.

I know that \rightmark and \leftmark are used to represent the current section and chapter heading respectively, but I can not find anything similar to represent the part name.

lockstep
  • 250,273
yolo
  • 3,303

6 Answers6

17

Putting the part title into the header is not common, therefore there are no default macros for this.

You could redefine the \part macro to save its argument to e.g. a \parttitle macro which then can be used in the custom defined headers:

\newcommand*\parttitle{}
\let\origpart\part
\renewcommand*{\part}[2][]{%
   \ifx\\#1\\% optional argument not present?
      \origpart{#2}%
      \renewcommand*\parttitle{#2}%
   \else
      \origpart[#1]{#2}%
      \renewcommand*\parttitle{#1}%
   \fi
}
Martin Scharrer
  • 262,582
16

That would depend on what type of "part" you are refering to, \chaptername, \sectionname, \thepage, \thesection, \thechapter, etc... See this link for details.

EDIT: In light of your comment, this thread seems to be discussing a solution (note: there is a typo, it must be \renewcommand{\part}..., i.e. a backslash must be added):

For using the parts, you can do the following:

\let\Oldpart\part
\newcommand{\parttitle}{}
\renewcommand{part}[1]{\Oldpart{#1}\renewcommand{\parttitle}{#1}}

and then use \parttitle in the header.

Another alternative is to use the titlesec package which provides commands like \partmark that you can use.

Speravir
  • 19,491
Benjamin
  • 496
  • 3
  • 13
  • I am refering to \part - http://en.wikibooks.org/wiki/LaTeX/Document_Structure#Sectioning_Commands – yolo Mar 14 '11 at 14:16
  • thank you very much - the solution from the thread works! – yolo Mar 14 '11 at 14:44
  • 7
    Note that this redefinition doesn't support the optional argument of \part. – Martin Scharrer Mar 14 '11 at 15:48
  • 4
    I just installed titlesec for this. However \partmark doesn't do anything for me. I checked their documentation and cannot find the phrase \partmark. – Kit Johnson May 01 '13 at 00:35
  • Of course the third line should start with \renewcommand{\part} (missing \ in front of part). – Turion Jul 27 '15 at 20:16
  • 4
    To avoid a bug when using this together with \AtBeginPart, the last line could rather be: \renewcommand{\part}[1]{\renewcommand{\parttitle}{#1}\Oldpart{#1}} – Turion Jul 27 '15 at 20:21
  • 1
    I personally used \renewcommand{\part}[1]{\pagebreak\Oldpart{#1}\renewcommand{\parttitle}{#1}}. Without it, the last page before the new part got the wrong heading. – Derlin Feb 23 '16 at 16:13
13

In the KOMA-classes \part executes a \partmark command which you could use to set either \rightmark or \leftmark to the part title.

But as \part always generates a new page and you probably need the header only on the next page you can also do something simple like this:

\newcommand\partcontent{}
...

\part{Blub}
\renewcommand\partcontent{Blub}

and then use \partcontent in the header.

Ulrike Fischer
  • 327,261
9

You could use \markboth{left headmark}{right headmark}% to put the title into the header. Some classes provide a \partmark command for it, which you could redefine for calling \markboth inside.

Standard classes don't provide \partmark. For example, book.cls even calls \markboth{}{} to remove the header entries when \part is called. You could redefine \part or hook somehow into it.

I would choose an extended class such as those of KOMA-Script instead of reinventing the wheel every time a new feature is required.

Deferring \markboth to the following page could be solved using the afterpage package:

\afterpage{\markboth{}{part title}}
Stefan Kottwitz
  • 231,401
9

I couldn't find a macro that saves the part's title, so I used the etoolbox package to patch the internals within the \part command to do that. Then you just take that save macro and put it wherever.

\documentclass{report}
\title{part name in right mark}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{etoolbox}

\makeatletter
% patch \@part[#1]{#2} and \@spart (see the class file) to save the part name
\pretocmd{\@part}{\gdef\parttitle{#1}}{}{}
\pretocmd{\@spart}{\gdef\parttitle{#1}}{}{}
\makeatother

\pagestyle{fancy}
\renewcommand{\rightmark}{\parttitle}


\begin{document}


\maketitle
\tableofcontents

\part{First part}

\chapter{first chapter}

\lipsum\lipsum\lipsum

\chapter{second chapter}

\lipsum\lipsum\lipsum

\part{Second part}

\chapter{third chapter}

\lipsum\lipsum\lipsum

\chapter{fourth chapter}

\lipsum\lipsum\lipsum

\end{document}
Matthew Leingang
  • 44,937
  • 14
  • 131
  • 195
0

I would like to expand a little on Martin Scharrer's answer. If you also need the chapter number, you can do this:

\directlua{partcount = 0}
\newcommand*\partnumber{}
\newcommand*\parttitle{}
\let\origpart\part
\renewcommand*{\part}[2][]{%
   \ifx\\#1\\% optional argument not present?
      \origpart{#2}%
      \renewcommand*\parttitle{#2}%
   \else
      \origpart[#1]{#2}%
      \renewcommand*\parttitle{#1}%
   \fi
   \directlua{partcount = partcount + 1}%
   \renewcommand*\partnumber{\directlua{tex.sprint(partcount)}}%
}

It defines the \partnumber command, to get the part number, as well as the \parttitle command.

This is using LuaTex, but something similar is probably also possible in plain LaTeX.