\documentclass{article}
\usepackage{xcolor,fancyhdr}
\pagestyle{fancy}
% This line causes error message "Package xcolor Error: Undefined color `RED'".
% If this line is removed, or replaced by \pagestyle{empty}, everything goes all right.
% Does that mean the fancy style and xcolor can not go together?
\begin{document}
\section{\color{red}AAA\hrule} %Removing "\color{red}" outside of \section causes the section number also be colored red, I only want the section text is in red.
section
\end{document}
1 Answer
This is not caused by fancyhdr
but by the standard LaTeX classes. These define the headers in such a way that \MakeUppercase
is applied to them. This uppercases everything in the header, including the "red". To show that it isn't fancyhdr
s fault, remove fancyhdr
from the \usepackage
and use \pagestyle{headings}
instead. It will give the same error.
As @UlrikeFischer said, you shouldn't put the color directly in the section heading. It isn't forbidden, but it is bad practice, as your example shows.
If you don't want to follow Ulrike's advice to use something like titlesec
there are two ways to solve it. But you are warned that you may still encounter other problems. For example, your \hrule
will interfere with fancyhdr
s \headrule
.
- Define
RED
to bered
:\colorlet{RED}{red}
- Remove the uppercasing, by either redefining
\sectionmark
or usingfancyhdr
s\nouppercase
. This will cause your page headings not to be in uppercase. Of course if it is already all uppercase, you won't notice that.
\documentclass{article}
\usepackage{xcolor,fancyhdr}
%\colorlet{RED}{red} % not necessary with \nouppercase
\pagestyle{fancy}
\fancyhead[R]{\nouppercase{\leftmark}}
\begin{document}
\section{\color{red}AAA\hrule} %Removing "\color{red}" outside of \section causes the section number also be colored red, I only want the section text is in red.
section
\end{document}
-
Many thanks! Do I understand this right -- no headers are given, no error will occur?– lylMay 5, 2022 at 13:34
-
And how to prevent the section title in TOC from being uppercased when
\section{aaa}
– lylMay 5, 2022 at 13:39 -
Section title in the TOC will just be
aaa
, it will not be uppercased. May 5, 2022 at 15:47 -
"no headers are given, no error will occur?". Yes, the error occurred while formatting the header, because there it had become
\color{RED}
because of the uppercasing. I there is no header this will not happen. May 5, 2022 at 15:48
\definecolor{RED}{rgb}{0,1,0}
(i.e, to produce green color), you will realize instantly the reason for the error and that is not related at all with using "red" in the\section
.titlesec
will change all title format in an article. I think if I can change the format of one certain title only.\section
: the fact that it doesn't give an error message in all cases is just unfortunate lack of error checking.\section
. What I was trying to show is that the compilation not failed directly because there is "red" in the section title, but when LaTeX try to apply "RED" in the header.