Color-coded chapter tabs to match flower colors in a field guide

I am working on a booklet for my wife who is a grade school teacher. The booklet is field guide about the plants in the fairly large playground area at Gemini Elementary. Tufte-Latex is my "go-to" template for everything these days - especially thanks to this fine group showing me the code to make a change to the color of chapter headings! I am befuddled by another chapter/color task. What I would like to do is assign a little tab of color to associate each chapter with the color of a flower, or green for trees and shrubs. Introductory chapters could just have a white tab so nothing shows up. The colored tabs would be in the table of contents and then at the top, maybe a cm or two from outer corner of each page. Am I nuts for even thinking about doing this?

-
Welcome to TeX.SX! What have you tried? –  Jubobs Mar 2 at 17:37

Here's an option meeting all your requirements:

1. Color marking can be activated/deactivated at any point of your document and as many times as required.

2. When color marking is active, each chapter entry in the ToC will have a tab to the left with the corresponding color; the chapter heading will also have this color and all pages of the chapter will also have a colored tab at the top (to the left for even-numbered pages and to the right for odd-numbered pages).

3. The original settings of tufte-book for ToC entries and chapter headings are preserved.

Simplified version:

\documentclass[dvipsnames]{tufte-book}
\usepackage{background}
\usepackage{lipsum}

\newif\ifcolor
\colorfalse
\backgroundsetup{
angle=0,
scale=1,
opacity=1,
contents={}
}

\makeatletter
\backgroundsetup{
contents={
\ifcolor
\textcolor{\chaptercolor}{\rule{3em}{3em}}
\fi}
}
\ifodd\value{page}
\backgroundsetup{
position={current page.north east},
hshift=-1.5cm,vshift=-1cm
}
\else
\backgroundsetup{position={current page.north west},
hshift=1.5cm,vshift=-1cm
}
\fi
\BgMaterial}

\def\chaptercolor{black}
\newcommand\normalchapter{%
\colorfalse%
\titleformat{\chapter}[display]
{\relax\ifthenelse{\NOT\boolean{@tufte@symmetric}}{\begin{fullwidth}}{}}
{\itshape\huge\thechapter}
{0pt}
{\huge\rmfamily\itshape}
[\ifthenelse{\NOT\boolean{@tufte@symmetric}}{\end{fullwidth}}{}]
}

\newcommand\colorchapter{%
\colortrue%
\titleformat{\chapter}[display]
{\relax\color{\chaptercolor}\ifthenelse{\NOT\boolean{@tufte@symmetric}}{\begin{fullwidth}}{}}
{\itshape\huge\thechapter}
{0pt}
{\huge\rmfamily\itshape}
[\ifthenelse{\NOT\boolean{@tufte@symmetric}}{\end{fullwidth}}{}]
}

