1

I'm using fancyhdr and I want to have the header with chapters name without number opposite to section name. I've tried a lot of configurations, but none works with in this case.

How could be fixed?

ps: This short document has a different style in the Chapter page, but in the real document this is fixed.

\documentclass[11pt,a4paper,twoside,titlepage,openany]{book}
\usepackage{lipsum}
\usepackage[T1]{fontenc}
\usepackage{amsfonts}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage[italian]{babel}
\usepackage{graphicx}
\usepackage{lmodern}
\usepackage{sectsty}

\usepackage{geometry}
\geometry{
    left=35mm,
    right=30mm,
    top=27mm,
    bottom=30mm
}

\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

\fancyhead[LE,RO]{\rightmark}
\fancyhead[LO,RE]{\chaptermark}

\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}}

\fancyfoot[LE,RO]{\thepage} % Left side on Even pages; Right side on Odd pages

\begin{document}
\chapter{Chapter Intro}
\lipsum
\chapter{Chapter with subsections}
\section{section foo}
\lipsum
\subsection{subsection bar}
\lipsum
\end{document}
4
  • 1
    Does the folowing link helps? tex.stackexchange.com/questions/89569/… I mean the '\rhead' and '\lhead' commands... You don't need others I think
    – koleygr
    Commented Aug 25, 2017 at 15:51
  • Nope, because I need a book style so I have odd and even pages with opposite headers. I can do this, anyway, the problem is that I can't take away the "Chapter 1." from "Chapter 1. INTRO", for example. Commented Aug 25, 2017 at 15:58
  • I removed just babel and nowhere is shown a chapter (no number no name)
    – koleygr
    Commented Aug 25, 2017 at 16:02
  • Exactly, this is the problem. With this configuration, there is no chapter and no name. Remove \fancyhf{} to see something, but this make double numbering page in the foot. Commented Aug 25, 2017 at 16:17

2 Answers 2

1

With

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

each chapter sets the left mark to the chapter name. So you have to use

\fancyhead[LO,RE]{\leftmark}

to get the desired result.

enter image description here

Code:

\documentclass[11pt,a4paper,twoside,titlepage,openany]{book}
\usepackage{lipsum}% only for dummy text

\usepackage{geometry}
\geometry{
    left=35mm,
    right=30mm,
    top=27mm,
    bottom=30mm,
    headheight=13.6pt% <- added as suggest by fancyhdr
}

\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

\fancyhead[LE,RO]{\rightmark}
\fancyhead[LO,RE]{\leftmark}% <- changed

\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}}

\fancyfoot[LE,RO]{\thepage}

\begin{document}
\chapter{Chapter Intro}
\lipsum
\chapter{Chapter with subsections}
\section{section foo}
\lipsum
\subsection{subsection bar}
\lipsum
\end{document}

If there are some starred chapters which should get a header entry too, use \markboth manually. Example:

\chapter*{Introduction}\markboth{Introduction}{}

Of course you can define a macro to do that.

2
  • I tested your code with a chapter* (see his comments on my solution) and doesn't give the chapter there... I think your way with my definitions of chapter will give the same result as mine and is simpler (Or any way you can include this option (of starred) in your code)
    – koleygr
    Commented Aug 25, 2017 at 18:29
  • @koleygr Perfect! Thanks for the help. I've tried that two options, simplicity is better of course :D Commented Aug 28, 2017 at 10:36
0

Last Edit Answer

With this answer you can select the depth of sectioning that appear in fancyhead using \MaxLevel variable. (For example if you use number 2, only sections will be shown on fancy header)

