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 withtitlesec
, is to redefine\section
with the help ofxparse
:I've also redefined how you set the headers and footers; a
tabular
is surely better than having loads ofUnderfull \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.