[Tex/LaTex] Unnumbered sections and section marks

header-footersectioning

In my document I have numbered and unnumbered sections.
Unnumbered sections look like this:

\section*{Introduction}

I use fancyhdr and add section and subsection marks like this.

\pagestyle{fancy}
\fancyhf{} % clear all header and footers
\renewcommand{\sectionmark}[1]{\markboth{\thesection\ #1}{}}
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}
\fancyhead[L]{\rule[-0.25in]{0pt}{0.25in}\parbox{0.9\textwidth}{%
  \hdrfont\textbf{\leftmark\\\rightmark}}}
\fancyhead[R]{\hdrfont\thepage\ /~\pageref{LastPage}}
\fancyfoot[L]{\rule[-0.25in]{0pt}{0.25in}\parbox{0.9\textwidth}{%
  \hdrfont\textbf{\leftmark\\\rightmark}}}
\fancyfoot[R]{\hdrfont\thepage\ /~\pageref{LastPage}}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

As a result, the unnumbered sections are ignored in headers.

I tried to use this command in unnumbered sections

\sectionmark{Introduction}

Now, I have 0 index for these sections in headers.

How to fix this?
Any solutions to create unnumbered section which would be treated as numbered ones (without sectionmark, addcontentsline, …)?

MWE

\documentclass[12pt]{article}

\usepackage{lipsum}
\usepackage{fancyhdr}

% -----------------------------------------------------------------------------
\pagestyle{fancy}
\fancyhf{} % clear all header and footers
\renewcommand{\sectionmark}[1]{\markboth{\thesection\ #1}{}}
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}
\fancyhead[L]{\rule[-0.25in]{0pt}{0.25in}\parbox{0.9\textwidth}{%
  \textbf{\leftmark\\\rightmark}}}
\fancyhead[R]{\thepage}
\fancyfoot[L]{\rule[-0.25in]{0pt}{0.25in}\parbox{0.9\textwidth}{%
  \textbf{\leftmark\\\rightmark}}}
\fancyfoot[R]{ \thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

\begin{document}
\tableofcontents
\section{Section1}
\lipsum[4-8]
\newpage
\section*{Section2}
\lipsum[4-8]
\newpage
\section{Section3}
\lipsum[4-8]
\newpage
\section*{Section4}
\lipsum[4-8]
\end{document}

Best Answer

Issue the appropriate \markboth command when doing \section*; the easiest method, without plunging in the depth of the definition of \@startsection, which won't probably work with titlesec, is to redefine \section with the help of xparse:

\documentclass[12pt]{article}

\usepackage[compact]{titlesec}
\usepackage{fancyhdr}
\usepackage{lipsum}

\usepackage{xparse}
\let\CLASSsection\section

\RenewDocumentCommand{\section}{som}{%
  \IfBooleanTF{#1}
   {% there's a *
    \CLASSsection*{#3}\markboth{#3}{}%
   }
   {% no *
    \IfNoValueTF{#2}
     {% no opt arg
      \CLASSsection{#3}%
     }
     {% opt arg
      \CLASSsection[#2]{#3}%
     }%
   }%
}

\makeatletter
% fix \tableofcontents
\renewcommand{\tableofcontents}{%
  \section*{\contentsname}%
  \@starttoc{toc}%
}
\makeatother


% -----------------------------------------------------------------------------
\pagestyle{fancy}
\fancyhf{} % clear all header and footers
\renewcommand{\sectionmark}[1]{\markboth{\thesection\ #1}{}}
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}

\fancyhead[L]{%
  \bfseries\begin{tabular}{@{}l@{}}\mbox{}\leftmark\\ \mbox{}\rightmark\end{tabular}%
}
\fancyhead[R]{\thepage}
\fancyfoot[L]{%
  \bfseries\begin{tabular}{@{}l@{}}\mbox{}\leftmark\\ \mbox{}\rightmark\end{tabular}%
}
\fancyfoot[R]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

\setlength{\headheight}{30pt}

\begin{document}
\tableofcontents
\clearpage
\section{Section1}
\lipsum[4-8]
\newpage
\section*{Section2}
\lipsum[4-8]
\newpage
\section{Section3}
\lipsum[4-8]
\newpage
\section*{Section4}
\lipsum[4-8]
\end{document}

I've also redefined how you set the headers and footers; a tabular is surely better than having loads of Underfull \hbox messages when there's no right mark to typeset. Also \tableofcontents needs to be redefined, because it normally issues \@mkboth (an alias for \markboth).

Don't forget to follow fancyhdr's advice about increasing the \headheight; with this setting it requires just a bit less than 30pt.

Related Question