[Tex/LaTex] How to center text in a page heading

header-footerhorizontal alignment

I'm using \markright to create a page heading. I'm having trouble centering some text in this heading, and I'd like to know how to do this.

Here's my current setup:

\documentclass{article}

\newcommand{\TitleString}{This Is The Title}

\begin{document}

\pagestyle{myheadings}

\markright{\TitleString \hfill Matt Groff \hfill}

Title and copyright are above.

\end{document}

I use \hfill to fill in the space so that my name is close to centered, but for large titles it's way off. Is there a way to fix this? I'd like my name to be completely centered.

Best Answer

Here's a simplified variant of Geoffrey's solution that avoids \protect and the doubling of \TitleString:

\documentclass{article}

\newcommand{\TitleString}{This Is A Rather Long Title}

\begin{document}

\pagestyle{myheadings}

\markright{\rlap{\TitleString}\hfill Matt Groff\hfill}

Title and copyright are above.

\end{document}

(Note that there are no spaces before the \hfill!)

Explanation: \rlap writes the text but just "overlaps it to the right" without it taking up any space.

Related Question