2

I know some of the main commands that control headers and footers in the LaTeX document class book which can be found under the section 6.4 of the Standard Document Classes for the LaTeX documentation, such as

\def\ps@headings{%
    \let\@oddfoot\@empty\let\@evenfoot\@empty
    \def\@evenhead{\thepage\hfil\slshape\leftmark}%
    \def\@oddhead{{\slshape\rightmark}\hfil\thepage}%
    \let\@mkboth\markboth
    \def\chaptermark##1{%
        \markboth {\MakeUppercase{%
            \ifnum \c@secnumdepth >\m@ne
                \if@mainmatter
                    \@chapapp\ \thechapter. \ %
                \fi
            \fi
            ##1}}{}}%
    \def\sectionmark##1{%
        \markright {\MakeUppercase{%
            \ifnum \c@secnumdepth >\z@
                \thesection. \ %
            \fi
        ##1}}}}

But how to generate decorations such as a headrule (or footrule) as fancyhdr does (I tried inserting \hrulefill but it did not work)? How to put the name of the subsection e.g. on left pages headers? I'm just interested on how to do the basic things without calling other packages, also because I want to have a certain control on the output.

I know it's not so specific, but maybe with some examples I can elaborate some ideas on how to edit headings without external packages.

9
  • 3
    Don't reinvent the wheel It is much easier to customise headers and footers with one of the companion packages of titlesec: titleps, which can be used independently from titlesec. No juggling with leftmarks, rightmarks and the like.
    – Bernard
    Jun 22, 2018 at 15:03
  • I can understand it, but since I started modifying on my own pre-defined commands trying not to use packages I got much better results, so I would like to do this acting directly on the definitions.
    – GiuTeX
    Jun 22, 2018 at 15:20
  • 3
    Yet, you're using LaTeX, a very large TeX macros package, not plain TeX…
    – Bernard
    Jun 22, 2018 at 15:26
  • 1
    The memoir class has inbuilt commands for customising headers and footers. See Pagination and headers in the manual (> texdoc memoir). Jun 22, 2018 at 18:24
  • 1
    Since you question is basically how to do what fancyhdr, memoir or whatever does without loading them, why don't you look at how they do it? Fine if you want to reinvent the wheel, but there doesn't seem any point in asking other people to reinvent it for you. Just take the wheel you've got apart, twiddle it a bit and put it back together in its new octagonal form. (But don't complain if it doesn't work as well as the circular one you dismantled.)
    – cfr
    Jun 23, 2018 at 1:36

1 Answer 1

2

I globally agree with Bernards comment. And why not use the fancyhdr or titlepspackages ?

generate decorations such as a headrule (or footrule) as fancyhdr does

IMO, headrule or footrule are evil

How to put the name of the subsection e.g. on left pages headers?

Have you had a try to \pagestyle{myheadings} ?

Here are some ideas, assuming that you have used neither titlesec nor any other pagestyle packages, and use the standard class book with customized \pagestyle{headings}. It uses mostly the patching commads of etoolbox:

\documentclass{book}
\usepackage{etoolbox}
\begin{document} 
\makeatletter
  % define headrule and footrule
  \long\def\myline{\linebreak\hb@xt@\textwidth{\hrulefill\par}}
  % collect names from sectioning commands
  \apptocmd{\@chapter}{\gdef\currentchaptername{#1}}{}{}
  \apptocmd{\@schapter}{\gdef\currentchaptername{#1}}{}{}
  \apptocmd{\@sect}{{\ifnum#2=1\gdef\currentsectionname{#7}\else
  \ifnum#2=2 \gdef\currentsubsectionname{#7}\fi\fi}}{}{} 
  % enable multiline header
  \patchcmd{\@outputpage}{\hb@xt@\textwidth{\@thehead}}{\parbox{\textwidth}{\@thehead}}{}{}
  % redefine the \ps@headings macro by inserting \myline and \currentsubsectionname
  \def\ps@headings{%
    \let\@oddfoot\myline \let\@evenfoot\myline
    \ifundef{\currentsectionname}{\def\currentsectionname{}}{}
    \ifundef{\currentsubsectionname}{\def\currentsubsectionname{}}{}
    \long\def\@evenhead{\thepage\quad \slshape\leftmark\hfill \currentsectionname\hfill \currentsubsectionname\myline}%
    \long\def\@oddhead{{\slshape\rightmark}\hfil\thepage\myline}%
    \let\@mkboth\markboth
    \def\chaptermark##1{%
       \markboth {\MakeUppercase{%
         \ifnum \c@secnumdepth >\m@ne \if@mainmatter \@chapapp\ \thechapter. \ % \fi \fi
          ##1}}{}}%
    \def\sectionmark##1{%
       \markright {\MakeUppercase{%
         \ifnum \c@secnumdepth >\z@ \thesection. \ \fi
          ##1}}}}
\makeatother

\begin{document}
\pagestyle{headings}

This patching does't alter the toc, and also works with babel, and with hyperref provided it is loaded after.

1
  • I see there's no simple answer and customisation of headers without fancyhdr. I naively thought that there would have been a simple solution, but seems that lot of workaround is needed instead. Thanks anyway for your nice solution!
    – GiuTeX
    Jun 26, 2018 at 19:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .