TeXblog

 Typography with TeX and LaTeX

Fancy chapter headings with TikZ

11 October 2008 by Stefan Kottwitz

A question on LaTeX-Community.org inspired me to try pgf/TikZ together with titlesec to produce fancy chapter headings. The result is this small compilable demonstration example:

\documentclass[svgnames]{report}
\usepackage{tikz}
\usepackage{kpfonts}
\usepackage[explicit]{titlesec}
\newcommand*\chapterlabel{}
\titleformat{\chapter}
  {\gdef\chapterlabel{}
   \normalfont\sffamily\Huge\bfseries\scshape}
  {\gdef\chapterlabel{\thechapter\ }}{0pt}
  {\begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3cm] at (current page.north west)
      {\begin{tikzpicture}[remember picture, overlay]
        \draw[fill=LightSkyBlue] (0,0) rectangle
          (\paperwidth,3cm);
        \node[anchor=east,xshift=.9\paperwidth,rectangle,
              rounded corners=20pt,inner sep=11pt,
              fill=MidnightBlue]
              {\color{white}\chapterlabel#1};
       \end{tikzpicture}
      };
   \end{tikzpicture}
  }
\titlespacing*{\chapter}{0pt}{50pt}{-60pt}
 
\begin{document}
\tableofcontents
\chapter{Introduction}
Text
\chapter{Main}
\section{Section}
Text
\begin{thebibliography}{99}
\bibitem{Test} test reference
\end{thebibliography}
\end{document}

Screenshot:

TikZ fancy chapter headings

See also pdf output of this test & demo file.

This entry was posted on 11 October 2008 at 10:31 PM and is filed under pgf/TikZ, Sectioning, Layout. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

