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.

My question is directly related to post-processing using authoring packages like etoolbox for instance. I've included the package as follow :

\usepackage{etoolbox}

what i try to do is to prepend \color{myColor} to every \chapter commands in the whole document. I'm using :

\preto\chapter{\color{myColor}}

That could actually work, but not only colors my chapter but also all the text after \chapter commands (therefore all my document..).

Is there some way to wrap the color to only every chapter ?

ps : I'm using report class

share|improve this question
1  
This reminds me of another question: Is it possible to change text color for all headings? –  cgnieder Feb 20 '13 at 22:40
add comment

4 Answers 4

An example using the sectsty package:

\documentclass{book}
\usepackage{xcolor}
\usepackage{sectsty}

\chapterfont{\color{red}}

\begin{document}

\chapter{Test Chapter}
test

\end{document}

enter image description here

And now using the titlesec package:

\documentclass{book}
\usepackage{xcolor}
\usepackage{titlesec}

\titleformat{\chapter}[display]
  {\normalfont\huge\bfseries\color{red}}{\chaptertitlename\ \thechapter}{20pt}{\Huge}

\begin{document}

\chapter{Test Chapter}
test

\end{document}

As I see, you added the information that you are using the report document class; my example codes will work also for this class.

share|improve this answer
add comment

Just for completeness: If you use KOMA-Script you can simply use the font elements. Use chapter to affect only the \chapter headings or disposition do change all section levels.

\documentclass{scrreprt}

\usepackage{xcolor}

\addtokomafont{disposition}{\color{red}}
\addtokomafont{chapter}{\color{blue}}

\begin{document}
\chapter{My Chapter}
\section{My Section}
\subsection{My Subsection}
Text
\end{document}

colored chapters

share|improve this answer
add comment

You probably could patch in a closing group but you need to be careful not to interfere with the way section headings look ahead for * or optional arguments. Also putting a color command immediately before the chapter command is quite likely to affect the vertical spacing.

It is better really simply to add the colour at the point the font is added. In book class the chapter head is defined by

\def\@makechapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
      \if@mainmatter
        \huge\bfseries \@chapapp\space \thechapter
        \par\nobreak
        \vskip 20\p@
      \fi
    \fi
    \interlinepenalty\@M
    \Huge \bfseries #1\par\nobreak
    \vskip 40\p@
  }}

so I'd do

\def\@makechapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
      \if@mainmatter
        \huge\bfseries \textcolor{myColor}{\@chapapp\space \thechapter}%
        \par\nobreak
        \vskip 20\p@
      \fi
    \fi
    \interlinepenalty\@M
    \Huge \bfseries \textcolor{myColor}{#1}\par\nobreak
    \vskip 40\p@
  }}

Which avoids adding the colour in vertical mode.


I see you added a comment that you are using report so you would make the same additions to:

\def\@makechapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
        \huge\bfseries \@chapapp\space \thechapter
        \par\nobreak
        \vskip 20\p@
    \fi
    \interlinepenalty\@M
    \Huge \bfseries #1\par\nobreak
    \vskip 40\p@
  }}
share|improve this answer
2  
When I caught a glimpse of the TeX code here, I didn't need to check whose answer this is. I just knew... somehow. –  mbork Feb 20 '13 at 22:18
    
Is there any danger in renewing the relevant \@startsection command's sixth argument to contain something like \color{red} (between the \makeat<letter|other> commands where needed)? –  jon Feb 21 '13 at 0:37
    
in principle yes but probably not in practice, I went to some lengths to avoid starting the color in vmode here but I tried to find an example that went wrong with some of the other answers but couldn't come up with one (but it is 1 in the morning so who knows:-) –  David Carlisle Feb 21 '13 at 0:44
add comment

Since \color{myColor} starts coloring text "from now on", you might want to do something like

\preto\chapter{\begingroup\color{myColor}}
\appto\chapter{\endgroup}

This is definitely not a clean way of doing this, but should work. Instead, I'd look for solutions using fncychap, memoir or similar packages/classes.

share|improve this answer
1  
The best solution for fncychap is not using it. ;-) –  Speravir Feb 20 '13 at 22:45
1  
Well, I've actually never used it, I only looked at its docs and remembered about its existence. What exactly is the problem with that package? –  mbork Feb 20 '13 at 22:51
1  
The styles are mostly ugly and some only work well for one liners. –  Speravir Feb 20 '13 at 22:55
1  
Well, but there's some support for customization, like font or color changing. And that's what needed here. Edit: and I didn't look at predefined styles, btw. –  mbork Feb 20 '13 at 22:58
add comment

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.