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 use a book class and here is a part of my fancy title page for the mainmatter chapters.

\newlength\chapterwidth
\settowidth\chapterwidth{\huge\chaptertitlename}
\titleformat{\chapter}[display]
{\normalfont\filcenter}
{\tikz[remember picture,overlay]{
\node[fill=nicedarkblue,font=\sffamily\fontsize{96}{72}\bf\selectfont\color{white},anchor=north east, minimum width=3cm, minimum height=3.4cm] 
at ([xshift=-1cm,yshift=-1cm]current page.north east) 
(numb) {\thechapter};
\node[rotate=90,anchor=south,inner sep=4pt,font=\Huge\sffamily] at (numb.west) {Chapter};
}
}{20pt}{\scshape\Huge\color{nicedarkblue}#1}[\vskip10pt\Large***]

I do not master conditions in latex, but I would like to have four different styles:

  • chapter in frontmatter
  • chapter in mainmatter
  • appendix in mainmatter
  • chapter in backmatter

How to branch on these conditions ?

share|improve this question
    
I would include a new \titleformat wherever I want a new title format to start... The \titleformat should act like a switch (similar to a font changing macro like \bfseries or \itshape), changing formatting from that point onward; so there's technically no "conditioning" required. A bigger question though: Why do you want to switch formats and not keep things consistent? –  Werner Apr 5 at 6:27
    
@How do I define other formats with \titleformat? For the bigger question, in fact I want to keep the same "global idea" but I want to change things like the "Chapter" word and the color of the node. –  Vincent Apr 5 at 6:36
    
You could have a look on the titlesec package (the documentation contains some examples) or using the KOMA classes in order to change the titleformat. –  Christian Hupfer Apr 5 at 6:52
    
What do you mean by saying "appendix in mainmatter"? There is a \appendix switch to be used to create appendecies. Shouldn't your third requirement be "chapter in appendix"? –  Ruben Apr 5 at 8:32

1 Answer 1

up vote 5 down vote accepted

Generally speaking, rather than including "conditionals" in the title format to work out where in the document the title is used, you either (a) include macros in the format itself which will expand appropriately (e.g. \chaptertitlename which will be redefined once \appendix is given) or (b) redefine with a new \titleformat at the appropriate point in the document.

Since you didn't give a complete compilable MWE I'm guessing somewhat about your settings. But here's a short example. This is basically your code, but (a) instead of "hardcoding" the word "Chapter" in the bit that prints the colored box, I've used \chaptertitlename and (b) instead of hard-coding a color in the two places you used it, I put a macro which can therefore easily be redefined.

\documentclass{book}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\newcommand{\chaptercolor}{blue}
\usepackage{tikz}

\titleformat{\chapter}
[display]
{\normalfont\filcenter}
{\tikz[remember picture,overlay]
  {\node[fill=\chaptercolor,%<--- Not hardcoded color
       font=\sffamily\fontsize{96}{72}\bf\selectfont\color{white},anchor=north east, 
       minimum width=3cm, 
       minimum height=3.4cm] 
       at ([xshift=-1cm,yshift=-1cm]current page.north east) 
         (numb) {\thechapter};
     \node[rotate=90,
           anchor=south,
           inner sep=4pt,
           font=\Huge\sffamily]
       at (numb.west) {\chaptertitlename};%<-- Not hardcoded "CHAPTER"
    }}
  {20pt}
  {\Huge\bfseries\color{\chaptercolor}#1}%< Not hardcoded color
  [\vskip10pt\Large\bfseries***]
\begin{document}

\frontmatter

\chapter{Preface}

\lipsum

\mainmatter\renewcommand{\chaptercolor}{red}

\chapter{Chapter}

\lipsum

\appendix\renewcommand{\chaptercolor}{green!40!black!60}

\chapter{Appendix Something}

\end{document}

chapter in red appendix in green

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.