[Tex/LaTex] Header style with page number under bleeded rule

book-designheader-footermemoir

I'm trying to create this header style in a memoir document (this is a mock-up in Inkscape):

goal header style

I thought of tikz, and I've seen this cool one, but I'm thinking this isn't so complex and tikz for every header is perhaps overkill.

I can't think of how to do this:

  • page number under the head rule in a color box
  • lowered head rule (default is just under the header baseline)
  • rule and page number bleeding 3mm into the margins

I have got this far:

got this far

The layout and so on code is on Github, ready to compile. The part dealing with the headers is in theheaders.sty:

\nouppercaseheads

\newlength\BOOK@bleed
\setlength{\BOOK@bleed}{3mm}

\newlength\BOOK@headWidth
\newlength\BOOK@footWidth

% TODO: header should bleed on each side
%\setlength{\BOOK@headWidth}{\textwidth + 2\foremargin + 2\BOOK@bleed}
\setlength{\BOOK@headWidth}{\textwidth + 2\foremargin}
\setlength{\BOOK@footWidth}{\textwidth}

\makepagestyle{toprightnum}

\makerunningwidth{toprightnum}[\BOOK@footWidth]{\BOOK@headWidth}
\makeheadposition{toprightnum}{center}{center}{center}{center}
\makeheadrule{toprightnum}{\BOOK@headWidth}{0.5pt}

\newcommand\headerFmt{\headerFont\headerSize\color{header}}
\newcommand\pageNumFmt{\pageNumFont\pageNumSize\color{pagenum}}

% TODO: page number should drop under the headrule with a gray
% background

\makeoddhead{toprightnum}%
{\headerFmt%
  \hspace*{\foremargin}\MakeLowercase{\rightmark}}%
{}%
{\headerFmt%
  \MakeLowercase{\leftmark}%
  \parbox[c][][t]{\foremargin}{%
    \pageNumFmt%
    \mbox{}\hfill\textbf{\thepage}\hfill\mbox{}%
  }%
}

\makeevenhead{toprightnum}%
{\headerFmt%
  \parbox[c][][t]{\foremargin}{%
    \pageNumFmt%
    \mbox{}\hfill\textbf{\thepage}\hfill\mbox{}%
  }%
  \MakeLowercase{\leftmark}%
}%
{}%
{\headerFmt%
  \MakeLowercase{\rightmark}\hspace*{\foremargin}%
}

\makeoddfoot{toprightnum}{}{}{}
\makeevenfoot{toprightnum}{}{}{}

\makepsmarks{toprightnum}{%
  \def\chaptermark##1{%
    \markboth{##1}{\rightmark}}%
  \def\partmark##1{%
    \markright{##1}{}}%
  \let\sectionmark\@gobble
}

Best Answer

I discovered xcoffins through this design, after that it was a breeze:

solved

\nouppercaseheads

\newlength\BOOK@bleed
\setlength{\BOOK@bleed}{3mm}

\NewCoffin\headerWrap
\NewCoffin\headerText
\NewCoffin\headerRule
\NewCoffin\headerPageNum

\newlength\BOOK@headWidth
\newlength\BOOK@headExtra
\setlength{\BOOK@headExtra}{2em}
\setlength{\BOOK@headWidth}{\textwidth + \BOOK@headExtra}

\makepagestyle{toprightnum}

\makerunningwidth{toprightnum}{\BOOK@headWidth}
\makeheadposition{toprightnum}{flushright}{flushleft}{center}{center}

\newcommand\headerFmt{\headerFont\headerSize\color{header}}
\newcommand\pageNumFmt{\pageNumFont\pageNumSize\color{pagenum}}

\makeoddhead{toprightnum}%
{%
  \SetHorizontalCoffin\headerText{%
    \parbox{\BOOK@headWidth}{%
      \headerFmt%
      \MakeLowercase{\rightmark}%
      \hfill%
      \MakeLowercase{\leftmark}%
    }%
  }%
  \SetHorizontalCoffin\headerRule{%
    \color{headrule}\rule{\paperwidth + 2\BOOK@bleed}{0.5pt}%
  }%
  \SetHorizontalCoffin\headerPageNum{%
    \setlength{\fboxsep}{0pt}%
    \colorbox{headrule}{\parbox[t][25pt][c]{\foremargin - \BOOK@headExtra + \BOOK@bleed}{%
      \raggedright
      \hspace*{15pt}%
      \pageNumFmt%
      \textbf{\thepage}%
    }}%
  }%
  \JoinCoffins\headerWrap\headerText
  \JoinCoffins*\headerWrap[l,b]\headerRule[l,t]({-\spinemargin-\BOOK@bleed},-2\onelineskip)%
  \JoinCoffins*\headerWrap[r,b]\headerPageNum[l,t](0pt,-2\onelineskip)%
  \TypesetCoffin\headerWrap
}{}{}%

\makeevenhead{toprightnum}%
{%
  \SetHorizontalCoffin\headerText{%
    \parbox{\BOOK@headWidth}{%
      \headerFmt%
      \MakeLowercase{\leftmark}%
      \hfill%
      \MakeLowercase{\rightmark}%
    }%
  }%
  \SetHorizontalCoffin\headerRule{%
    \color{headrule}\rule{\paperwidth + 2\BOOK@bleed}{0.5pt}%
  }%
  \SetHorizontalCoffin\headerPageNum{%
    \setlength{\fboxsep}{0pt}%
    \colorbox{headrule}{\parbox[t][25pt][c]{\foremargin - \BOOK@headExtra + \BOOK@bleed}{%
      \raggedleft
      \pageNumFmt%
      \textbf{\thepage}%
      \hspace*{15pt}%
    }}%
  }%
  \JoinCoffins\headerWrap\headerText
  \JoinCoffins*\headerWrap[l,b]\headerRule[l,t]({-\foremargin+\BOOK@headExtra-\BOOK@bleed},-2\onelineskip)%
  \JoinCoffins*\headerWrap[l,b]\headerPageNum[r,t](0pt,-2\onelineskip)%
  \TypesetCoffin\headerWrap
}{}{}%

\makeoddfoot{toprightnum}{}{}{}
\makeevenfoot{toprightnum}{}{}{}

\makepsmarks{toprightnum}{%
  \def\chaptermark##1{%
    \markboth{##1}{\rightmark}}%
  \def\partmark##1{%
    \markright{##1}{}}%
  \let\sectionmark\@gobble
}
Related Question