# Section title formatting also in the toc

I have searched the web to no avail trying to figure out how to redefine the section title format in a way that will be transmitted to the way that same chapter appears in the Table of Contents.

I would like to define a new environment that alters the colour of the text as well as the section titles as seen in the following minimal example:

\documentclass{article}
\usepackage{xcolor,titlesec}

\newcommand{\mystyle}{}
\titleformat{\section}{\normalfont\Large\bfseries\mystyle}{\thesection}{1em}{}
\newenvironment{myenv}
{\renewcommand{\mystyle}{\color{red}} \mystyle}
{\renewcommand{\mystyle}{}}

\begin{document}
\tableofcontents

\section{One}
Section one.

\begin{myenv}
\section{Two}
Section two.
\end{myenv}

\section{Three}
Section three.
\end{document}


This code gives me the below result, which only sets the colour of the section in the text.

However, what I would like to achieve, without modifying each section manually and without defining a new section command is to have the specific section name appear the same colour in the table of contents too, as seen below.

EDIT 1: This post is somewhat related, but it only works if the style of the section titles is changed across the entire document, therefore it does not solve my problem.

EDIT 2: Thanks very much for the really fast answers! However, I think I found the way that works best for me as described in my answer to myself below, inspired by this post.

• While trying to make the minimal example and the accompanying images I came across this post. I haven't figured out exactly how to apply the advice there to my problem, but I will try to pursue it as it seems promising. – Wolfgang Feb 1 '16 at 20:46
• do you wand exactly what the image shows? i.e. in contents only Two is red not section number not page number – touhami Feb 1 '16 at 21:12
• I don't get this. If not any of the section names should appear in a specific colour (other than black), it's very likely to apply myenv to those ones explicitly or using a switch. I don't get the purpose of the environment as well – user31729 Feb 1 '16 at 21:14
• @touhami Ideally, no, the number 2 would also be red, but I couldn't figure out how to make that image. – Wolfgang Feb 1 '16 at 21:25
• @ChristianHupfer I'm not sure I completely understand what you mean, but I would like to have not only the section title, but also the text of the section in that colour ("Section two."). – Wolfgang Feb 1 '16 at 21:27

Here is a solution. I will try to explain if needed.

\documentclass{article}
\usepackage{xcolor}

\newenvironment{myenv}%
{\color{red}%

\makeatletter
\let\mtl@section\l@section
\newenvironment{mytocenv}%
{\renewcommand*\l@section[2]{\mtl@section{{\color{red}##1}}{##2}}}%
{}
\makeatother
\begin{document}
\tableofcontents

\section{One}
Section one.

\begin{myenv}
\section{Two}
Section two.
\end{myenv}

\section{Three}
Section three.
\end{document}


Update I did not take care of subsection and so on (well that was not explicit in the question) so I think one should do

\makeatletter
\let\mtl@section\l@section
\let\mt@dottedtocline\@dottedtocline
\newenvironment{mytocenv}%
{\renewcommand*\l@section[2]{\mtl@section{{\color{red}##1}}{##2}}%
\renewcommand*\@dottedtocline[5]{\mt@dottedtocline{##1}{##2}{##3}{{\color{red}##4}}{##5}}}%
{}
\makeatother


You could also define a command instead of an environment; makes it easier IMO if you have consecutive sections which you want colored:

\documentclass{article}
\usepackage{xcolor,titlesec,titletoc}

\newcommand{\mystyle}[1][black]{%
\titleformat
{\section}%
{\normalfont\Large\bfseries\color{#1}}%
{\thesection}%
{1em}{}%
%
\titlecontents
{section}[0em]%
{}{\hspace*{0em}}%
{\titlerule*[0.7pc]{.}\bfseries\contentspage}%
}

\mystyle

\begin{document}
\tableofcontents

\section{One}
Section one.

\mystyle[red]
\section{Two}
Section two.

\section{Three}
Section three.

\mystyle
\section{Four}
Section four.

\end{document}


In fact, I think I found a way that I like the most, because the code is the most easy to modify (2 added lines) and understand (pure LaTeX). I got the idea reading this post on text in different colours by different authors.

I have not yet seen any downsides to this, apart from the fact that you have to assume the rest of the document colour is black, which is also the case for the answers by @touhami and @ArashEsbati, if I understand correctly. Ideally I would not like to be making this assumption, but I don't know how to implement this more generally.

It is also easy to add subsections etc with single lines, by adding the following:

\titleformat\subsection}{\normalfont\large\bfseries\ignorestyle}{\thesubsection}{1em}{}
\titleformat\subsubsection}{\normalfont\normalsizees\ignorestyle}{\thesubsubsection}{1em}{}


This my minimal code:

\documentclass{article}
\usepackage{xcolor,titlesec}

\newcommand{\mystyle}{}
\titleformat{\section}{\normalfont\Large\bfseries\mystyle}{\thesection}{1em}{}
\newenvironment{myenv}
{\renewcommand{\mystyle}{\color{red}}
\mystyle}
{\renewcommand{\mystyle}{\color{black}}

\begin{document}
\tableofcontents

\section{One}
Section one.

\begin{myenv}
\section{Two}
Section two.
\end{myenv}

\section{Three}
Section three.
\end{document}


and this is the result: