1

I would like to change the color of the chapter label (small rectangle box) everytime I call the command \chapter{}, possibly passing the color as an argument.

Thus the part I need to address with the argument is the command \draw[fill=black].

Here a compilable example:

\documentclass{book}
\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) {\normalfont\fontsize{24}{26}\bfseries} % format of the chapter {\gdef\chapterlabel{\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 black 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} \gdef\chapterlabel{} } % code before the title body \usepackage[utf8]{inputenc}

\title{test}

\begin{document}

\maketitle

\chapter{Introduction}

\end{document}

I tried to pass the argument to the \chapterlabel function but didn't work. I also found a very similar question "https://tex.stackexchange.com/questions/641554/change-color-for-each-chapter" that didn't really received an answer (I tried the solution proposed but didn't work).

I would be really greatful if someone can help.

081N
  • 13
  • You could define colours with numbers in their names which will correspond to the chapter number. Can you make a small but compilable minimal working example? – samcarter_is_at_topanswers.xyz Jan 25 '23 at 15:51
  • @samcarter_is_at_topanswers.xyz thanks for your comment. I added a compilable working example. I'll try to see if I manage to do what you suggest. – 081N Jan 25 '23 at 16:45

1 Answers1

0

I suggest to define a series of colours with numbers in their names and the chapter will then use the colour depending on the current chapter number:

\documentclass{book}
\usepackage{color}
\usepackage{tikz}
\usepackage[explicit]{titlesec}

\colorlet{chapter1}{red} \colorlet{chapter2}{blue}

\newcommand*\chapterlabel{} %\renewcommand{\thechapter}{\Roman{chapter}} \titleformat{\chapter}[display] % type (section,chapter,etc...) to vary, shape (eg display-type) {\normalfont\fontsize{24}{26}\bfseries} % format of the chapter {\gdef\chapterlabel{\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=chapter\the\value{chapter}] (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 black 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} \gdef\chapterlabel{} } % code before the title body \usepackage[utf8]{inputenc}

\title{test}

\begin{document}

\maketitle

\chapter{Introduction}

\chapter{foo}

\end{document}

enter image description here


Alternatively you could colour the chapter with a user defined colour and change its definition before each chapter:

\documentclass{book}
\usepackage{color}
\usepackage{tikz}
\usepackage[explicit]{titlesec}

\colorlet{chapter}{red}

\newcommand*\chapterlabel{} %\renewcommand{\thechapter}{\Roman{chapter}} \titleformat{\chapter}[display] % type (section,chapter,etc...) to vary, shape (eg display-type) {\normalfont\fontsize{24}{26}\bfseries} % format of the chapter {\gdef\chapterlabel{\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=chapter] (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 black 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} \gdef\chapterlabel{} } % code before the title body \usepackage[utf8]{inputenc}

\title{test}

\begin{document}

\maketitle

\colorlet{chapter}{green} \chapter{Introduction}

\colorlet{chapter}{orange} \chapter{foo}

\end{document}