1

In looking at Highlight text with colored underline link to margin notes, I was struck by (to me) the unexpectedly attractive look of text that is both highlighted and underlined -- certainly not recommended in all cases, but in this it would seem useful. I am somewhat familiar with soul and can easily achieve either, but not both. The documentation to soul says that nesting is not possible (which I can confirm :<( ). The code for soul is daunting and I am disinclined to tinker.

Is this possible?

4
  • well you probably can define your own highlighting style. But it would be easier to use lualatex and lua-ul. Commented Jan 4, 2022 at 13:33
  • Hmmm... lualatex... hmmm...
    – sgmoye
    Commented Jan 4, 2022 at 14:22
  • Besides lua-ul and censor, have a look at soulpos (caveat: I’m the author). Commented Jan 5, 2022 at 18:04
  • @JavierBezos Have looked and am intrigued!
    – sgmoye
    Commented Jan 5, 2022 at 20:18

1 Answer 1

4

A suitably modified censor package may provide something useful, in this regard. Make sure you get the latest [2021-12-23] version loaded, https://www.ctan.org/pkg/censor.

\documentclass{article}
\usepackage{censor}[2021-12-23]
\usepackage{xcolor,stackengine}
\makeatletter
\renewcommand\censorrule[1]{\textcolor{yellow}{\protect
  \rule[\censorruledepth]{#1}{\censorruleheight}}}

\newcommand\censorul[1]{\textcolor{blue}{\protect
  \rule[\censorruledepth]{#1}{1pt}}}

\renewcommand\@cenword[1]{\bgroup
  \renewcommand\stacktype{L}%
  \setstackgap{L}{0pt}%
  \stackon{\stackon{\censorrule{\widthofpbox{#1}}}
            {\censorul{\widthofpbox{#1}}}}%
    {#1}%
  \egroup
}

\def\censpace{\rlap{\censorrule{\spacelap}}%
  \rlap{\censorul{\spacelap}} \llap{\censorrule{\spacelap}}%
  \llap{\censorul{\spacelap}}}
\let\sv@censpace\censpace

\censorruledepth -\dp\strutbox
\censorruleheight \baselineskip
\makeatother
\begin{document}
We now have to test this approach.
\xblackout{Testing of a theory is proceeding at this point.
We must see whether line breaking occurs.  I can even include
  expandable macros if I use v 4.0.  See: \expanded{\today}---however, 
  hyphenation will not occur with this approach.}
The test is now complete.
\end{document}

enter image description here

SUPPLEMENT

The OP points out an underlying problem, which extends not only to this solution, but to the censor package itself, which I will have to repair. The problem concerns macros like \$, \#, \&, etc., which should be consor-able, but which do not expand (via \expanded) into simple text.

The workaround provided was: \xblackout{...\censor{\$}...}.

A better solution is redefine \xblackout to look for such characters in the first place and, finding them, censor them without extra user intervention. The following code block demonstrates how that might be done. Note, however, a solution such as this will be eventually incorporated into the censor package.

Here is the added code needed to redefine \xblackout and to incorporate such a character check. Other character macros can be added to the \testchars macro:

\makeatletter
\xtokcycleenvironment\xblackoutenv
  {\ifx.##1\@dump\censored@word\addcytoks[1]{\censordot}\else
    \@append\censored@word{##1}%
    \tcpeek\@next\ifx\@next\@tcEscapeptr\@dump\censored@word\fi\fi}
  {\tctestifcon\ifexpandarg{\expandafter\processtoks\expandafter{\expanded{##1}}}%
    {\processtoks{##1}}\@dump\censored@word\expandargfalse}
  {\@dump\censored@word\tctestifx{~##1}{\@append\censored@word{##1}}%
    {\tctestifx{\expanded##1}{\expandargtrue}{%
    \testchars{##1}\ifchar\addcytoks{\censor{##1}}\else\addcytoks{##1}\fi}}}
  {\@dump\censored@word\addcytoks{\censpace}}
  {\def\censored@word{}}
  {\@dump\censored@word}

\newif\ifchar
\newcommand\testchars[1]{\charfalse
  \ifx\$#1\chartrue\else
  \ifx\&#1\chartrue\else
  \ifx\##1\chartrue\else
  \fi\fi\fi
}
\makeatother
3
  • This is excellent and extremely useful! The only problem I can find (by no means a deal breaker) is with \$ which is not obscured, as in \xblackout{ABCD\$EFGH}. Thank you very much.
    – sgmoye
    Commented Jan 5, 2022 at 13:54
  • @sgmoye As a workaround, you can use \xblackout{ABCD\censor{\$}EFGH}, but I will try to find a better way. Commented Jan 5, 2022 at 15:59
  • @sgmoye Please see the supplement to my answer. Commented Jan 5, 2022 at 16:32

You must log in to answer this question.

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