4

I have the following code:

\documentclass{article}

\makeatletter
\renewcommand\ps@headings{
  \def\@evenhead{foo bar}
  \def\@oddhead{foo bar}
}
\makeatother
\begin{document}
\pagestyle{headings}

\section{Introduction}
foo bar baz

\end{document}

(This is just an example. The real document is a bit more complicated...)

How can I have a rule below the »foo bar« header?

I'd like to get something like this:

header with horizontal rule

That is: a rule size \hsize. I don't want to use fancyhdr.

Edit: More requirements: I need to configure the rule thickness and the distance between the words »foo ... bar« and the horizontal rule.

2
  • Would you consider using titleps(a sibling of titlesec)?
    – Bernard
    Commented Mar 27, 2015 at 13:46
  • @Bernard no... The problem is, that I have a medium complex document class with already predefined headers and footers. I don't want to add another layer of complexity on top of it, so I wanted to have a solution based on the LaTeX's commands for headers.
    – topskip
    Commented Mar 27, 2015 at 14:06

3 Answers 3

2

My \myhrule for rules in headers looks slight different than David's. The position of the heading text doesn't depend on the depth of the text. And, of course, I needn't any additional package.

\makeatletter
\def\myhrule{\lower1ex\null\vadjust{\hrule}}
\renewcommand\ps@headings{%
  \def\@evenhead{foo\hfill bar\myhrule}%
  \def\@oddhead{foo\hfill bar\myhrule}%
}
\makeatother
6
  • yes this is a nice alternative in the in the non-color case. In color it fails for same reason mine did (there is an extra \hbox{{ ...}} around the heading to constrain colour specials) Although given this information you should be able to fix:-) (see my not so pretty edited answer) Commented Mar 27, 2015 at 17:39
  • Yes, the usage is simple in plain TeX: \headline={\myhrule foo\hfil bar}, but LaTeX brings complications (as usual:). When color LaTeX package is loaded then \aftergroup\myhrule works. Note: only one \aftergroup.
    – wipet
    Commented Mar 27, 2015 at 18:02
  • That's cool. I don't understand it yet, but less \aftergroups than David's solutions
    – topskip
    Commented Mar 27, 2015 at 20:16
  • 1
    @wipet David told me that your answer is better than his answer, but he also told me that I should not tell you. So ignore my comment from now and accept the fact that some uniDentified Person Came here and suggested me to use your answer.
    – topskip
    Commented Mar 27, 2015 at 20:21
  • @topskip: I see what you did there. :) I think we could agree that DPC = Donald P. Cnuth. :) Commented Mar 28, 2015 at 11:05
3

You can just under line them.

\documentclass{article}
\usepackage[normalem]{ulem}

\makeatletter
\renewcommand\ps@headings{
  \def\@evenhead{\uline{foo bar}}
  \def\@oddhead{\uline{foo bar}}
}
\makeatother
\begin{document}
\pagestyle{headings}

\section{Introduction}
foo bar baz
\clearpage
\section{Introduction}
foo bar baz

\end{document}

enter image description here

You can change the thickness and depth of the rule by

\setlength{\ULdepth}{2ex}
\renewcommand{\ULthickness}{2pt}

Same with soul:

\usepackage{soul}

\makeatletter
\renewcommand\ps@headings{%
  \def\@evenhead{\ul{foo bar}}%
  \def\@oddhead{\ul{foo bar}}%
}
\makeatother

The depth and thickness can be changed by

\setul{2ex}{2pt}

Sorry about the bad boxes. I am not responsible :-)

4
  • Looks great, thanks. This reminds me that I have more requirements. See the edit of the question (sorry about that).
    – topskip
    Commented Mar 27, 2015 at 13:26
  • Perfect thanks!! I chose the ulem way, works nicely.
    – topskip
    Commented Mar 27, 2015 at 13:33
  • @topskip I have added \setul{2ex}{2pt} for soul too. :)
    – user11232
    Commented Mar 27, 2015 at 13:33
  • Bad boxes are here because between foo and bar isn't infinitely stretchable glue, only normal space is here. You need to declare \def\@evenhead{\uline{foo\hfil bar}} but soul package doesn't work with this.
    – wipet
    Commented Mar 27, 2015 at 13:35
3

You can simply add a rule after the box that holds the heading:

enter image description here

\documentclass{article}

\makeatletter
\def\myhrule{\vskip1ex\hrule height 5pt}
\renewcommand\ps@headings{%
  \def\@evenhead{foo\hfill bar\aftergroup\myhrule}%
  \def\@oddhead{foo\hfill bar\aftergroup\myhrule}%
}
\makeatother
\begin{document}
\pagestyle{headings}

\section{Introduction}
foo bar baz

\end{document}

Or if you have color loaded

\documentclass{article}

\usepackage{color}

\makeatletter
\def\myhrule{\vskip1ex\hrule height 5pt}
\renewcommand\ps@headings{%
  \def\@evenhead{foo\hfill bar\aftergroup\aftergroup\aftergroup
\aftergroup\aftergroup\aftergroup\aftergroup\myhrule}
  \def\@oddhead{foo\hfill bar\aftergroup\aftergroup\aftergroup
\aftergroup\aftergroup\aftergroup\aftergroup\myhrule}
}
\makeatother
\begin{document}
\pagestyle{headings}

\section{Introduction}
foo bar baz

\end{document}
4
  • Looks much better, but it doesn't work with my real document.... ! Missing \endgroup inserted. <inserted text> \endgroup . I am not too keen on reducing the problem right now.
    – topskip
    Commented Mar 27, 2015 at 14:28
  • @topskip fixed (My earlier self added extra groups to latex's heading code to control colour specials so my current self added a few extra \aftergroup to escape) Commented Mar 27, 2015 at 15:22
  • @topskip see tex.stackexchange.com/a/232003/1090 Commented Mar 27, 2015 at 20:05
  • OK, trivial then. I understand ;) (No, I think I understand why the many \aftergroup, but I would not have found out about the extra group the color package adds).
    – topskip
    Commented Mar 27, 2015 at 20:09

You must log in to answer this question.

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