[Tex/LaTex] Putting a coloured background behind text without adding whitespace

framedhighlightingpstricksspacingtikz-pgf

Consider this:

enter image description here

I want to be able to highlight text like this without adding space around it — so each word in the first line should line up with the corresponding word in the second line. (I know that I may have to fiddle with the size of the highlighting to stop it overflowing onto other words.)

That example was generated with PSTricks, although I'd much prefer a tikz solution.

\documentclass[a4paper]{article}  
\usepackage{color}
\usepackage{pstricks}
\newrgbcolor{vlgray}{0.87 0.87 0.87}
\newcommand{\highlight}[1]{\psframebox[linearc=1.4mm,cornersize=absolute,fillcolor=vlgray, fillstyle=solid,linecolor=white]{#1}}
\begin{document}
Lorem ipsum \highlight{dolor} sit amet, \highlight{consectetuer} adipiscing elit, sed diam 

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam 
\end{document}

Edit: also see the answer at https://tex.stackexchange.com/a/89145/17049 , which I particularly like because it fits the highlighting around the text more tightly than the answers below (or my example above).

Best Answer

Since the OP stated his preference for a tikz solution, there is one. The trick is to draw the frame in the background, while interrupting the computation of the bounding box, so that this background is not taken into account for the bounding box.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit,backgrounds}
\newcommand{\highlight}[1]{%
\begin{tikzpicture}[baseline = (text.base)]
  \node[inner sep=0pt] (text) {#1};
  \begin{pgfinterruptboundingbox}
    \begin{pgfonlayer}{background}
    \node[fit=(text), rounded corners, fill=black!20, draw=none] {};
    \end{pgfonlayer}
  \end{pgfinterruptboundingbox}
\end{tikzpicture}%
}
\begin{document}
Lorem ipsum \highlight{dolor} sit amet, \highlight{consectetuer} adipiscing elit, sed diam 

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam 
\end{document}

Result