10

I want the header to be empty in the page were section begins and showed in the other pages. How can I do that?

\documentclass[12pt, titlepage, onecolumn, a4paper]{article}

\usepackage{lipsum}    %% for dummy text

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markboth{#1}{}} % set the \leftmark
\fancyhf{}

\lhead{\leftmark}
\chead{}
\rhead{\rightmark}
\cfoot{\footnotesize\selectfont\bfseries\thepage}

\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
\setlength{\headheight}{8pt}

\renewcommand{\thefigure}{\thesection.\arabic{figure}}
\renewcommand{\thetable}{\thesection.\arabic{table}}

\begin{document}
\lipsum[1]
  \pagenumbering{roman}
  \setcounter{page}{2}
\lipsum[1]
  \rhead{\footnotesize{\thesection{ } \leftmark}}
  \pagenumbering{arabic}
  \setcounter{page}{1}
\section{First Section}
\lipsum[2-5]
\section{Second Section}
\lipsum[2-8]
\end{document}
5

I'm not sure what do you mean by empty but if you are looking for the normal layout without the text of the header you can add this pagestyle to your preamble:

\fancypagestyle{FooBar}{%
    \fancyhead{}
    \renewcommand{\headrulewidth}{1pt}
}

and then start your sections by declaring

\section{Second Section}\thispagestyle{FooBar}

This will give you

enter image description here

If you literally need an empty layout, you can use \thispagestyle{empty} as it is already defined by the package (an other predefined style is plain).

  • it was the FooBar solution what I was looking for. Thank you very much! – Kaiser Schwarcz Jul 3 '14 at 14:07
5

You can make the choice automatic, by adding some code to \section

\documentclass[12pt, titlepage, onecolumn, a4paper]{article}
\usepackage{fancyhdr}
\usepackage{chngcntr}
\usepackage{xparse}

\usepackage{lipsum}    %% for dummy text

\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markboth{#1}{}} % set the \leftmark
\fancyhf{}
\fancyhead[L]{\leftmark}
\fancyfoot[C]{\footnotesize\bfseries\thepage}

\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

\setlength{\headheight}{14.5pt} % <-------- 8pt is too small!

\fancypagestyle{section}{%
  \fancyhf{}%
  \fancyfoot[C]{\footnotesize\bfseries\thepage}
  \renewcommand{\headrulewidth}{0.4pt}%
  \renewcommand{\footrulewidth}{0.4pt}%
}


\counterwithin{figure}{section}
\counterwithin{table}{section}

% the starting page of a section will not have a header
\let\originalsection\section
\RenewDocumentCommand{\section}{som}{%
  \IfBooleanTF{#1}
    {\originalsection*{#3}}
    {\IfNoValueTF{#2}
      {\originalsection{#3}}
      {\originalsection[#2]{#3}}%
    }%
  \thispagestyle{section}%
}

\begin{document}
\lipsum[1]
\lipsum[1]
\section{First Section}
\lipsum[2-5]
\section{Second Section}
\lipsum[2-8]
\end{document}

I added also the correct size for \headheight (unless you use a smaller font 14.5pt is necessary). Also I removed the setting of \rightmark, add it if really wanted, but then you probably have to redefine \subsectionmark.

Also I defined the counters for tables and figures using chngcntr and \counterwithin; this way the counters will be reset at each section.

  • Nice answer! Unfortunately the right- and leftmarks are gone so it will alwasy print the table of contents title as title when the header is printed again. – Tim Dec 16 '16 at 21:25
  • @Tim I'm not sure what you mean. I tried with an extended example filling two pages of the TOC, but the headers are as expected. – egreg Dec 16 '16 at 21:32
  • I implemented this answer and your answer from tex-sx id:101915 together. Atleast when having it together the leftmark and rightmark is gone. Instead I always have the german translation of "toc" in my header. Before I implemented this answer it worked. But nonetheless this answer worked as expected besides the toc in header now. – Tim Dec 16 '16 at 21:37
  • If it's still not clear I can upload a MWE here. – Tim Dec 16 '16 at 21:39
  • 1
    @Tim You should ask a fresh question, linking to this answer. – egreg Dec 16 '16 at 21:44
2

It can also be achieved if you define the \fancypagestyle, as Pouya did:

\fancypagestyle{FooBar}{% 
   \fancyhead{}
   \renewcommand{\headrulewidth}{1pt}
}

and somewhere in the preamble (before \begin{document}) state:

\let \OldSection \section
\renewcommand{\section}[1]{\OldSection{#1} \thispagestyle{FooBar}}

:: Update ::

In my case, sometimes, I want to open a section on a new page, using memoir. So, I use the following tex:

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

\usepackage{lipsum}    %% for dummy text

\let\footruleskip\undefined
\usepackage{fancyhdr}

% Apply section style
\let \OldSection \section
\renewcommand{\section}[1]{\OldSection{#1} \thispagestyle{plainsec}}


% Define your page styles
\pagestyle{fancy}               
\renewcommand{\chaptermark}[1]{%
\markboth{\chaptername
\ \thechapter\ - #1}{}}
\renewcommand{\sectionmark}[1]{%
            \markright{\thesection\ - #1}}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt}

\fancypagestyle{plain}{  %chapter openning
        \fancyhf{}
        \fancyhead[RO]{\bfseries\thepage}
        \renewcommand{\headrulewidth}{0pt}
}
\fancypagestyle{plainsec}{  %section openning
        \fancyhf{}
        \fancyhead[RO]{\bfseries\thepage}
        \renewcommand{\headrulewidth}{0pt}
}


\begin{document}
\chapter{First Chapter}
\lipsum[1]
\section{A Section after some text}
\lipsum[2-5]
\subsection{A Sub-section}
\lipsum[2-5]

\cleardoublepage
\section{A Section at a new page}
\lipsum[2-8]
\end{document}
  • Please elaborate on this and show a screen shot, for example – user31729 Dec 30 '14 at 13:10
  • I add an example above. If you want, you can define a specific section command, whenever you need a section opening like a chapter usually does. – tasilva Dec 31 '14 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.