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 Sectioning, Graphics, 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.

11 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

Leave a Reply