latex

How to Align Text and Formulates in LaTeX

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.

\usepackage[document]{ragged2e}

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:

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:

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:

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:

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:

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.

\usepackage{amsmath}

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:

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

\begin{equation}

E = mc^2

\end{equation
}

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:

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

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:

Conclusion

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

About the author

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