0

In the attempt of reproducing the layout of a book, I need to use the title of the book in one of the headings. Specifically, using fancyhdr, I'd do something like this

\fancyhead[CE]{\scshape \lowercase{the title of the book}}

However, even though I could hardcode the title of the book in there, I'd rather avoid it, because most likely I'll use that title elsewhere (in the cover, which I'd do with LaTeX as well, but also in first page, or whatever).

So my inclination is to do something like this,

\title{the title of the book}

and then use \thetitle or something whenever I needed it. However, I'm not sure what is the proper way of doing it.

I've found this question on StackOverflow, which has answers of various ages. The most recent answer suggests to \usepackage{authoraftertitle}+\MyTitle, while another suggests to \renewcommand{\title} so that \title also defines \thetitle.


The suggested existing question gives an answer, but I can't seem to apply it directly to my usecase.

For instance, this doesn't work (I'm not reporting the errors, because I'm sure expert readers know why it's wrong, even if I don't),

\fancyhead[CE]{\scshape \lowercase{\makeatletter\@title\makeatother}}

whereas this only partially works, as \lowercase seems to be ineffective,

\makeatletter
\fancyhead[CE]{\scshape \lowercase{\@title}}
\makeatother

It turns out that this works

\makeatletter
\fancyhead[CE]{\scshape \MakeLowercase{\@title}}
\makeatother

but I don't know why. Maybe an explanation or at least a hint about why this works woud be nice.

Enlico
  • 2,592
  • @Marijn, I've updated the question. Yes, that answer gives me a lot, but I'm still curious about why the solution works. – Enlico Dec 25 '22 at 14:18
  • 1
    The way \makeatletter and \makeatother work is that they make the @ character a normal character if the commands are executed when the code is loaded and all the commands are 'tokenized' as it is called. This means the first variant does not work because \@title is encountered when the file is loaded, but \makeatletter is not executed at that point because it is inside of the \fancyhead declaration. As a rule of thumb \makeatletter and \makeatother should always be at the 'main' level of the code and never inside a command or environment. – Marijn Dec 25 '22 at 15:19
  • 1
    The difference between \lowercase and \MakeLowercase is exactly what you have encountered, i.e., one is a low-level command that tries to change case of the input directly, while the other is a more complex ('expandable') macro that can be used inside of other complex macros. – Marijn Dec 25 '22 at 15:24

0 Answers0