0

Below is the code I have currently and the output which it brings, but I was wondering if there was a way to get the section title to show in the same way without having to type it in /rhead for every section?

\documentclass[a4paper, 12pt, fleqn]{article}
\usepackage{fancyhdr}
\titleformat{\section}[display]{\centering\Large\bfseries}{}{0pt}{\underline} \sectionfont{\centering}
\pagestyle{fancy}
\fancyhf{}
\rhead{Unit 1: Algebra}
\lhead{MST124}
\rfoot{Page \thepage \hspace{1pt} of \pageref{LastPage}}
\section{Unit 1: Algebra}

enter image description here

2
  • You could switch to report or book class and use \chapter. Jan 12, 2021 at 15:01
  • You speak about chapters but you are using the article class, which does not provide the \chapter command. The code you make available lacks some packages and it does not compile at all. You should provide a minimal working example. However I will try to give you an answer.
    – Ivan
    Jan 12, 2021 at 15:06

2 Answers 2

1

As you load titlesec, I would use \titleps via the [pagestyles] option, which has a \sectiontitle command. Here is a code for a two-sided layout:

\documentclass[a4paper, twoside, 12pt, fleqn]{article}
\usepackage{lastpage}
\usepackage[pagestyles]{titlesec} %
\titleformat{\section}[display]{\centering\Large\bfseries}{}{0pt}{\underline}
\usepackage{lipsum}

\newpagestyle{own}{\headrule%
\sethead[\sectiontitle][][MST124]{MST124}{}{\sectiontitle}
\setfoot[Page \thepage \hspace{1pt} of \pageref{LastPage}][][]{}{}{Page \thepage \hspace{1pt} of \pageref{LastPage}}
}
\pagestyle{own}

\begin{document}

\section{Unit 1: Algebra}
\lipsum[1-20]

\end{document} 

enter image description here

0

To get the right headers you have to use the correct marks. In your case \leftmark should respond to your need:

\documentclass[a4paper, 12pt, fleqn]{article}
\usepackage{fancyhdr}
\usepackage{titlesec}
\usepackage{lastpage}
\usepackage{lipsum}
\titleformat{\section}[display]
  {\centering\Large\bfseries}
  {Unit \thesection}
  {0pt}
  {} 

\pagestyle{fancy}
  \fancyhf{}
  \fancyhead[R]{Unit \leftmark}
  \fancyhead[L]{MST124}
  \fancyfoot[R]{Page \thepage\ of \pageref{LastPage}}

\begin{document}

\section{Algebra}
\lipsum

\section{Foo}
\lipsum

\end{document}
0

You must log in to answer this question.

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