[Tex/LaTex] titlesec footers and headers

header-footertitlesectitletoc

I've read through the titlesec package, and I don't understand why my headers don't appear (in chapters, not TOC or intro) and why the page numbers on the first page of each chapter are in the center and not on the right.

MWE:

\documentclass{book}
\usepackage{lipsum}

\usepackage[compact,pagestyles]{titlesec}
\usepackage{titletoc}

        %page numbering and headers
        \newpagestyle{mystyle}{
            \setfoot[\thepage][][]{}{}{\thepage}
            \sethead[\chaptertitle][][]{}{}{\chaptertitle}
            }
        \pagestyle{mystyle}
        %

%creates the chapter heading style  
\titleformat{\chapter}[display]
    {\normalfont\huge\filcenter\bfseries\rmfamily}
    {\titlerule[1pt]%
    \vspace{2pt}%
    \titlerule
    \vspace{1pc}%
    \LARGE\MakeUppercase{\chaptertitlename} \thechapter}
    {1pc}
    {\titlerule[1pt]
    \vspace{0.5pc}%
    \huge\bfseries\rmfamily}
    %   

%to get rid of headers on blank page after clelardoublepage 
%https://tex.stackexchange.com/a/323284/121944
\makeatletter
\def\cleardoublepage{%
    \clearpage\if@twoside \ifodd\c@page\else%
     \setlength{\columnseprule}{0pt}\thispagestyle{empty}\hbox{}\newpage\setlength{\columnseprule}{1pt}%
     \if@twocolumn\setlength{\columnseprule}{0pt}\hbox{}\newpage\setlength{\columnseprule}{1pt}\fi%
    \fi\fi%
}
\makeatother

\begin{document}

\cleardoublepage
\pagenumbering{gobble}
\tableofcontents

\frontmatter
\chapter{Introduction}

\mainmatter
\chapter{}
    \lipsum[1-10]
\chapter{}
    \lipsum[11-20]
\end{document}

Best Answer

Here is a solution (hopefully): I define a mystylefrontand a mystylemain pagestyles, for frontmatter and mainmatter. The first style displays the chapter title in the header, and the second displays \chaptertitlename \thechapter, followed by the chapter title if there's any. The code can easily be simplified if no chapter in mainmatter has a title.

Note that, for empty pages, you don't have to patch whatever: simply load the option [clearempty]. If you want empty pages to have a page number printed, remove this option.

\documentclass{book}
\usepackage{lipsum}

\usepackage[pagestyles]{titlesec}
\usepackage{titletoc}
        %page numbering and headers
        \newpagestyle{mystylefront}{ % for frontmatter
            \setfoot[\thepage][][]{}{}{\thepage}
          \sethead[\chaptertitle][][]{}{}{\chaptertitle}
          }
        \newpagestyle{mystylemain}{ % for mainmatter
        \setfoot[\thepage][][]{}{}{\thepage}
          \sethead[\chaptertitlename\,\thechapter~\chaptertitle][][]{}{}{\chaptertitlename\,\thechapter~\chaptertitle}
          }
\renewpagestyle{plain}{%
\sethead{}{}{}
\setfoot[\thepage][][]{}{}{\thepage}
}

%creates the chapter heading style
\titleformat{\chapter}[display]
    {\normalfont\huge\filcenter\bfseries\rmfamily}
    {\titlerule[1pt]%
    \vspace{2pt}%
    \titlerule
    \vspace{1pc}%
    \LARGE\MakeUppercase{\chaptertitlename} \thechapter}
    {1pc}
    {\titlerule[1pt]
    \vspace{0.5pc}%
    \huge\bfseries\rmfamily}

\begin{document}

\pagestyle{empty}
\tableofcontents%
\thispagestyle{empty}

\frontmatter
        \pagestyle{mystylefront}
\chapter{Introduction}
 \lipsum[1-5]
\mainmatter
        \pagestyle{mystylemain}
\chapter{}
    \lipsum[1-10]
\chapter{}
    \lipsum[11-20]

\end{document} 

enter image description here enter image description here

Related Question