# Unnumbered sections and section marks

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}}
\hdrfont\textbf{\leftmark\\\rightmark}}}
\fancyfoot[L]{\rule[-0.25in]{0pt}{0.25in}\parbox{0.9\textwidth}{%
\hdrfont\textbf{\leftmark\\\rightmark}}}
\fancyfoot[R]{\hdrfont\thepage\ /~\pageref{LastPage}}
\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}}
\textbf{\leftmark\\\rightmark}}}
\fancyfoot[L]{\rule[-0.25in]{0pt}{0.25in}\parbox{0.9\textwidth}{%
\textbf{\leftmark\\\rightmark}}}
\fancyfoot[R]{ \thepage}
\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}

• please provide a complete MWE starting with \documentclass and ending with \end{document}, else people willing to help have to compose it themselves. – user4686 Mar 29 '14 at 15:11

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}}

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

\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.

• Thanks, works fine. It seems like I still need to add addcontentsline in unnumbered sections, but I don't mind to do that. Thanks for unrelated fixes. Seems like you partially answered to my another question (tex.stackexchange.com/questions/168413/…). – user14416 Mar 29 '14 at 18:35
• @user14416 Just add \addcontentsline{toc}{section}{#3} after \markboth{#3}{} in the code. – egreg Mar 29 '14 at 18:37

This uses \pretocmd to patch a suitable internal latex2e macro involved in typesetting starred sections.

\documentclass{article}
\usepackage{hyperref}% checking compatibility
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % clear all header and footers

\let\hdrfont\relax % don't know what this font is supposed to be
\AtEndDocument{\label{LastPage}} % very deficient work-around for LastPage which comes from some package.

\renewcommand{\sectionmark}[1]{\markboth{\thesection\ #1}{}}
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}
\hdrfont\textbf{\leftmark\\\rightmark}}}
\fancyfoot[L]{\rule[-0.25in]{0pt}{0.25in}\parbox{0.9\textwidth}{%
\hdrfont\textbf{\leftmark\\\rightmark}}}
\fancyfoot[R]{\hdrfont\thepage\ /~\pageref{LastPage}}
\renewcommand{\footrulewidth}{0.4pt}

\providecommand\phantomsection{}

\usepackage{etoolbox}
\makeatletter
\def\@@@SECTION{section}
\pretocmd{\@startsection}{\def\@@@SECTIONING{#1}}{}{}
\pretocmd{\@ssect}{%
\expandafter\in@\expandafter{\@@@SECTIONING}{section,subsection}%
\ifin@
\phantomsection
\ifx\@@@SECTIONING\@@@SECTION
\markboth{#5}{}%
\else\markright{#5}%
\fi
\fi}{}{}
\makeatother
\begin{document}

\tableofcontents
\section*{Introduction}

one\newpage
\section*{Unnumbered}

two\newpage
\section{Numbered}

three\newpage

\section{Again Numbered}

four
\end{document}

• I have \usepackage[compact]{titlesec} probably and it spoils the solution. Any workaround? – user14416 Mar 29 '14 at 15:50
• @user14416 If you use titlesec, you should add it to your example. – egreg Mar 29 '14 at 17:52