139

Sometimes I want to underline some text, and it extends past the end of a line. Why does \underline{} not automatically wrap my text for me ?

Also, how can I underline text so that it will still wrap ?

0

3 Answers 3

156

In text mode, the \underline command will enclose its argument in a horizontal box, which doesn't allow linebreaks. Use the \ul command of the soul package instead.

\documentclass[12pt]{article}

\usepackage{soul}

\begin{document}

A sentence that is just included to fill the line. \underline{Some text with underlining.}

A sentence that is just included to fill the line. \ul{Some text with underlining.}

\end{document}

enter image description here

EDIT: As for why \underline works the way it does, see this entry in the TeX FAQ for starters. It would seem that Leslie Lamport (the author of LaTeX) implemented just a "quick fix", and that only later package authors came up with more satisfactory solutions for underlining. See sections 2 and 7 of the soul documentation to get an idea of how complicated things are.

6
  • 1
    This mostly answers my question, but do you also know why \underline has this behavior ?
    – gabkdlly
    Commented Jan 24, 2011 at 10:03
  • 16
    There is also the ulem package with gives \uline. The package should be loaded with the normalem option, otherwise the \emph macro is changed to underline the text. Commented Apr 17, 2011 at 19:48
  • 5
    @gabkdlly: Basically \underline boxes the text using \hbox, i.e. the primitive version of \mbox. Both aren't breakable by design. The ulem package for example avoids this issue by boxing each word separately by searching for the spaces between them. This still doesn't allow for hyphenation. Commented Apr 17, 2011 at 19:51
  • 5
    The soul package gave me trouble when the underlined text contained cite. The ulem package worked better. Commented Aug 15, 2012 at 21:20
  • 1
    If there is a better way to do this that is non-breaking, it should be replaced in the main environment. I am restricted on the packages I can use, therefore I cannot fix this behavior because the improvements were never backported.
    – Elliot
    Commented Apr 22, 2013 at 18:24
60

The \ul command from the soul package has troubles with Umlauts -- the \uline command from the ulem package doesn't and therefore seems to be the better option for those not exclusively writing in English.

3
  • 2
    One should, however, be aware that ulem doesn't support automatic hyphenation, so one has to manually hyphenate using \- with it.
    – Skillmon
    Commented Apr 20, 2021 at 13:39
  • Note: One issue I faced is with macros, which don't work with multi-line. I tried with many packages, but none of them worked. I recommend that you include the \uline OR \ul, etc. in your macro to get the intended effect of underlining.
    – gagarwa
    Commented Mar 21, 2023 at 2:11
  • \ul{\mbox{Ü}} or \ul{\"U} works but they're certainly not very convenient.
    – user202729
    Commented May 12, 2023 at 5:56
11

If you're using the LuaTeX engine (or can use it) you could use the lua-ul package, that allows underlining without any restrictions on the input (unlike soul) and with fully functioning kerning, hyphenation, etc. (unlike ulem).

\documentclass[]{article}

\usepackage{lua-ul}

\begin{document}
\begin{minipage}{3cm} % to show automatic hyphenation works
  \underLine
    {%
      This is some text that gets automatically hyphenated
      while being under%
    }lined.
\end{minipage}
\end{document}

enter image description here

As you can see, hyphenation even works correctly for words not completely inside the argument of \underLine.

9
  • Do somebody know, why the hyphenation works with \underLine, but doesn't with \underline (lower 'l' in command name)? Is \underline a different command which is not from suggested lua-ul? I'm just curious what's going on. Commented Apr 5, 2023 at 13:54
  • 2
    @MichalVician yes, \underline is not part of lua-ul. In LaTeX \underline uses the \underline-primitive in maths mode, and in text mode it's just a thin wrapper around the primitive using a \hbox and as such isn't line breakable.
    – Skillmon
    Commented Apr 5, 2023 at 16:06
  • I checked the documentation to find ways to set the thickness and depth of the \underLine command but couldn't find any helpful information. Do you have any suggestions on how to do this? For context, I am trying to adapt one of your answers to use the lua-ul package (you can check what I have done so far here). @Skillmon
    – Mycroft_47
    Commented Jun 21 at 1:51
  • @Mycroft_47 see the documentation, lua-ul supports the keys top, bottom, and height with which you can change the placement and thickness of the underline (it's the last paragraph before section 2 starts).
    – Skillmon
    Commented Jun 21 at 7:20
  • Thank you for the guidance! I managed to get the \underLine command to work. For anyone else needing this, here's the command that worked for me: \underLine[height=.3ex, top=.3ex]{text to be underlined} @Skillmon
    – Mycroft_47
    Commented Jun 21 at 15:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .