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?

fefrei
  • 1,339

2 Answers2

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.

Robert
  • 14,181
  • 1
  • 52
  • 77
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 http://tex.stackexchange.com/questions/126291/list-of-underlining-packages-pros-and-cons/126358#126358 for an improved version of this approach, which leaves a gap around descenders, and does math underlining, too. – Steven B. Segletes Aug 01 '13 at 15:56