29

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.

15

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.

  • I am refering to \part - en.wikibooks.org/wiki/LaTeX/… – yolo Mar 14 '11 at 14:16
  • 7
    Note that this redefinition doesn't support the optional argument of \part. – Martin Scharrer Mar 14 '11 at 15:48
  • 3
    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 1 '13 at 0:35
  • 3
    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
15

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
}
  • Note that this solution (though it works very well in LaTeX) leads to an error on conversion with pandoc (v2.3.1) to docx (cf. my question on Stackoverflow ). Benjamin's solution converts fine with pandoc. – Luc M Oct 9 '18 at 17:58
12

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.

  • +1 for \partmark, which is not mentioned in the KOMA-script manual. – lockstep Mar 14 '11 at 14:36
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}}
8

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}
  • a more elegant solution... the marked answer was causing a few problems in another document – yolo May 15 '11 at 11:48
  • @umar: It is perfectly admissible to "un-accept" an answer and accept another one instead. – lockstep Dec 4 '11 at 20:45
  • No works in book class, just appears "#1" ... at least in my case – juanuni May 2 '15 at 1:39
  • @juanuni: The book class might have a different implementation of \part. My patching of auxiliary macros was specific to how report did it. – Matthew Leingang May 8 '15 at 15:01
  • @MatthewLeingang I saw that ... – juanuni May 8 '15 at 15:56
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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