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 trying to change the background color of the chapter headings. I'd like the full top part of the page, including top and right margins to turn gray until the titlerule.

Currently they look like this:

\documentclass[10pt,twoside,openright]{book}
\usepackage{titlesec}
\titleformat{\chapter}[display]{\LARGE\sffamily\bfseries\raggedright}
  {\leftskip2cm {\Large\textmd{\chaptertitlename~\thechapter}}}
{0cm}{\leftskip2cm}[\vspace{4mm} {\color{dgray}\titlerule[.6mm]} \enlargethispage{-\parskip}]

The \enlargethispage is used to make some extra space for the title number, which appears at the bottom of the page only at the first page of the chapter. My attempts with using the framed environment or colorbox have so far not been successful.

share|improve this question
    
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. In particular, you may want to look at How do I mark inline code? and How do I mark code blocks?, which can help increase the readability of your questions and answers. –  Adam Liter Aug 13 '13 at 10:44

4 Answers 4

up vote 2 down vote accepted

Because there were quite a lot of changes following your request from my other answer, I decided to write a new one instead of editing my old answer. Here you go.

I took the macro definitions of \blap and \tlap from here: google-groups

\documentclass[10pt,twoside,openright]{book}
\usepackage[explicit]{titlesec}
\usepackage{color}
\usepackage{eso-pic}
\usepackage{geometry}

\newcommand\chapterBox{%
  \put(0,0){%
    \parbox[b][\paperheight]{\paperwidth}{%
      \color{blue}\rule{\paperwidth}{8.1cm}
      \vfill
    }
}}

\def\tlap#1{\vbox to 0pt{\vss#1}}
\def\blap#1{\vbox to 0pt{#1\vss}}

\titleformat{\chapter}[display]
            {\LARGE\sffamily\bfseries\raggedright}
            {}
            {0cm}
            {\AddToShipoutPicture*{\chapterBox}
              \tlap{\parbox[b]{\linewidth}{%
                  \Large\textmd{\chaptertitlename~\thechapter}\newline
                  \Huge\bfseries #1
              }}
            }
            [{\color{red}\titlerule[.6mm]}\enlargethispage{-\parskip}]

\begin{document}
\chapter{My chapter}

text

\chapter{My extremely long second chapter heading}

\chapter{My last and even longer chapter heading that is so long, it does actually span three lines}

text

\clearpage

text

\end{document}
share|improve this answer
1  
Your code will produce undesired results (the label "Chapter #") will be present in unnumbered chapters. You can prevent this, for example, by using a similar definition using the numberless key (as in my answer). –  Gonzalo Medina Aug 14 '13 at 4:26

Here's one solution using TikZ (through tikzpagenodes). Here, unnumbered chapters (ToC, LoF, LoT , \chapter*) will behave as expected, using the numberless key from titlesec (in the example code I changed from openright to openany just for commodity):

\documentclass[10pt,twoside,openany,draft]{book}
\usepackage{tikzpagenodes}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}

\titleformat{\chapter}[display]
  {\LARGE\sffamily\bfseries\raggedright}
  {}{0cm}
  {\begin{tikzpicture}[remember picture,overlay]
     \fill[gray!20] 
        (current page.north west) rectangle ([yshift=-7cm]current page.north east);
      \node[inner sep=0pt,anchor=south west] (title) at 
        ([yshift=-6.5cm]current page text area.west|-current page.north)
        {\parbox[t]{2cm}{\mbox{}}%
          \parbox[t]{\dimexpr\linewidth-2cm\relax}{\raggedright#1}};
       \node[inner sep=0pt,anchor=north west] (label) at 
        ([yshift=-4cm]current page text area.west|-current page.north)
        {\hspace*{2cm}{\Large\textmd{\chaptertitlename~\thechapter}}};
      \node[inner sep=0pt,anchor=north west] at ([yshift=-7cm]current page.north west)
        {\color{gray}\rule{\paperwidth}{.6mm}};
    \end{tikzpicture}%
  }
  [\enlargethispage{-\parskip}]

\titleformat{name=\chapter,numberless}[display]
  {\LARGE\sffamily\bfseries\raggedright}
  {}{0cm}
  {\begin{tikzpicture}[remember picture,overlay]
     \fill[gray!20] 
        (current page.north west) rectangle ([yshift=-7cm]current page.north east);
      \node[inner sep=0pt,anchor=south west] (title) at 
        ([yshift=-6.5cm]current page text area.west|-current page.north)
        {\parbox[t]{2cm}{\mbox{}}%
          \parbox[t]{\dimexpr\linewidth-2cm\relax}{\raggedright#1}};
      \node[inner sep=0pt,anchor=north west] at ([yshift=-7cm]current page.north west)
        {\color{gray}\rule{\paperwidth}{.6mm}};
    \end{tikzpicture}%
  }
  [\enlargethispage{-\parskip}]

\titlespacing*{\chapter}
  {0pt}{50pt}{10pt}

\begin{document}

\tableofcontents
\chapter{Test chapter}
\lipsum[4]
\chapter[Test chapter]{Test chapter with a really long title just for the example}
\lipsum[4]
\chapter*{Test unnumbered chapter}

\end{document}

enter image description here

Some zoomed images:

enter image description here

A numbered chapter:

enter image description here

share|improve this answer

I tried around a little and it turned out tougher than I thought. However, here's my suggestion:

You might want to play around with the length in the line marked with %* to fit your page geometry.

Also, because I didn't have a nice dark grey color at hand, I simply used blue. You can of course exchange blue with dgray, but I'll leave it this way so that the code compiles for other people as well.

I was a little suprised by the fact that I didn't need to \ClearShipoutPicture, but anyway... if the colored box appears on other pages as well, you might want to include it within the last argument to \titleformat.

\documentclass[10pt,twoside,openright]{book}
\usepackage{titlesec}
\usepackage{color}
\usepackage{eso-pic}
\usepackage{geometry}

\newcommand\chapterBox{%
  \put(0,0){%
    \parbox[b][\paperheight]{\paperwidth}{%
      \color{blue}\rule{\paperwidth}{7.5cm} %*
      \vfill
    }
}}

\titleformat{\chapter}[display]
            {\LARGE\sffamily\bfseries\raggedright}
            {\leftskip2cm\Large\textmd{\chaptertitlename~\thechapter}}
            {0cm}
            {\AddToShipoutPicture*{\chapterBox}\leftskip2cm}
            [\vspace{4mm}{\color{red}\titlerule[.6mm]}\enlargethispage{-\parskip}]

\begin{document}
\chapter{My chapter}

text

\clearpage

text

\end{document}
share|improve this answer
    
This almost generates the effect I want. The background looks exactly like I want it, except the chapter titles don't. The TOC title does not extend down as far and with multiline chapters, the second line crosses the bottom of the colored area. So in addition, I'd like a way to vertically align the last line of the chapter title, such that it always ends at the same position. –  GrowMyHair Aug 13 '13 at 14:13
    
Would it be acceptable to bottom-align the chapter heading with the bottom of the box (such that multi-line chapter headings start higher on the page), or do you insist on the colored box to extend according to the chapter header height? –  carsten Aug 13 '13 at 14:19
    
That would even be preferable. I'd like all the boxes to be the same size. The 7.5 cm even works nice for me. Also, I made a small modification. I removed the \titlerule in my own code and added a second .6mm dark grey rule extending over the width of the page to your code. I'm very pleased with the resulting box. –  GrowMyHair Aug 13 '13 at 14:38

Using Carstens answer as a basis, I started hacking my own style. I now use a big chapternumber followed by a vertically centered title, both in white on a gray background.

Indeed there were some modifications needed, which could have been done easier had I known of the numberless option.

Because of the length differences in the titles I decided to use two different font sizes. One for short titles such as 'contents', 'introduction' and 'conclusions', the other for the longer multiline chapter titles. Thank you both for the help!

\usepackage[explicit]{titlesec}
\usepackage{eso-pic}
\definecolor{lgray}{gray}{.5}

\newcommand{\bsf}{\sffamily\bfseries}
\newcommand\chapterBox{%
\put(0,0){%
\parbox[b][\paperheight]{\paperwidth}{%
  \color{lgray}\rule{\paperwidth}{75mm}\vspace{-5mm}
  \color{black}\rule{\paperwidth}{.5mm}
  \vfill }}}

\def\tlap#1{\vbox to 0pt{\vss#1}}
%\def\blap#1{\vbox to 0pt{#1\vss}}

\newcommand{\chapternum}{%
  \ifnum\value{chapter}>0
    \thechapter%
  \else
    { }%
  \fi}

\newcommand{\chaptersize}{%
  \ifnum\value{chapter}<2
    \fontsize{.75cm}{.75cm}\selectfont%
  \else
    \ifnum\value{chapter}>5
        \fontsize{.75cm}{.75cm}\selectfont%
    \else
        \fontsize{.64cm}{.64cm}\selectfont%
    \fi
  \fi}

\titleformat{\chapter}[display]
    {\sffamily}{\vspace{-9mm}}{0mm}{\vspace*{-10mm}%
    \AddToShipoutPicture*{\chapterBox}
    \tlap{\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} m{2cm} m{9.65cm}}
    \color{white}\fontsize{3cm}{3cm}\selectfont\bsf{\chapternum} &\parbox[c][4cm]        {9.66cm}
    {\color{white}\chaptersize\bsf\raggedright{#1}}
    \end{tabular*}}}
    [\enlargethispage{-\parskip}]

Edit: I now simplified the code quite a bit. The following creates a dark grey border around the the white title. Where the chapter number is in a large font and the chapter title in a smaller font next to it.

\usepackage[explicit]{titlesec}
\usepackage{eso-pic}
\newcommand{\bsf}{\sffamily\bfseries}

\newcommand{\chapternum}{
  \ifnum\value{chapter}>0
  \thechapter  \fi}

\newcommand{\chaptersize}{
  \ifnum\value{chapter}<2
    \fontsize{.75cm}{.75cm}\selectfont%
  \else
    \ifnum\value{chapter}>5
        \fontsize{.75cm}{.75cm}\selectfont%
    \else
        \fontsize{.64cm}{.75cm}\selectfont%
    \fi
  \fi}

\titleformat{\chapter}[display]
    {}{}{0mm}
    {\AddToShipoutPicture*{\parbox[b][\paperheight]{\paperwidth}{      \vspace{22mm}
    \color{black!50}\rule{\paperwidth}{45mm}\vspace{-\baselineskip}
    \color{black}   \rule{\paperwidth}{.3mm}\vspace{\dimexpr-45mm-\baselineskip}
    \color{black}   \rule{\paperwidth}{.3mm}
    \parbox[c][45mm]{\paperwidth}{
    \begin{tabular}{m{2cm} m{2.2cm} m{10cm}}
    & \color{white}\fontsize{35mm}{35mm}\selectfont\bsf{\chapternum} & \parbox[c][45mm]{10cm}
    { \color{white}\chaptersize\bsf\raggedright{#1}}
    \end{tabular}}  \vfill }}}
    [\vspace{-2mm}\enlargethispage{-\parskip}]
share|improve this answer

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.