12

The macro ul provided by soul works great, allowing linebreaks within an underlined text block. It even supports macros - if they were registered with soulregister.

But if these registered macros produce text on their own, it isn't underlined:

\documentclass{article}

\usepackage{soul}

\newcommand{\one}[1]{\ul{#1}}
\newcommand{\two}[1]{not underlined #1}

\soulregister{\two}{1}

\begin{document}
test1 \one{test2 \two{test3}}.
\end{document}

"not underlined" isn't underlined

How can I make sure this text is underlined correctly?

  • 3
    It's not math-mode that's causing the problems. Replace your math content with any text, you'll still have the same effect. – A.Ellett Jul 31 '13 at 16:42
  • Oops - I thought I had checked that, thanks for pointing that out. I'll edit my question. – fefrei Jul 31 '13 at 16:46
  • 2
    Can someone enlighten me please - if you're just underlining, why use soul and not ulem? – Chris H Jul 31 '13 at 18:07
  • @ChrisH: Because I didn't think of it. ulem seems to handle this perfectly - thanks! – fefrei Jul 31 '13 at 18:16
  • 1
    @ChrisH: Hopefully we will both get some enlightenment once there are answers to List of Underlining Packages - Pros and Cons. – Peter Grill Aug 1 '13 at 1:47
11

For registered commands, soul will split up the string, turning it basically into

\ul{first part}\command{\ul{second part}}

So if \command produces any text, this will end up outside of the scope of soul's scanner. To solve this you can pre-expand the string before feeding it to soul:

\newcommand{\one}[1]{\protected@edef\@tempa{#1}\ul\@tempa}

EDIT: Another possibility would be to register your \two command with identifier 7 instead of 1, as explained in this answer.

3

If you aren't tied into soul for some other reason, then a modified \xblackout of the censor package might suffice for this task.

EDITED to fix problem with line breaking.

\documentclass{article}
%\usepackage{soul}
\usepackage{censor}
\censorruledepth=-.3ex
\censorruleheight=.1ex
\newlength\x
\makeatletter
\renewcommand\@cenword[1]{\censorrule{\widthofpbox{#1}}%
  \setlength{\x}{\widthofpbox{#1}}%
  \hspace{-\x}%
  #1}
\long\def\xblackout#1{\rule{0ex}{0ex}%
  \def~{-}%
  \def\@justpar{F}%
  \def\@justperiod{F}%
  \def\@justspace{F}%
  \protected@edef\save@arg{#1}%
  \expandafter\xcensor@Block\save@arg\stringend%
  \let~\sv@tilde%
 }
\makeatother
\newcommand{\one}[1]{\xblackout{#1}}
\newcommand{\two}[1]{not underlined #1}
%\soulregister{\two}{1}
\begin{document}
test1 \one{test2 \two{test3}}.
test1 \one{test2 \two{test3}}.
test1 \one{test2 \two{test3}}.
test1 \one{test2 \two{test3}}.
test1 \one{test2 \two{test3}}.
test1 \one{test2 \two{test3}}.
test1 \one{test2 \two{test3}}.
test1 \one{test2 \two{test3}}.
test1 \one{test2 \two{test3}}.
\end{document}

enter image description here

  • That's a quite interesting use of censor. However, it does seem to have problems with line breaking: Sometimes, a word breaks to a new line, but the underline stays in the previous line. – fefrei Jul 31 '13 at 17:59
  • @fefrei Hmm. I can now see how that would happen, if the breaking phrase were part of the embedded macro. Noting that significant deficiency, shall I remove the answer? – Steven B. Segletes Jul 31 '13 at 18:07
  • Of course, that's up to you - but still, the answer may be useful in some circumstances. – fefrei Jul 31 '13 at 18:12
  • @fefrei Fixed the censor approach to work with line breaks (also using a \protected@edef). – Steven B. Segletes Jul 31 '13 at 18:49
  • @fefrei If interested, see tex.stackexchange.com/questions/126291/… for an improved version of this approach, which leaves a gap around descenders, and does math underlining, too. – Steven B. Segletes Aug 1 '13 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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