1

I am trying to create this effect:

enter image description here

(without red underlines, I did this example with Word that emphasizes the words he doesn't know in red).

I used the package alltt this:

\begin{alltt}
     text 1:    Quicq fyre  |   text 1: Slow fool
     text 2:    Quick fire  |   text 2: Quick fire
     Diff:  Quic\colorbox{lightred}{q}\colorbox{lightgreen}{k} f\colorbox{lightred}{y}\colorbox{lightgreen}{i}re    |   Diff:   SlowQuick foolire
     Split: Quic\colorbox{lightred}{q}\colorbox{lightgreen}{k}\colorbox{lightred}{ f}\colorbox{lightgreen}{ f}\colorbox{lightred}{y}\colorbox{lightgreen}{i}re  |   Split:  SlowQuick f foolire
     Merge: Quic\colorbox{lightred}{q fy}\colorbox{lightgreen}{k fi}re  |   Merge:  Slow foolQuick fire 
\end{alltt}

and the result is:

enter image description here

As you can see the underlines are staggered. So the result is not as expected.

How can I use to get something more like the first picture? I didn't use the package listing because I already used it for some code that is formatted differently to how it should be in this text.

0

1 Answer 1

4

I suggest you remove the padding added by \colorbox; the staggering can be remedied by adding a phantom with ascenders and descenders.

\documentclass{article}
\usepackage{xcolor,alltt}

\colorlet{lightgreen}{green!30}
\colorlet{lightred}{red!30}
\newcommand{\?}[2]{%
  \begingroup\setlength{\fboxsep}{0pt}%
  \colorbox{#1}{\vphantom{fg}#2}%
  \endgroup
}

\begin{document}

\begin{alltt}
text 1:    Quicq fyre  |   text 1: Slow fool
text 2:    Quick fire  |   text 2: Quick fire
Diff:  Quic\?{lightred}{q}\?{lightgreen}{k} f\?{lightred}{y}\?{lightgreen}{i}re    |   Diff:   SlowQuick foolire
Split: Quic\?{lightred}{q}\?{lightgreen}{k}\?{lightred}{ f}\?{lightgreen}{ f}\?{lightred}{y}\?{lightgreen}{i}re  |   Split:  SlowQuick f foolire
Merge: Quic\?{lightred}{q fy}\?{lightgreen}{k fi}re  |   Merge:  Slow foolQuick fire 
\end{alltt}

\end{document}

enter image description here

Striking out or underlining can be obtained by storing the bits in a box and then overprinting a rule after having printed the item.

\documentclass{article}
\usepackage{xcolor,alltt}

\colorlet{lightgreen}{green!30}
\colorlet{lightred}{red!30}
\newsavebox{\RGbox}
\newcommand{\R}[1]{%
  \begingroup\setlength{\fboxsep}{0pt}%
  \sbox{\RGbox}{\colorbox{lightred}{\vphantom{fg}#1}}%
  \usebox{\RGbox}%
  \makebox[0pt][r]{\vrule width \wd\RGbox height .5ex depth -\dimexpr.5ex-0.4pt\relax}%
  \endgroup
}
\newcommand{\G}[1]{%
  \begingroup\setlength{\fboxsep}{0pt}%
  \sbox{\RGbox}{\colorbox{lightgreen}{\vphantom{fg}#1}}%
  \usebox{\RGbox}%
  \makebox[0pt][r]{\vrule width \wd\RGbox height -\dimexpr\dp\RGbox-0.4pt\relax depth \dp\RGbox}%
  \endgroup
}


\begin{document}

\begin{alltt}
text 1:    Quicq fyre  |   text 1: Slow fool
text 2:    Quick fire  |   text 2: Quick fire
Diff:  Quic\R{q}\G{k} f\R{y}\G{i}re    |   Diff:   SlowQuick foolire
Split: Quic\R{q}\G{k}\R{ f}\G{ f}\R{y}\G{i}re  |   Split:  SlowQuick f foolire
Merge: Quic\R{q fy}\G{k fi}re  |   Merge:  Slow foolQuick fire 
\end{alltt}

\end{document}

enter image description here

Zoom on the colored entries

enter image description here

2
  • Thanks! And to add the underscore (if green) or strikethrough text (if red)? Oct 31, 2015 at 21:04
  • @lonely Added a full version
    – egreg
    Oct 31, 2015 at 21:29

You must log in to answer this question.

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