11

What is the simplest way to flushleft, flushright and to center in the same line (preferably without using special environments such as tables or arrays) . Based on the question, I tried the following.

But the problem is that I would like that the place of blablablablablablablablabla be exactly at the middle of the page, which is not fulfilled by the below solution.

enter image description here

\documentclass{article}
\title{T} \date{}
\begin{document}
\maketitle 
\noindent
ABC \hfill blablablablablablablablabla \hfill Spring 1985-86\\
\end{document}

4 Answers 4

9

With \rlap and \llap (or \mathrlap and \mathllap from mathtools) if you have maths formulæ). Two posssible codes:

\documentclass{article}
\usepackage[showframe]{geometry}
\title{T} \date{}

\begin{document}

\maketitle

\noindent
\rlap{ABC} \hfill blablablablablablablablabla \hfill \llap{Spring 1985-86}

\bigskip

\noindent
\makebox[\linewidth][s]{\rlap{ABC} blablablablablablablablabla \llap{Spring 1985-86}}

\end{document} 

enter image description here

7

using \makebox or \rlap is simple if you know there is no possibility of overlap but for longer texts, suitably sized parboxes can work well:

enter image description here

\documentclass{article}

\begin{document}


\noindent
\parbox[t]{.2\textwidth}{\raggedright
some text some text some text}\hfill
\parbox[t]{.55\textwidth}{\centering
some text some text some text
some text some text some text
}\hfill
\parbox[t]{.2\textwidth}{\raggedleft
some text some text some text}%


\end{document}
4
\documentclass{article}
\title{T} \date{}
\begin{document}
\maketitle 
\noindent
\makebox[0pt][l]{ABC}\hfill\makebox[0pt]{blablablablablablablablabla}\hfill%
\makebox[0pt][r]{Spring 1985-86}\\
\end{document}

enter image description here

As frougon notes, the \makebox in the center text is not needed (though see David's comment as to how it differs in the "error" case where the center content is too long):

\noindent
\makebox[0pt][l]{ABC}\hfill blablablablablablablablabla\hfill%
\makebox[0pt][r]{Spring 1985-86}\\

One could also use \centerline:

\noindent
\makebox[0pt][l]{ABC}%
\centerline{blablablablablablablablabla}%
\makebox[0pt][r]{Spring 1985-86}\\

For completeness, I show it here with \usepackage[pass,showframe]{geometry} added.

enter image description here

7
  • You've been faster than me. :) The \makebox[0pt]{...} is not necessary for the centered chunk (blablablablablablablablabla here).
    – frougon
    Jun 26, 2018 at 11:15
  • @frougon I was just about to note that. Jun 26, 2018 at 11:15
  • 1
    @frougon the behaviour though is different in the error case where bla... is too long with makebox it stays centred and overprints which may be more understandable than the behaviour if it wraps, putting the two hfill in different lines so centring is lost Jun 26, 2018 at 11:21
  • @DavidCarlisle I was thinking of noting that too! :^) But I'll instead let your comment do it for me. Jun 26, 2018 at 11:23
  • 1
    @DavidCarlisle True, but either way the behavior is bad if any of the strings is too long (overlapping text, etc.). One might want to replace the second \makebox[0pt]{...} with just \mbox{...} to at least get a warning if the middle string exceeds the line width, but that still wouldn't protect against overlapping with the left and right strings.
    – frougon
    Jun 26, 2018 at 15:00
3

I have used this in a number of places in which the center item might be too long and so overprint the left and/or right text. This is not quite 'simple' but it is useful.

\documentclass{article}

\usepackage{calc}
\usepackage{ifthen}

\newsavebox{\leftbox}
\newsavebox{\centerbox}
\newsavebox{\rightbox}
\newlength{\testln}
\newlength{\testwd}

\newcommand{\entry}[3]{%
    \sbox{\leftbox}{#1}
    \sbox{\centerbox}{#2}
    \sbox{\rightbox}{#3}
    \def\tmp{0pt}%
    \ifthenelse{\lengthtest{\wd\leftbox>\wd\rightbox}}% Which is longer, left or right?
        {\setlength{\testln}{\widthof{\usebox{\leftbox}}}}
        {\setlength{\testln}{\widthof{\usebox{\rightbox}}}}
    \setlength{\testwd}{(\textwidth - \wd\centerbox)/2-\testln}%
    \ifdim\testwd<1em\def\tmp{\baselineskip}\fi %% 1 em is the minimum separation from right/left text; change to suit.
    \noindent
    \makebox[\textwidth]{%
        \makebox[0pt][l]{\usebox{\leftbox}}%
        \hfill
        \raisebox{-\tmp}{\usebox{\centerbox}}%
        \hfill
        \makebox[0pt][r]{\usebox{\rightbox}}%
    }%
}

\begin{document}

\entry{Left}{Center is actually quite long and would overprint}{Right xx xx xx xx yyy yy}

\bigskip

\entry{Left}{$x^2+y^2=z^2$}{Right xx xx xx xx yyy yy}

\end{document}

Example of left-center-right text

Update: I simplified the math of \setlength{\testwd}... a bit and removed \makebox[0pt]... in placing the centered text (not needed).

You must log in to answer this question.

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