\newcommand\ContentColor[1]{%
\titlecontents{chapter}[0em]
{\vspace{1.5\baselineskip}\begin{fullwidth}\LARGE\rmfamily\itshape%
\makebox[0pt]{\textcolor{#1}{\rule{1em}{1em}}\hspace{2em}}
}
{\hspace*{0em}\thecontentslabel{2em}}
{\hspace*{0em}}
[\end{fullwidth}]
}

\newcommand\ContentNoColor{%
\titlecontents{chapter}%
[0em]
{\vspace{1.5\baselineskip}\begin{fullwidth}\LARGE\rmfamily\itshape}
{\hspace*{0em}\thecontentslabel{2em}}
{\hspace*{0em}}
[\end{fullwidth}]
}
\renewcommand\mainmatter{%
\cleardoublepage%
\@mainmattertrue%
\colorchapter
\fancyhf{}%
\ifthenelse{\boolean{@tufte@twoside}}%
{% two-side
\renewcommand{\chaptermark}[1]{\markboth{##1}{}}%
}%
{% one-side
}%
}

\renewcommand\backmatter{%
\if@openright%
\cleardoublepage%
\else%
\clearpage%
\fi%
\@mainmatterfalse%
\normalchapter%
\ContentNoColor%
}
\makeatother

\newcommand\ChapColor[1]{%
\def\chaptercolor{#1}
\ContentColor{#1}
}

\begin{document}

\frontmatter
\tableofcontents
\cleardoublepage
\chapter{Introduction}
\lipsum[1-2]
\cleardoublepage

\mainmatter
\ChapColor{Maroon!80}
\chapter{Test chapter one}
\lipsum[1-4]
\cleardoublepage
\ChapColor{MidnightBlue!70}
\chapter{Test chapter two}
\lipsum[1-4]
\cleardoublepage
\ChapColor{Dandelion}
\chapter{Test chapter three}
\lipsum[1-4]
\cleardoublepage

\backmatter
\chapter{Appendix}
\lipsum[1-4]

\end{document}


An image of the ToC:

An image of the document, showing normal chapters and chapters with the color specification:

Most of the work now is delegated to \frontmatter, \mainmatter and \backmatter; in this version, \frontmatter activates the color marking for headings, ToC entries and for the tabs on each page of a chapter. \backmatter deactivates these settings. The only thing that has to be done now is to use \ChapColor{<color>} before each \chapter in which a color change is required.

First version:

The code:

\documentclass[dvipsnames]{tufte-book}
\usepackage{background}
\usepackage{emptypage}
\usepackage{lipsum}

\newif\ifcolor
\colorfalse
\backgroundsetup{
angle=0,
scale=1,
opacity=1,
contents={}
}

\makeatletter
\backgroundsetup{
contents={
\ifcolor
\textcolor{\chaptercolor}{\rule{3em}{3em}}
\fi}
}
\ifodd\value{page}
\backgroundsetup{
position={current page.north east},
hshift=-1.5cm,vshift=-1cm
}
\else
\backgroundsetup{position={current page.north west},
hshift=1.5cm,vshift=-1cm
}
\fi
\BgMaterial}

\def\chaptercolor{black}
\newcommand\normalchapter{%
\colorfalse%
\titleformat{\chapter}[display]
{\relax\ifthenelse{\NOT\boolean{@tufte@symmetric}}{\begin{fullwidth}}{}}
{\itshape\huge\thechapter}
{0pt}
{\huge\rmfamily\itshape}
[\ifthenelse{\NOT\boolean{@tufte@symmetric}}{\end{fullwidth}}{}]
}
\newcommand\colorchapter{%
\colortrue%
\titleformat{\chapter}[display]
{\relax\color{\chaptercolor}\ifthenelse{\NOT\boolean{@tufte@symmetric}}{\begin{fullwidth}}{}}
{\itshape\huge\thechapter}
{0pt}
{\huge\rmfamily\itshape}
[\ifthenelse{\NOT\boolean{@tufte@symmetric}}{\end{fullwidth}}{}]
}

\newcommand\ContentColor[1]{%
\titlecontents{chapter}[0em]
{\vspace{1.5\baselineskip}\begin{fullwidth}\LARGE\rmfamily\itshape%
\makebox[0pt]{\textcolor{#1}{\rule{1em}{1em}}\hspace{2em}}
}
{\hspace*{0em}\thecontentslabel{2em}}
{\hspace*{0em}}
[\end{fullwidth}]
}
\newcommand\ContentNoColor{%
\titlecontents{chapter}%
[0em]
{\vspace{1.5\baselineskip}\begin{fullwidth}\LARGE\rmfamily\itshape}
{\hspace*{0em}\thecontentslabel{2em}}
{\hspace*{0em}}
[\end{fullwidth}]
}
\makeatother
\newcommand\ChapColor[1]{%
\def\chaptercolor{#1}
\ContentColor{#1}
}

\begin{document}

\tableofcontents
\cleardoublepage
\chapter{Introduction}
\lipsum[1-2]
\cleardoublepage
\colorchapter
\ChapColor{Maroon}
\chapter{Test chapter one}
\lipsum[1-4]
\cleardoublepage
\ChapColor{MidnightBlue}
\chapter{Test chapter two}
\lipsum[1-4]
\cleardoublepage
\ChapColor{Dandelion}
\chapter{Test chapter three}
\lipsum[1-4]
\cleardoublepage
\ContentNoColor
\normalchapter
\chapter{Appendix}
\lipsum[1-4]

\end{document}


From the point where you want the color specifications to be active, add

\colorchapter


to your document. For each chapter in this section, select the color with

\ChapColor{<color>}


You can deactivate the color specification at any point using

\normalchapter


To place the colored marks in chapters with color specification, the background package wes used; this allows you to have complete freedom in the location of the marks.

-
When I run that I get about 60 "undefined control sequence} errors and some code up by the tabs on each page: contents= 1.5cm,vshift=-1cm position=current page.north east, hshift=- I can't figure out why it won't compile the way yours does. –  Tim Mar 2 at 20:03
@Tim you need an updated version of the background package. Following the link I provided, download the .zip file, run latex on background.ins and move the generated background.sty file either to your local TDS tree or to your working directory (where your .tex file resides). –  Gonzalo Medina Mar 2 at 22:13
@Tim let me know if you have any problems with the installation process and I can give you directly the .sty file. –  Gonzalo Medina Mar 2 at 22:17
Works beautifully! That's a wonderful little package. Thanks for your help. –  Tim Mar 3 at 0:02
@Tim I added a simplified version of the code that might be of interest for you. –  Gonzalo Medina Mar 3 at 0:24

The tufte bundle loads the titletoc and fancyhdr packages, so we can access all of their powerful commands.

In particular, in the code below I have used

% format the toc entries for chapter
\titlecontents{chapter}
[0cm]             % left margin
{}             % above code
{%             % numbered format
{\llap{\color{\chaptercolor{\thecontentslabel}}\rule{.5cm}{.5cm}}\thecontentslabel\hskip .5cm\Large}%
}%
{}         % unnumbered format
{\titlerule*[.5pc]{.}\contentspage}%


to format the chapter entry in the toc.

I have also used

\fancyhead[R]{\color{\chaptercolor{\the\value{chapter}}}\rule{1cm}{1cm}}


to add a little coloured box to the top of each page.

Both of these commands use the command

\newcommand{\chaptercolor}[1]{%
\ifcase#1\relax
\or
blue%
\or
red%
\fi%
}


to select the color. You'll need to add more colors as you add more chapter.

I took the liberty of numbering your chapters using

\setcounter{secnumdepth}{0}


If your document is different, we'll need to see a MWE.

% arara: pdflatex
% arara: pdflatex
% !arara: animate: {delay: 80}
\documentclass[a4paper,twoside]{tufte-book}

\setcounter{secnumdepth}{0}
\usepackage{xcolor} % for colour
\usepackage{lipsum} % just for sample text

\newcommand{\chaptercolor}[1]{%
\ifcase#1\relax
\or
blue%
\or
red%
\fi%
}

% format the toc entries for chapter
\titlecontents{chapter}
[0cm]             % left margin
{}             % above code
{%             % numbered format
{\llap{\color{\chaptercolor{\thecontentslabel}}\rule{.5cm}{.5cm}}\thecontentslabel\hskip .5cm\Large}%
}%
{}         % unnumbered format
{\titlerule*[.5pc]{.}\contentspage}%

\begin{document}

\tableofcontents

\chapter{Chapter}
\lipsum[1]
\section{Section}
\lipsum[1]
\subsection{Sub section}
\lipsum[1]
\chapter{Chapter}
\lipsum[1]
\section{Section}
\lipsum[1]
\subsection{Sub section}
\lipsum[1]

\end{document}

-