Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Like

Hello World 
...........    

or

Hello World
-----------

And not so

Hello World  // \underline{Hello World}
___________  //
share|improve this question

migrated from stackoverflow.com Sep 2 '11 at 13:38

This question came from our site for professional and enthusiast programmers.

    
You're asking how to get a non-solid underline effect in the output from LaTeX right? –  Flexo Sep 2 '11 at 13:13
add comment

1 Answer 1

up vote 27 down vote accepted

(Disclaimer: It may be considered a typographically bad practice to underline text.)

I can think of two ways to do this.

ulem

This option is good if you want a fast solution. Use \dotuline or \dashuline from the package ulem.

\documentclass{article}

\usepackage{ulem}

\begin{document}

\dotuline{dotty}

\dashuline{dashing}

\end{document}

Dotted and dashed underline using ulem

TikZ

This option is good if you want control. You can create your own macro with TikZ. The following is an example of six macros. The first three produces dotted underlining and the last three dashed underlining.

\documentclass{article}

\usepackage{tikz}

\newcommand{\udot}[1]{%
    \tikz[baseline=(todotted.base)]{
        \node[inner sep=1pt,outer sep=0pt] (todotted) {#1};
        \draw[dotted] (todotted.south west) -- (todotted.south east);
    }%
}%

\newcommand{\udensdot}[1]{%
    \tikz[baseline=(todotted.base)]{
        \node[inner sep=1pt,outer sep=0pt] (todotted) {#1};
        \draw[densely dotted] (todotted.south west) -- (todotted.south east);
    }%
}%

\newcommand{\uloosdot}[1]{%
    \tikz[baseline=(todotted.base)]{
        \node[inner sep=1pt,outer sep=0pt] (todotted) {#1};
        \draw[loosely dotted] (todotted.south west) -- (todotted.south east);
    }%
}%

\newcommand{\udash}[1]{%
    \tikz[baseline=(todotted.base)]{
        \node[inner sep=1pt,outer sep=0pt] (todotted) {#1};
        \draw[dashed] (todotted.south west) -- (todotted.south east);
    }%
}%

\newcommand{\udensdash}[1]{%
    \tikz[baseline=(todotted.base)]{
        \node[inner sep=1pt,outer sep=0pt] (todotted) {#1};
        \draw[densely dashed] (todotted.south west) -- (todotted.south east);
    }%
}%

\newcommand{\uloosdash}[1]{%
    \tikz[baseline=(todotted.base)]{
        \node[inner sep=1pt,outer sep=0pt] (todotted) {#1};
        \draw[loosely dashed] (todotted.south west) -- (todotted.south east);
    }%
}%


\begin{document}

\uloosdot{dotty}

\udot{dotty}

\udensdot{dotty}

\uloosdash{dashing}

\udash{dashing}

\udensdash{dashing}

\end{document}

Six examples of underlinings done with TikZ

Note that you can modify these macros to fit your needs. For example you can change the thickness styles by adding thick as an option to \draw, e.g.

\draw[dotted, thick] (todotted.south west) -- (todotted.south east);

Other modification might be to change the color and opacity, e.g.

\draw[dotted, blue, opacity=0.25] (todotted.south west) -- (todotted.south east);

or to change the distance between the text and the decoration by editing the measures in [inner sep=1pt,outer sep=0pt], e.g.

\node[inner sep=0.8pt,outer sep=0pt] (todotted) {#1};

All TikZ code in this answer is a simple modification of \cancel draws under thing being canceled

share|improve this answer
    
Thanks for the detailed answer! The TikZ code, however draws the underline below the bottom of a character, so lines below q and y are lower than other letters. –  Jakob Sep 5 '12 at 18:40
1  
@Jakob The ulem solution also draws the line lower for characters below the baseline. –  N.N. Sep 5 '12 at 19:09
1  
The downside of TikZ approach seems to be that it doesn't allow the text to break across multiple lines. Is there a way to fix that, or work around it? The flexibility and the styles are much more appealing in the second solution. –  foxcub May 19 '13 at 16:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.