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}
\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


Additional remark: the next KOMA-Script version 3.27 will provide additional hooks. Then you can use

\documentclass[chapterprefix=true]{scrreprt}[2019/07/29]% needs prerelease of 3.27 or newer
\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}

The prerelease of the KOMA-Script version 3.27 is available on the KOMA-Script website.

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.