I have a very similar question to this old (and already answered) one:
Giving headlines a background color spanning across the entire typearea
However, the solutions provided there only work for sections and below. As I want to use the class "scrreprt" from komascripts, I want to be able to give a background color to chapter headings as well. It should work for levels below, too.
Therefor I tried adapting the given solution in the following way:
\documentclass{scrreprt}
\usepackage{xcolor}
\usepackage{lipsum}
\addtokomafont{section}{\mysection}
\newcommand{\mysection}[1]{%
\setlength{\fboxsep}{0cm}%already boxed
\colorbox{orange}{%
\begin{minipage}{\linewidth}%
\vspace*{2pt}%Space before
#1
\vspace*{2pt}%Space after
\end{minipage}%
}}
\addtokomafont{chapter}{\mychapter}
\newcommand{\mychapter}[1]{%
\setlength{\fboxsep}{0cm}%already boxed
\colorbox{red}{%
\begin{minipage}{\linewidth}%
\vspace*{2pt}%Space before
#1
\vspace*{2pt}%Space after
\end{minipage}%
}}
\begin{document}
\tableofcontents
\chapter{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long chapter}
\lipsum[1]
\section{Example section}
\lipsum[1]
\section*{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
\lipsum[1]
\end{document}
This gives me the following result:
The solution works fine for \section but not for \chapter. For \chapter the title gets flushed to the outside margin. Interestingly enough, it seems to work for "chapters" such as the toc, that were created automatically.
I also tried this solution from the editor of the koma scripts and edited it a little:
\documentclass{scrreprt}
\usepackage{xcolor}
\addtokomafont{section}{%
\makebox[0pt][l]{%
\color{red}\rule[-\dp\strutbox]{\linewidth}{\baselineskip}%
}%
}
\addtokomafont{chapter}{%
\makebox[0pt][l]{%
\color{red}\rule[-\dp\strutbox]{\linewidth}{\baselineskip}%
}%
}
\begin{document}
\chapter{Chapter - not ok}
\section{Section - ok}
\end{document}
This also works well for sections. For chapters however, the background color spans wider to the margin that the text's width as seen here:
The provided code from Ulrike's link works for chapters, but only chapters:
\documentclass[12pt,a4paper,twoside]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\inputencoding{utf8}
\usepackage{mathpazo}
\usepackage{setspace, lipsum}
\usepackage{xcolor}
\usepackage{mdframed}
\mdfdefinestyle{chapter}{innerrightmargin=3cm,backgroundcolor=red,hidealllines=true}
\addtokomafont{chapter}
\makeatletter
\renewcommand*{\@@makechapterhead}[1]{%
\chapterheadstartvskip
\begin{mdframed}[style=chapter]
\setlength{\parindent}{\z@}\setlength{\parfillskip}{\fill}%
\normalfont\sectfont\nobreak\size@chapter{}%
\if@chapterprefix
\let\@tempa\raggedsection
\else
\let\@tempa\@hangfrom
\fi
\@tempa{\ifnum \c@secnumdepth >\m@ne%
\if@chapterprefix
\expandafter\size@chapterprefix
\else
\expandafter\size@chapter
\fi
{\chapterformat}%
\if@chapterprefix
\size@chapterprefix{}\endgraf\nobreak\vskip.5\baselineskip
\fi
\fi
}%
{\raggedsection \interlinepenalty \@M \size@chapter{#1}\par}%
\end{mdframed}
\nobreak\chapterheadendvskip
}
\renewcommand*{\@@makeschapterhead}[1]{%
\chapterheadstartvskip%
\begin{mdframed}[style=chapter]
{\normalfont\sectfont\nobreak\size@chapter{}%
\setlength{\parindent}{\z@}\setlength{\parfillskip}{\fill}%
\raggedsection \interlinepenalty \@M \size@chapter{#1}\par}%
\end{mdframed}%
\nobreak\chapterheadendvskip%
}
\makeatother
\begin{document}
\tableofcontents
\chapter{Introduction}
\lipsum[1]
\chapter{Main Part}
\lipsum[2]
\chapter{Conclusion}
\lipsum[3]
\section{Summary}
\section{Future developments}
\end{document}
The result is almost exactly what I want:
The background is only limited to chapters now and I don't know any TeX in order to know how to adept this to sections (and possibly subsections etc.). I tried it by replacing chapter with section but this (obviously?) didn't work.
Is there a possibility to fix the heading's alignment in the 1st way or make the background color smaller in the 2nd way or adept the 3rd way to sections as well?
Thanks in advance and best regards :)