7

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.

2 Answers 2

7

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}

4
  • 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.
    – patrick
    Nov 23, 2013 at 11:51
  • Also… leaving out my custom tocleft settings fixed my \listoffigures and \listoftables not getting the ABC marker.
    – patrick
    Nov 23, 2013 at 12:03
  • I've been struggling (for quite a while now) to get this working again with my updated binaries on (Yosemite and) El Capitan. There is no spacing between the prefix and the title ( see f.cl.ly/items/2J0I2G2K2A0W0N2O0H3R/title-space.png ). Can you help me out? Tested with your MWE and biber 2.1 and 2.3, pdfTeX 3.14159265-2.6-1.40.16 (TeX Live 2015), xpdf 3.04. PS: This is proof that I still don't understand the magic behind your code.
    – patrick
    Jan 14, 2016 at 1:51
  • Error log: f.cl.ly/items/3R1m1z2O1f2O230s0K42/mwe.log
    – patrick
    Jan 14, 2016 at 2:00
1

I found a new workaround for the problem explained in the comment to the original question. Basically one needs to replace the old \makeatletter from @marco-daniel with this one:

\usepackage{xcolor}
\colorlet{chapterbackground}{red}
\addtokomafont{chapter}{\color{white}}
\makeatletter
\renewcommand\chapterlinesformat[3]{%
  \colorbox{chapterbackground}{%
    \parbox[c][\dimexpr\totalheight+0.3em]{\dimexpr\linewidth-2\fboxsep}{%
      \raggedchapter%
      \@hangfrom{#2}#3%
    }}%
}
\renewcommand\chapterlineswithprefixformat[3]{%
  \colorbox{chapterbackground}{%
    \parbox[c][\dimexpr\totalheight+1em]{\dimexpr\linewidth-2\fboxsep}{%
      \raggedchapter%
      #2#3%
    }}%
}
\makeatother

All I need to figure out now is how to slightly indent the chapter prefix so that there is a small horizontal space as padding on the left.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .