2

I would like chapter titles added with \chapter{title} to be aligned to the right (i.e. with \raggedleft), but everything else that classifies as a chapter (i.e. a chapter added with \addchap{title} or the title of the Bibliography) to remain aligned to the left of the page.

The solution I found aligns every chapter heading to the left. Is it possible to change this locally within the document?

MWE:

\documentclass[chapterprefix=true]{scrreprt}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}

\usepackage{lipsum}

\renewcommand{\raggedchapter}{\raggedleft}

\begin{document}

\chapter{The first chapter} 
\lipsum[1-3]

\addchap{A chapter that should be flushleft}
\lipsum[4]

\end{document}

enter image description here

2

You can redefine \chapterlineswithprefixformat:

\documentclass[chapterprefix=true]{scrreprt}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}

\usepackage{lipsum}

\newcommand*{\originalchapterlineswithprefixformat}{}
\let\originalchapterlineswithprefixformat\chapterlineswithprefixformat

\renewcommand\chapterlineswithprefixformat[3]{%
  \Ifstr{#1}{chapter}
    {\Ifstr{#2}{}{\raggedright}{\raggedleft}}
    {}%
  \originalchapterlineswithprefixformat{#1}{#2}{#3}
}

\begin{document}
\tableofcontents
\chapter{The first chapter} 
\lipsum[1-3]

\addchap{A chapter that should be flushleft}
\lipsum[4]
\end{document}

Result:

enter image description here


KOMA-Script version 3.27 provides additional hooks. Then you can use

\documentclass[chapterprefix=true]{scrreprt}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{lipsum}

\AddtoDoHook{heading/begingroup/chapter}{\chapteralignment}
\newcommand*\chapteralignment[1]{%
  \IfUseNumber{\let\raggedchapter\raggedleft}{\let\raggedchapter\raggedright}%
}

\begin{document}
\tableofcontents
\chapter{The first chapter} 
\lipsum[1-3]

\addchap{A chapter that should be flushleft}
\lipsum[4]
\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.