1

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}%
                                   {\headingfont\Large\bfseries\sectioncolor}}
5
  • Options to classes should be passed in squared brackets: \documentclass[]{}
    – Sigur
    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.
    – Vebjorn
    Nov 28, 2019 at 20:26
  • 2
    @Vebjorn: You seem to need the following definition of \setsectioncolor: \newcommand{\setsectioncolor}[1]{\renewcommand{\sectioncolor}{\color{#1}}}. And your (re)definition of \section with \sectioncolor should then work as expected (assuming you've also defined \headingfont as well which, by the way, should probably follow \Large\bfseries so that \headingfont would add to the default style).
    – Werner
    Nov 28, 2019 at 20:46
  • Thanks @Werner. I think you should make it an answer to the question. It was very useful.
    – Vebjorn
    Nov 29, 2019 at 12:46

2 Answers 2

2

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.

enter image description here

\documentclass{article}

\usepackage{xcolor}

\newcommand{\setsectioncolor}[1]{\renewcommand{\sectioncolor}{\color{#1}}}
\newcommand{\sectioncolor}{}% Default is no colour
\newcommand{\resetsectioncolor}{\renewcommand{\sectioncolor}{}}% Restore (default) section colour

\newcommand{\setsubsectioncolor}[1]{\renewcommand{\subsectioncolor}{\color{#1}}}
\newcommand{\subsectioncolor}{}% Default is no colour
\newcommand{\resetsubsectioncolor}{\renewcommand{\subsectioncolor}{}}% Restore (default) subsection colour

\newcommand{\setsubsubsectioncolor}[1]{\renewcommand{\subsubsectioncolor}{\color{#1}}}
\newcommand{\subsubsectioncolor}{}% Default is no colour
\newcommand{\resetsubsubsectioncolor}{\renewcommand{\subsubsectioncolor}{}}% Restore (default) subsection 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}
-1

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}{}

\newcommand{\headingfont}{\Large\bfseries}%

\begin{document}

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

\section{Test}

\end{document}

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...

2
  • 1
    Why do you define a default colour for \sections to be cyan? Also, have you tried changing it to something else via \setsectioncolor{<colour>}?
    – Werner
    Nov 29, 2019 at 6:29
  • Also, I swapped \normalfont with \headingfont to make a command \setheadingfont that enables you to change the font. \rmfamily, \ttfamily, etc.
    – Vebjorn
    Nov 29, 2019 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.