8

I am currently writing a custom class that inherits from a standard class (article), and I would like to redefine the \title command, so that the end user can call it, and I would be able to get the result of that command, modify it a tiny bit, and input it to the \title command of the base class.

Actually, when writing this question, I have found a way of doing this. Here is a MWE, with the class file Test.cls:

\ProvidesClass{Test}
\LoadClass{article}

\title{\@testTitle, isn't it?}
\renewcommand*{\title}[1]{\def\@testTitle{#1}}

\endinput

And the user file:

\documentclass{Test}

\title{A great article}

\begin{document}
\maketitle
\end{document}

So in the end, the user chooses a title, but the results outputs an article with the modified title.

Is it the standard way of doing? How would you improve it?

Mico
  • 506,678
FelixCQ
  • 1,443
  • 1
  • 13
  • 11

1 Answers1

11

Similar to this question, you can use etoolbox's \patchcmd to modify the \maketitle command:

enter image description here

\documentclass{Test}  
\title{A great article}  
\begin{document}  
\maketitle  
\end{document}

Test.cls

\ProvidesClass{Test}  
\LoadClass{article}  
\RequirePackage{etoolbox}
\patchcmd{\@maketitle}{\@title}{\@title{}, isn't it?}{}{}
\endinput  

In general, you can use \show\somecommand to see what the definition of \somecommand is, and then find the part you want to patch.

Mike Renfro
  • 20,550