14

Question

How can I put the section name in the top right header on every page of my custom style mainmatter, but without the number. I do not want any subsections: ONLY SECTIONS.

Problem

First of all, I have an incomplete understanding of the mark commands in LaTeX:

  • \leftmark (higher-level)=whatever this means... sections? chapters?
  • \rightmark (lower-level)=whatever this means... subsections?
  • \markboth

This was taken from chapter 9 The scoop on LaTeX's marks of the fancyhdr documentation version March 2, 2004.

Secondly, I cannot seem to get the \renewcommand{\sectionmark}[1]{\markright{#1}} of this question to work.

Example

\documentclass[10pt]{article}
\usepackage[quiet]{fontspec}
% !TEX TS-program = XeLaTeX
\setmainfont{SourceSansPro}[%
%    Path= /usr/local/texlive/2014/texmf-dist/fonts/opentype/adobe/sourcesanspro/ ,
    Extension = .otf ,
    UprightFont = *-Regular ,
    ItalicFont = *-RegularIt ,
    BoldFont = *-Bold ,
    BoldItalicFont = *-BoldIt ]
\usepackage[%
    a4paper,
%   includeheadfoot,
    head=\baselineskip,  % distance from bottom of header to block of text aka \headsep e.g. \baselineskip
    foot=2.3cm,  % distance from top of footer to block of text aka \footskip
    headheight=12pt,     % height for the header block (no equivalent for footer)
%   heightrounded,       % ensure an integer number of lines
    marginparwidth=2cm,  % right marginal note width
    marginparsep=2mm,    % distance from text block to marginal note box
%   height=\textheight,  % height of the text block
%   width=\textwidth,    % width of the text block
    top=2.5cm,           % distance of the text block from the top of the page
    bottom=3cm,
    left=2.5cm,
    right=2.5cm,
%    showframe,           % show the main blocks
%    verbose,             % show the values of the parameters in the log file
]{geometry}

\usepackage{fancyhdr}


\pagestyle{fancy} % This must be here, because defaults are set and renewcommand for section marks will work.
\renewcommand{\sectionmark}[1]{\markright{#1}}


%Fancyhdr Styles
\fancypagestyle{frontmatter}{%
   \fancyhf{} % clear all fields
   \renewcommand{\headrulewidth}{0pt}
   \lhead{}
   \lfoot{}
   \cfoot{}
   \rfoot{}
}%
\fancypagestyle{mainmatter}{%
   \fancyhf{} % clear all fields
   \renewcommand{\headrulewidth}{0pt}
   \lhead{Test Left Header}
   \rhead{\leftmark}
   \lfoot{}
   \cfoot{}
   \rfoot{}
}%

\begin{document}
\pagestyle{frontmatter}
\section{Monkey}
Some text about monkeys.
\newpage
\pagestyle{mainmatter}
\section{Yack}
Some text about yacks.
\newpage
\section{Alpine ibex}
Some text about alpine ibexes.
\end{document}
2
  • 5
    Well, you never use \rightmark, so how'd you expect it appears somewhere?
    – egreg
    Commented Jan 9, 2015 at 10:47
  • 1
    @egreg +1 Buahaha I actually laughed after reading that comment! This is very true :D Commented Jan 9, 2015 at 10:55

1 Answer 1

9

You have to change \leftmark to \rightmark in the definition of mainmatter pagestyle.

Also you have to nullify the effect of \subsectionmark

\renewcommand{\subsectionmark}[1]{}

MWE

\documentclass[10pt]{article}
\usepackage[quiet]{fontspec}
% !TEX TS-program = XeLaTeX
\setmainfont{SourceSansPro}[%
%    Path= /usr/local/texlive/2014/texmf-dist/fonts/opentype/adobe/sourcesanspro/ ,
    Extension = .otf ,
    UprightFont = *-Regular ,
    ItalicFont = *-RegularIt ,
    BoldFont = *-Bold ,
    BoldItalicFont = *-BoldIt ]
\usepackage[%
    a4paper,
%   includeheadfoot,
    head=\baselineskip,  % distance from bottom of header to block of text aka \headsep e.g. \baselineskip
    foot=2.3cm,  % distance from top of footer to block of text aka \footskip
    headheight=12pt,     % height for the header block (no equivalent for footer)
%   heightrounded,       % ensure an integer number of lines
    marginparwidth=2cm,  % right marginal note width
    marginparsep=2mm,    % distance from text block to marginal note box
%   height=\textheight,  % height of the text block
%   width=\textwidth,    % width of the text block
    top=2.5cm,           % distance of the text block from the top of the page
    bottom=3cm,
    left=2.5cm,
    right=2.5cm,
%    showframe,           % show the main blocks
%    verbose,             % show the values of the parameters in the log file
]{geometry}

\usepackage{fancyhdr}

\usepackage{lipsum}


\pagestyle{fancy} % This must be here, because defaults are set and renewcommand for section marks will work.
\renewcommand{\sectionmark}[1]{\markright{#1}}
\renewcommand{\subsectionmark}[1]{}


%Fancyhdr Styles
\fancypagestyle{frontmatter}{%
   \fancyhf{} % clear all fields
   \renewcommand{\headrulewidth}{0pt}
   \lhead{}
   \lfoot{}
   \cfoot{}
   \rfoot{}
}%
\fancypagestyle{mainmatter}{%
   \fancyhf{} % clear all fields
   \renewcommand{\headrulewidth}{0pt}
   \lhead{Test Left Header}
   \rhead{\rightmark}
   \lfoot{}
   \cfoot{}
   \rfoot{}
}%

\begin{document}
\pagestyle{frontmatter}
\section{Monkey}
Some text about monkeys.
\lipsum[1-20]
\newpage
\pagestyle{mainmatter}
\section{Yack}
Some text about yacks.
\lipsum[1-20]
\newpage
\section{Alpine ibex}
\subsection{A subsection}
Some text about alpine ibexes.
\lipsum[1-20]
\end{document} 

Output:

enter image description here

1
  • 6
    Could you please explain what "higher-level" and "lower-level" means? It is one aspect of the question you did not address. Am I right with my assumption? Commented Feb 29, 2016 at 7:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .