Page Layout ⇒ Add a horizontal Line in the Header
Add a horizontal Line in the Header
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 ?
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.
NEW: TikZ book now 40% off at Amazon.com for a short time.
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Add a horizontal Line in the Header
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.
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
Code: Select all
Code, edit and compile here:
\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}
As always, the blindtext package is only for creating dummy text, thus not part of the solution.
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Add a horizontal Line in the Header
Cool ! It works (however, it clashed with my previous \pagestyle{headings} command).localghost wrote:The below example uses the latter one because it is capable of much more things than only page styles.
...
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 ?
Code: Select all
Code, edit and compile here:
\usepackage[]{titlesec}\newpagestyle{main}{%\sethead[\thepage][][\chaptername\ \thechapter. \chaptertitle]{\thesection\ \sectiontitle}{}{\thepage}\headrule}
Code: Select all
Code, edit and compile here:
\usepackage[]{titlesec}\newpagestyle{main}{%\sethead[\thepage][][\chaptername\ \textbf{\thechapter}. \textsc{\chaptertitle}]{\thesection\ \textsc{\sectiontitle}}{}{\thepage}\headrule}
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Re: Add a horizontal Line in the Header
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.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Add a horizontal Line in the Header
Intrusive ? What do you mean ?localghost wrote:Actually the headers by default are completely in capitals. ... Your code looks OK, although the header now looks a bit intrusive.
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: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Add a horizontal Line in the Header
It's a bit overdone. The header shall not catch one's eye too much.Cham wrote:Intrusive ? What do you mean ? […]
To mimic the default style, you can use the well known commands to control font styles. See the below example.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)
Code: Select all
Code, edit and compile here:
\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}
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Add a horizontal Line in the Header
This works.localghost wrote: To mimic the default style, you can use the well known commands to control font styles. See the below example.
However, I noticed that there are chapters numbers 0.0 and wrong titles appearing in the header, for the frontmatter and backmatter parts.
Add a horizontal Line in the Header
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 ?
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 ?
Code: Select all
Code, edit and compile here:
\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}
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Add a horizontal Line in the Header
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
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).
Code: Select all
Code, edit and compile here:
\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}
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Re: Add a horizontal Line in the Header
Thanks a lot for your precious help ! 
