[Tex/LaTex] Changing section and subsection format not affecting headmark

scrlayer-scrpagetitlesec

I want to have the following document structure:

Exercise 1 %sections
a) %subsections
b)
Exercise 2

I found the following solution to put the section title before the numbering and changing the subsection number format:

\renewcommand{\thesubsection}{\alph{subsection})}
\titleformat{\section}[runin]{\normalfont\Large\bfseries}{}{0pt}{#1\ \thesection\\}
\titleformat{\subsection}[runin]{\normalfont\large\bfseries}{}{0pt}{#1\ \thesubsection\\}

Btw: Can I shift the whole subsection part to the right?

However I am using the package scrlayer-scrpage to define a footer and a header. In the inner footer a text should display the exercise number, which is the section number. So I used \headmark for this but it always displays the number before the term "Exercise". How can I change it?

This is a minimal example:

% !TeX encoding = UTF-8
\documentclass[a4paper,10pt,headsepline,footsepline,abstracton,notitlepage]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{scrlayer-scrpage}
\usepackage[explicit]{titlesec}

\renewcommand{\thesubsection}{\alph{subsection})}

\titleformat{\section}[runin]{\normalfont\Large\bfseries}{}{0pt}
{#1\ \thesection\\}
\titleformat{\subsection}[runin]{\normalfont\large\bfseries}{}{0pt}
{#1\ \thesubsection\\}

\automark{section}
\clearpairofpagestyles
\ihead{Author}
\ohead{\today}

\ifoot{\headmark}
\cfoot{Course xxx }
\ofoot{\pagemark}

\author{Chuck Norris}
\date{\today}

\title{xxx - Sheet 42}

\begin{document}


\maketitle
\begin{abstract}
    asdfahadga
\end{abstract}

\newpage

\section{Exercise}
tsestsfadf
\newpage
\subsection{Part }
asdf
\newpage
\section{Exercise}
\subsection{Part}
asdfadga
\subsection{Part}
\newpage

\end{document}

Best Answer

To change the order of section text and number in the page header or footer redefine \sectionmark

\renewcommand*\sectionmark[1]{%
  \markright{#1 \ifnumbered{section}{\sectionmarkformat}{}}%
}
\renewcommand*\sectionmarkformat{\thesection}

But note that it is not recommended to use titlesec together with a KOMA-Script class. You can redefine \sectionlinesformat to change the order of text and number for section and subsection headings:

\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \ifstr{#1}{section}{\@hangfrom{\hskip #2}{#4 #3}}{%
    \ifstr{#1}{subsection}{\@hangfrom{\hskip #2}{#4 #3}}{%
      \@hangfrom{\hskip #2#3}{#4}}}%
}
\makeatother

To redefine the format of the numbers in section and subsection headings use

\renewcommand*\sectionformat{\thesection}
\renewcommand*\subsectionformat{\thesubsection)}
\renewcommand*{\thesubsection}{\alph{subsection}}

To change or set the font of the section and subsection headings use

\addtokomafont{disposition}{\rmfamily}
\setkomafont{section}{\Large}
\setkomafont{subsection}{\large}

To change the order of section text and number in the page header or footer redefine \sectionmark as suggested above

\renewcommand*\sectionmark[1]{%
  \markright{#1 \ifnumbered{section}{\sectionmarkformat}{}}%
}
\renewcommand*\sectionmarkformat{\sectionformat}

The last line ensures that the section number in the page header/footer is formatted in the same way as in the section heading.

enter image description here

Code:

\documentclass[
  fontsize=10pt,
  headsepline,
  footsepline,
  abstract=on
]{scrartcl}[2015/10/03]
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[automark]{scrlayer-scrpage}

\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \ifstr{#1}{section}{\@hangfrom{\hskip #2}{#4 #3}}{%
    \ifstr{#1}{subsection}{\@hangfrom{\hskip #2}{#4 #3}}{%
      \@hangfrom{\hskip #2#3}{#4}}}%
}
\makeatother
\renewcommand*\sectionformat{\thesection}
\renewcommand*\subsectionformat{\thesubsection)}
\renewcommand*{\thesubsection}{\alph{subsection}}

\addtokomafont{disposition}{\rmfamily}
\setkomafont{section}{\Large}
\setkomafont{subsection}{\large}

\renewcommand*\sectionmark[1]{%
  \markright{#1 \ifnumbered{section}{\sectionmarkformat}{}}%
}
\renewcommand*\sectionmarkformat{\sectionformat}

\clearpairofpagestyles
\ihead{Author}
\ohead{\today}

\ifoot{\rightmark}
\cfoot{Course xxx }
\ofoot{\pagemark}

\author{Chuck Norris}
\date{\today}
\title{xxx - Sheet 42}

\usepackage{lipsum}% dummy text
\begin{document}
\maketitle
\begin{abstract}
  \lipsum[1]
\end{abstract}
\clearpage
\section{Exercise}
\lipsum
\subsection{Part}
\lipsum[2]
\section{Exercise}
\subsection{Part}
\lipsum[3]
\subsection{Part}
\lipsum
\end{document}

To indent the whole subsection part you could use addmargin.

 \documentclass[
  fontsize=10pt,
  headsepline,
  footsepline,
  abstract=on
]{scrartcl}[2015/10/03]
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[automark]{scrlayer-scrpage}

\newcommand*\exercisepartindent{20pt}

\RedeclareSectionCommand[
  afterskip=.5\baselineskip
]{section}

\RedeclareSectionCommand[
  beforeskip=-.5\baselineskip,
  afterskip=1sp,
  indent=\exercisepartindent
]{subsection}

\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \ifstr{#1}{section}{\@hangfrom{\hskip #2}{#4 #3}}{%
    \ifstr{#1}{subsection}{\@hangfrom{\hskip #2}{#4 #3}}{%
      \@hangfrom{\hskip #2#3}{#4}}}%
}
\makeatother
\renewcommand*\sectionformat{\thesection}
\renewcommand*\subsectionformat{\thesubsection)}
\renewcommand*{\thesubsection}{\alph{subsection}}

\addtokomafont{disposition}{\rmfamily}
\setkomafont{section}{\Large}
\setkomafont{subsection}{\large}

\automark{section}
\renewcommand*\sectionmark[1]{%
  \markright{#1 \ifnumbered{section}{\sectionmarkformat}{}}%
}
\renewcommand*\sectionmarkformat{\sectionformat}

\clearpairofpagestyles
\ihead{Author}
\ohead{\today}

\ifoot{\rightmark}
\cfoot{Course xxx }
\ofoot{\pagemark}

\author{Chuck Norris}
\date{\today}
\title{xxx - Sheet 42}

\newenvironment{exercisepart}{%
  \subsection{Part}
  \begin{addmargin}[\exercisepartindent]{0cm}%
}{%
  \end{addmargin}
}

\usepackage{lipsum}% dummy text
\begin{document}
\maketitle
\begin{abstract}
  \lipsum[1]
\end{abstract}
\clearpage
\section{Exercise}
\lipsum[1-2]
\begin{exercisepart}
  \lipsum[2]
\end{exercisepart}
\section{Exercise}
\begin{exercisepart}
  \lipsum[3]
\end{exercisepart}
\begin{exercisepart}
  \lipsum
\end{exercisepart}
\end{document}

Result

enter image description here


With a KOMA-Script Version older than 3.19 (current is 3.21) you have to use titlesec to change the order of the section number and text. Note that this is not recommended for version 3.19 or newer (see above).

\documentclass[
  fontsize=10pt,
  headsepline,
  footsepline,
  abstract=on
]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[automark]{scrlayer-scrpage}

\newcommand*\exercisepartindent{20pt}

\usepackage[explicit]{titlesec}
\titleformat{\section}{\usekomafont{disposition}\usekomafont{section}}{}{0pt}{#1\ \sectionformat}
\titleformat{\subsection}{\usekomafont{disposition}\usekomafont{section}}{}{0pt}{#1\ \subsectionformat}
\titlespacing*{\section}{0pt}{3.5ex plus 1ex minus .2ex}{.5\baselineskip}
\titlespacing*{\subsection}{\exercisepartindent}{.5\baselineskip}{0pt}


\def\sectionformat{\thesection}
\def\subsectionformat{\thesubsection}
\renewcommand*{\thesubsection}{\alph{subsection}}

\addtokomafont{disposition}{\rmfamily}
\setkomafont{section}{\Large}
\setkomafont{subsection}{\large}

\automark{section}
\renewcommand*\sectionmark[1]{%
  \markright{#1 \ifnumbered{section}{\sectionmarkformat}{}}%
}
\renewcommand*\sectionmarkformat{\sectionformat}

\clearpairofpagestyles
\ihead{Author}
\ohead{\today}

\ifoot{\rightmark}
\cfoot{Course xxx }
\ofoot{\pagemark}

\author{Chuck Norris}
\date{\today}
\title{xxx - Sheet 42}

\newenvironment{exercisepart}{%
  \subsection{Part}
  \begin{addmargin}[\exercisepartindent]{0cm}%
}{%
  \end{addmargin}
}

\usepackage{lipsum}% dummy text
\begin{document}
\maketitle
\begin{abstract}
  \lipsum[1]
\end{abstract}
\clearpage
\section{Exercise}
\lipsum[1-2]
\begin{exercisepart}
  \lipsum[2]
\end{exercisepart}
\section{Exercise}
\begin{exercisepart}
  \lipsum[3]
\end{exercisepart}
\begin{exercisepart}
  \lipsum
\end{exercisepart}
\end{document}
Related Question