How to use underline in LaTeX?

LaTeX gives detail direction on how to properly format text. Text formatting helps to increase readability but more importantly it helps the reader to know where to lay emphasis and what to take note of.

Some of common ways of formatting text in LaTeX include:

Putting text in bold, putting text in caps, underline text (which is the subject of our tutorial) etc. We shall explore different methods of formatting text by underlining in LaTeX.

Underlining using \underline{text}

In LaTeX, you can format your text by underlining using \underline{text} as follows.

\documentclass{article}
\begin{document}

Lorem \underline{ipsum dolor sit
amet, consectetur} adipiscine
elit,

\end{document}

Output :

Use default command.

\underline{text} permits us to underline just a few strings of text in a single line or at maximum, we can underline entire text on a single line.

\underline{text} command cannot underline more than one line of text or a block of text.

It tries to force text to appear on a single line which ends up messing up the beauty of text by forcing it to stretch to the margins.

Below is a demonstration

\documentclass{article}
\begin{document}

\underline{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incidid} -unt ut labore et dolore magna aliqua. 
    
Ut enim ad minim veniam, quis nostrud exercitation \underline{ullamco laboris nisi utaliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur.} Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

\end{document}

Output :

Default command are not best practice.

Above we have apply \underline{text} at two different spots. The first command takes a string of text equivalent to a single line i.e Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incidid and prints out the entire underlined text, well formatted.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incidid

But second example shows us inability of \underline{text} to underline more than one line of text. At it forces text off the margin as an attempt to underline it just in a single line.

This is question or problem, how can we underline a block of text in LaTeX?

LaTeX provides us a solution to this problem in ulem package which comes with extra functionality as we shall be seeing in our next section.

Underlining text using ulem package

To solve the problem of \underline{text} not being able to underline through broken text, LaTeX provides us with ulem package which permits us to underline even broken lines or block of text.

ulem package comes with more ability to format, how you wish to underline your text. Below are some commands provided by ulem package and what they do.

All commands of ulem package.

Above table gives us more ways to format our text by underlining. To begin add the \usepackage{ulem} in your preamble.

\documentclass{article}
\begin{document}

\uline{Lorem ipsum dolor sit amet}, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation \uline{ullamco laboris nisi ut aliquip ex
ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident}, sunt in culpa qui officia deserunt mollit anim id
est laborum. \uuline{Commodo sed egestas egestas fringilla phasellus faucibus scelerisque eleifend donec. Nisl pretium fusce id velit ut tortor pretium.} Dignissim cras tincidunt lobortis feugiat vivamus at augue eget arcu. \uwave{Venenatis a condimentum vitae sapien pellentesque habitant morbi.} Pulvinar pellentesque habitant morbi tristique senectus et netus et
malesuada. \dashuline{Aliquet eget sit amet tellus cras. Elit at imperdiet dui accumsan sit amet nulla. Venenatis urna cursus eget nunc scelerisque viverra mauris.} etc

\end{document}

Output :

Use of all commands of ulem package.

ulem normally replaces italics with underlining in text emphasized by \emph, and to some extent by \em.

To resolve this, to the \usepackage{ulem}, add the option [normalem] which will disables this feature. i.e to your preamble add \usepackage[normalem]{ulem}.

Underlining text using soul package

soul package is another solution created to enhance formatting of text in LaTeX in the context of underlining. This package give you ability to underline text with colored lines(rules). soul package works along side xcolor package.

Package provides us with basically three commands:

1. \ul{text} – This is command that takes the text and underlines it.

2. \setulcolor{color name or definition} – enables you to give color name or define the color of line.

3. \setul{depth}{line thickness} – Depth parameter in first argument sets how much underline is below the base line, and line thickness parameter simple defines thickness of line.

To use this formatting in your work, begin by adding \usepackage{soul, xcolor} to your preamble. Then you can set underline color, depth and line thickness either in your preamble or locally within your work.

For example, in your preamble add:

\setulcolor{red}
\setul{1pt}{0.25em}

Below is a demonstration of the above command.

\documentclass{article}
\usepackage{xcolor,soul}
\setulcolor{red}
\setul{1pt}{0.1em}

\begin{document}

\ul{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incidid} -unt ut labore et dolore magna aliqua. 
Ut enim ad minim veniam, quis nostrud exercitation \ul{ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur.} Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.


\end{document}

Output :

Use soul package with single color .

Looking at the output, you will notice that, soul package automatically handle breaks within text as it applies the underline to text.

It is equally possible to locally define the colors which will enable you to use multiple colors to underline you desire text that you want to format. For example

\documentclass{article}
\usepackage{xcolor,soul}

\begin{document}

{\setulcolor{red}
\setul{1pt}{0.15em}
\ul{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incidid -unt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat}. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.} Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

{\setulcolor{blue}
\setul{1pt}{0.15em}
\ul{Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.}Aliquet eget sit amet tellus cras. Elit at imperdiet dui accumsan
sit amet nulla. Venenatis urna cursus eget nunc scelerisque viverra mauris}


\end{document}

Output :

Use soul package with different colors.

Conclusion

This tutorial guides you on how to format your document by underlining either a single line of text or a block of text. We have exhaustively gone through the various methods.

Practicing this concept will give you great understanding in LATEX text formatting.

Md Jidan Mondal

LaTeX expert with over 10 years of experience in document preparation and typesetting. Specializes in creating professional documents, reports, and presentations using LaTeX.

Leave a Comment

Your email address will not be published. Required fields are marked *