Fancy chapter headings with TikZ

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.

Later question on TeX.SX: Fancy chapter headings – perhaps you have an addition to that?

11. October 2008 by stefan
Categories: Uncategorized | 80 comments

Comments (80)

  1. 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. Hi Kjell! Of course you’re allowed add it, I would see it with pleasure in your gallery.

    Stefan

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

    Thank you for sharing.

    - Kjell Magne

  4. 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. 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. 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. hey,

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

    -bob
    (uni manchester)

  8. 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. 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. 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. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. Stefan,

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

    Roberto

  17. Hi Stefan,

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

  18. 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

  19. 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

  20. 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?

  21. 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

  22. 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}
    {gdefchapterlabel{}
    normalfontsffamilyHugebfseriesscshape}
    {gdefchapterlabel{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=.9paperwidth,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=.9paperwidth,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}

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

    Rafael

  24. 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

  25. how i can write in 2 languages in a chapter??
    for example english,greek:

    grchapter{???????? en {abc} }

    but this is not correct!!! how???

  26. 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.

  27. 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

  28. 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

  29. Dear Stefan,
    Thank you for this example. Can I add the above example to my blog?

  30. Hi Vahid,

    of course you can add it, feel free to use the code. Perhaps provide a link back to this page allowing the visitors of your blog to read the discussion and examples here as well.

    Best regards,

    Stefan

  31. Hi Stefan,

    I’m a LaTeX noobie just started working with it.
    I want to have a chapter title without the chapter part and a big number instead of a small. I found out how to do the first part, but i cant get a bigger number.

    i want this for example:

    “a big number” 2.
    title name: Introduction

    tnx.

  32. Hi,

    that’s no problem if you would use the titlesec package, try this example:

    documentclass[a4paper,10pt]{book}
    usepackage[english]{babel}
    usepackage{blindtext}
    usepackage{lmodern}
    usepackage{fixltx2e}
    usepackage{titlesec}
    titleformat{chapter}[display]
      {fontsize{80pt}{90pt}selectfontbfseries}
      {thechapter}{20pt}{Huge}
    begin{document}
    Blinddocument
    end{document}

    titlesec provides many features to customize chapter headings. The fncychap package offers more chapter heading styles.

    Stefan

  33. Wow! this is great code and helpful comments! I’m a very bad LaTeX noob and this is very insightful. One question: Is there any way to make this bar appear at the bottom of the page (with the page number embedded into it) instead of the top?

  34. Hi Stefan.

    I have a problem with a custom header created with titlesec: it does not apply to the table of contents, list of figures and list of tables.
    I tried your examples posted here and even though it should work, as I saw in the screenshot, it still doesn’t. I hoped you might have an idea about why this is happening. Maybe you can point me to a package that could solve this issue.

    Thank you.
    Serban

  35. Hi Serban,

    could you create a compilable example that shows the problem and post it here? Without it I cannot see what your code actually does. If I could look at your titlesec related code, the best would be a small example, I could make a suggestion to fix it.
    I think there’s no need for an additional package, but the titleformat calls or something else would have to be changed.

    Stefan

  36. Great post!
    I have tried your code and it works out exactly as it is designed. However, it seems my hyperref anchors are now a little off the desired position. When I click a page number that points to a chapter, it only shows from beneath the chapter title. I guess it might have something to do with hyperref, which sometimes links to floats points below the caption and not at the beginning of the float. The caption package might be able to correct this, but I don’t know how, since I cannot find a place to command capstart.

    Any suggestions?
    Thanks!

  37. Hi yanlong!

    The anchor of the concerned hyperlink points to the beginning of the chapter, not at the heading created here. Perhaps in that case here the links should be adjusted to refer to the beginning of the page instead.
    A redefinition of a hyperref macro could fix it perhaps, but this would need some time to investigate as the hyperref code is complex.
    But it isn’t related to the caption package, this package has nothing to do with section or chapter headings.

    Stefan

  38. Hi, Stefan,
    Many thanks for your prompt response! I believe you are right that it is the way hyperref sets anchors for chapter titles. Even without your chapter labels, hyperref does not anchor at the top of the page. when I try the geometry package, I find that hyperref anchors at geometry{top=xx}. If I reset titlespacing* accordingly, I can only make the chapter title page look nice, but the rest pages would need further adjustment.

    By my random and impatient guessing, you can tell that I am just a beginner, as I have only weeks of experience of using LaTeX, and I have tons of works due … The reason that I want to give up MS Office as a whole is TikZ and R …

    I am glad that I found your blog and I will keep following your posts. Thanks!

    yanlong

  39. Hi again.

    Thank you for the fast response.

    Here is a running example with my titleformat for chapters and all the packages that I am using. As I said before, the contents heading does not change the style. If for you it works, maybe you can have an idea about what is different.

    Thanks again,
    Serban.

    documentclass[pdftex,a4paper,12pt]{report}
    usepackage[pdftex]{graphicx}
    usepackage[a4paper]{geometry}
    usepackage[x11names]{xcolor}
    usepackage{colortbl, tabularx, fancyhdr, lscape, layout}
    usepackage{tocloft, titlesec, appendix, caption, pifont}

    titleformat{chapter}[block]
    {LARGEbfseriescolor{blue}titlerulesetlength{parskip}{0pt}}
    {colorbox{blue}{parbox[c][50pt][c]{60pt}{
    hfilcolor{white}centeringnormalsizetextit{Chapter}\Hugethechapterhfil}}}
    {0pt}
    {hspace{30pt}MakeUppercase}
    [titlerule]

    begin{document}
    tableofcontents
    newpage
    chapter{Aaaa}
    chapter{Bbbb}
    chapter{Cccc}
    end{document}

  40. Sorry to be posting again like this, but through an interesting trial an error approach and a bit if luck I found out where the problem is: the tocloft package. If you remove that from the packages declaration it will work just fine. So it comes down to a question related to the tocloft package instead.

    I know that this is not the topic of this post but if you have any solution on how I can make the two packages cooperate, I will be most grateful. I have posted the code that I am using from the tocloft package: some snippet to add the words chapter and appendix in front of the respective chapter numbers/letters in the table of contents. I apologize for the ugly code but I have no idea how to put it in the code box.

    Thank you.
    Serban

    documentclass[pdftex,a4paper,12pt]{report}

    usepackage[x11names]{xcolor}
    usepackage{titlesec, tocloft}

    titleformat{chapter}[block]
    {LARGEbfseriescolor{blue}titlerulesetlength{parskip}{0pt}}
    {colorbox{blue}{parbox[c][50pt][c]{60pt}{
    hfilcolor{white}centeringnormalsizetextit{Chapter}\Hugethechapterhfil}}}
    {0pt}
    {hspace{30pt}MakeUppercase}
    [titlerule]

    newlength{tocnumwidth}
    newlength{temp}
    setlength{tocnumwidth}{cftchapnumwidth}
    renewcommand{cftchappresnum}{color{blue}chapternamespacecolor{red}}
    renewcommand{cftchapaftersnum}{.}
    settowidth{temp}{bfseriescftchappresnumcftchapaftersnum}
    addtolength{cftchapnumwidth}{temp}
    DeclareRobustCommand{changetoapp}{
    renewcommand*{cftchappresnum}{color{blue}appendixnamespacecolor{red}}
    settowidth{temp}{bfseriescftchappresnumcftchapaftersnum}
    setlengthcftchapnumwidth{tocnumwidth}
    addtolength{cftchapnumwidth}{temp}}
    letoldappendixappendix
    renewcommand*{appendix}{oldappendix addtocontents{toc}{changetoapp}}

    begin{document}
    tableofcontents
    chapter{Aaaa}
    chapter{Bbbb}
    chapter{Cccc}
    appendix
    chapter{Xxxx}
    chapter{Yyyy}
    chapter{Zzzz}
    end{document}

  41. Hi Serban,

    here’s a quick workaround:

    makeatletter
    renewcommand*{@cftmaketoctitle}{%
    chapter*{contentsname}}
    makeatother

    Add this after loading tocloft, it will cause the tocloft toc heading to look like the normal chapter* heading.
    Note, with chapter* there won’t be a number of course.

    Stefan

  42. Perfect. Thank you so much for your help.

    Keep up the excellent work.

    All the best,

    Serban

  43. I have read this nice forum, and as others at this forum, I im a noob at latex and tikzpictures, but I would like to know how to make both the header and the pagenumber at the same time, then i have a theme four my schoolproject.

  44. Hi Jan,

    that’s no problem, just put thepage somewhere. For instance like here as additional node:

    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=.9paperwidth,rectangle,
                  rounded corners=15pt,inner sep=11pt,
                  fill=MidnightBlue]
                  {color{white}#1};
            node[anchor=west,xshift=1cm,yshift=1cm]
                {color{white}Largebfseriesthepage};
           end{tikzpicture}
          };
       end{tikzpicture}}

    The addition was

      node[anchor=west,xshift=1cm,yshift=1cm]
          {color{white}Largebfseriesthepage};

    Stefan

  45. Thx Stefan

    looking at your suggestion i found out what i really wanted, se the “theme” as i took the header and footer and put them together in the same document.

    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=.9paperwidth,rectangle,
    rounded corners=15pt,inner sep=11pt,
    fill=MidnightBlue]
    {color{white}#1};
    end{tikzpicture}
    };
    end{tikzpicture}}

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

    clearscrheadings
    ifoot{tikzfoot{Page thepage}}
    ihead{tikzhead{headmark}}
    pagestyle{scrheadings}
    begin{document}
    tableofcontents
    clearpage
    blinddocument
    end{document}

  46. Dear Stefan

    this is very very nice work! How would you, for a book-class document with a single main titlepage, create a oval coloured box containing and putting the title on the main front page?

    Thanks a lot

    Paul

  47. usepackage[math]{easyeqn}
    generates error messages with your beautiful heading generation program. Please let me know how I overcome this problem.

  48. usepackage[math]{easyeqn}
    generates error messages with your beautiful heading generation program. Please let me know how I can overcome this problem.

  49. I want to create a text with a banner with tikz. I want also be able to write and put graphics inside the banner like http://imagebin.ca/view/xI1TRFa.html. Please help in designing this page layout.
    Many thanks

  50. Hi Jacqueline,

    have a look at the TikZ example gallery to find good source code to start with.

    Stefan

  51. Yes, the one that I can start with is right on this page but I could not adapt it to my new situation. I also try to find another example in the library but most of them are graphics. Need help. Thanks

  52. Is there an elegant solution to make this compatible with “twoside”?
    I can make two very similar tikzpicture and use them in lefoot and rofoot respectively, but I’d rather just change “node[anchor=east, xshift=0.9paperwidth]”.

  53. Hi Christoph!

    Sorry, I didn’t have time to write on the blog, due to the daily work.
    You could solve it using commands like
    ifthispageodd{true}{false}

    or
    checkoddpage

    of the changepage package, see: Finding if you’re on an odd or an even page in the UK TeX FAQ.

    Stefan

  54. The latex compiler says:
    (/usr/share/texmf/tex/latex/xcolor/svgnam.def
    ! Undefined control sequence.
    l.32 preparecolorset
    {rgb}{}{}{%
    if i use:
    documentclass[svgnames]{scrreprt}
    usepackage{tikz}

    If i drop usepackage{tikz} and any tikz-relevant stuff, it will compile. But if i use those commands together, it won’t work out for me?

  55. Hello!

    A beautiful example! I have successfully employed the code to create a very nice looking chapter-header. However I’m facing at the moment the problem that I would like to share this chapter-header with people that use dvi+ps+gs to compile their .tex-files. It so happens that the outcome is not the same :-(
    With pdftex your example looks perfectly fine, when compiling with TeX+dvi it has a white space above the bright-blue-background-bar.
    I think the bug comes from the node&remember picture commands…but I must admit that after trying out several things that might resolve it, I still couldn’t really find a solution… I’m not a tikx-expert unfortunately :-/

    Any thoughts how to make this work with dvi+ps+gs?

    With best regards!

    Mat

  56. Hello again,

    I have just found the error linked to the example here:
    It seems that on my configuration the dvi package uses a different default page-size than the pdftex -> when specifying in the first line of your example:
    documentclass[a4paper,svgnames]{report}
    there is no difference any more between the compilation with TeX+dvi instead of pdftex. Strange…since if things would work as they should, tikz should not have a different reference point for the current page anchors if there is no paper-size definition… but well: it works without problems when a4 (or a different paper format) is explicitly specified.
    (Btw: I’m working with the Mac TeX Live 2010 distribution)

    However this does not resolve yet the problem with my own example :-(
    Here is a minimal working example:

    documentclass[a4paper,svgnames]{report}
     %%%%% CHAPTER HEADER %%%%
    usepackage{color}
    usepackage{tikz}
    usepackage[explicit]{titlesec}
    newcommand*chapterlabel{}
    %renewcommand{thechapter}{Roman{chapter}}
    titleformat{chapter}[display]  % type (section,chapter,etc...) to vary,  shape (eg display-type)
    	{normalfontbfseriesHuge} % format of the chapter
    	{gdefchapterlabel{thechapter }}     % the label
     	{0pt} % separation between label and chapter-title
     	  {begin{tikzpicture}[remember picture,overlay]
        node[yshift=-8cm] at (current page.north west)
          {begin{tikzpicture}[remember picture, overlay]
            draw[fill=black] (0,0) rectangle(35.5mm,15mm);
            node[anchor=north east,yshift=-7.2cm,xshift=34mm,minimum height=30mm,inner sep=0mm] at (current page.north west)
            {parbox[top][30mm][t]{15mm}{raggedleft $phantom{textrm{l}}$color{white}chapterlabel}};  %the phantom l is just to get better base-line alingement
            node[anchor=north west,yshift=-7.2cm,xshift=37mm,text width=textwidth,minimum height=30mm,inner sep=0mm] at (current page.north west)
                  {parbox[top][30mm][t]{textwidth}{color{black}#1}};
           end{tikzpicture}
          };
       end{tikzpicture}
       gdefchapterlabel{}
      } % code before the title body
     
    titlespacing*{chapter}{0pt}{50pt}{30pt}
    titlespacing*{section}{0pt}{13.2pt}{*0}  % 13.2pt is line spacing for a text with 11pt font size
    titlespacing*{subsection}{0pt}{13.2pt}{*0}
    titlespacing*{subsubsection}{0pt}{13.2pt}{*0}
     
    begin{document}
    tableofcontents
    chapter{Introduction}
    Text
    chapter{Main}
    section{Section}
    Text
    begin{thebibliography}{99}
    bibitem{Test} test reference
    end{thebibliography}
    end{document}

    The compilation with pdftex works fine -> the output is a kind of thumbindex-like chapter-header style where the chapter-number appears in the black box.
    However when compiling with TeX+dvi the chapter-names disappear behind the black box.
    An additionally strange thing is: it needs to be compiled 3 times in order to appear correctly. at the second time the chapter headers are still at the bottom of the page… I was not that worried since at the third compilation it works without trouble, but its an additional thing that I didn’t understand and might be linked to the problem?

    Any help would be highly appreciated!

    Mat

  57. Hi Mat,

    which is your final output format? If it’s DVI or PS, pdfTeX is not an option. However, if you finally produce PDF, I would use pdfTeX. It offers some advantages: besides better PDF support it works with PNG and JPEG, further it provides microtypographic features. If you depend on DVI/TeX because of EPS support, this can be resolved by using the epstopdf package.

    Stefan

  58. Hello Stefan,

    Thanks for your reply. Personally I prefer pdfTeX as well. The output is much neater than when produced with dvi/PS. I would be very happy to leave it there. My chapter header proposition is however for a template for a larger community (my university) and it seems that some would really much prefer the dvi/PS option :-/

    I expect there is probably just some little bug in my code which prevents the correct output for the TeX+DVI compilation.
    But I also don’t want to bother you for too long with this unless you directly “see” where my bug is coming from :-)

    Mat

  59. hi stefan,

    very nice blog

    i am using pdflatex package to write my phd thesis. some of my chapter headings has greek letters in it but when i run pdflatex it shows error. if i remove the greek letter it runs smoothly while except the heading in other portion of the text greek letters are not creating any problem. what can be the problem.

  60. Hi Paul,

    you could show the error message or a code sample which produces that error. Greek letters in headings could cause problems in PDF bookmarks.

    Stefan

  61. Hi Stefan, I’m having a little problem with your original example of fancy chapter headings; I just copied/pasted the code in Texmaker (I’m a windows user) and I have some errors such as:
    “Paragraph ended before ttl@format@ii was complete (line 12)”

    “You can’t use’ macro parameter character #’ in restricted horizontal mode (line 21)”

    ” missing begin{document} (line 25)”…

    I’m a little bit of a latex noob, so please excuse me if they are trivial errors…

    Thank you !

    Remi

    PS: I produce my file with pdflatex… maybe it has something to see…

  62. Hi Remi,

    I guess there’s in error in your titleformat line (because of the ttl@format@ii error), such as a missing argument, you could show that line here.

    Stefan

  63. Hi Stefan,
    thank you for the quick answer !

    the line :

    titleformat{chapter}
    {gdefchapterlabel{}
    normalfontsffamilyHugebfseriesscshape}
    {gdefchapterlabel{thechapter }}{0pt}

    Thanks.

    Remi

  64. Hi Stefan!

    That’s really nice! Im really a newbie so excuse me in advance for my probably stupid question :)

    Im writing a report (class article) and I wanted put my section headers in boxes. I really like your example so I tried to modify it just to keep the blue box for my section titles. But I have some problems: the box always stay in the top of the page and not in the place of the section, and anyway I cannot take just the box, without your lighter background..

    I would greatly appreciate your help!

    Francois

  65. It is alright I found what I wanted!

    Thanks for your nice blog!

  66. Hi Francois, nice to hear that you found it!

  67. Hi Stefan -

    This is incredible. Thanks very much for putting all this up. I’ve been looking for a way to insert a colored header for weeks. I tried making an eps file, drawing pictures, lines and inputting it as a picture, etc. and nothing worked!

    I’m working with the example you posted on April 1 2009 so that I can control the headings on each page:


    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}

    My goal is to combine the above code with something to also add a footer as well- so that I have a colored footer and header for each page. I have tried by looking at your reply to Kevin on March 11th but have been unsuccessful.

    Do you have any suggestions for including a colored header and footer on each page? Would greatly appreciate any help.

    Kyle

  68. Just came across this today — it’s much nicer than what I was looking for. Thanks for sharing the code. :)

  69. Hello Stefan!

    Have you by any chance ever tried (and managed to) get the lovely style you defined for your chapters/sections/… work for the bibliographic part of the same document as well?
    Cause that’s the issue I’m currently working on. I’ve got all those nice and fancy headings thoughout my work, and for the “References” header, Latex swaps back to the predefined style. Which is a pity.

    If you happen to have an idea on how to solve this, I’d be much obliged.

    Greetings.

  70. I have a problem when using the above code along with
    bibliographystyle{spmpsci}
    bibliography{../references}

    Anybody have any idea why?

    I get the error message:
    ! LaTeX Error: Something’s wrong–perhaps a missing item.

    See the LaTeX manual or LaTeX Companion for explanation.
    Type H for immediate help.

    l.1 begin{thebibliography}{100}

    I did notice that I have more that 100 references but this file is automatically created.

    Thank you for any info.

  71. Hi Stefan,

    I am writing a post on chapter styles and would like to mention your example above. I’ll add a link to this site. Would that be ok with you?

    Thanks and regards, Tom.

  72. Hi Tom,

    of course that’s ok! The information here is for using and sharing, and linking back to the original is very good.

    Stefan

  73. Hi, Stefan!

    Great work and thank you, im using it on my works and everybody liked it!

    I am a beginner in Latex and Tikz and I was thinking, is it possible to make a fancy cover for a report that follows the style of the chapter headers, perhaps modifying maketitle macro?

    Thank you!

  74. Thank you!!!

  75. Thank you Stefan, I am using your crafting and it produces great results. Like the comments here too,

  76. Hi!
    This is a great, I was wondering how to keep the chapter heading on every page, not only only on the first page?

  77. I have a problem with the package yhmath. When i use it my document stops building. I think the problem is with \wideparen{} command.

    Somebody found out that problem too? Can be solved in some way?

    Thanks

  78. Hello.

    I am using two header styles,

    1 – For pre-textual parts to summary (in preamble):

    \newcommand*\chapterlabel{}
    \titleformat{\chapter}
    {\gdef\chapterlabel{}
    \Huge\bfseries}
    {\gdef\chapterlabel{\thechapter\ }}{0pt}
    {\begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3cm] at (current page.north west)
    {\begin{tikzpicture}[remember picture, overlay]
    \draw[fill=LightSalmon1,opacity=.7] (0,0) rectangle
    (\paperwidth,3cm);
    \node[anchor=east,xshift=.9\paperwidth,rectangle,
    rounded corners=20pt,inner sep=11pt,
    fill=Sienna!80!black,text=white]
    {\color{white}\chapterlabel#1};
    \end{tikzpicture}
    };
    \end{tikzpicture}
    }
    \titlespacing*{\chapter}{0pt}{50pt}{-60pt}

    2 – For the text body, ie to the chapters (in begin{document}) :

    \newcommand\chapterillustration{}

    \titleformat{\chapter}{}{}{0pt}{
    \ThisULCornerWallPaper{1}{\chapterillustration}
    \tikz[overlay,remember picture]
    \fill[LightSalmon1,opacity=.7]
    (current page.north west) rectangle
    ([yshift=-3cm] current page.north east);

    \strictpagecheck\checkoddpage

    \ifoddpage{
    \ThisLRCornerWallPaper{.35}{fern_mo_01}
    \begin{tikzpicture}[overlay,remember picture]
    \node[anchor=south west,
    xshift=20mm,yshift=-30mm,
    font=\sffamily\bfseries\huge]
    at (current page.north west)
    {\chaptername\ \thechapter};
    \node[fill=Sienna!80!black,text=white,
    font=\Huge\bfseries,
    inner ysep=12pt, inner xsep=20pt,
    rounded rectangle,anchor=east,
    xshift=-20mm,yshift=-30mm]
    at (current page.north east) {#1};
    \end{tikzpicture}
    }
    \else {
    \ThisLLCornerWallPaper{.35}{fern_mo_01}
    \begin{tikzpicture}[overlay,remember picture]
    \node[anchor=south east,
    xshift=-20mm,yshift=-30mm,
    font=\sffamily\bfseries\huge]
    at (current page.north east)
    {\chaptername\ \thechapter};
    \node[fill=Sienna!80!black,text=white,
    font=\Huge\bfseries,
    inner sep=12pt, inner xsep=20pt,
    rounded rectangle,anchor=west,
    xshift=20mm,yshift=-30mm]
    at ( current page.north west) {#1};
    \end{tikzpicture}
    }
    \fi
    }
    \titlespacing*{\chapter}{0pt}{0pt}{135mm}

    \newlength\pagenumwidth
    \settowidth{\pagenumwidth}{105}

    \tikzset{pagefooter/.style={
    anchor=base,font=\sffamily\bfseries\small,
    text=white,fill=Sienna!80!black,text centered,
    text depth=17mm,text width=\pagenumwidth}}

    \definecolor[named]{GreenTea}{HTML}{CAE8A2}
    \definecolor[named]{MilkTea}{HTML}{C5A16F}

    \fancypagestyle{headings}{%
    \fancyhf{}
    \fancyhead[RO]{%
    \begin{tikzpicture}[remember picture,overlay]
    \fill[MilkTea!25!white] (current page.north east) rectangle (current page.south west);
    \fill[white, rounded corners] ([xshift=-10mm,yshift=-20mm]current page.north east) rectangle ([xshift=15mm,yshift=17mm]current page.south west);
    \end{tikzpicture}
    \begin{tikzpicture}[xshift=-.75\baselineskip,yshift=.25\baselineskip,remember picture, overlay,fill=GreenTea,draw=GreenTea]\fill circle(3pt);\draw[semithick](0,0) — (current page.west |- 0,0);\end{tikzpicture} \sffamily\itshape\small\nouppercase{\rightmark}
    }

    \fancyhead[LE]{%
    \begin{tikzpicture}[remember picture,overlay]
    \fill[MilkTea!25!white] (current page.north east) rectangle (current page.south west);
    \fill[white, rounded corners] ([xshift=-15mm,yshift=-20mm]current page.north east) rectangle ([xshift=10mm,yshift=17mm]current page.south west);
    \end{tikzpicture}
    \sffamily\itshape\small\nouppercase{\leftmark}\
    \begin{tikzpicture}[xshift=.5\baselineskip,yshift=.25\baselineskip,remember picture, overlay,fill=GreenTea,draw=GreenTea]\fill (0,0) circle (3pt); \draw[semithick](0,0) — (current page.east |- 0,0 );\end{tikzpicture}
    }
    \fancyfoot[RO,LE]{\tikz[baseline]\node[pagefooter]{\thepage};}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
    }

    \fancypagestyle{plain}{%
    \fancyhf{}
    \fancyfoot[RO,LE]{\tikz[baseline]\node[pagefooter]{\thepage};}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
    }

    \renewcommand\chapterillustration{cherry-tomatos}
    \chapter{Introdução} \label{chp:1}

    \pagenumbering{arabic} \setcounter{page}{1} %\thispagestyle{empty}

    It is compiling correctly, but …
    The reference and the index are left with the second style of appearance and having five chapters in the model, the text “Chapter 5″ on both.

    I would like the reference and the index stay with the appearance of the first style.

    I hope you can help me. Thank you in advance.

  79. Hi, this is a really nice chapter style.

    However, if using memoir document class, the style doesn’t effect the ‘Table of Contents’ title, which just displays as normal.

    Is it possible to make the code work for the ‘Table of Contents’ title too using memoir class?

Leave a Reply to Moses Cancel reply

Required fields are marked *