# Changing text color with new command in .cls file

I'm writing a class which I can call myclass. I try to define the following commands for changing the text colors (text, section, etc.):

\RequirePackage{xcolor}
\newcommand{\setsectioncolor}[1]{\color[#1]}
\newcommand{\sectioncolor}{}

\newcommand{\setsubsectioncolor}[1]{\color{#1}}
\newcommand{\subsectioncolor}{}

\newcommand{\setsubsubsectioncolor}[1]{\color{#1}}
\newcommand{\subsubsectioncolor}{}

\newcommand{\settxtcolor}[1]{\color{#1}}
\newcommand{\txtcolor}{}


The problem is that I don't know how to define \sectcolor, etc. and could really use some help. I will try to explain what I want to do with these commands.

I would like to redefine section, etc. using \sectcolor:

\renewcommand{\section}{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%

• Options to classes should be passed in squared brackets: \documentclass[]{} Nov 25, 2019 at 20:13
• How does myclass define \section and \subsection? For the set commands you probably want something like \colorlet{sectcol}{#1} rather than a macro \sectcol although it isn't very clear how you're planning to use things.
– cfr
Nov 25, 2019 at 20:20
• @cfr I edited the question. Nov 28, 2019 at 20:26
– Werner
Nov 28, 2019 at 20:46
• Thanks @Werner. I think you should make it an answer to the question. It was very useful. Nov 29, 2019 at 12:46

Here is an implementation that allows you to adjust the colour of the sectional units. Additional \resetXcolor macros are provided to remove any previous colour definitions.

\documentclass{article}

\usepackage{xcolor}

\newcommand{\sectioncolor}{}% Default is no colour

\newcommand{\subsectioncolor}{}% Default is no colour

\newcommand{\subsubsectioncolor}{}% Default is no colour

\makeatletter
% Taken from https://www.tug.org/svn/texlive/trunk/Master/texmf-dist/tex/latex/base/article.cls?view=co
\renewcommand\section{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\normalfont\Large\bfseries\sectioncolor}}
\renewcommand\subsection{\@startsection{subsection}{2}{\z@}%
{-3.25ex\@plus -1ex \@minus -.2ex}%
{1.5ex \@plus .2ex}%
{\normalfont\large\bfseries\subsectioncolor}}
\renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}%
{-3.25ex\@plus -1ex \@minus -.2ex}%
{1.5ex \@plus .2ex}%
{\normalfont\normalsize\bfseries\subsubsectioncolor}}
\makeatother

\begin{document}

\tableofcontents

\setsectioncolor{red}
\section{A red section}

\setsubsectioncolor{green}
\subsection{A green subsection}

\setsubsubsectioncolor{blue}
\subsubsection{A blue subsubsection}

\resetsectioncolor
\section{A section}

\end{document}


Note that sectsty already provides a similar setup:

\documentclass{article}

\usepackage{xcolor,sectsty}

\begin{document}

\tableofcontents

\sectionfont{\color{red}}
\section{A red section}

\subsectionfont{\color{green}}
\subsection{A green subsection}

\subsubsectionfont{\color{blue}}
\subsubsection{A blue subsubsection}

\sectionfont{\color{black}}
\section{A section}

\end{document}


You have to pass the color model to the tag \sectioncolor, modified MWE given below:

\documentclass{book}

\RequirePackage{xcolor}
\newcommand{\setsectioncolor}[1]{\color[#1]}
\newcommand{\sectioncolor}{\color{cyan}}

\newcommand{\setsubsectioncolor}[1]{\color{#1}}
\newcommand{\subsectioncolor}{}

\newcommand{\setsubsubsectioncolor}[1]{\color{#1}}
\newcommand{\subsubsectioncolor}{}

\newcommand{\settxtcolor}[1]{\color{#1}}
\newcommand{\txtcolor}{}

\begin{document}

\makeatletter
\renewcommand{\section}{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%

I have defined a new tag \headingfont and placed the \Large\bfseries into the tag, so it is easy if you want to change font style for \section...
• Why do you define a default colour for \sections to be cyan? Also, have you tried changing it to something else via \setsectioncolor{<colour>}?
• Also, I swapped \normalfont with \headingfont to make a command \setheadingfont that enables you to change the font. \rmfamily, \ttfamily, etc. Nov 29, 2019 at 12:45