[Tex/LaTex] Center-justify with hrule above and below

horizontal alignmentletterspacingrulesspacing

On my title page, I have something like this:

\rule{\linewidth}{0.25mm}\\[0.25cm]
\huge
\textbf{This is my title, it is somewhat long and split over two lines!}\\[0.25cm]
\normalsize
\rule{\linewidth}{0.25mm}

and this is the corresponding output:

--------------------------------------
   This is my title, it is somewhat
    long and split over two lines!
--------------------------------------

Notice two things:

  1. The top first and the (wrapped) second line are similar, but different lengths. I would like to justify them (compress the top line slightly, and expand the bottom slightly).
  2. The \rule is extended to the \linewidth, whereas I would like it to be the width of the text in between the rules.

The result I'd like is something like:

--------------------------------
This is my title, it is somewhat
long and  split over  two lines!
--------------------------------

Any help with how to accomplish this? Thank you 🙂

Best Answer

Pilfering from @egreg's answer... You could modify the inter-word spacing or just add a couple of \hfills inbetween:

enter image description here

\documentclass{article}
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\begin{document}

\mbox{\huge\bfseries
  \begin{tabular}{@{}c@{}}
  \toprule[.25mm]
  This is my title, it is somewhat\\
  long\hfill and\hfill split\hfill over\hfill two\hfill lines!\\
  \bottomrule[.25mm]
  \end{tabular}%
}

\end{document}

On its own, \hfill will do nothing inside a tabular if there isn't enough room to stretch. However, since the first line is longer than the second, there is room to stretch.


\makebox[<len>][<justification>]{<stuff>} can also be used with <justification> given by [s] (for stretch) if you know the <len> that <stuff> should be spread over:

enter image description here

\documentclass{article}
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\begin{document}

\newlength{\mylen}
\noindent{\huge\bfseries
  \settowidth{\mylen}{This is my title, it is somewhat}%
  \begin{tabular}{@{}c@{}}
  \toprule[.25mm]
  This is my title, it is somewhat\\
  \makebox[\mylen][s]{long and split over two lines!}\\
  \bottomrule[.25mm]
  \end{tabular}%
}

\end{document}