[Tex/LaTex] `fancyhdr` and automatic line length

automationfancyhdrheader-footer

I want to create a template for notes and I was thinking to use fancyhdr package. So far I face two problems:

[1] I don't want the header in the first page, but only the footer

[2] I would like to have a line—centered, that is it does not appear as an underline—in the middle of header and footer the length of which can be automatically adjusted given the entries on the far left and right. This line should not touch the left an right entries.

I shall provide a minimal example.

\documentclass[11pt,a4paper]{article}
\usepackage[utf8x]{inputenc}
\usepackage{fancybox}

\usepackage{lastpage}
 \usepackage{setspace}
\usepackage[left=2.5cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{lipsum}

\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt}  


\lhead{Notes on ABC}
\chead{\line(1,0){200}} 
\rhead{\textit{\textcopyright \, Author, \the\year}}   
\lfoot{Chapter 1: A brief introduction in }
\cfoot{\line(1,0){200}}       
\rfoot{Page \thepage \hspace{1pt} of \pageref{LastPage}}

%  Defining the vertical distance in enumeration and itemization
 \usepackage{enumitem}
 \setlist{itemsep=2pt,parsep=2pt,topsep=15pt}


\begin{document}
\pagestyle{fancy}

\vspace{2cm}

\Ovalbox{
\begin{minipage}{0.85\textwidth}
\begin{center}
{\normalsize Notes in ... \\ 
Do not distribute without author's approval.
}
\end{center}
\end{minipage}
}

\vspace{4cm}

\begin{center}
{\Huge \textbf{Chapter 1}} \\ 
{\LARGE \textbf{A Brief Introduction in ... }}
\end{center}

\vspace{0.25cm}

\section{The origins}

\par \lipsum 

\newpage

\section{Another section}

\par Something ...

\end{document}

Best Answer

This sets things up to use semantic markup in the document e.g. \title, \chapter etc. It patches \chapter so that it does not start a new page and uses the report class with no title page. Note that your title is off-centre. Assuming that is deliberate, I've emulated it here. I've substituted pagelts for lastpage since lastpage has been issuing warnings telling you to do this for ages. (I know because I ignored them until things wouldn't compile, too.) I've also adjusted headheight because fancyhdr is complaining that your page layout will be inconsistent (and it is changing the value anyway whether you like it or not).

\documentclass[11pt,a4paper,notitlepage]{report}
\usepackage[utf8x]{inputenc}
\usepackage{fancybox}
\usepackage{textcomp}
\usepackage{pageslts}
\usepackage{setspace}
\usepackage{titling}
\usepackage{etoolbox}
\usepackage[left=2.5cm,right=2cm,top=2cm,bottom=2cm, headheight=14pt]{geometry}
\usepackage{fancyhdr}
\usepackage{xhfill}
\renewcommand{\headrulewidth}{0pt}
\usepackage{lipsum}



\fancyhf{}
\chead{\parbox{\textwidth}{\thetitle\ \hrulefill\ \textit{\textcopyright\ \theauthor, \the\year}}}
\cfoot{\parbox{\textwidth}{\nouppercase{\leftmark}\ \xrfill[1ex]{.4pt}\ Page \thepage\ of \pageref{LastPage}}}

\fancypagestyle{plain}{%
  \fancyhf{}%
  \cfoot{\parbox{\textwidth}{\nouppercase{\leftmark}\ \xrfill[1ex]{.4pt}\ Page \thepage\ of \pageref{LastPage}}}}

%  Defining the vertical distance in enumeration and itemization
\usepackage{enumitem}
\setlist{itemsep=2pt,parsep=2pt,topsep=15pt}

\pretitle{%
  \vskip 20mm
  \begin{Sbox}%
    \begin{minipage}{.85\textwidth}% off-centre title ??
      \begin{center}%
        \normalsize}
\posttitle{%
      \par
      \end{center}%
    \end{minipage}%
  \end{Sbox}\Ovalbox{\TheSbox}%
  \vskip .5em}
\title{Notes in \dots\\Do not distribute without author's approval.}
\date{}
\author{}

\makeatletter
  \renewcommand \thesection {\@arabic\c@section}
\makeatother

\patchcmd{\chapter}{\clearpage}{\relax}{}{}
\patchcmd{\chapter}{\cleardoublepage}{\relax}{}{}

\begin{document}
  \pagenumbering{arabic}
  \pagestyle{fancy}
  \maketitle\thispagestyle{plain}
  \author{Author}
  \title{Notes on ABC}

  \chapter{A Brief Introduction in \dots }

  \section{The origins}

  \lipsum

  \newpage

  \section{Another section}

  Something ...

\end{document}

I've emulated some, but not all of your formatting. You may wish to change the format of chapters, for example. If so, there are plenty of questions on that topic. The main thing is to do this stuff in your preamble and not to include explicit markup in the document itself.

first two pages