1

I am using the article class and a custom package where I use the fancyhdr package. I define my header using :

\fancyhead[CO,CE]{}
\fancyhead[RO,RE]{\scshape\leftmark}

The problem is that I have several sections and sections*. At each section* page, the header displays the name of the previous section and not the name of the section *.

I have no clue on how to change that, any help would be appreciated.

  • 1
    It's easy using package titleps which comes with titlesec, as it defines a \sectiontitle command. – Bernard Aug 19 '19 at 09:41

1 Answers1

1

Here is a way from my old answer here:

\documentclass{article}
\usepackage{lipsum}
\usepackage{fancyhdr}
%\fancyhead[CO,CE]{}
\fancyhead[R]{\scshape\leftmark}
\let\oldsection\section
\makeatletter
\def\section{%
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have parameters. I am going to ignore them!}\@StarredWith}%
{\@StarredWithout}%
}      
\def\@StarredWith[#1]#2{%
\oldsection*{#2}%
\renewcommand\leftmark{#1}%
}
\def\@StarredWithout#1{
\oldsection*{#1}%
\renewcommand\leftmark{#1}
}
\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[#1]#2{%
\oldsection[#1]{#2}%
\renewcommand\leftmark{#1}%
}
\def\@nonStarredWithout#1{%
\oldsection{#1}%
\renewcommand\leftmark{#1}%
}
\makeatother

\pagestyle{fancy}

\title{Test redefined sections}

\author{Kostis Leledakis}

\begin{document}
\maketitle
\tableofcontents
\clearpage

\section{A section}
\lipsum[1-8]
\section{Another section}
\lipsum[1-6]
\section*{A starred section}
\lipsum[1-8]
\section{Another numbered  section}
\lipsum[1-6]
\section*{Another starred section}
\lipsum[1-8]
\section[Short title of section \thesection]{Another numbered  section with short title}
\lipsum[1-6]
\section*[Short title of starred section]{Another starred  section with short title}
\lipsum[1-8]
\end{document}

enter image description here

enter image description here

enter image description here

koleygr
  • 20,105