107

I am trying to align some text to left/center/right in the same line. For example, I want to put my phone number on the left, my name in the center, and my email on the right, how do I do that?

5 Answers 5

87

Using \hfill won't necessarily result in the middle text being centered, as the example below demonstrates. If you want to place the text in such a way that the middle text is really centered, I would suggest using \parboxes, as the example shows (I used the \lipsum[2] command to generate text to be used only as a reference):

\documentclass{article}
\usepackage{lipsum}

\newcommand\textbox[1]{%
  \parbox{.333\textwidth}{#1}%
}

\begin{document}

\noindent Left longer sample simple text\hfill Center?\hfill Right

\noindent\textbox{Left longer sample text\hfill}\textbox{\hfil Center\hfil}\textbox{\hfill Right}

\noindent\lipsum[2]

\end{document}

enter image description here

If this construct will be used many times, it would be better to have a command; something along the lines of the \textline command whose definition and use are illustrated in the following example (in which I also incorporated egreg's suggestion about \raggedleft, \centering and \raggedright):

\documentclass{article}
\usepackage{lipsum}

\newcommand\textline[4][t]{%
  \par\smallskip\noindent\parbox[#1]{.333\textwidth}{\raggedright\texttt{+}#2}%
  \parbox[#1]{.333\textwidth}{\centering#3}%
  \parbox[#1]{.333\textwidth}{\raggedleft\texttt{#4}}\par\smallskip%
}

\begin{document}

\lipsum[2]
\textline[t]{555\,555\,555}{Some Name}{[email protected]}
\lipsum[2]

\end{document}

enter image description here

3
  • 1
    I'd use \raggedright in the left box, \centering in the middle box and \raggedleft in the right box.
    – egreg
    May 12, 2012 at 9:51
  • 2
    What if any of the parts are wider than 0.333\textwidth? Then it will be wrapped to fit into the \textbox, and everything will not be at the same line anymore. Jan 17, 2014 at 16:51
  • This approach mostly works but does not handle empty arguments, longer text, and other edge cases. egreg's solution cleanly handles these. Apr 4, 2017 at 5:10
39

The task is accomplished easily with boxes:

\noindent
\makebox[0pt][l]{+999\,555\,999\,555}%
\makebox[\textwidth][c]{Ben Lee User}%
\makebox[0pt][r]{\texttt{[email protected]}}

This doesn't check for overlaps, but the chance to getting overlaps is very small.

One macro for it might be

\newcommand{\headerline}[3]{%
  \par\medskip\noindent
  \makebox[0pt][l]{#1}%
  \makebox[\textwidth][c]{#2}%
  \makebox[0pt][r]{\texttt{#3}}\par\medskip}

to be used as

\headerline{+999\,555\,999\,555}{Ben Lee User}{[email protected]}

A more efficient (and more obscure) solution

\newcommand{\headerline}[3]{%
\par\medskip\noindent
\makebox[\textwidth][s]{\rlap{#1}\hfill#2\hfill\llap{\texttt{#3}}}%
\par\medskip}
11
  • +1, but wouldn't \rlap and \llap be easier than the outer \makebox's? May 12, 2012 at 10:02
  • @MartinScharrer LaTeX syntax versus Plain. :)
    – egreg
    May 12, 2012 at 10:06
  • Are \rlap and \llap considered plain syntax? I didn't know that. May 12, 2012 at 10:07
  • @MartinScharrer They are slightly dangerous because they don't start horizontal mode.
    – egreg
    May 12, 2012 at 10:11
  • 2
    @LWZ It's for the cognoscenti. :) Don't worry: use what's documented in the manual (\makebox). When you'll be an expert, you'll know also when to take a shortcut.
    – egreg
    May 12, 2012 at 19:21
29
Left \hfill Center \hfill Right
3
  • If this answers your question, please consider marking it as ‘Accepted’ by clicking on the tickmark below the vote count. This assigns reputation points to the author of the answer (and to you!) Additionally, you can upvote answers (and questions) that you think are good, by clicking the upward-pointing triangle next to the post.
    – JohnD
    May 12, 2012 at 3:37
  • 2
    @texasAUtiger \noindent will probably be necessary and you have to be careful with spurious blank spaces. May 12, 2012 at 3:48
  • 12
    I downvoted this answer because it only centers the center part if the left and the right parts are equally long. If the left part is much longer than the right part, or the other way around, the center part will be way off center. Jan 17, 2014 at 16:54
3

Your question sounds to me like you are doing your CV in LaTeX, or something similar. Yet another solution to achieve neat alignment would be to use a table. Using the tabularxpackage, for example, you can stretch a table to the size \textwidth and place each item into its own respective cell.

\documentclass{article} 
\usepackage{float,tabularx}
    \newcolumntype{Y}{>{\raggedleft\arraybackslash}X}
    \newcolumntype{Z}{>{\centering\arraybackslash}X}
\begin{document}

\begin{tabularx}{\textwidth}{X Z Y}
+123 456 789 & Your Name & [email protected]
\end{tabularx}

\end{document}

This produces a table with three identically-spaced columns, stretching in sum to the size of \textwidth. As Z is specified, the content of the cells is also centered. Alternatively, X would left-align and Y right-align. You can use p{1.5cm} or so to have a specific column stretch to a predefined size. As long as you use any of X, Y, Z together with it, the table will stretch to the width of your text margin.

4
  • 1
    Did you mean to write X Y Z instead of Z Z Z? Separately, since the material isn't meant to "float" anyway, it seems unnecessary to wrap the tabularx environment in a table[H] environment; prefixing a \noindent instruction before \begin{tabularx}{...}{...} will do the job.
    – Mico
    Jan 5, 2014 at 18:41
  • You are also missing an \end{tabularx}
    – quinmars
    Jan 5, 2014 at 20:09
  • Thanks for the tip quinmars. Mico, I just wrap a table environment around tabularx to ensure it is placed exactly where it's meant to be, via the float package.
    – altabq
    Jan 6, 2014 at 23:39
  • 2
    ... which sort of makes the whole table environment pointless, as long as you're not adding a caption as well. Jan 6, 2014 at 23:42
2

Left \hfill Center \hfill Right
 

Result

Use a \phantom !


\phantom{phantom} \hfill \phantom{phantom} \hfill Right
 

Result


Left \hfill \phantom{phantom} \hfill Right
 

Result

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .