LaTeX allows us to determine and control text alignment in our documents. In this tutorial, we will discuss how to align blocks of text or an entire document.

How to Align Text In LaTeX

By default, LaTeX aligns text using a fully justified method. In most cases, this method works great, especially when working with Mathematical formulas or chemical equations.

However, to use a custom alignment method, we need to import the ragged2e package. You can do this by setting the entry below in your document preamble.

The example code below shows how to use the ragged2e package. As mentioned, the text is aligned to the left unless explicitly specified.

documentclass{article}

usepackage[utf8]{inputenc}

usepackage[document]{ragged2e}

begin{document}

title{Text Alignment}

author{Linuxhint}

maketitle

section{Using Ragged2e Package}


———————-TEXT BLOCKS———————-


————————————————————–

end{document
}

The above code should give a sample document similar to the one shown below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-234.png60f5adf0abce9.jpg" data-lazy- height="834" src="data:image/svg xml,” width=”705″>

Left Justify Text

To use left-alignment on a document, use the FlushLeft command. The general syntax for left-align text is:

begin{FlushLeft}

end{FlushLeft

}

The following sample code illustrates how to use the flush left command.

documentclass{article}

usepackage[utf8]{inputenc}

usepackage[document]{ragged2e}

begin{document}

title{Text Alignment}

author{Linuxhint}

maketitle

section{Using Ragged2e Package}

begin{FlushLeft}


————– Document content here———————


—————————————————————

end{FlushLeft}

end{document
}

This will align the text to the left; here is an example output:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-235.png60f5adf160f48.jpg" data-lazy- height="632" src="data:image/svg xml,” width=”610″>

Right Align Text

To align text to the right, use the RightAlign command. The syntax for that is:

begin{FlushRight}

end{FlushRight}

The following example illustrates how the command works.

documentclass{article}

usepackage[utf8]{inputenc}

usepackage[document]{ragged2e}

begin{document}

title{Text Alignment}

author{Linuxhint}

maketitle

section{Using Ragged2e Package}

begin{FlushRight}

———————Document content here—————-

—————————————————————-

end{FlushRight}

end{document

}

The output result for this command are:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-236.png60f5adf2141ed.jpg" data-lazy- height="641" src="data:image/svg xml,” width=”645″>

NOTE: LaTeX also supports the RaggedLeft command to align text to the right. The syntax for that is:

RaggedLeft {Text Content}

Center Align Text

To align text to the center using the ragged2e package, use the Center command as:

begin{Center}

end{center

}

The resulting alignment for this is:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-237.png60f5adf2b64d3.jpg" data-lazy- height="730" src="data:image/svg xml,” width=”680″>

Full Justify Text

By default, LaTeX fully justifies text in LaTeX documents. However, you can explicitly specify this if you are using a different alignment method.

To do this, use the justify command.

LaTeX aligns the resulting content as shown below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-238.png60f5adf3645ba.jpg" data-lazy- height="699" src="data:image/svg xml,” width=”650″>

How to Align Equations In LaTeX

The amsmath package determines equations and formula alignment in LaTeX documents. Import the package by adding the entry below in the document preamble.

For Simple Equations:

To write a simple equation, use the equation environment as:

begin{equation}

TERMS

end{equation}

The following example code shows how to add an equation to a document.

documentclass{article}

usepackage[utf8]{inputenc}

usepackage{amsmath}

begin{document}

title{Aligning Formulas}

author{Linuxhint}

maketitle

section{Writing a simple equation}

begin{equation*}

E = mc^2

end{equation*

}

The output for this is:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-239.png60f5adf3d434a.jpg" data-lazy- height="308" src="data:image/svg xml,” width=”571″>

NOTE: If you want the equation numbered, use the equation environment (without an asterisk) as shown below:

begin{equation}

E = mc^2

end{equation}

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-240.png60f5adf44ea0b.jpg" data-lazy- height="273" src="data:image/svg xml,” width=”596″>

For Long Equations

To display equations that span more than a single line, use the {multiline*} environment. As:

begin{multiline*}

end{multiline*}

An example for that is:

begin{multline*}

a(b) = c^c d_{c} \

– e(a) – f^g

end{multline*

}

The output for this is:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-243.png60f5adf4bdf99.jpg" data-lazy- height="304" src="data:image/svg xml,” width=”513″>

How to Align an Equation

You can align an equation by using {align*} environment. The general syntax is:

begin{align*}

end{align*}

For example, the example below aligns the equations vertically.

begin{align*}

y-9 = 25 \

x 15 = 6 \

3x = 9 \

end{align*

}

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-244.png" data-lazy- height="187" src="data:image/svg xml,” width=”648″>

You can also align the by columns, as shown by the code below:

begin{align*}

x&=y & a&=b \

a^2 b2 = c^2 & & ab=sqrt{23}

end{align*

}

The example above aligns the equations in two columns. Here is a sample output:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-247.png" data-lazy- height="102" src="data:image/svg xml,” width=”503″>

Conclusion

This tutorial has shown you how to use LaTeX alignment features and packages to align text and formulates.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/john-150×150.png60f5adf580dd4.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list