Tx · LaTeX & TikZ

How to Put Text on Both Sides of a Line in LaTeX

Published By the 9to5Science editorial team

At a glance

  1. 1. Write the left text

    Start a paragraph or line and enter the text you want aligned at the left.

  2. 2. Add stretchable space

    Place \hfill between the left and right text to push them apart.

  3. 3. Add the right text

    Enter the second text before the line ends; it will sit at the right margin.

If you want one piece of text at the left edge of a line and another at the right edge, use \hfill between them. It inserts horizontal space that stretches to use the available room.

Align two pieces of text on one line

The basic pattern is:

Left text\hfill Right text

Here is a complete example you can compile with a standard LaTeX installation:

\documentclass{article}

\begin{document}

Left text\hfill Right text

\end{document}

The first text begins at the paragraph’s normal starting position (which may be indented), and the second is pushed to the right edge of the current line. No extra package is needed.

How \hfill works

LaTeX treats \hfill as stretchable horizontal glue. Unlike a fixed space such as \quad, it can grow to fill the remaining horizontal space on the line. With one \hfill between two text fragments, the space expands and separates them.

You can also use it for three fragments:

Left\hfill Center\hfill Right

The two stretchable spaces share the available room, so the middle text is not necessarily centered precisely on the page. If you need the middle item centered while the outer items sit at the margins, use a three-column layout, such as a tabular* environment, or position the items in a fixed-width box.

Keep the text on the same line

\hfill works within a paragraph. If the line is too long to fit, LaTeX may break it, and the right-hand text will no longer appear at the end of the same line. For a short header or label, you can prevent a line break with a fixed-width box:

\noindent\hbox to \textwidth{Left text\hfill Right text}

The \hbox to creates a box as wide as the text area, and the \hfill distributes the two pieces inside it. \noindent removes the usual paragraph indent at the start of the line. This is useful when you want the alignment to match the page margins exactly.

Common mistakes

  • Using a fixed space instead: \quad adds a predetermined amount of space; it does not push the right-hand text to the margin.
  • Putting \hfill on a separate paragraph: A paragraph break ends the line, so the two pieces will not be separated by the same stretchable space.
  • Expecting it to work across a line break: \hfill only fills available space on its current line. If text wraps, shorten it or use a fixed-width box.
  • Using it for page headers everywhere: For recurring left/right page headers, configure the document’s page style rather than repeating this inline technique.

For a single line of opposing text, Left\hfill Right is usually the simplest solution. Use a box or a table when the alignment must be fixed or repeated.

References

  1. LaTeX2e unofficial reference manual: \hfill

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.