[Tex/LaTex] How to insert the section name into the header

fancyhdrheader-footerkoma-script

I'm trying to use fancyhdr to put the current section name into my header, but the below code just returns a blank header. I can display the current page number without a problem.

\documentclass[paper=a4,fontsize=11pt]{scrartcl}
\usepackage[letterspace=150]{microtype}
\usepackage{sectsty}
\usepackage{parskip}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\chead{\rightmark}
\renewcommand{\headrulewidth}{0pt}

\begin{document}
\section*{sect.name}
\subsection*{sub.sect.name.a}
\section*{sub.sect.name.b}
\end{document}

What is the correct way to do this?

Best Answer

It is not necessary to use the packages sectsty, parskip and fancyhdr together with a KOMA-Script class.

This classes define the two commands \setkomafont and addtokomafont to change the font of the different elements in a document.

The class option parskip can be used to get a parskip instead of a parindent.

Headers and footers can be set by the package scrlayer-scrpage or the older scrpage2. Both are part of the KOMA-Script bundle.

The KOMA-Script classes have a special command for unnumbered section headings producing an entry in the page header and an entry in the table of contents: \addsec. Use this instead of \section*.

For further Information have a look at the KOMA-Script documentation.

\documentclass[parskip]{scrartcl}
\usepackage[letterspace=150]{microtype}

\usepackage[automark]{scrlayer-scrpage}% or alternativly scrpage2  
\pagestyle{scrheadings}
\clearscrheadfoot
\chead{\headmark}
\setkomafont{pagehead}{\normalfont\bfseries}

\usepackage{blindtext}% dummy text

\begin{document}
\addsec{sect.name.1}
\subsection*{sub.sect.name.a}
\Blindtext[10]
\subsection*{sub.sect.name.b}
\addsec{sect.name.2}
\end{document}

enter image description here