1

I use this code to batch modify the title color of a large number of latex files

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

But I found that the program will get wrong results when the title has optional parameters.

\documentclass[reqno]{amsart}
\usepackage{xcolor}

\makeatletter
\renewcommand{\title}[1]{\renewcommand{\@title}{\color{\@titlecolor}#1}}
\newcommand{\@titlecolor}{black}
\newcommand{\titlecolor}[1]{\renewcommand{\@titlecolor}{#1}}
\makeatother
\begin{document}
\title[Boundary layer ansatz for the steady MHD equations]{Verification of Prandtl boundary layer ansatz for the steady electrically conducting fluids with a moving physical boundary}

\email{[email protected]}

\maketitle

\end{document}

I hope to modify this code to adapt to the situation where there are optional parameters. Every help and its description is highly welcome.

4
  • 1
    Where does the optional argument for \title end up in your title? Is it a subtitle? Can you provide a complete minimal document that we can use to replicate your current setup? It should start with \documentclass and end with \end{document}. Can you do that?
    – Werner
    Jan 2, 2022 at 7:36
  • 1
    \title is defined by the document class so you have not provided any indication of the original definition. The definitions in the standard classes such as article do not have an optional argument. Jan 2, 2022 at 10:34
  • Sorry, the code was not complete just now, now the complete code has been added
    – happy
    Jan 2, 2022 at 12:02
  • Erh, that is not how \title works in amsart you have just killed \shorttitle
    – daleif
    Jan 2, 2022 at 13:08

1 Answer 1

2

As so many others, don't put formatting into data holder macros, it never belongs there. Add the formatting to the macro that actually typesets the data.

Looking at the source of amsarty.cls we can deduce that within \maketitle we go through \@maketitle and end up typesetting the title via \@settitle, which is defined as

\def\@settitle{\begin{center}%
  \baselineskip14\p@\relax
    \bfseries
    \uppercasenonmath\@title
  \@title
  \end{center}%
}

thus we can patch this to add color after \bfseries therefore this works

\documentclass[reqno]{amsart}
\usepackage{xcolor,etoolbox}

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

\patchcmd\@settitle{\bfseries}{\bfseries\color{\@titlecolor}}{}{\ERROR}


\titlecolor{orange}

\makeatother
\begin{document}

\title[Boundary layer ansatz for the steady MHD equations]{Verification of Prandtl boundary layer ansatz for the steady electrically conducting fluids with a moving physical boundary}

\email{[email protected]}

\maketitle

\end{document}

You must log in to answer this question.

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