# Align horizontal footer rule to outside margin with memoir class

I'm using the memoir document class with the twoside option, and want to use a \textwidth horizontal rule underneath my header, but a smaller horizontal rule above my footer. Unfortunately, the footer rule is always aligned to the left margin, whereas I want it aligned to the outer margin, above the page number.

Minimal working example:

\documentclass[twoside]{memoir}

\begin{document}

Test.
\newpage
Test.
\newpage
Test.

\end{document}


As you can see, page 2 has the line nicely above the page number, while pages 1 and 3 have the line on the other side.

I have tried to add \makeheadposition{myheadings}{}{}{\flushleft}{\flushright}, but that did not change anything.

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Mar 26 '15 at 12:43

We first have to load the etoolbox package and patch the definition of \makefootrule:

\usepackage{etoolbox}
\makeatletter
\patchcmd{\makefootrule}
{\hrule\@width #2\@height #3 }
{\rule{#2}{#3}}
{}
{}
\makeatother


Then we define \makeheadfootruleprefix to add the needed space in odd pages:

\makeheadfootruleprefix{myheadings}{}{%
\checkoddpage\ifoddpage\hspace*{\dimexpr\textwidth-2cm\relax}\fi}


That's all.

MWE:

\documentclass[twoside]{memoir}

\usepackage{etoolbox}
\makeatletter
\patchcmd{\makefootrule}
{\hrule\@width #2\@height #3 }
{\rule{#2}{#3}}
{}
{}
\makeatother

\checkoddpage\ifoddpage\hspace*{\dimexpr\textwidth-2cm\relax}\fi}

\begin{document}

Test.
\newpage
Test.
\newpage
Test.

\end{document}


Output:

Notice that I've adjusted \makefootrule to have a -4pt skip

\makefootrule{myheadings}{2cm}{\normalrulethickness}{-4pt}


since \rule is placed higher than \hrule.

• Nice solution, but you are aware that \rule and \hrule are not placed the same?, \rule sits a few mms higher. – daleif Mar 26 '15 at 14:09
• Excellent, thank you. However, the solution does not quite work within a multicols environment that stretches multiple pages; the rule is flushed to the right of the first column then. Any thoughts on how to get it to the right of the page instead of the column? (Again I tried to fix it myself, but got stuck.) – EelkeSpaak Mar 26 '15 at 14:12
• @EelkeSpaak I've changed the answer and now it works even if you use multicols. – karlkoeller Mar 26 '15 at 16:09
• @daleif Yes, you're right. Added a fix for that in the answer. – karlkoeller Mar 26 '15 at 16:10
• @karlkoeller Great solution, thank you. I tried something with \hspace*{} myself, but could not get it to work. – EelkeSpaak Mar 26 '15 at 16:25