[Tex/LaTex] Customize \chaptermark for \mainmatter

fancyhdrheader-footersectioning

Hey guys it's me again,

I tried to add \leftmark to my header, but I want to change it from Chapter 1. Introduction to 1 Introduction. My MWE:

\documentclass[12pt, oneside, a4paper]{book}
\usepackage{titlesec}
\titleformat{\chapter}{\huge\bfseries}{\thechapter}{20pt}{\huge}
\usepackage{fancyhdr}
\fancypagestyle{plain}
{
    \fancyhf{}
    \fancyhead[L]{\rightmark}
    \fancyfoot[R]{\thepage}
    \renewcommand{\headrulewidth}{0.5pt}
    \renewcommand{\footrulewidth}{0pt}
}

\begin{document}
    \frontmatter
        \tableofcontents
    \mainmatter
        \chapter{Introduction}
        \chapter{Blablabla}
\end{document}

I googled a lot a found \renewcommand{\chaptermark}[1]{\markboth{#1}{}} and other variations and inserted it at different places of the preamble, but it doesn't work.

Am I missing something important again? Guess so, but I couldn't point it out.

Best Answer

\chaptermark can be redefined the following way:

\makeatletter
\if@twoside
  \renewcommand*{\chaptermark}[1]{%
    \markboth{%
      \ifnum\value{secnumdepth}>-1 %
        \thechapter\ %
      \fi
      #1%
    }{}%
  }%
\else
  \renewcommand*{\chaptermark}[1]{%
    \markright{%
      \ifnum\value{secnumdepth}>-1 %
        \if@mainmatter  
          \thechapter\ %
        \fi
      \fi
      #1%
    }%
  \fi
}
\makeatother

It prints the headers:

CONTENTS
1 Introduction
2 Blablabla

The upper casing of CONTENTS can be removed by the redefinition of \tableofcontents:

\usepackage{etoolbox}
\patchcmd{\tableofcontents}{\MakeUppercase\contentsname}{\contentsname}{}{}
\patchcmd{\tableofcontents}{\MakeUppercase\contentsname}{\contentsname}{}{}

That removed \MakeUppercase from the definition of \tableofcontents (twice).