Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

colored-latex-chapter-titles

\documentclass[12pt,a4paper,
%   Add lists to TOC
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
\addtokomafont{sectioning}
    {\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.

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

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

\documentclass[12pt,a4paper,
%   Add lists to TOC
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}
\addtokomafont{chapter}{\color{white}}
\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}
%   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}

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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