跳到内容

Introduction

By default, LaTeX typesets text as fully-justified, but occasionally left-aligned or "ragged right" text (for right-to-left languages) may be more appropriate—such as text within narrow columns. This article explains how to change text alignment for parts, or all, of your document using LaTeX's built-in features and the package ragged2e.

The standard LaTeX commands and environments

LaTeX does have built-in commands for changing the typeset alignment of text:

  • ragged-right (\raggedright)
  • ragged-left (\raggedleft)
  • centred (\centering)

together with corresponding environments:

  • ragged-right (flushleft environment)
  • ragged-left (flushright environment)
  • centred (centering environment)

However, the ragged2e package provides some refinements which improve upon these standard LaTeX commands and environments.

Using the package ragged2e

To start with, here is an example using the ragged2e package and its [document] option to typeset the entire document text as ragged-right (left-aligned). The sample code adds the line

\usepackage[document]{ragged2e}

to the document preamble:

\documentclass{article}
\usepackage[document]{ragged2e}

\begin{document}
\section{Heading on Level 1 (section)}
Hello, here is some text without a meaning. This text should show what a printed text will look like at this place.  If you read this text, you will get no information.  Really?  Is there no information?  Is there a difference between this text and some nonsense like not at all!  A blind text like this gives you information about the selected font, how the letters are written and an impression of the look.
\end{document}

 Open this ragged2e example in Overleaf

This example produces the following output:

Ragged2eOLV21.png

Comparing \raggedright (LaTeX) vs \RaggedRight (ragged2e)

The LaTeX command \raggedright sometimes produces results that appear to look "too ragged". The package ragged2e tackles this problem by allowing hyphenation when a line is too short, generating a more uniformly ragged text-edge. The following example shows ragged-right text produced via the LaTeX command \raggedright and, for comparison, the same text typeset using the \RaggedRight command provided by ragged2e:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{blindtext}

\begin{document}
\setlength{\hsize}{0.9\hsize}% emphasize effects

\subsection*{Left-aligned example with 
\texttt{\string\raggedright}\\ (standard \LaTeX{} command)}
\raggedright\blindtext[2]\par

\subsection*{Left-aligned example with \texttt{\string\RaggedRight}\\ (\texttt{ragged2e} command)}
\RaggedRight\blindtext[2]\par 
\end{document}

 Open this example in Overleaf

Leftaligncompare2.png

As discussed in the ragged2e package documentation, ragged2e provides alternative commands and environments for modifying text alignment and we'll take a look at some of these.

Environments for ragged-right (aligned-left) text

The default LaTeX environment for producing ragged-right text is flushleft. The equivalent in ragged2e is called FlushLeft (note capitalization). The following example compares the output of those environments:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{blindtext}
\begin{document}
\setlength{\hsize}{0.9\hsize}% emphasize effects

\subsection*{\texttt{flushleft} environment: (standard \LaTeX{})}

\begin{flushleft}
\blindtext[1]
\end{flushleft}

\subsection*{\texttt{FlushLeft} environment: (\texttt{ragged2e})}

\begin{FlushLeft}
\blindtext[1]
\end{FlushLeft}

\end{document}

 Open this example in Overleaf


Flcompare.png

Environments for ragged-left (right-aligned) text

The default LaTeX environment for producing ragged-left (right-aligned) text is flushright. The equivalent in ragged2e is called FlushRight (note capitalization). The following example compares the output of those environments:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{blindtext}
\begin{document}
\setlength{\hsize}{0.9\hsize}% emphasize effects

\subsection*{\texttt{flushright} environment: (standard \LaTeX{})}

\begin{flushright}
\blindtext[1]
\end{flushright}

\subsection*{\texttt{FlushRight} environment: (\texttt{ragged2e})}

\begin{FlushRight}
\blindtext[1]
\end{FlushRight}

\end{document}

 Open this example in Overleaf


FRcompare.png

Environments to centre text

The default LaTeX environment for producing centred text is center. The equivalent in ragged2e is called Center (note capitalization). The following example compares the output of those environments:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{blindtext}
\begin{document}
\setlength{\hsize}{0.9\hsize}% emphasize effects

\subsection*{\texttt{center} environment: (standard \LaTeX{})}

\begin{center}
\blindtext[1]
\end{center}

\subsection*{\texttt{Center} environment: (\texttt{ragged2e})}
\begin{Center}
\blindtext[1]
\end{Center}
\end{document}

 Open this example in Overleaf

Centrecompare.png

Fully justified text

The package ragged2e provides the command \justifying which you can use as shown in the example below:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{blindtext}
\begin{document}
\setlength{\hsize}{0.9\hsize}% emphasize effects

\Centering
\blindtext[1]

\vspace{5mm}
\justifying
\blindtext[1] 
\end{document}

 Open this example in Overleaf

Justifyingdemo.png

Reference guide

Summary of environments and commands for text alignment

Alignment Environment Switch command ragged2e environment ragged2e switch command
Left flushleft \raggedright FlushLeft \RaggedRight
Right flushright \raggedleft FlushRight \RaggedLeft
Centre center \centering Center \Centering
Fully justified justify \justifying

Further reading

For more information see :

Overleaf guides

LaTeX Basics

Mathematics

Figures and tables

References and Citations

Languages

Document structure

Formatting

Fonts

Presentations

Commands

Field specific

Class files

Advanced TeX/LaTeX