12

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.

0

2 Answers 2

17

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
8
  • Kidding me? So that simple? Thanks a lot :) Mar 22, 2016 at 18:59
  • <sarcasm>LaTeX is better than Word because LaTeX uses semantic markup.</sarcasm>
    – Aditya
    Mar 22, 2016 at 20:46
  • I down-voted. There has to be a better way to do this rather than explicitly writing the color command.
    – Aditya
    Mar 22, 2016 at 20:49
  • @Aditya: Sure. One could load etoolbox and patch \maketitle, or update the \title command to store its contents differently. All of these go far beyond the one-time use of \title. I'll add the above suggestions to my answer for completeness.
    – Werner
    Mar 22, 2016 at 21:09
  • @Aditya Semantic markup is a good thing, if the author just wants to write the content. Nowadays every author wants to change the appearance, hence has intermix style and content somehow. Werner has shown how to do this, which is for a beginner looking scry i think. It was asked for a simple solution, which \title{\color{red}{title}} provides.
    – Johannes_B
    Mar 23, 2016 at 6:46
5

It is cleaner and clearer to avoid hard-coding formatting in the content of commands like \title. Although this is typically a once-off command in a document - if only because \maketitle enforces this by wiping everything - it is still best avoided, I think.

And the alternative is not the scary-looking patching of internal commands. It is, as in the case of section headings, to use a package which does the scary stuff for you. In this case titling specialises in customising the format of document titles.

For example:

\pretitle{\begin{center}\LARGE\color{astral}}
\posttitle{\par\end{center}\vskip 0.5em}

This is slightly more complex than putting the colour in \title but not much. I started with the defaults which I copied from titling's documentation:

\pretitle{\begin{center}\LARGE}
\posttitle{\par\end{center}\vskip 0.5em}

I then simply added the colour specification following the font specification \LARGE.

And that's it:

custom title

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

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

\pretitle{\begin{center}\LARGE\color{astral}}
\posttitle{\par\end{center}\vskip 0.5em}

\title{Title}
\author{Author}

\begin{document}
\maketitle
\begin{abstract}
  An abstract
\end{abstract}
\smallskip
\noindent \textbf{Klíčová slova:}\dots
\section{A section}
\end{document}

You must log in to answer this question.

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