[Tex/LaTex] Change the color of title

colortitles

I successfully changed a color of sections and subsections to my defined one. However I'd like to change the \title with the same way, but \titlefont{•} doesn't work (and I guess it's not supposed to). What is the simple way to change the \title color as well? I have the simplified following code:

\documentclass[11pt,a4paper]{article}
\usepackage{amsmath, url}
\usepackage[utf8x]{inputenc}
\usepackage[czech]{babel}
\usepackage{xcolor}
\usepackage{sectsty}

\definecolor{astral}{RGB}{46,116,181}
\subsectionfont{\color{astral}}
\sectionfont{\color{astral}}

\title{...}
\author{...}

\begin{document}
    \maketitle    
    \begin{abstract} ...
    \end{abstract}    
    \smallskip
    \noindent \textbf{Key words:} ...   
    \section{...}
\end{document}

Every help and its description is highly welcome.

Best Answer

Insert the colour directly in the \title:

enter image description here

\documentclass{article}

\usepackage{lipsum}% Just for this example
\usepackage{xcolor,sectsty}

\definecolor{astral}{RGB}{46,116,181}
\subsectionfont{\color{astral}}
\sectionfont{\color{astral}}

\title{\color{astral} My title}
\author{An Author}

\begin{document}

\maketitle

\begin{abstract}
  \lipsum[1]
\end{abstract}

\smallskip

\noindent \textbf{Summary:}
\lipsum*[2]

\section{A section}
\lipsum[3]

\end{document}

The above suggestion may seem crude. It is, however, inserted as a part of a one-time usage macro \title, and therefore sufficient for the means. For a more formalized approach, you could patch the inner \@maketitle macro - responsible for setting up the title display:

\usepackage{etoolbox}
\makeatletter
\patchcmd{\@maketitle}% <cmd>
  {\@title}% <search>
  {\color{\@titlecolor}\@title}% <replace>
  {}{}% <success><failure>
\newcommand{\@titlecolor}{black}
\newcommand{\titlecolor}[1]{\renewcommand{\@titlecolor}{#1}}
\makeatother

The above code provides \titlecolor{<color>} which allows you to switch to colour as needed (for example, \titlecolor{astral} would provide the same output). This code is also dependent on the structure of \@maketitle, which may be different for other classes or influenced by certain packages.


Another (less formal) way of address a title colour change could be to only update the way \title handles its argument:

\makeatletter
\renewcommand{\title}[1]{\renewcommand{\@title}{\color{\@titlecolor}#1}}
\newcommand{\@titlecolor}{black}
\newcommand{\titlecolor}[1]{\renewcommand{\@titlecolor}{#1}}
\makeatother