1
\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}
5
  • 2
    You shouldn't use formatting commands like \color and \hrule in the heading. Or do you want both also in the header and in the table of contents? Use a package like titlesec to define a layout. May 5, 2022 at 7:15
  • The error message not always are indecipherable verbiage and sometimes explain clearly the problem, so always think if it could be a clue for a solution. In this case, the problem is that "RED" is undefined, so a solution could be to define "RED". If you define "RED" very different of "red" (in lowercase) at the end of the preamble, e.g, \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.
    – Fran
    May 5, 2022 at 8:54
  • @ Thank you all. Heading set from titlesec will change all title format in an article. I think if I can change the format of one certain title only.
    – lyl
    May 5, 2022 at 13:28
  • @Fran well that's the same sort of solution as curing a sore toe by cutting off your leg. The error is putting formatting in the argument of \section : the fact that it doesn't give an error message in all cases is just unfortunate lack of error checking. May 5, 2022 at 14:42
  • @DavidCarlisle Yeah, I have explained myself terribly. Obviously, the origin of the problem is that "RED" is created unexpectedly, and this happens because "red" exists in \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.
    – Fran
    May 5, 2022 at 20:53

1 Answer 1

2

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 fancyhdrs 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 fancyhdrs \headrule.

  1. Define RED to be red: \colorlet{RED}{red}
  2. Remove the uppercasing, by either redefining \sectionmark or using fancyhdrs \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}
4
  • Many thanks! Do I understand this right -- no headers are given, no error will occur?
    – lyl
    May 5, 2022 at 13:34
  • And how to prevent the section title in TOC from being uppercased when \section{aaa}
    – lyl
    May 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

You must log in to answer this question.

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