# How the order you define commands in a class file changes the output?

I'm working with a class file, trying to adjust it to my needs, and somewhere in the middle of it is defined a function that writes the title of the paper. What I have is this:

\title{
\vspace{-30pt}
\begin{flushleft}
\HorRule
\\
\fontsize{35}{40}
\usefont{T1}{phv}{b}{n}
\color{titlecolor}
\@TheTitle
\\
\end{flushleft}
}

\renewcommand{\title}[2]{\def\@TheTitle{#1}\def\@ShortTitle{#2}}


Let us begin with the last line of code. It defines a command \title with two arguments. When you call \title inside your .tex file what it does is to define two text replacements inside the .cls file. The \@ShortTitle will be used in the footer and \@TheTitle in the title. Now, going back to the first block of code, it calls a function \title which uses the value stored in \@TheTitle. My doubt is this: how can \title be called before it is defined (in the last line of the code)? And how can it use something that was not defined yet (the \@TheTitle)?

I tried to pass the line

\renewcommand{\title}[2]{\def\@TheTitle{#1}\def\@ShortTitle{#2}}


to the begining of the code but it simply does not work that way. What is going on?

• Instead of \title define maybe \createtitle and then define a command to change the variables used within \createtitle: \newcommand{\mytitle}[2]{\def\@TheTitle{#1}\def\@ShortTitle{#2}}. So, in your tex file you can use \title{}{} and \createtitle. – Sigur Nov 6 '16 at 2:12
• The way things are in the first block of code I have written above, the .tex file works without a problem. Your suggestion helps me to understand the code because the \title function is not called before it is defined anymore but the confusion remais with the \@TheTitle. How can \title use it as an argument before it is defined in the \newcommand bit? – Gabu Nov 6 '16 at 2:20
• The command is in the cls file. It will be executed only on tex file when you call it. In the cls you can change orders, no problem. – Sigur Nov 6 '16 at 2:22
• Note that macro expansion works lazily in LaTeX, i.e. all commands/macros defined by \def or \newcommand are only checked for existance at the time they are expanded. So in your case \@TheTitle can be used in the \title command even if it hasn't been defined so far. Problems only arise if \title is actually called in your code without \@TheTitle being defined at that point. – siracusa Nov 6 '16 at 2:29

Typical document classes only store the contents of \title inside a macro, let's say, \@title. The common procedure for this resembles

\def\title#1{\def\@title{#1}}


This allows users to specify \title inside the preamble (say) and only later issue \maketitle (within the document environment) to set the title. Whenever a call to \title is made, it can contain any "garbage" that may or may not exist - the problem (of undefined control sequences) will only realise itself when you actually set the title via \maketitle.

So, in your case, \title probably only stores it's contents inside \@title like the usual classes do, even though \@TheTitle is undefined at the time \title is called. When the user of your document class calls \title, it uses the redefined version of \title which sets the values of \@TheTitle and \@ShortTitle.

The reason for this ordering is probably to ensure that the default \title procedure (set via \maketitle) is still usable in the traditional sense. One issue may be that \title typically only takes a single argument, while the redefinition clearly requires two mandatory arguments.

You have (again) only posted fragments, not a reproducible test case, so it is not possible to give a full answer but

my doubt is this: how can \title be called before it is defined (in the last line of the code)?

the clue is in the last line

\renewcommand{\title}..


which is not a definition of \title but a redefinition. This necessarily means that \title must already have been defined (presumably with a different definition) and it is that (unshown) definition that is used in the call to \title at the start of your fragment.

The previous, unshown, definition presumably defined \title with one argument not two, looking at the use here.

Unrelated to your question but the use of \title containing negative spacing, low level font calls and unwanted space tokens is just horrible if you are learning latex, you really shouldn't be learning from code like this!

• Thank you for your reply. All the tutorials I found on how to code a class file were really shallow. I don't know about the elegance of the code but this was the better example I could find, producing a good looking paper. I didn't posted the whole code because it's long and I think we might drift away from the point. What do you mean by low level font calls and unwanted space tokens? – Gabu Nov 6 '16 at 16:32
• @Gabu the use of \title at the start of your fragment has 9 space tokens (from ends of lines) in its argument. that may or may not produce bad space in the output depending on its definition (which you have not shown) you should almost never have code such as \fontsize{35}{40} \usefont{T1}{phv}{b}{n} within another macro, font setup should normally be document wide. If this document is using helvetica for titles that should have been set up elsewhere in the definition of the title page, not in the argument to \title. – David Carlisle Nov 6 '16 at 16:40
• @Gabu you are asking about the use of \title but did not post the definition of \title because you thought that may cause people to drift from the point of your question???? – David Carlisle Nov 6 '16 at 16:43
• The only time \title appears in the whole file is in the fragment of code I provided . This class loads an article-like class (scrartcl), which I presume have \title defined in it. Sure, I didn't tell what \HorRule does, it draws a horizontal line, but I didn't think it would matter for the question I have. – Gabu Nov 6 '16 at 16:58
• @Gabu so now you have said what \title is defined as (scratcl's definition) the posted code in your question is completely meaningless without that information/ – David Carlisle Nov 6 '16 at 17:00