26

With pdfLaTeX I typically used the following code to globally set the text color for all text in the document:

\documentclass{scrreprt}
\usepackage{xcolor}
\begin{document}
\color{red}
\section{A section heading}
some test text
\end{document}

Running the document through pdfLaTeX produces red text. Running this through XeLaTeX produces black text. Using package color instead of xcolor doesn't make a difference.

I'd like to globally set the text color for all text appearing in the document, i.e. the headings, the table of contents, all normal text, in figures, in equations, if possible in TikZ nodes, just everything.

2
  • 4
    Try placing \color{red} right after \begin{document}. Aug 25, 2011 at 20:40
  • @Gonzalo Unfortunately, that doesn't work with Komascript. I've changed the example document accordingly.
    – kongo09
    Aug 25, 2011 at 20:45

4 Answers 4

22

Here's a way to do it which will work whether you use XeLaTeX or not, and whether you use Komascript or not. It works by redefining the default color used by LaTeX.

\documentclass{report}
%\documentclass{scrreprt}

\usepackage{xcolor}

\makeatletter
\newcommand{\globalcolor}[1]{%
  \color{#1}\global\let\default@color\current@color
}
\makeatother

\AtBeginDocument{\globalcolor{red}}

\begin{document}

\section{A section heading}

some test text

\end{document}
4
  • This seems to be the only way working around a XeTeX bug that creates a problem with white text as illustrated here: tex.stackexchange.com/questions/28305/… So thanks a lot!
    – kongo09
    Sep 13, 2011 at 21:23
  • BTW, this will also set the default drawing color for figures created using TikZ – which may or may not be desirable :-)
    – ilpssun
    Aug 10, 2012 at 11:52
  • @ilpssun: you can get around this by using \usepackage{etoolbox} and then doing \makeatletter\preto{\tikzpicture}{\color{black}\let\default@color\current@color}\makeatother Aug 10, 2012 at 15:59
  • 4
    Only LaTeX could be a document processing language where something as simple as setting foreground and background color requires importing packages and defining custom commands...
    – picomancer
    Sep 27, 2013 at 18:54
15

Since you say you're using XeLaTeX, you can also define a colour in the font definition:

In order to change colours if you do this, you need to use fontspec's \addfontfeature instead of the regular \color or \textcolor commands. Alternatively, you can define a new font family for the other colour(s).

\documentclass{scrreprt}
\usepackage{xcolor}
\usepackage{fontspec}
\setmainfont[Color=red]{Linux Libertine O}
\setsansfont[Color=red]{Linux Biolinum O}
\setmonofont[Color=red]{Inconsolata}
% If you will be changing colours a lot, it's best to define a new font family
% for each colour that you will use; if you're just changing a few times, then
% \addfontfeature is fine
\newfontfamily\blueroman[Color=blue]{Linux Libertine O} 

\begin{document}
\chapter{A chapter}
\section{A section}
\texttt{Some mono text}

Some regular text.

{\addfontfeature{Color=blue} Some blue text without defining a new font family.}

{\blueroman Some blue text with a new font family command.}
\end{document}
4
  • then \textcolor{blue}{blue} won't work
    – user2478
    Aug 26, 2011 at 15:45
  • 1
    @Herbert True. You need to use \addfontfeature to do this, or make a new fontfamily. I'll add this to my answer.
    – Alan Munn
    Aug 26, 2011 at 17:11
  • @Alan: I would only use the Color font feature if I really wanted all the colors specified in the document to be overwritten (e.g. to convert a document to black and white, pictures non-withstanding). Otherwise, redefining LaTeX's \default@color is much more natural, doesn't interfere with \color or \textcolor and works whatever the engine or the document class. Aug 26, 2011 at 18:38
  • @Philippe Thanks for your comment and answer. Indeed, the above solution does not work for the text colour of white as illustrated by my other question: tex.stackexchange.com/questions/28305/…
    – kongo09
    Sep 13, 2011 at 21:21
3
\documentclass{scrreprt}
    
\usepackage{xcolor}
    
\AtBeginDocument{\color{red}}
    
\addtokomafont{sectioning}{\color{red}}
    
\begin{document}
    
\section{A section heading}

    some test text
    
\end{document}
4
  • Sorry, that doesn't work with Komascript. Section headings stay black. I've changed the minimum example accordingly.
    – kongo09
    Aug 25, 2011 at 20:50
  • sure, it does ;-)
    – user2478
    Aug 25, 2011 at 20:51
  • Thanks a lot, Herbert! And please ignore my comment above, adding \addtokomafont fixes the problem, now it is working. I found that I can also get it working by putting one \color just before and one \color just after the \begin{document} (thanks to @Gonzalo)
    – kongo09
    Aug 25, 2011 at 20:53
  • While easy and elegant, it doesn't work for all the Komascript elements unless listed individually with \addtokomafont
    – kongo09
    Sep 13, 2011 at 21:20
2

As Alan Munn suggested, since you're using XeLaTeX, you could use fontspec's capabilities to set the color of the font. If you want all the text to be a given color, you can even set that in the default font features:

\documentclass{scrreprt}
\usepackage{xcolor}
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX,Color=red}
\setmainfont{Linux Libertine O}
\setsansfont{Linux Biolinum O}
\setmonofont{Inconsolata}

\begin{document}
\chapter{A chapter}
\section{A section}
\texttt{Some mono text}
Some regular text.
\end{document}
2
  • same here: color changes are not possible with color commands, onyl with another \addfontfeatures{Color=blue}
    – user2478
    Aug 26, 2011 at 15:58
  • Also, this doesn't work with white
    – kongo09
    Sep 13, 2011 at 21:22

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.