# Positioning text both left and right on the same line

I am having a hard time positioning text. Especially if I need to 'float' elements to different sides. Currently, I'm trying to show a header with a 'subheader' that should be to the right of the header. Exactly to the right; not under it, not above it. Something like this:

MAIN TITLE                                 SUB TITLE

And here goes all the text, over the full page width.
The sub title should be exactly where the content box
ends, no matter what the size of the main title, nor
what the size of the sub title.

ANOTHER LONGER MAIN TITLE            ANOTHER SUBTITLE

This is another set of titles that shows just exactly
page, no matter the size.


I've tried many things but can not get this done. My closest fix was something like this:

\documentclass[11pt]{article}

\usepackage{parskip}
\usepackage{blindtext}

\begin{document}

\newcommand{\subtitle}[1]{\fbox{\vbox to 0pt{\hbox to 12.8cm{\hfill {#1}}}}}
\newcommand{\maintitle}[1]{\noindent\emph{#1 \vspace{0.01in}}}

\fboxsep0pt

\subtitle{Subtitle}
\maintitle{Title}

\blindtext

\subtitle{And a longer subtitle}
\maintitle{A longer title}

\blindtext

\end{document}


And this is what it looks like:

My problems are the following:

• In order to prevent that the position of the box is being influenced by the title, I need to actually put it before the title (so subtitle precedes title). This feels semantically wrong.
• Whatever I do, I can not get it to align at the same line. Always, whatever I put first pushes the other text down, even though it is a little bit.
• Creating a \hfill in an \hbox in an \vbox in an \fbox looks too complicated if I just want to float this box to the right. This should be easier, or not...?

What is the right way to tackle this issue?

-

To duplicate your ASCII art and add a box around it, the following works:

\documentclass[11pt]{article}
\usepackage{parskip}% http://ctan.org/pkg/parskip
\usepackage{blindtext}% http://ctan.org/pkg/blindtext
\begin{document}
\newcommand{\mytitles}[2]{% \mytitles{<main>}{<sub>}
\noindent\framebox[\textwidth]{\uppercase{#1}\hfill\uppercase{#2}}%
}

\mytitles{Title}{Subtitle}

\blindtext

\mytitles{A longer title}{And a longer subtitle}

\blindtext

\end{document}


The above MWE provides \mytitles{<main>}{<sub>} for setting the <main> and <sub> titles. It creates a box of size \textwidth but corrects for the double \fboxsep and \fboxrule used to surround the titles (using \framebox rather than \makebox). Since the box has a specific width, \hfill "knows" how far to stretch, pushing either titles flush with the (box) margin.

I've introduced \uppercase, but this is not necessary. You can, of course, also change the font shape, as in your MWE:

\newcommand{\mytitles}[2]{% \mytitles{<main>}{<sub>}
\noindent\makebox[\textwidth]{\textit{#1}\hfill#2}%
}


It would also be possible to add more automated checks of title widths (to avoid overlap or encourage wrapping).

-
Great, thanks, works as a charm! In fact, I only put the borders to show how the boxes were being placed, but it was no problem disabling the boxes in your code. Also, I've been wondering for a while what MWE stands for...? (I understand it refers to the OP sample code, but what do the letters mean?) –  user Feb 21 '12 at 12:58
@user1162541 -- MWE = "minimal working example". here's an explanation that will tell you more than you want to know. –  barbara beeton Feb 21 '12 at 14:57
@barbarabeeton Thanks! –  user Feb 22 '12 at 3:18