# Include title in header using subfiles

I split my LaTeX document into subfiles and I try to have the title in the header, using \fancyhead. I define \thetitle in my cover file called "cover.tex", which I reproduced in the MWE below

\documentclass[main.tex]{subfiles}
\title{This is my title}
\author{My Name}

\makeatletter
\let\thetitle\@title
\let\theauthor\@author
\makeatother

\fancyhf{}
\fancyhead[R]{\theauthor}
\fancyhead[L]{\thetitle}

\begin{document}
\begin{titlepage}
\Large \thetitle\\
\normalsize \theauthor\\
\end{titlepage}
\end{document}


I use it in a main file like this

\documentclass[12pt]{article}
\usepackage{subfiles} % split the document in multiple files
\usepackage{fancyhdr} % header and footer
\pagestyle{fancy}
\begin{document}
\subfile{cover}
\newpage
Here I will include my text from multiple subfiles.
\end{document}


I get the error message

! Undefined control sequence.
\f@nch@olh ->\thetitle
\strut


I identified the problem to be within the subfiles. I noticed that I don't get the error, if I put the content of the "cover.tex" file directly into the main file. But for readability I would like to avoid doing that. I also tried placing the \fancyhead[L]{\thetitle} line in the main document, which produced the same error.

I assume that the problem is within propagating the \thetitle variable throughout subfiles. My question is, how can I best place the title in the header without defining it multiple times and without moving the complete "cover.tex" into the main file?

Thanks in advance for any help.

• Why don't you use a header file (containing the preamble, packages you load, ...) with \input? You could put everything before \begin{document} into it and just load it via \input{yourfile}. – nox Jul 25 '18 at 10:07
• From subfiles manual, p. 2: “If the subordinated file was \subfile’d, it ignores anything before and including \begin{document}…”. Indeed, your \let\thetitle\@title etc. declarations are placed before \begin{document}. – GuM Jul 25 '18 at 10:15
• @nox I am actually using a pgstyle.sty file, containing the preamble and packages I load. Moving the part \title and \makeatletter... to that file solved my problem. Thank you – rome Jul 25 '18 at 10:23
• Hm, I don't recommend you to set the title in a style file, because a style file should be general enough to use it for other documents as well. It's probably better to use another file and load it with \input containing the title and specific stuff for your document. – nox Jul 25 '18 at 10:27
• I’d also suggest that you read carefully the contents of section 2.3 of the manual of the subfiles package, especially the part that says that “each subfile is \input within a group”… – GuM Jul 25 '18 at 10:30

## 1 Answer

Just a recommendation:

%% main.tex
\input{preamble}

% start of main document
\begin{document}
\input{cover}

Here I will include my text from multiple (sub)files.
\end{document}

%% preamble.tex
\documentclass[12pt]{article}
%\usepackage{subfiles} % split the document in multiple files
\usepackage{fancyhdr} % header and footer
\pagestyle{fancy}

\makeatletter
\author{My Name}
\title{This is my title}
\let\thetitle\@title
\let\theauthor\@author

\fancyhf{}
\fancyhead[R]{\@author}
\fancyhead[L]{\@title}
\makeatother

%% cover.tex
\begin{titlepage}
\Large \thetitle\\
\normalsize \theauthor\\
\end{titlepage}

• That looks very neat. But your preamble.tex file looks very much like my pgstyle.sty file. Could you explain why one or the other is better? Or what belongs in which one? – rome Jul 25 '18 at 11:51
• @rome in latex, a style file is usually loaded via \usepackage and provides functionality for multiple documents rather than just one document, e.g., via optional arguments or general macros. See also tex.stackexchange.com/questions/91167/why-use-sty-files – nox Jul 25 '18 at 18:24