LaTeX forum ⇒ Page LayoutAdd a horizontal Line in the Header

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

Add a horizontal Line in the Header

Postby Cham » Tue Jun 28, 2011 6:36 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.

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
localghost
Site Moderator
Posts: 9206
Joined: Fri Feb 02, 2007 12:06 pm
Location: Braunschweig, Germany

Postby localghost » Tue Jun 28, 2011 6:54 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.
  1. \documentclass[11pt,a4paper,english]{book}
  2. \usepackage[T1]{fontenc}
  3. \usepackage[utf8]{inputenc}
  4. \usepackage{babel}
  5. \usepackage[pagestyles,raggedright]{titlesec}
  6. \usepackage{blindtext}
  7.  
  8. \newpagestyle{main}{%
  9. \sethead[\thepage][][\chaptername\ \thechapter. \chaptertitle]{\thesection\ \sectiontitle}{}{\thepage}
  10. \headrule
  11. }
  12. \pagestyle{main}
  13.  
  14. \begin{document}
  15. \blinddocument
  16. \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
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 13.1 (Linux 3.11.10), TeX Live 2013 (vanilla), TeXworks 0.5 (r1351)
² Posting stopped indefinitely due to offenses

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
Cham
Posts: 591
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Tue Jun 28, 2011 7:08 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 ?
  1. \usepackage[]{titlesec}
  2. \newpagestyle{main}{%
  3. \sethead[\thepage][][\chaptername\ \thechapter. \chaptertitle]{\thesection\ \sectiontitle}{}{\thepage}
  4. \headrule
  5. }


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

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
localghost
Site Moderator
Posts: 9206
Joined: Fri Feb 02, 2007 12:06 pm
Location: Braunschweig, Germany

Postby localghost » Tue Jun 28, 2011 7:25 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
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 13.1 (Linux 3.11.10), TeX Live 2013 (vanilla), TeXworks 0.5 (r1351)
² Posting stopped indefinitely due to offenses

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
Cham
Posts: 591
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Tue Jun 28, 2011 7:44 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)

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
localghost
Site Moderator
Posts: 9206
Joined: Fri Feb 02, 2007 12:06 pm
Location: Braunschweig, Germany

Postby localghost » Tue Jun 28, 2011 8:07 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.
  1. \documentclass[11pt,a4paper,english]{book}
  2. \usepackage[T1]{fontenc}
  3. \usepackage[utf8]{inputenc}
  4. \usepackage{babel}
  5. \usepackage[pagestyles,raggedright]{titlesec}
  6. \usepackage{blindtext}
  7.  
  8. \newpagestyle{main}{%
  9. \sethead[\thepage][][\itshape\MakeUppercase{\chaptername\ \thechapter. \chaptertitle}]{\itshape\MakeUppercase{\thesection\ \sectiontitle}}{}{\thepage}
  10. \headrule
  11. }
  12. \pagestyle{main}
  13.  
  14. \begin{document}
  15. \blinddocument
  16. \end{document}
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 13.1 (Linux 3.11.10), TeX Live 2013 (vanilla), TeXworks 0.5 (r1351)
² Posting stopped indefinitely due to offenses

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
Cham
Posts: 591
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Tue Jun 28, 2011 11:29 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.

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
Cham
Posts: 591
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Wed Jun 29, 2011 2:34 am

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 ?
  1. \documentclass[12pt,letterpaper,twoside]{book}
  2. \usepackage[T1]{fontenc}
  3. \usepackage{lmodern}
  4. \usepackage[utf8]{inputenc}
  5. \usepackage[french]{babel}
  6.  
  7. \usepackage[]{titlesec}
  8. \raggedbottom
  9. \usepackage[bottom]{footmisc}
  10.  
  11. \pagestyle{headings}
  12.  
  13. %%%% End of preamble
  14.  
  15. \begin{document}
  16. \frontmatter
  17.  
  18. \thispagestyle{empty}
  19.  
  20. \newpage
  21.  
  22. \tableofcontents
  23.  
  24. \mainmatter
  25. \newpagestyle{main}{
  26. \sethead[\thepage][][\itshape\MakeUppercase{\chaptername\ \thechapter. \chaptertitle}]{\itshape\MakeUppercase{\thesection\ \sectiontitle}}{}{\thepage}
  27. \headrule
  28. }
  29. \pagestyle{main}
  30.  
  31. \input{Chapitre1}
  32. \input{Chapitre2}
  33.  
  34. \backmatter
  35. \pagestyle{headings}
  36.  
  37. \appendix
  38.  
  39. \begin{thebibliography}{999}
  40. \input{Bibliographie}
  41. \end{thebibliography}
  42.  
  43. \end{document}

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
localghost
Site Moderator
Posts: 9206
Joined: Fri Feb 02, 2007 12:06 pm
Location: Braunschweig, Germany

Postby localghost » Wed Jun 29, 2011 4:52 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
  1. \documentclass[12pt,english]{book}
  2. \usepackage[T1]{fontenc}
  3. \usepackage[utf8]{inputenc}
  4. \usepackage{babel}
  5. \usepackage[pagestyles,raggedright]{titlesec}
  6. \usepackage{blindtext}
  7.  
  8. \newpagestyle{front}{%
  9. \sethead[\thepage][][\itshape\MakeUppercase{\chaptertitle}]{\itshape\MakeUppercase{\sectiontitle}}{}{\thepage}
  10. \headrule
  11. }
  12. \newpagestyle{main}{%
  13. \sethead[\thepage][][\itshape\MakeUppercase{\chaptername\ \thechapter. \chaptertitle}]{\itshape\MakeUppercase{\thesection\ \sectiontitle}}{}{\thepage}
  14. \headrule
  15. }
  16. \newpagestyle{back}{%
  17. \sethead[\thepage][][\itshape\MakeUppercase{\chaptertitle}]{\itshape\MakeUppercase{\sectiontitle}}{}{\thepage}
  18. \headrule
  19. }
  20.  
  21. \begin{document}
  22. \frontmatter
  23. \pagestyle{front}
  24. \tableofcontents
  25. \listoffigures
  26. \listoftables
  27.  
  28. \mainmatter
  29. \pagestyle{main}
  30. \blinddocument
  31.  
  32. \backmatter
  33. \pagestyle{back}
  34. \begin{thebibliography}{99}
  35. \bibitem{key} Bibliography Item
  36. \end{thebibliography}
  37. \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
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 13.1 (Linux 3.11.10), TeX Live 2013 (vanilla), TeXworks 0.5 (r1351)
² Posting stopped indefinitely due to offenses

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
Cham
Posts: 591
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Wed Jun 29, 2011 7:46 pm

Thanks a lot for your precious help ! :)

Link:
BBcode:
HTML:
Hide post links
Show post links


Return to “Page Layout”

Who is online

Users browsing this forum: Google [Bot] and 5 guests