Conditions for fancy chapter title

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 ?

-
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 '14 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 '14 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. –  SoundsOfSilence Apr 5 '14 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 '14 at 8:32

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}

-