Color and underline text in LaTeX

For a particular project, I had a requirement that the text be coloured as well as underlined. Now making text underlined in LaTeX has a default support in the form of \underline{text}, which simply produces an underlined text.

But what if you want to customise the underlining, for example, change the thickness of the underline, or its distance from the text baseline. Or simply you might want to have a different colour for the underline. There are several packages which allow you to customise this exact requirement.

For the present post I will choose the soul package which has some other goodies for typesetting as well. You can load the package with \usepackage{soul} Along with underlining, you have have strikeout and highlight. To setup the underline, soul package gives three options, underline depth, underline thickness and underline colour (page 12-13 of the package documentation).

You can set this in the preamble

\setul{⟨underline depth⟩}{⟨underline thickness⟩}

It is recommended that the units of these lengths are in ex, so that they are relative to the font size. For example,

\setul{0.3ex}{0.1ex}

To use it in the document \ul{underlined text} will produce

Another option is to set the colour of the underline. This can be done with

\setulcolor{gray}

With this option, the above code will look like

The color option can be changed anywhere in the document, so you can have change of colours as required. Defining in the preamble has a universal effect.

So far so good. Now in this case I wanted the underlined text to be of different colour. For this one can define a newcommand with text colour option.

\newcommand{\ublue}[1]{{\color{SteelBlue}\ul{#1}}}

I am also using the svgnames option from the xcolor package for calling colours by names.

\usepackage[svgnames]{xcolor}

In the main text, you can use it as \ublue{coloured and underlined text} to produce the required result. Will produce 

 

 

Leave a Reply

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