LaTeX text alignment

Original: Text alignment
Translated by: Xovee
Translation time: November 25, 2021

introduce

By default, most text in LaTeX documents is aligned. However, sometimes we want to align the text to the left (or right align the text from right to left), for example, when the line is short. This article describes how to change the alignment of text.

Standard LaTeX commands and environments

LaTeX has built-in commands to change text alignment:

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

And corresponding environment:

  • Ragged right (flushleft environment)
  • Ragged left (flush environment)
  • Centric (centring environment)

You can also use the ragged2e package, which provides more powerful alignment capabilities.

Using ragged2e package

Let's first introduce a user interface ragged2e package Examples. In this example, we use its [document] option to align the text of the whole document to the left (that is, the irregular right border). This example uses the following code in the preamble of the document:

\usepackage[document]{ragged2e}
\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}

Output:

\Difference between raggedright (LaTeX) and \ RaggedRight (ragged2e)

LaTeX's built-in command \ raggeright sometimes makes the text "too untidy". The ragged2e package allows words to be separated between different lines, resulting in a more uniform right boundary.

Let's visualize the results generated by these two commands:

\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}


ragged2e file More commands and environments for controlling text alignment are introduced in. We select some to introduce.

Aligned right environment

The flushleft environment in LaTeX corresponds to flushleft (initial capital) in the ragged2e package. The following example compares the two 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}


What if it's ragged left?

\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}

Text centered environment

The corresponding environment of the center environment in LaTeX in the ragged2e package is center (initial capital). The following example compares the two 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}

Aligned text

The ragged2e package also provides the \ justifying command:

\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}

Reference guide

The following is a summary of these commands and environments:

Tags: Latex

Posted on Thu, 25 Nov 2021 20:42:25 -0500 by Derleek