29 responses about “Fancy chapter headings with TikZ”

  1. Kjell Magne Fauske said:

    Nice example Stefan! I was thinking about creating something similar myself, but now I don’t have to :-) Can I add it to the TikZ examples gallery[1]?

    [1] http://www.texample.net/tikz/examples/

  2. Stefan Kottwitz said:

    Hi Kjell! Of course you’re allowed add it, I would see it with pleasure in your gallery.

    Stefan

  3. Kjell Magne Fauske said:

    Thanks Stefan. The example is now online: http://www.texample.net/tikz/examples/fancy-chapter-headings/

    Thank you for sharing.

    - Kjell Magne

  4. Gaveen said:

    Hi Stefan,

    I just tried the above example in a TeXLive installation (Ubuntu 8.04) and pdflatex gives me the following error:

    ! LaTeX Error: Unknown option `explicit’ for package `titlesec’.

    However your example at LaTeX-Community.org works fine. I checked the ‘titlesec’ doc and it says there’s a package option called “explicit”.

    I can’t seem to figure whats wrong. Can you reproduce the error?

    -Gaveen
    (SkyEye in LC)

  5. Stefan Kottwitz said:

    Hi Gaveen,

    Ubuntu 8.04 comes with titlesec v2.6. The “explicit” option was introduced in v2.7 2007-03-27.
    If you want to use this option you would have to update titlesec. I’ve installed titlesec v2.8 using the MiKTeX package manager, but you could get it from CTAN too.

    Stefan

  6. Kjell Magne Fauske said:

    Hi,

    I posted a related example today that shows how to modify the current page node in cases where you want to print a page on stock paper that is larger than your page size:
    http://www.texample.net/tikz/examples/modifying-current-page-node/

    It is written with the Memoir document class in mind, but it should be straightforward to adapt it to other document classes.

    - Kjell Magne

  7. bobmalaria said:

    hey,

    very nice example, i will keep this solution in mind.

    -bob
    (uni manchester)

  8. Sasha said:

    Hi Stefan,

    I like this solution very much! And I wonder whether it is difficult to extended it to title and parts (and probably subsections). Well, I think that I’m able to draw the same rectangle instead of title, but how to do this “uniformly”?

  9. Stefan Kottwitz said:

    Hi Sasha,

    thank you for your comment! I’m sorry that I didn’t respond quickly. I’m travelling since some weeks because of business.
    I think it’s not hard to adapt this example for sections etc. For instance use \titleformat{subsection}{…} similar.

    Stefan

  10. Andreas said:

    Hi,

    first I have to say that it is a nice use. I tried a similar thing using tikz and komascript for fancy page headings but the placement is certainly a problem because the placement of the actual heading is changing depending on the page spread and other factors. How would you do fancy page headings?

    Andreas

  11. Stefan Kottwitz said:

    Hi Andreas,

    the solution above could be easily applied together with fancyhdr or scrpage2 to be used in general headings.
    Here’s a modified compilable example using scrpage2 and scrartcl:

    \documentclass[svgnames]{scrartcl}
    \usepackage[automark]{scrpage2}
    \usepackage[english]{babel}
    \usepackage{blindtext}
    \usepackage{tikz}
    \newcommand*\tikzhead[1]{%
      \begin{tikzpicture}[remember picture,overlay]
        \node[yshift=-2cm] at (current page.north west)
          {\begin{tikzpicture}[remember picture, overlay]
            \draw[fill=LightSkyBlue] (0,0) rectangle
              (\paperwidth,2cm);
            \node[anchor=east,xshift=.9\paperwidth,rectangle,
                  rounded corners=15pt,inner sep=11pt,
                  fill=MidnightBlue]
                  {\color{white}#1};
           \end{tikzpicture}
          };
       \end{tikzpicture}}
    \clearscrheadings
    \ihead{\tikzhead{\headmark}}
    \pagestyle{scrheadings}
    \begin{document}
    \tableofcontents
    \clearpage
    \blinddocument
    \end{document}

    Output: fancy_headings.pdf.

    Stefan

  12. Jesse said:

    Hi Stefan,

    You seem to be highly skilled at Latex chapter headings, so I wondered if you could help me with a problem I’m currently having? I’m essentially trying to include a small picture to the right of a left-justified chapter heading. For example, it would look something like this:

    _____________
    Chapter 1 | |
    | Picture |
    INTRODUCTION |_____________|

    Any idea how to do that? Thanks!

    Jesse

  13. Jesse said:

    Sorry, the format of my little example got a little screwed up when I submitted it. But hopefully you get the idea that the *picture* is supposed to be to the right of the “Chapter 1, Introduction” bit…

    Thanks,

    Jesse

  14. Roberto said:

    Hi Stefan,

    thanks for you sharing on this magnificent and well designed chapter heading example. I’m wondering if you can help me to solve a problem: how to span long chapter names over a new (extra) line? I think we Brazilians (latinos) have some issue with trying to be minimalist ;)

    cheers,

    Roberto

  15. Stefan Kottwitz said:

    Hi Jesse,

    unfortunately I didn’t find time to write during the last days. Now here’s an example using titlesec. I’ve just used \includegraphics and \hfill to push the picture to the right:

    \documentclass{report}
    \usepackage{titlesec}
    \titleformat{\chapter}[display]
    {\normalfont\huge\bfseries}
    {\chaptertitlename\ \thechapter\hfill
    \includegraphics[width=4em,height=2ex]{file}}
    {20pt}{\Huge}
    \usepackage[demo]{graphicx}
    \begin{document}
    \chapter{One}
    \chapter{Two}
    \end{document}

    Stefan

  16. Stefan Kottwitz said:

    Hi Roberto,

    in the example above you could use \parbox if your chapter title is taking more than one line:

    \chapter[short title for the toc]{%
    \parbox[t]{\textwidth}{long title}}

    Stefan

  17. Roberto said:

    Stefan,

    many thanks for your prompt help — just in time to finish an ebook,

    Roberto

  18. Laki said:

    Hi Stefan,

    How can i adopt for book typing? I look forward from you.

  19. Jason Lewis said:

    Hi Stefan,

    I’m looking at your solution to do fancy headers in comment #11.

    I’d like to do something similar using the fancyhdr package but also, be able to change the header colour on a per chapter basis.

    Any idea how I might achieve that?

    thanks,

    Jason

  20. Stefan Kottwitz said:

    Hi Jason,

    here’s a solution using the article class and the fancyhdr package with the possibility to change the header color whenever you want:

    \documentclass[svgnames]{article}
    \usepackage{fancyhdr}
    \usepackage[english]{babel}
    \usepackage{blindtext}
    \usepackage{tikz}
    \newcommand*\tikzhead{%
      \begin{tikzpicture}[remember picture,overlay]
        \node[yshift=-2cm] at (current page.north west)
          {\begin{tikzpicture}[remember picture, overlay]
            \draw[fill=\headcolor] (0,0) rectangle
              (\paperwidth,2cm);
           \end{tikzpicture}
          };
       \end{tikzpicture}}
    \fancyhf{}
    \renewcommand*\headrulewidth{0pt}
    \chead{\tikzhead}
    \pagestyle{fancy}
    \newcommand*\headcolor{Aqua}
    \newcommand*\setheadcolor[1]{\renewcommand*\headcolor{#1}}
    \begin{document}
    \setheadcolor{Gold}
    \tableofcontents
    \clearpage
    \setheadcolor{Olive}
    \blinddocument
    \end{document}

    Stefan

  21. Aleksandra said:

    Hi Stefan,

    I have used your idea to get this beautiful headings in my book. :) So I have used solution with tikz and fancyhdr packages in book class, and tried to move chapter heading to the left on left hand side pages by using ifthen package, but as I am not very skilled user, all I got was a bunch of mistakes.

    Do you have any suggestion how to solve this?

    Thank you in advance,
    Aleksandra

  22. yell said:

    Hi Stefan,

    i want to use your awesome headings for my thesis. Unfortunately i just discovered, that my page numbering disappeared. is there a way to include them in the header or display them at the bottom of the page?

  23. Stefan Kottwitz said:

    Hi yell,

    just use commands of the package (fancyhdr or scrpage2) that you are using, for instance

    \cfoot{\thepage}

    If you would need more help just provide more information, which class and which package(s) you are using, or a small compilable example like I’ve given above.

    Stefan

  24. yell said:

    Unfortunately it doesnt work with that fix either.
    I get pagenumbers at the bottom of the first content page, biblio page and on every page i just started a new chapter, but nowhere else.
    Below is a small example i am using, which doesnt have a pagenumber on page 2.

    \documentclass[svgnames, a4paper, twoside, titlepage, 11pt,pagenumber=botleft, BCOR12mm]{book}
    \usepackage{tikz}
    \usetikzlibrary{decorations}
    \usetikzlibrary{decorations.pathmorphing}
    \usetikzlibrary{decorations.markings}

    \usepackage[automark]{scrpage2}
    \usepackage[english]{babel}
    \usepackage{blindtext}
    \cfoot{\thepage}

    \usepackage{kpfonts}
    \usepackage[explicit]{titlesec}
    \newcommand*\chapterlabel{}
    \titleformat{\chapter}
    {\gdef\chapterlabel{}
    \normalfont\sffamily\Huge\bfseries\scshape}
    {\gdef\chapterlabel{\thechapter\ }}{0pt}
    {\begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3cm] at (current page.north west)
    {\begin{tikzpicture}[remember picture, overlay]
    \draw[fill=LightSkyBlue] (0,0) rectangle
    (\paperwidth,3cm);
    \node[anchor=east,xshift=.9\paperwidth,rectangle,
    rounded corners=20pt,inner sep=11pt,
    fill=MidnightBlue]
    {\color{white}\chapterlabel#1};
    \end{tikzpicture}
    };
    \end{tikzpicture}
    }
    \titlespacing*{\chapter}{0pt}{50pt}{-60pt}

    \newcommand*\tikzhead[1]{%
    \begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-2cm] at (current page.north west)
    {\begin{tikzpicture}[remember picture, overlay]
    \draw[fill=LightSkyBlue] (0,0) rectangle
    (\paperwidth,2cm);
    \node[anchor=east,xshift=.9\paperwidth,rectangle,
    rounded corners=15pt,inner sep=11pt,
    fill=MidnightBlue]
    {\color{white}#1};
    \end{tikzpicture}
    };
    \end{tikzpicture}}
    \clearscrheadings
    \ihead{\tikzhead{\headmark}}
    \pagestyle{scrheadings}

    %\usepackage[top=3cm, inner=3cm,outer=3cm,bottom=3cm]{geometry}

    \usepackage[thmmarks, amsthm]{ntheorem}
    \usepackage{amsmath, amssymb}
    \usepackage[english]{babel}
    \usepackage[latin1]{inputenc}

    \usepackage[draft]{fixme}%fixme-randbemerkungen
    \setlength{\marginparwidth}{25mm} %breite fuer fixme
    %\usepackage{xcolor}

    \usepackage{xy}
    %\usepackage{graphicx}
    \usepackage{verbatim}
    \usepackage{enumerate}
    \usepackage{url}
    \usepackage{sseq} %spectral sequence

    \usepackage{makeidx}%index machen
    \usepackage{hyperref}%klickbare links

    \makeindex

    \interfootnotelinepenalty=2000

    \input xy
    \xyoption{all}

    \include{commands}
    %\newcommand{\coloneqq}{:=}
    \linespread {1.1}

    %entrymodifiers lets xypic and limits work nicely together
    \entrymodifiers={+!!}

    \begin{document}
    \tableofcontents

    \newpage
    test

    \printindex
    \bibliographystyle{amsalpha}
    %\renewcommand{\bibname}{Literature}
    \addcontentsline{toc}{chapter}{\bibname}
    \bibliography{asd}
    \end{document}

  25. Rafael R. Pappalardo said:

    Could you try to put the \cfoot command just below the \begin{document} line?
    It works for me.

    Rafael

  26. Stefan Kottwitz said:

    Hi Rafael,

    yell has already told me by email that his problem is solved, though he did not post the corrected code.
    I think it’s just the \clearscrheadings in his code that cleared also the entry in the page footer. Your suggestion would fix that. It would be enough to place the \cfoot command right after \clearscrheadings or just not to use \clearscrheadings but \chead[]{} etc. instead.

    Stefan

  27. Campbel said:

    Hi, I have a section heading that is spanning more than one line and when i used \parbox, it was out of the paper margin. I mean the total length is more than what it should be. How can i handle this and i dont want to use - when breaking the last word. I want the last word to go to another line and at the same time not to maintain a double space between the section name.

  28. Stefan Kottwitz said:

    Hi Campbel!

    You could just use \\ or \newline to break the line inside the \parbox. Perhaps show a minimal example that shows the problem in detail, the code example would allow to give really appropriate help.

    Stefan

  29. Jennifer said:

    Hi Stefan,

    I am currently writing on my Uni thesis and because I really enjoy good looking layouts and liked your idea, I was wondering if you could answer me two questions (Please not that I am fairly new to Latex):

    1. Is it possible to split the chapter number and chapter name into 2 different boxes?

    2. Can I fill the big lightskyblue box with external pictures (i.e. jpgs or similar)?

    Thank you
    Jennifer

Leave a Reply