# Color background for chapter titles which stretches horizontally to the margins

I'm working on my chapter titles. I like them to have a background which stretches to the edge of the page.

The drawbacks of the minimal document below are:

1. The TOC also has the background, too. I'd rather have the TOC text color in the red background color I used for fill and have no background fill at all in the TOC. Eventually the default black would work, too (but red would be cooler, including the TOC page numbers).
2. I need to move the \rule I used a fair bit down since a bit of padding there looks better (and have background for letters like p, q, g, etc.)
3. I'm not sure it works well with multiple line titles, but that could probably be puzzled out with enough padding and increasing the size of \rule.

\documentclass[12pt,a4paper,
listof=numbered,numbers=noenddot,version=first,
]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\inputencoding{utf8}
\usepackage{helvet}
\usepackage{mathpazo}
\usepackage{setspace, lipsum}
\usepackage{xcolor}
\usepackage{tocloft}

%   SECTION TITLES
% apply only singlespace to section starters (chapters, etc.)
% can also be set only for chapter instead of sectioning
\makeatletter
{\color[RGB]{255,255,255}
\makebox[0pt][l]{{\color[RGB]{212,17,37}\rule{1.3\textwidth}{2em}}}
}
{\setstretch{\setspace@singlespace}
}
\makeatother

\begin{document}
%   TOC
\tableofcontents
\pagebreak

\chapter{Introduction}
\lipsum[1]
\chapter{Main Part}
\lipsum[2]
\chapter{Conclusion}
\lipsum[3]

\end{document}


I also saw this similar thread, but I'm \blap and \tlap might not be capable of adjusting well to multi-line chapter titles.

Then again, I'm positive there's a better way to do what I want than my current approach, maybe even a \colorbox. But my head is hurting from the LaTeX input of the last days and I figured for this tricky problem some professional help might be beneficial.

Does anyone know how to pull it off?

Status: LaTeX user for 3 days.

-

Here a suggestion using mdframed. I also removed the package tocloft which isn't needed.

\documentclass[12pt,a4paper,
listof=numbered,numbers=noenddot,version=first,
]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\inputencoding{utf8}
\usepackage{helvet}
\usepackage{mathpazo}
\usepackage{setspace, lipsum}
\usepackage{xcolor}
\usepackage{mdframed}
\mdfdefinestyle{chapter}{rightmargin=-3cm,innerrightmargin=3cm,backgroundcolor=red,hidealllines=true}
\makeatletter
\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}
}
\begin{mdframed}[style=chapter]
{\normalfont\sectfont\nobreak\size@chapter{}%
\setlength{\parindent}{\z@}\setlength{\parfillskip}{\fill}%
\raggedsection \interlinepenalty \@M \size@chapter{#1}\par}%
\end{mdframed}%
}
\makeatother

\begin{document}
%   TOC
\tableofcontents
\pagebreak

\chapter{Introduction}
\lipsum[1]
\chapter{Main Part}
\lipsum[2]
\chapter{Conclusion Conclusion Conclusion Conclusion Conclusion Conclusion Conclusion Conclusion Conclusion }
\lipsum[3]

\end{document}


-
Wow. mdframed  is an awesome package. Coming from HTML/CSS I feel right at home. Thanks for helping me out and pointing me to a great package. –  pattulus Nov 23 '13 at 11:51
Also… leaving out my custom tocleft settings fixed my \listoffigures and \listoftables not getting the ABC marker. –  pattulus Nov 23 '13 at 12:03