0

I want to set a default text colour for the whole document, but I don't want to colour the section headings.

In my case I want all of the default text body with paragraphs etc. to be a very dark shade of grey, while all the section headings remain black (including nested subsections).

I tried this brute-force example, using popular packages, with colours red and blue but it still doesn't work. Everything, including the section heading, is red.

\documentclass[12pt]{article}
\usepackage{xcolor}
\usepackage{sectsty}
\sectionfont{\color{blue}}
\color{red}
\sectionfont{\color{blue}}

\begin{document}
\section{I should be blue}
\subsection{I should also be blue}
I should be red
\end{document}
6
  • Code works OK for me.
    – Cicada
    Commented Sep 14, 2022 at 14:55
  • @Cicada My bad the example wasn't general enough. This should work for all sections and sub...subsections, but it doesn't!
    – Nermin
    Commented Sep 14, 2022 at 15:00
  • For subsections, you need \subsectionfont.
    – Cicada
    Commented Sep 14, 2022 at 15:06
  • @Cicada okay but what about subsubsections and subsubsubsections? Isn't there a catch-all command for all nested levels of sections?
    – Nermin
    Commented Sep 14, 2022 at 15:07
  • I don't know - I haven't read the manual yet. Don't forget chapters and parts, and paragraphs etc.
    – Cicada
    Commented Sep 14, 2022 at 15:10

1 Answer 1

1

If you use fontspec package, the colour can be set as an attribute of the font:

colour

MWE

\documentclass[12pt]{article}
\usepackage{xcolor}
\usepackage{fontspec}
\setmainfont{Noto Serif}[Colour=red]
\newfontfamily\ffontb{Noto Serif}[Colour=blue]
\usepackage{sectsty}
\sectionfont{\ffontb}
\subsectionfont{\ffontb}

\begin{document}
\section{I should be blue}
I should be red
\subsection{I should be blue}
\end{document}

Added:

The \allsectionsfont command does all heading levels.

all sections

MWE

\documentclass[12pt]{report}
\usepackage{xcolor}
\usepackage{fontspec}
\setmainfont{Noto Serif}[Colour=red]
\newfontfamily\ffontb{Noto Serif}[Colour=blue]
\usepackage{sectsty}
%\sectionfont{\ffontb}
%\subsectionfont{\ffontb}
\allsectionsfont{\ffontb}


\begin{document}
\section{I should be blue}
I should be red
\subsection{I should be blue}
\part{part}
\chapter{chapter} x x x x x x x x x x x
\section{section} x x x x x x x x x x x
\subsection{subsection} x x x x x x x x x x x
\subsubsection{subsubsection} x x x x x x x x x x x
\paragraph{paragraph} x x x x x x x x x x x
\subparagraph{subparagraph} x x x x x x x x x x x
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .