[Tex/LaTex] LaTeX issues Undefined control sequence \Chaptername

errorsfancyhdrmacros

We're back again with the undefined control sequence error, but this time I am not capable of finding it. It is something related with the header and footer:

\let\Chaptermark\chaptermark
\def\chaptermark#1{\def\Chaptername{#1}\Chaptermark{#1}}
\fancyhead{}
\fancyhead[L]{\small \emph{Text}}
\fancyhead[R]{\small Text}
\fancyfoot{}
\fancyfoot[R]{\small \thepage}
\fancyfoot[L]{\small \emph{Chapter \thechapter. \Chaptername}}

AND Log report (near the error message):

]) (./Chapters/02_Abstract.tex) [2] [3

] (./Report.toc [4

]) \tf@toc=\write5 \openout5 = `Report.toc'.

 [5] (./Report.lof [6

] [7]) \tf@lof=\write6 \openout6 = `Report.lof'.

 [8] (./Report.lot Overfull \hbox (1.74966pt too wide) in paragraph at
lines 4--4  [][] []\OT1/cmr/m/n/10.95 Summary of power-trains used
and/or de-vel-op-ment i n pas-sen-ger light-weight |  []

) \tf@lot=\write7 \openout7 = `Report.lot'.

 [9

] ! Undefined control sequence. <argument> Chapter \thechapter .
\Chaptername 
                                               l.121 \chapter
              {Presentation} The control sequence at the end of the top line 
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct spelling
(e.g., `I\hbox'). Otherwise just continue, and I'll forget about
whatever was undefined.

AND Here is the code from the main .tex file that calls several .tex files.

\documentclass[11pt, twoside, draft]{report}

\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm,bindingoffset=6mm]{geometry}
\usepackage{fancyhdr}
\usepackage{subcaption}
\usepackage[numbers,sort&compress]{natbib}
\usepackage{color}
\usepackage{soul}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{longtable}
\usepackage{tabu}
\usepackage{array}
\usepackage{colortbl}
\usepackage{hyperref}
\usepackage{xr}
\usepackage{textcomp}
\usepackage{courier}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{fancyvrb}
\usepackage{siunitx}
\usepackage{fixltx2e}
\usepackage{draftwatermark}
\usepackage[figuresright]{rotating}
\usepackage{helvet}
\usepackage{footnote}

\captionsetup{font=footnotesize, labelfont=sc}
\graphicspath{ {images/} }
\pagestyle{fancy}

\definecolor{bblue}{HTML}{A9D0F5}
\definecolor{lightbblue}{HTML}{CEF6EC}
\definecolor{darkbblue}{HTML}{4189F5}
\definecolor{ggreen}{HTML}{D8F6CE}
\definecolor{darkggreen}{HTML}{41F583}
\definecolor{verydarkggreen}{HTML}{31B404}
\definecolor{rred}{HTML}{F5A9A9}
\definecolor{verydarkrred}{HTML}{DF0101}
\definecolor{oorange}{HTML}{F5ECCE}
\definecolor{verydarkoorange}{HTML}{DF7401}
% \def\tabularxcolumn#1{m{#1}} This is for vertical centering in tabularx.

\SetWatermarkText{DRAFT}
\SetWatermarkScale{3}
\SetWatermarkLightness{0.9}

\newcolumntype{R}{>{\raggedleft\arraybackslash}X}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}

\pgfplotsset{every tick label/.append style={font={{\scriptsize}}}}
\pgfplotsset{/pgf/number format/use comma}
\pgfplotsset{compat=1.5}

\author{Gerard Illana Meler}
\date{September 2016}

\begin{document}

\pagestyle{fancy}
\fancyhead{}
\fancyhead[L]{\small \emph{TEXT}}
\fancyhead[R]{\small TEXT}
\fancyfoot{}
\fancyfoot[R]{\small \thepage}

\input{Chapters/01_Title}

\input{Chapters/02_Abstract}

\chapter*{Acknowledgements}
\emph{Text}

\tableofcontents

\listoffigures

\listoftables

\let\Chaptermark\chaptermark % THIS IS THE ERROR SOURCE
\def\chaptermark#1{\def\Chaptername{#1}\Chaptermark{#1}} % THIS IS THE ERROR SOURCE
\fancyhead{} % THIS IS THE ERROR SOURCE
\fancyhead[L]{\small \emph{Text}} % THIS IS THE ERROR SOURCE
\fancyhead[R]{\small Text} % THIS IS THE ERROR SOURCE
\fancyfoot{} % THIS IS THE ERROR SOURCE
\fancyfoot[R]{\small \thepage} % THIS IS THE ERROR SOURCE
\fancyfoot[L]{\small \emph{Chapter \thechapter. \Chaptername}} % THIS IS THE ERROR SOURCE

\chapter{Presentation}
\externaldocument{Chapters/03_Presentation}
\input{Chapters/03_Presentation}

\chapter{Introduction}
\externaldocument{Chapters/04_Introduction}
\input{Chapters/04_Introduction}

\chapter{Experimental data}
\externaldocument{Chapters/05_Experimental_data}
\input{Chapters/05_Experimental_data}

\chapter{Analysis of the vehicle usage} \label{Analysis_of_the_vehicle_usage}
\externaldocument{Chapters/06_Analysis_of_the_vehicle_usage}
\input{Chapters/06_Analysis_of_the_vehicle_usage}

\chapter{Definition of driving patterns}
\externaldocument{Chapters/07_Definition_of_driving_patterns}
\input{Chapters/07_Definition_of_driving_patterns}

\chapter{Estimation of alternative power-train solutions}
\externaldocument{Chapters/08_Estimation_of_alternative_power_train_solutions}
\input{Chapters/08_Estimation_of_alternative_power_train_solutions}

\chapter{Impact of alternative power-trains}
\externaldocument{Chapters/09_Impact_of_alternative_power_trains}
\input{Chapters/09_Impact_of_alternative_power_trains}

\chapter{List of abbreviations}
\externaldocument{Chapters/98_List_of_abbreviations}
\input{Chapters/98_List_of_abbreviations}

\bibliographystyle{IEEEtran}
\bibliography{References/Bibliography_report}

\appendix

\chapter{Appendix}
\externaldocument{Chapters/99_Appendix}
\input{Chapters/99_Appendix}

\end{document}

Best Answer

Analysis of the problem

The problem is that when the new page style is being defined, LaTeX is in the middle of the last page in \listoftables; so when \chapter{Presentation} is executed, a page is ejected to begin with, but \Chaptername has not yet been set, because \chaptermark is executed after the \clearpage that starts the work of\chapter`.

Solution

Issue \clearpage after \listoftables, before \let\Chaptermark\chaptermark.

Miscellaneous comments

  1. You can also avoid defining \Chaptername and redefining \chaptermark: just do

    \fancyfoot[L]{\small\emph{\nouppercase{\leftmark}}}
    
  2. All your \externaldocument commands do exactly nothing. When you do \input{file} it's the same as if you added the contents of file.tex at the spot. And LaTeX doesn't need to rely on \externaldocument for labels and cross-references in the document it's processing.

  3. Also load fix-cm in order to remove the draftwatermark warning about font substitution. You won't need \SetWatermarkScale{3}.