Also, you have the extra option to use a deferent fancy head content from the name of the chapter or section etc by using starred sectioning with optional argument (something that doesn't allowed in ordinary latex)

An extra feature that may not be desired is that labels are added automaticaly to every chapter, section or subsection as {c:1} for chapter 1 (real first) {s:2} for second section {sss:3} for third subsubsection etc.

\documentclass[11pt,a4paper,twoside,titlepage,openany]{book}
\usepackage{lipsum}
\usepackage[T1]{fontenc}
\usepackage{amsfonts}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{lmodern}
\usepackage{sectsty}
\usepackage{bophook}
\usepackage{refcount}

\usepackage{geometry}
\geometry{
    left=35mm,
    right=30mm,
    top=27mm,
    bottom=30mm
}

\usepackage{fancyhdr}

\newcounter{realchapter}
\newcounter{realsection}
\newcounter{realsubsection}
\newcounter{realsubsubsection}

\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

\newcount\Level
\def\MaxLevel{3} % Change this to show only desired depth on fancy header




\def\PrevSec{}

\makeatletter
\let\oldchapter=\chapter
\def\chapter{%
\xdef\ind{c:0}
\stepcounter{realchapter}
\xdef\PrevSec{\curfmark}
\global\Level=1%
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\@ifnextchar[%
{\@StarredWith}%
{\@StarredWithout}%
}      
\def\@StarredWith[#1]#2{%
\xdef\ind{c:\therealchapter}
\oldchapter*{#2}\index{\ind}\label{\ind}\def\Chaptername{#1}%
}
\def\@StarredWithout#1{%
\xdef\ind{c:\therealchapter}
\oldchapter*{#1}\index{\ind}\label{\ind}\def\Chaptername{#1}%
}
\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[#1]#2{%
\xdef\ind{c:\therealchapter}
\oldchapter[#1]{#2}\index{\ind}\label{\ind}\def\Chaptername{#1}%
}
\def\@nonStarredWithout#1{%
\xdef\ind{c:\therealchapter}
\oldchapter{#1}\index{\ind}\label{\ind}\def\Chaptername{#1}%
}

\let\oldsection=\section
\def\section{%
\stepcounter{realsection}
\xdef\PrevSec{\curfmark}
\global\Level=2%
\@ifstar{\@StarredS}{\@nonStarredS}%
}
\def\@StarredS{%
\@ifnextchar[%
{\@StarredWithS}%
{\@StarredWithoutS}%
}      
\def\@StarredWithS[#1]#2{%
\xdef\ind{s:\therealsection}
\oldsection*{#2}\index{\ind}\label{\ind}\def\Sectionname{#1}% Here you can use starred chapter, section etc with optional argument for chaptermark or sectionmark etc
}
\def\@StarredWithoutS#1{%
\xdef\ind{s:\therealsection}
\oldsection*{#1}\index{\ind}\label{\ind}\def\Sectionname{#1}%
}
\def\@nonStarredS{%
\@ifnextchar[%
{\@nonStarredWithS}%
{\@nonStarredWithoutS}%
}
\def\@nonStarredWithS[#1]#2{%
\xdef\ind{s:\therealsection}
\oldsection[#1]{#2}\index{\ind}\label{\ind}\def\Sectionname{#2}%
}
\def\@nonStarredWithoutS#1{%
\xdef\ind{s:\therealsection}
\oldsection{#1}\index{\ind}\label{\ind}\def\Sectionname{#1}%
}

\let\oldsubsection=\subsection
\def\subsection{%
\stepcounter{realsubsection}
\xdef\PrevSec{\curfmark}
\global\Level=3%
\@ifstar{\@StarredSS}{\@nonStarredSS}%
}
\def\@StarredSS{%
\@ifnextchar[%
{\@StarredWithSS}%
{\@StarredWithoutSS}%
}      
\def\@StarredWithSS[#1]#2{%
\xdef\ind{ss:\therealsubsection}
\oldsubsection*{#2}\index{\ind}\label{\ind}\def\Subsectionname{#1}% Here you can use starred chapter, section etc with optional argument for chaptermark or sectionmark etc
}
\def\@StarredWithoutSS#1{%
\xdef\ind{ss:\therealsubsection}
\oldsubsection*{#1}\index{\ind}\label{\ind}\def\Subsectionname{#1}%
}
\def\@nonStarredSS{%
\@ifnextchar[%
{\@nonStarredWithSS}%
{\@nonStarredWithoutSS}%
}
\def\@nonStarredWithSS[#1]#2{%
\xdef\ind{ss:\therealsubsection}
\oldsubsection[#1]{#2}\index{\ind}\label{\ind}\def\Subsectionname{#1}%
}
\def\@nonStarredWithoutSS#1{%
\xdef\ind{ss:\therealsubsection}
\oldsubsection{#1}\index{\ind}\label{\ind}\def\Subsectionname{#1}%
}

\let\oldsubsubsection=\subsubsection
\def\subsubsection{%
\stepcounter{realsubsubsection}
\xdef\PrevSec{\curfmark}
\global\Level=4%
\@ifstar{\@StarredSS}{\@nonStarredSS}%
}
\def\@StarredSSS{%
\@ifnextchar[%
{\@StarredWithSSS}%
{\@StarredWithoutSSS}%
}      
\def\@StarredWithSSS[#1]#2{%
\xdef\ind{sss:\therealsubsubsection}
\oldsubsubsection*{#2}\index{\ind}\label{\ind}\def\Subsubectionname{#1}% Here you can use starred chapter, section etc with optional argument for chaptermark or sectionmark etc
}
\def\@StarredWithoutSSS#1{%
\xdef\ind{sss:\therealsubsubsection}
\oldsubsubsection*{#1}\index{\ind}\label{\ind}\def\Subsubsectionname{#1}%
}
\def\@nonStarredSSS{%
\@ifnextchar[%
{\@nonStarredWithSSS}%
{\@nonStarredWithoutSSS}%
}
\def\@nonStarredWithSSS[#1]#2{%
\xdef\ind{sss:\therealsubsubsection}
\oldsubsubsection[#1]{#2}\index{\ind}\label{\ind}\def\Subsubsectionname{#2}%
}
\def\@nonStarredWithoutSSS#1{%
\xdef\ind{sss:\therealsubsubsection}
\oldsubsubsection{#1}\index{\ind}\label{\ind}\def\Subsubsectionname{#1}%
}
\makeatother




\def\mysecdepth#1{\ifcase#1  
Part
\or \empty%
\or Sectionname%
\or Subsectionname%
\or Subsubsectionname%
\fi}


\fancyhead[LE,RO]{\fmark}
\fancyhead[LO,RE]{\smark}

\def\curfmark{\ifnum\thepage<\getpagerefnumber{\ind}\PrevSec\else\ifnum\Level>\MaxLevel\csname\mysecdepth{\MaxLevel}\endcsname\else\csname\mysecdepth{\the\Level}\endcsname\fi\fi}



\newcommand{\fmark}{\curfmark}
\newcommand{\smark}{\Chaptername}

\fancyfoot[LE,RO]{\thepage} % Left side on Even pages; Right side on Odd pages

\begin{document}

\chapter{Chapter Intro}
\lipsum
\lipsum

\chapter*{Chapter 1 with subsections}
\section{section foo 1}
\lipsum[2-13]
\subsection{subsection bar 1}
\lipsum

\chapter{Chapter 2 with subsections}
\section{section foo 2}
\lipsum[2-13]
\subsection{subsection bar 2}
\lipsum

\chapter*[Different title in fancyheader]{Chapter 3 with subsections}
\section{section foo 3}
\lipsum
\subsection{subsection bar 3}
\lipsum

the last sebsection was \ref{ss:3} with starting on page \pageref{ss:3}

\end{document}

(Used Detecting if inside \section) and from this my answer

Old answer Test this to see if it is ok

\documentclass[11pt,a4paper,twoside,titlepage,openany]{book}
\usepackage{lipsum}
\usepackage[T1]{fontenc}
\usepackage{amsfonts}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{lmodern}
\usepackage{sectsty}

\usepackage{geometry}
\geometry{
    left=35mm,
    right=30mm,
    top=27mm,
    bottom=30mm
}

\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

\fancyhead[LE,RO]{\fmark}
\fancyhead[LO,RE]{\smark}

\let\Chaptermark\chaptermark
\def\chaptermark#1{\def\Chaptername{#1}\Chaptermark{#1}}

\newcommand{\fmark}{\rightmark}
\newcommand{\smark}{\Chaptername}

\fancyfoot[LE,RO]{\thepage} % Left side on Even pages; Right side on Odd pages

\begin{document}

\chapter{Chapter Intro}
\lipsum
\lipsum

\chapter{Chapter with subsections}
\section{section foo}
\lipsum
\subsection{subsection bar}
\lipsum
\end{document}

Used answer here: how to get the current chapter name, section name, subsection name, etc?

10
  • You can do the same as I did with chapter to the section... but I did not really understood what you want with the sections... (Numbered or not)
    – koleygr
    Commented Aug 25, 2017 at 16:35
  • You are close to my idea, thanks! But I want to remove the numbers also from chapter, leaving only the name, because I've got some \chapter* that breaks this things. Commented Aug 25, 2017 at 16:42
  • @IlDordollano Edited my answer but removed sections numbering too (If you want I can add it back)
    – koleygr
    Commented Aug 25, 2017 at 17:22
  • Doesn't work... changing it!
    – koleygr
    Commented Aug 25, 2017 at 17:27
  • 1
    Sorry, but the issue is in the Last Edit Answer too: insert \lipsum[2-5] in Chapter 1 ... before the \subsection command.
    – esdd
    Commented Aug 25, 2017 at 20:11

You must log in to answer this question.

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