REVISED SOLUTION
With the recent changes in the censor
package to incorporate a tokcycle
-based cycle to censor the tokens, the original approach given below requires revision...in fact, it becomes much simpler.
Math censoring can occur with \mblackout
(for character by character censoring) or with \mcensor
(block underlining).
\documentclass{article}
\usepackage[none]{hyphenat}
\usepackage{censor}
\usepackage{xcolor}
\usepackage{stackengine}
\usepackage{scalerel,lipsum}
\censorruledepth=-.25ex
\censorruleheight=.1ex
\newlength\maxkern
\setlength{\maxkern}{.14ex}
\newlength\nextcharwidth
\makeatletter
\renewcommand\@cenword[1]{%
\setlength{\nextcharwidth}{\widthof{#1}}%
\censorrule{\nextcharwidth}%
\kern -\nextcharwidth%
\color{white}%
\kern -.5\maxkern #1\kern .5\maxkern%
\kern -\nextcharwidth%
\kern -\maxkern #1\kern \maxkern%
\kern -\nextcharwidth%
\kern .5\maxkern #1\kern -.5\maxkern%
\kern -\nextcharwidth%
\kern \maxkern #1\kern -\maxkern%
\kern -\nextcharwidth%
\color{black}%
#1}
\let\sv@cenword\@cenword
\newcommand\m@cenword[1]{\ThisStyle{%
\stackengine{\mcensorruledepth}{$\SavedStyle#1$}%
{\rule{\widthof{$\SavedStyle#1$}}{\the\censorruleheight}}{U}{c}{F}{T}{L}}}
\newcommand\mblackout[2][\dp\strutbox]{%
\let\@cenword\m@cenword%
\def\mcensorruledepth{#1}%
\blackout{#2}%
\let\@cenword\sv@cenword%
}
\newcommand\mcensor[2][\dp\strutbox]{%
\let\@cenword\m@cenword%
\def\mcensorruledepth{#1}%
\censor{#2}%
\let\@cenword\sv@cenword%
}
\makeatother
\begin{document}
\blackout{This is a great test today}
{\raggedright\xblackout{This is a great test today}}
\[ \mblackout{y = A x^{2}} + Bx + \mblackout{C} \]
\[ \mblackout[.3ex]{y = \frac{a^{3} + b^{2} + c}{d_{3}}}\]
\[ \mcensor[3ex]{y = \frac{a^{3} + b^{2} + c}{d_{3}}}\]
\xblackout{Quisque ullamcorper placerat ipsum. Cras nibh. Morbi vel
justo vitae lacus tincidunt ultrices. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In hac habitasse platea dictumst.
Integer tempus convallis augue. Etiam facilisis. Nunc elementum
fermentum wisi. Aenean placerat. Ut imperdiet, enim sed gravida sollicitudin, felis odio placerat quam, ac pulvinar elit purus
eget enim. Nunc vitae tortor. Proin tempus nibh sit amet nisl.
Vivamus quis tortor vitae risus porta vehicula.}
\end{document}
ORIGINAL SOLUTION (based on outdated censor package)
This is a hack on the censor
package to do that which was not intended. All things considered, not too bad. It makes space for descenders, with a user-settable gap (\maxkern
)! The depth (\censorruledepth
) and thickness (\censorruleheight
) of the underrule are fully settable. It does linebreaking and page breaking. And with this EDITED version, it now does math underlining.
However, it does not do hyphenation. For text, it has two forms, using the censor
syntax, \blackout
(which skips over spaces and periods, and \xblackout
which attempts, through the use of carefully selected measures, to fill in underlines over the blanks and periods. As the parameters are set in my MWE, \xblackout
has a negative side effect of spilling slightly into the left margin.
\documentclass{article}
\usepackage[none]{hyphenat}
\usepackage{lipsum}
\usepackage{censor}
\usepackage{xcolor}
\usepackage{stackengine}
\usepackage{scalerel}
\censorruledepth=-.25ex
\censorruleheight=.1ex
\newlength\maxkern
\setlength{\maxkern}{.14ex}
\newlength\nextcharwidth
\makeatletter
\renewcommand\@cenword[1]{%
\setlength{\nextcharwidth}{\widthof{#1}}%
\censorrule{\nextcharwidth}%
\kern -\nextcharwidth%
\color{white}%
\kern -.5\maxkern #1\kern .5\maxkern%
\kern -\nextcharwidth%
\kern -\maxkern #1\kern \maxkern%
\kern -\nextcharwidth%
\kern .5\maxkern #1\kern -.5\maxkern%
\kern -\nextcharwidth%
\kern \maxkern #1\kern -\maxkern%
\kern -\nextcharwidth%
\color{black}%
#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%
}
\long\def\blackout#1{%
\def~{-}%
\protected@edef\save@arg{#1}%
\expandafter\censor@Block\save@arg\stringend\let~\sv@tilde}
\newcommand\singlelipsum[1]{%
\begingroup\let\lips@par\relax\csname lipsum@\@roman{#1}\endcsname
\endgroup }
\let\sv@cenword\@cenword
\newcommand\m@cenword[1]{\ThisStyle{%
\stackengine{\mcensorruledepth}{$\SavedStyle#1$}%
{\rule{\widthof{$\SavedStyle#1$}}{\the\censorruleheight}}{U}{c}{F}{T}{L}}}
\newcommand\mblackout[2][\dp\strutbox]{%
\let\@cenword\m@cenword%
\def\mcensorruledepth{#1}%
\blackout{{#2}}%
\let\@cenword\sv@cenword%
}
\makeatother
\periodrlap=1ex
\afterperiodlap=.4ex
\lletterlap=.0ex
\rletterlap=.6ex
\afterspacelap=.6ex
\begin{document}
\blackout{\singlelipsum{4}}
{\raggedright\xblackout{\singlelipsum{4}}}
\[ \mblackout{y = A x^2} + Bx + \mblackout{C} \]
\[ \mblackout[3ex]{y = \frac{a^3 + b^2 + c}{d_3}}\]
\blackout{\singlelipsum{4}}
\end{document}
If space is desired between words, \blackout
works real nice.
If you want the spaces to be underlined, use \xblackout
. With my selection of the parameters to tune \xblackout
, the underline extends slightly into both margins. Also, I am using \raggedright
here, because stretching glue can make gaps form in the underline.
For math mode, the approach is slightly different. First difference is that it cannot make a gap for descenders (because I don't know how to convert a length variable into mu's). So, as a result, I place, by default, the math underline (named \mblackout
, keeping with the censor syntax) at the depth of the \strutbox
. However, for math that drops below this depth, it takes an optional argument of how far below the baseline to do the underline.
Thus,
\[ \mblackout{y = A x^2} + Bx + \mblackout{C} \]
\[ \mblackout[3ex]{y = \frac{a^3 + b^2 + c}{d_3}}\]
gives the following:
If you are afraid the underline might be confused for math division, you could always thicken it up with a change in \censorruleheight
.
Simply enclose the \uline{}
inside a pair of braces. You do not need any other package.
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage[normalem]{ulem}
\usepackage{soul}
\newcommand{\bg}[2]{\sethlcolor{#1}\hl{#2}}
\definecolor{ColorFg}{HTML}{FFFFFF}
\definecolor{ColorBg}{HTML}{008000}
\begin{document}
{\color{ColorFg}\bg{ColorBg}{Some {\uline{Text}}}} another text.
\end{document}
And here is the output,
Question on answer
Could somebody please tell me why the following vanishes "Text"?
{\color{ColorFg}\bg{ColorBg}{Some \protect\uline{Text}}} another text.
Best Answer
cancel
is intended for indicating mathematic cancellation, and consequently only works in math mode; it also strikes out diagnoally.soul
andulem
seem pretty similar. One basic semantic difference between them is thatulem
per default changes the definition of\emph{...}
to underline its argument instead of putting it in italics. This seems to aim at the reproduction of typewriter typesetting, when underlining often was the only means of indicating emphasis. Since using underlining for emphasis is mostly deprecated nowadays, I've always usedsoul
when I did need to underline something.The one big reason in favor of
soul
, however, is that it's able to deal with line breaks and hyphenation:(The
X
are necessary because LaTeX won't hyphenate the first word in a paragraph.)