6

What's the simplest way to achieve coloured background for the inline equation in the following example:

\documentclass{article}
\begin{document}
Completely unimportant text 
$ABC_{1234} = ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234}$
Some more text.
\end{document}

So the result would look like: enter image description here

10

You need to run this twice

enter image description here

\documentclass{article}
\usepackage{color}
\makeatletter
\def\savepos#1{\leavevmode\pdfsavepos\write\@auxout{%
\gdef\string\save@#1{{\the\pdflastxpos sp }{\the\pdflastypos sp }}}}

\def\xx#1{\expandafter\expandafter\expandafter\@firstoftwo\csname save@#1\endcsname}
\def\yy#1{\expandafter\expandafter\expandafter\@secondoftwo\csname save@#1\endcsname}

\def\hl#1#2{%
\ifx\save@a\@undefined
\else
\leavevmode\smash{\rlap{\vtop{%
\parindent\z@\parskip\z@
\leavevmode\color{yellow}\endgraf
\vskip-\baselineskip
\dimen@\xx{#1}%
\@tempdima\yy{#1}%
\@tempdimb\yy{#2}%
\loop
\ifdim\@tempdima>\@tempdimb
\leavevmode
      \kern\dimexpr\dimen@-\xx{#1}\relax
      \vrule width \dimexpr\xx{b}-\dimen@\relax height\ht\strutbox depth\dp\strutbox\endgraf
\dimen@\xx{a}%
\advance\@tempdima-\baselineskip
\repeat
\ifdim\@tempdima=\@tempdimb
\leavevmode
      \kern\dimexpr\dimen@-\xx{#1}\relax
      \vrule width \dimexpr\xx{#2}-\dimen@\relax height\ht\strutbox  depth\dp\strutbox\endgraf
\fi
}}}%
\fi}
\makeatother

\begin{document}



\noindent\savepos{a}\hfill\savepos{b}

Completely unimportant text 
\hl{c}{d}\savepos{c}$ABC_{1234} = ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234}$\savepos{d}
Some more text.

\end{document}
  • Can you explain the code David? – cacamailg May 12 '13 at 1:13
  • @cacamailg not at this time of night:-) – David Carlisle May 12 '13 at 1:25
  • 1
    @cacamailg basically, the positions at the beginning and at the end are saved, if they differ, a loop is used to produce the highlighting for the first line and all (possible) intermediate lines; when they differ, the highlighting for the only line is produced. – Gonzalo Medina May 12 '13 at 1:26
  • I understand that positions are saved (and it seems to use a pdfprimitive for that. But what I want to understand is how is it saved, in which file, and how LaTeX in the second run gets the first and latest positions to highlight. – cacamailg May 12 '13 at 1:30
  • 1
    @cacamailg That's the \savepos macro which writes teh x,y coordinates (in sp) to the aux file. Look in the aux file and you will see the coordinates of a b c and d saved. (It would perhaps be better not to do a and b this way, they are the page margins so could be calculated in advance but allowing for then to be saved this way means it can work in minipage or tabular columns. – David Carlisle May 12 '13 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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