[Tex/LaTex] Header on odd pages

fancyhdrheader-footermargins

Is there any way to create a (multiline!) header on odd pages and no header at all on even pages?

It should look like this:

Document

My code so far:

\documentclass[a4paper, twoside, 12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[a4paper, includeheadfoot, left=2cm, top=2cm, right=2cm, bottom=2cm]{geometry}
\usepackage{fancyhdr}

\usepackage{etoolbox}
\usepackage{ifthen}

\usepackage{showframe}

\fancyhead{}
\fancyhead[ol]{COURSE}
\fancyhead[or]{
    \begin{tabular}{ c c }
        NAME & NO \\
        NAME1 & xxx \\
        NAME2 & yyy \\
    \end{tabular}
}

\begin{document}
...
\end{document}

My issues:

(1) Fancyhdr warning: \headheight is too small.

The first page is messed up because the \headheight is by default 12pt but should be 44pt in this case. Is there any way to calculate this automatically? I would like to avoid typing "44pt" manually.

(2) Even pages leave empty space for headers and include a horizontal rule.

Even though the header on even pages is empty, the space is not available for the text. I would like to start the text immediately after the 2cm margin so that no trace of the header is left on even pages.

I've been looking for a solution to this simple problem for hours now and any help would be appreciated!

EDIT: I don't care about the footer, it would be nice to have the option to include or disable page numbers as well.

Best Answer

Since you have to define the \headheight at the begining of the code within geometry package irredeemably, this will stablish that same height for the header throughout the document.

To change that, you can define a \newgeometry with the \headheight equal to 0pt at the begining of even pages, but you have to redefine your previous geometry in the next page again. This is a manual procedure, but I don't know another way to do that.

Here I post you some code. As you can see, I have defined two styles with \fancypagestyle, one for even pages and another one for de odd ones. If you want to disable the numbering of the pages, just remove the \fancyfoot and \renewcommand\footrulewidth lines inside each style.

\documentclass[12pt,twoside]{article}
\usepackage{fancyhdr}
\usepackage[a4paper,margin=2cm,includeheadfoot,headheight=44pt]{geometry}
\usepackage{lipsum}

\fancypagestyle{odd}{ % 
\fancyhf{} 
\fancyhead[OL]{COURSE}
\fancyhead[OR]{
    \begin{tabular}{ c c }
        NAME  & NO  \\
        NAME1 & xxx \\
        NAME2 & yyy \\
    \end{tabular}
    }
\renewcommand{\footrulewidth}{0.4pt} 
\fancyfoot[C]{\thepage}
}

\fancypagestyle{even}{ %
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}  
\renewcommand{\footrulewidth}{0.4pt}
\fancyfoot[C]{\thepage}
}

\begin{document}

\thispagestyle{odd}

\lipsum[1-2]

\newpage

\thispagestyle{even}
\newgeometry{margin=2cm,includefoot,headheight=0pt} %Just \includefoot

\lipsum[1-2] 

\newpage

\thispagestyle{odd} 
\newgeometry{margin=2cm,includeheadfoot,headheight=44pt}

\lipsum[1-2]  

\end{document} 

enter image description here enter image description here enter image description here

The first page is messed up because the \headheight is by default 12pt but should be 44pt in this case. Is there any way to calculate this automatically? I would like to avoid typing 44pt manually.

I don't know any way to avoid calculate this manually. Just compile your document and wait for the log message referring to the \headheight and set that lenght as minimum.