Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In this post I've found the following amazing code to highlight text on several lines.

The problem is that code dosen't work well with formulas : for example, fraction using the mode display are partially out of the highlighting frame.

Here is the code with one content showing the problem.

\documentclass{article}
    \usepackage{soul}
    \usepackage{tikz}
    \usetikzlibrary{calc}
    \usepackage{amsmath}
    \usepackage{xcolor}

    \makeatletter
        \newcommand{\defhighlighter}[3][]{%
            \tikzset{every highlighter/.style={color=#2, fill opacity=#3, #1}}%
        }

        \defhighlighter{yellow}{.5}

        \newcommand{\highlight@DoHighlight}{
            \fill[
                outer sep = -15pt, inner sep = 0pt, 
                every highlighter, this highlighter 
            ](
                $(begin highlight)+(0,8pt)$) rectangle ($(end highlight)+(0,-3pt)$
            );
        }

        \newcommand{\highlight@BeginHighlight}{
            \coordinate (begin highlight) at (0,0) ;
        }

        \newcommand{\highlight@EndHighlight}{
            \coordinate (end highlight) at (0,0) ;
        }

        \newdimen\highlight@previous
        \newdimen\highlight@current

        \DeclareRobustCommand*\highlight[1][]{%
            \tikzset{this highlighter/.style={#1}}%
            \SOUL@setup
            %
            \def\SOUL@preamble{%
                \begin{tikzpicture}[overlay, remember picture]
                    \highlight@BeginHighlight
                    \highlight@EndHighlight
                \end{tikzpicture}%
            }%
            %
            \def\SOUL@postamble{%
                \begin{tikzpicture}[overlay, remember picture]
                    \highlight@EndHighlight
                    \highlight@DoHighlight
                \end{tikzpicture}%
            }%
            %
            \def\SOUL@everyhyphen{%
                \discretionary{%
                    \SOUL@setkern\SOUL@hyphkern
                    \SOUL@sethyphenchar
                    \tikz[overlay, remember picture] \highlight@EndHighlight ;%
                }{}{%
                    \SOUL@setkern\SOUL@charkern
                }%
            }%
            %
            \def\SOUL@everyexhyphen##1{%
                \SOUL@setkern\SOUL@hyphkern
                \hbox{##1}%
                \discretionary{%
                    \tikz[overlay, remember picture] \highlight@EndHighlight ;%
                }{}{%
                    \SOUL@setkern\SOUL@charkern
                }%
            }%
            %
            \def\SOUL@everysyllable{%
                \begin{tikzpicture}[overlay, remember picture]
                    \path let \p0 = (begin highlight), \p1 = (0,0) in \pgfextra
                    \global\highlight@previous=\y0
                    \global\highlight@current =\y1
                    \endpgfextra (0,0) ;
                    \ifdim\highlight@current < \highlight@previous
                        \highlight@DoHighlight
                        \highlight@BeginHighlight
                    \fi
                \end{tikzpicture}%
                \the\SOUL@syllable
                \tikz[overlay, remember picture] \highlight@EndHighlight ;%
            }%
            \SOUL@
        }
    \makeatother


\begin{document}

Lorem ipsum \highlight{dolor sit amet, consectetur adipis-icing elit, sed do
eiusmod tempor} incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation \highlight[red]{ullamco $laboris$ nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit} in
voluptate velit esse cillum dolore eu fugiat nulla pariatur.  Excepteur sint
occaecat \highlight[green, draw=blue]{cupidatat non proident 
$\dfrac{789}{7889}$ % The frame is not good for that formula !
suntinculpaquiofficiadeseruntmollitanimidestlaborum.
Loremipsumdolorsitametconsecteturadipisicingelitseddoeiusmodtemporincididuntutlabore-etdoloremagnaaliqua.}
I suppose I could write some more text here.

\end{document}

Highlighting text

Is someone could help me to fix it?

share|improve this question
4  
Welcome to TeX.sx! PLEASE don't use minimal for minimal working examples! It is not designed for that even if the name suggest it, has no real benefits and frequently causes issues! –  Martin Scharrer Jul 25 '11 at 9:49
    
Thanks for pointing me to that post. I just changed my TikZ code from my answer to Test if a paragraph has a page break in it? to do underlining. (I will publish it as soon I find time) Doing highlighting is also possible with it. However, it also doesn't take the size of large objects like math into account. –  Martin Scharrer Jul 25 '11 at 9:55
    
The MWE is missing \usepackage{xcolor}. Also, if you use \usepackage{amsmath} you can use \dfrac{}{} instead of \displaystyle \frac{}{}. –  Peter Grill Aug 5 '11 at 7:51
    
The MWE has been updated. –  projetmbc Aug 14 '11 at 17:11
    
A non-solution is to use \soulomit around the formula. That would at least prevent the highlighting from striking through it. –  Bruno Le Floch Sep 9 '11 at 3:06
show 2 more comments

2 Answers

As there has been no solution to this I thought a workaround might be worth consideration.

A similar effect to the soul highlighting but which can deal with material of non-standard size on a single line can be achieved with \fcolorbox or \colorbox, provided by the, already loaded, xcolor package.

For example, in the text above, replacing

\highlight[green, draw=blue]{cupidatat non proident 
$\dfrac{789}{7889}$ % The frame is not good for that formula !

with

\highlight[green, draw=blue]{cupidatat non proident\,}%
\definecolor{mid-grey}{gray}{0.5}%
\fcolorbox{blue}{green}{\color{mid-grey}$\dfrac{789}{7889}$}%
\noindent\highlight[green, draw=blue]{\,

Gives a green box with blue line around the whole fraction:

Two obvious deficiencies are

(a) material with a non-standard height has to be identified (and coded whilst bearing in mind the single line restriction) and

(b) the blue box is continuous round the whole perimeter of the three 'chunks'.

An obvious additional difference is that the whole line containing the displayed fraction is spaced from lines above and below.

For information: the mid-grey is defined to simulate the apparent greying of text in the highlight which, by default, does not happen with the fcolorbox. A similar correction to the green may also be helpful. For this example, the \, and % are to prevent non-highlighted whitespace between the three 'chunks'.

share|improve this answer
add comment

I tried the same text with the ConTeXt code for syntax highlighting (which the post linked by the OP links to). It works slightly better than the tikz solution, but is not perfect either.

\definebar[highlight]
          [order=background,
           rulethickness=2.5,
           offset=1.25,
           continue=yes,
           color=yellow]

\starttext
\startTEXpage[offset=2mm]
Lorem ipsum \highlight{$\displaystyle \sum_{i=1}^n x_n$ dolor sit amet, consectetur adipis-icing elit, sed do
eiusmod tempor} incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation \highlight{ullamco $\sum_{i=1}^n$ nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit} in
voluptate velit esse cillum dolore eu fugiat nulla pariatur.  Excepteur sint
occaecat \highlight{cupidatat non proident 
$\dfrac{789}{7889}$ % The frame is not good for that formula !
suntinculpaquiofficiadeseruntmollitanimidestlaborum.
Loremipsumdolorsitametconsecteturadipisicingelitseddoeiusmodtemporincididuntutlabore-etdoloremagnaaliqua.}
I suppose I could write some more text here
\stopTEXpage
\stoptext                     

enter image description here

Another way to highlight text in ConTeXt is using text backgrounds, which fails in a different way.

  \definetextbackground
      [highlight] 
      [
        alternative=0,
        frame=off,
        background=color,
        backgroundcolor=yellow,
      ]

enter image description here

But this suggests that one can combine the two mechanism to be a working solution:

  \definetextbackground
      [highlightbackground] 
      [
        alternative=0,
        frame=off,
        background=color,
        backgroundcolor=yellow,
      ]

\definebar[highlightbar]
          [order=background,
           rulethickness=2.5,
           offset=1.25,
           continue=yes,
           color=yellow] 

\def\highlight#1{\highlightbar{\highlightbackground{#1}}}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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