How do I center text in a page heading?

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}

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

\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.

-

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}

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

\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.

-

Use \phantom to "balance" your title line, like this:

\documentclass{article}
\usepackage[pass,showframe]{geometry}  % <-- help to see what's going on

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

\begin{document}

\markright{\TitleString \hfill%
Matt Groff%
\hfill\protect\phantom{\TitleString}}

\end{document}

-
Excellent! By the way, do you know why it puts everything in italics (or emphasis)? –  Matt Groff Nov 8 '10 at 1:05
@Matt Groff: sure - if you look in the definitions of the oneside and twoside header macros in article.cls (\ps@headings), you'll see that it applies \slshape to the header output stream (this sometimes maps to \itshape in some font families). All you need do to modify your header formatting is to change your invocation of \markright, e.g., to \markright{\textnormal{\TitleString ... }}. –  Geoffrey Jones Nov 8 '10 at 2:38
@Matt: Note the % after your name in Geoffrey's solution. This is important since otherwise there'll be an unwanted space after the name, and it won't be exactly centered. –  Hendrik Vogt Nov 8 '10 at 13:09

My usual choice would be the fancyhdr package:

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\begin{document}
Hello world
\end{document}

-

Use the scrpage2 package which is part of KOMA-Script.

\documentclass{article}

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

\usepackage[automark]{scrpage2}

\begin{document}


\setupheadertexts[Mark Groff]     % Center