## LaTeX forum ⇒ Page Layout ⇒ Add a horizontal Line in the Header

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Cham
Posts: 936
Joined: Sat Apr 02, 2011 4:06 pm

I'm currently using the "book" document class (the document is a huge book of about 900 pages), and the chapter/section titles and page number are showing in the header, which is what I want.

However, I would like to add a simple horizontal line, as what we have under the "paper" document class. How can I do that ?
Last edited by Cham on Wed Jun 29, 2011 7:46 pm, edited 3 times in total.

localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm
This is usually done by packages like fancyhdr or titlesec. The below example uses the latter one because it is capable of much more things than only page styles.
\documentclass[11pt,a4paper,english]{book}\usepackage[T1]{fontenc}\usepackage[utf8]{inputenc}\usepackage{babel}\usepackage[pagestyles,raggedright]{titlesec}\usepackage{blindtext} \newpagestyle{main}{%   \sethead[\thepage][][\chaptername\ \thechapter. \chaptertitle]{\thesection\ \sectiontitle}{}{\thepage}   \headrule}\pagestyle{main} \begin{document}   \blinddocument\end{document}

You can additionally declare other page styles for front and back matter of your book. That's up to you. For details see the package manual.

As always, the blindtext package is only for creating dummy text, thus not part of the solution.

Thorsten
LaTeX Community Moderator

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

Cham
Posts: 936
Joined: Sat Apr 02, 2011 4:06 pm
localghost wrote:The below example uses the latter one because it is capable of much more things than only page styles.
...

Cool ! It works (however, it clashed with my previous \pagestyle{headings} command).

However, the header's titles aren't in small caps. How should I modify the following code to make all header's titles in small caps ?
\usepackage[]{titlesec}\newpagestyle{main}{%   \sethead[\thepage][][\chaptername\ \thechapter. \chaptertitle]{\thesection\ \sectiontitle}{}{\thepage}   \headrule}

EDIT : Ok, I've found a solution to the previous question. But is this the proper way to do this ?
\usepackage[]{titlesec}\newpagestyle{main}{%   \sethead[\thepage][][\chaptername\ \textbf{\thechapter}. \textsc{\chaptertitle}]{\thesection\ \textsc{\sectiontitle}}{}{\thepage}   \headrule}

localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm
Actually the headers by default are completely in capitals. But that's the advantage of using this package. You can easily customize the settings. Your code looks OK, although the header now looks a bit intrusive.
LaTeX Community Moderator

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

Cham
Posts: 936
Joined: Sat Apr 02, 2011 4:06 pm
localghost wrote:Actually the headers by default are completely in capitals. ... Your code looks OK, although the header now looks a bit intrusive.

Intrusive ? What do you mean ?

Without the styles in my modification, the code gives simple lowecase titles. How can I make the title in slanted capitals ? (that was the default style with the default book documentclass)

localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm
Cham wrote:Intrusive ? What do you mean ? […]

It's a bit overdone. The header shall not catch one's eye too much.
Cham wrote:[…] Without the styles in my modification, the code gives simple lowercase titles. How can I make the title in slanted capitals ? (that was the default style with the default book documentclass)

To mimic the default style, you can use the well known commands to control font styles. See the below example.
\documentclass[11pt,a4paper,english]{book}\usepackage[T1]{fontenc}\usepackage[utf8]{inputenc}\usepackage{babel}\usepackage[pagestyles,raggedright]{titlesec}\usepackage{blindtext} \newpagestyle{main}{%   \sethead[\thepage][][\itshape\MakeUppercase{\chaptername\ \thechapter. \chaptertitle}]{\itshape\MakeUppercase{\thesection\ \sectiontitle}}{}{\thepage}   \headrule}\pagestyle{main} \begin{document}   \blinddocument\end{document}
LaTeX Community Moderator

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

Cham
Posts: 936
Joined: Sat Apr 02, 2011 4:06 pm
localghost wrote:To mimic the default style, you can use the well known commands to control font styles. See the below example.

This works.

However, I noticed that there are chapters numbers 0.0 and wrong titles appearing in the header, for the frontmatter and backmatter parts.

Cham
Posts: 936
Joined: Sat Apr 02, 2011 4:06 pm
Okay, I think I have it right. Some parts of the code above don't go into the preamble. It must be placed AFTER the \mainmatter statement, and BEFORE the \backmatter. After the \backmatter command, I need to add \pagestyle{headings} to revert to the default style.

Is that right ? I need a confirmation because I don't want to built a whole sand castle of missplaced commands. After a while, the document may turn into a commands mess !

Please, can you check the structure of the following code, just to confirm that everything is properly done ?
\documentclass[12pt,letterpaper,twoside]{book}\usepackage[T1]{fontenc}\usepackage{lmodern}\usepackage[utf8]{inputenc}\usepackage[french]{babel} \usepackage[]{titlesec}\raggedbottom\usepackage[bottom]{footmisc} \pagestyle{headings} %%%% End of preamble \begin{document}\frontmatter \thispagestyle{empty} \newpage \tableofcontents  \mainmatter\newpagestyle{main}{	\sethead[\thepage][][\itshape\MakeUppercase{\chaptername\ \thechapter. \chaptertitle}]{\itshape\MakeUppercase{\thesection\ \sectiontitle}}{}{\thepage}	\headrule}\pagestyle{main} 	\input{Chapitre1}	\input{Chapitre2} \backmatter\pagestyle{headings} \appendix \begin{thebibliography}{999}	\input{Bibliographie}\end{thebibliography} \end{document}

localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm
You're quite close. Instead of using the »headings« page style I would declare new page styles for front, main and back matter. Take a look at the below example
\documentclass[12pt,english]{book}\usepackage[T1]{fontenc}\usepackage[utf8]{inputenc}\usepackage{babel}\usepackage[pagestyles,raggedright]{titlesec}\usepackage{blindtext} \newpagestyle{front}{%   \sethead[\thepage][][\itshape\MakeUppercase{\chaptertitle}]{\itshape\MakeUppercase{\sectiontitle}}{}{\thepage}   \headrule}\newpagestyle{main}{%   \sethead[\thepage][][\itshape\MakeUppercase{\chaptername\ \thechapter. \chaptertitle}]{\itshape\MakeUppercase{\thesection\ \sectiontitle}}{}{\thepage}   \headrule}\newpagestyle{back}{%   \sethead[\thepage][][\itshape\MakeUppercase{\chaptertitle}]{\itshape\MakeUppercase{\sectiontitle}}{}{\thepage}   \headrule} \begin{document}   \frontmatter   \pagestyle{front}   \tableofcontents   \listoffigures   \listoftables    \mainmatter   \pagestyle{main}   \blinddocument    \backmatter   \pagestyle{back}   \begin{thebibliography}{99}     \bibitem{key} Bibliography Item   \end{thebibliography}\end{document}

Note that the page styles »front« and »back« are the same. They have been declared in the sense of logical mark-up. This way it will be easier to modify to one of them (if desired).
LaTeX Community Moderator

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

Cham
Posts: 936
Joined: Sat Apr 02, 2011 4:06 pm
Thanks a lot for your precious help !