6

I'm trying to place my chapter titles to the edge of the page, with a colored background box surrounding them. The surrounding boxes would need to be set outside of the body of the document. So far I have read the documentation of typearea, geometry, layouts, titlesec, TikZ and others but haven't been able to find a solution.

See http://latex-project.org/guides/lb2-ch4.pdf for an example of the effect I want to achieve. There the chapter title reaches farther right than the text body. What I want is to have my chapter titles in a a colored box similar to the headings on the 2nd page of the attached document on the left edge on the page.

Here is the code for the box. I just need a way to place it on the left page edge.

\documentclass[a4paper,12pt]{report}

\usepackage{tikz}
\usepackage[uppercase, sf, explicit, raggedright]{titlesec} %
\usepackage{xcolor}

\makeatletter
\titleformat{\chapter}
{\sffamily\huge\bfseries}
{}
{0pt}
{
    \usetikzlibrary{backgrounds}
    \tikzstyle{background rectangle}=[fill=cyan]
    \begin{tikzpicture}[framed]
    \color{white}   
    \draw (0,0) +(3ex,0) node[anchor=west]{Chapter \thechapter{} \filright#1};
    \end{tikzpicture}
}
\makeatother
\begin{document}
\chapter{Chaptertitle}
\end{document}
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Feb 28 '14 at 12:44
  • With your current settings, long title will protrude into the right margin. A solution would be to declare an appropriate value for text width for the node. Also, unnumbered chapters produced with \chapter* will have an spurious chapter number. – Gonzalo Medina Feb 28 '14 at 13:15
8

You can simply set the 4th argument of \titleformat to a negative value.

In the following MWE I've set it at the left margin of the page:

\documentclass[a4paper,12pt]{report}

\usepackage{tikz}
\usetikzlibrary{backgrounds}

\usepackage[uppercase, sf, explicit, raggedright]{titlesec} %
\usepackage{xcolor}
\usepackage{lipsum} % only for the example

\titleformat{\chapter}
{\sffamily\huge\bfseries}
{}
{\dimexpr-\oddsidemargin-\hoffset-1in}
{%
    \tikzstyle{background rectangle}=[fill=cyan]%
    \begin{tikzpicture}[framed]
    \color{white}
    \draw (0,0) +(3ex,0) node[anchor=west]{Chapter \thechapter{} \filright#1};
    \end{tikzpicture}%
}
\begin{document}
\chapter{Chaptertitle}
\lipsum[1-5]
\end{document} 

Output:

enter image description here

Some remarks:

  1. \makeatletter...\makeatother is not needed since no @ character is involved.
  2. I've put \usetikzlibrary{backgrounds} outside the definition.
  3. I've removed some spurious spaces in the definition adding % at the end of some lines

EDIT

To achieve the same result for unnumbered chapters as \tableofcontents you have to declare a separate \titleformat:

\titleformat{name=\chapter,numberless}
{\sffamily\huge\bfseries}
{}
{\dimexpr-\oddsidemargin-\hoffset-1in}
{%
    \tikzstyle{background rectangle}=[fill=cyan]%
    \begin{tikzpicture}[framed]
    \color{white}
    \draw (0,0) +(3ex,0) node[anchor=west]{#1};
    \end{tikzpicture}%
}

Complete MWE

\documentclass[a4paper,12pt]{report}

\usepackage{tikz}
\usetikzlibrary{backgrounds}

\usepackage[uppercase, sf, explicit, raggedright]{titlesec} %
\usepackage{xcolor}
\usepackage{lipsum} % only for the example

\titleformat{\chapter}
{\sffamily\huge\bfseries}
{}
{\dimexpr-\oddsidemargin-\hoffset-1in}
{%
    \tikzstyle{background rectangle}=[fill=cyan]%
    \begin{tikzpicture}[framed]
    \color{white}
    \draw (0,0) +(3ex,0) node[anchor=west]{Chapter \thechapter{} \filright#1};
    \end{tikzpicture}%
}

\titleformat{name=\chapter,numberless}
{\sffamily\huge\bfseries}
{}
{\dimexpr-\oddsidemargin-\hoffset-1in}
{%
    \tikzstyle{background rectangle}=[fill=cyan]%
    \begin{tikzpicture}[framed]
    \color{white}
    \draw (0,0) +(3ex,0) node[anchor=west]{#1};
    \end{tikzpicture}%
}

\begin{document}
\tableofcontents

\chapter{Chaptertitle}
\lipsum[1-5]
\end{document} 

Output (ToC)

enter image description here

  • Huh. I missed the forest for the trees there. How would I put it right at the edge of the page then? {-\oddsidemargin-\hoffset-1in} does not seem to work... – cygnus Feb 28 '14 at 13:02
  • That does the job just perfect. – cygnus Feb 28 '14 at 13:14
  • @Peter-BenediktvonNiebelschtz Happy to help :-) – karlkoeller Feb 28 '14 at 13:17
  • Addition to the problem: I added \tableofcontents and now the Box of every Chapter moves a bit to the right again. The Title of the TOC itself comes out as if I hadn't tried to move it at all... – cygnus Feb 28 '14 at 14:28
  • 1
    @P-B See the edit. – karlkoeller Feb 28 '14 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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