4

I'm trying to insert headings in my report using fancyhdr. I want the current chapter's name on the right corner of each page (one sided document). the problem is that when, inside a chapter, I add the second \section, the heading doesn't work and only the chapter number appears, not the name.

I know that the problem is inside the \titleformat line I wrote, but I don't know how to fix it. I need that \titleformat instruction in order to let a line appear under each chapter name, and to avoid the word "chapter nr." to appear at the beginning of each chapter.

Could anyone help me?

\documentclass[a4paper, titlepage, 12pt, oneside]{report}

\usepackage[T1]{fontenc}
\usepackage{setspace}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{titlesec}
\usepackage{booktabs}
\usepackage{fancyhdr} 
\usepackage{quoting}
\quotingsetup{font=small}

\usepackage{lipsum}

\pagestyle{fancy}

\titleformat{\chapter}
   {\normalfont\Huge\bfseries}{\arabic{chapter}}{1em}{}[{\titlerule[0.8pt]}]

\renewcommand{\chaptermark}[1]{\markboth{\thechapter. \ #1}{}}
\fancyhead[L]{\chaptermark}

\begin{document}
    \chapter{test_chapter1}
        \section{test_section1}
            \lipsum[1-15]
        \section{test_section2}
            \lipsum[1-15]
\end{document}

2 Answers 2

7

\chaptermark is executed by \chapter. Normally it sets the left and/or right mark for page header/footer using \markboth or \markright. And this marks (\leftmark and/or \rightmark) can then be used inside \fancyhead or \fancyfoot.

So you have to use

\fancyhead{}
\fancyhead[R]{\leftmark}

enter image description here

Code:

\documentclass[a4paper, titlepage, 12pt, oneside]{report}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{titlesec}
\usepackage{fancyhdr} 

\usepackage{lipsum}

\pagestyle{fancy}

\titleformat{\chapter}
   {\normalfont\Huge\bfseries}{\arabic{chapter}}{1em}{}[{\titlerule[0.8pt]}]

\renewcommand{\chaptermark}[1]{\markboth{\thechapter. \ #1}{}}
\fancyhead{}% <- added
\fancyhead[R]{\leftmark}% <- changed

\begin{document}
\chapter{test chapter1}
\section{test section1}
\lipsum[1-15]
\section{test section2}
\lipsum[1-15]
\end{document}
0
4

Since you load titlesec load it with option [pagestyles]: it's so much simpler with package titleps! Here's a code:

\documentclass[a4paper, titlepage, 12pt, italian, oneside]{report}

\usepackage[T1]{fontenc}
\usepackage{setspace}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[pagestyles]{titlesec}
\usepackage{booktabs}
\usepackage{quoting}
\quotingsetup{font=small}

\usepackage{lipsum}

\newpagestyle{mystyle}{%
\headrule
\sethead{}{}{\thechapter~\chaptertitle}
\setfoot{}{\thepage}{}}

\pagestyle{mystyle}

\begin{document}

    \chapter{test-chapter1}
        \section{test-section1}
            \lipsum[1-15]
        \section{test-section2}
            \lipsum[1-15]

\end{document} 

enter image description here

You must log in to answer this question.

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