0

I am working with the following code

    \documentclass[12pt]{scrreprt}

    \usepackage{atveryend}
    \usepackage{etoolbox}
    \usepackage{titlesec}
    \usepackage{fancyhdr}
    \usepackage{enumitem}
    \usepackage{fancyhdr}

    \titleformat{\chapter}{\normalfont\Large\bfseries}{\thechapter}{1em}{}
    \titleformat{\section}{\normalfont\large\bfseries}{\thesection}{1em}{}
    \titleformat{\subsection}{\normalfont\bfseries}{\thesubsection}{1em}{}
    \titleformat{\subsubsection}{\normalfont\fontsize{10}{0}\bfseries}{\thesubsubsection}{1em}{}
    \titleformat{\paragraph}{\normalfont\fontsize{10}{0}\bfseries}{\theparagraph}{1em}{}

    \titlespacing*{\chapter}{0pt}{0pt}{14pt}
    \titlespacing*{\section}{0pt}{14pt}{7pt}
    \titlespacing*{\subsection}{0pt}{14pt}{0pt}
    \titlespacing*{\subsubsection}{0pt}{7pt}{0pt}
    \titlespacing*{\paragraph}{0pt}{14pt}{0pt}
    \titlespacing*{\bhead}{0pt}{14pt}{0pt}

    \makeatletter
    \newlength{\secnumwidth}
    \setlength{\secnumwidth}{0pt}
    \providecommand*{\usesecnumwidth}{0pt}

    \newcommand*{\secnumwidthbox}[1]{%
    \begingroup
    \sbox0{#1}%
    \ifdim\wd0>\secnumwidth
    \global\secnumwidth=\wd0\relax
    \fi
    \endgroup
    \leavevmode
    \hbox to 
    \ifdim\usesecnumwidth>\secnumwidth
    \usesecnumwidth
    \else
    \secnumwidth
    \fi
    {#1\hfil}%
     }

     \let\org@chapterformat\chapterformat
     \renewcommand*{\chapterformat}{%
     \secnumwidthbox{\org@chapterformat}%
     }

     \renewcommand*{\othersectionlevelsformat}[3]{%
     \secnumwidthbox{#3\autodot\hfill\enskip}%
     }

     % Patch for titlesec
     \patchcmd\ttlh@hang{%
     \sbox\z@{#2\strut\ttl@calc\hspace{#3}}%
     }{%
     \sbox\z@{\secnumwidthbox{#2\strut\ttl@calc\hspace{#3}}}%
     }{}{\errmessage{Cannot patch \string\ttlh@hang}}

     \AfterLastShipout{%
     \if@filesw
     \immediate\write\@mainaux{%
     \string\gdef\string\usesecnumwidth{\the\secnumwidth}%
     }%
     \fi
     \ifdim\usesecnumwidth=\secnumwidth
     \else
     \@latex@warning@no@line{Rerun LaTeX, because \noexpand     \usesecnumwidth
     has changed}
     \fi
     }
     \makeatother

     \begin{document}
     \chapter{Chapter}
     \section{Section}
     \subsection{Subsection}
     \newpage
     Hi
     \newpage
     Hi
     \end{document}

I wish to use the fancyhdr package to add a basic header to each page, which would consist of the page number on the right hand side of the page for odd numbered pages, and the page number on the left hand side for even numbered pages, but I would also like to have the same style of header on chapter pages. I cannot seem to get this to work, ending up with the numbers all on the left, or all on the right. I'd appreciate it if anyone can find a working code.

  • 1
    Hm. You are really trying to make your life very difficult. KOMA isn't easy to understand and adapt anyway but the combination with titlesec and fancyhdr will give you a lot of headache. E.g. your redefinition of \othersectionlevelsformat is useless. – Ulrike Fischer Dec 7 '16 at 20:20
1

Report classes like report or scrreprt are onesided by default. With

\documentclass{report}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyfoot[RO,LE]{\thepage}
\usepackage{blindtext}
\begin{document}
\Blindtext[20]
\end{document}

you get the warning

Package Fancyhdr Warning: \fancyfoot's `E' option without twoside option is use less on input line 5.

because there are only odd pages. So the page numbers are all on the right side.

If you set the twoside option as suggested in the warning

\documentclass[twoside]{report}

you get

enter image description here

The page numbers are on the left side of even pages and on the right side of odd pages.

The KOMA-Script class scrreprt additionally knows option twoside=semi:

\documentclass[twoside=semi]{scrreprt}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyfoot[RO,LE]{\thepage}
\usepackage{blindtext}
\begin{document}
\Blindtext[20]
\end{document}

Result:

enter image description here


Additional remark: It is not recommended to use package titlesec together with a KOMA-Script class.

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.