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.
\input? You could put everything before \begin{document} into it and just load it via\input{yourfile}. – nox Jul 25 '18 at 10:07subfilesmanual, p. 2: “If the subordinated file was\subfile’d, it ignores anything before and including\begin{document}…”. Indeed, your\let\thetitle\@titleetc. declarations are placed before\begin{document}. – GuM Jul 25 '18 at 10:15pgstyle.styfile, containing the preamble and packages I load. Moving the part\titleand\makeatletter...to that file solved my problem. Thank you – rome Jul 25 '18 at 10:23\inputcontaining the title and specific stuff for your document. – nox Jul 25 '18 at 10:27subfilespackage, especially the part that says that “each subfile is\inputwithin a group”… – GuM Jul 25 '18 at 10:30