6

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).

3

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

6

Unless you really need round corners, you only need color package for this.

enter image description here

\documentclass[a4paper]{article}  
\usepackage{color}

\definecolor{vlgray}{rgb}{0.87,0.87,0.87}
\newcommand{\highlight}[1]{{\setlength\fboxsep{0pt}\colorbox{vlgray}{#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}
  • Is it possible to make a colored border around the colored box? For example, red box with black frame. – Sigur Jan 2 '13 at 18:46
  • Well... I was using rounded corners because I find they look much nicer. YMMV. – Mohan Jan 2 '13 at 18:48
  • 1
    yes just use fcolorbox instead of colorbox, and then put a \kern-\fboxrule to negate the space taken up by the rule. – David Carlisle Jan 2 '13 at 18:49
  • @Mohan in general I'd agree but since you are constrained for space here there isn't a lot of room to show the corners in any case. In any case I just offer it as an alternative (that's a lot simpler than tikz or pstricks, unless of course you have them loaded anyway) – David Carlisle Jan 2 '13 at 18:51
2

\psframebox inserts a box that has a frame border of width \pslinewidth and a separation of width \psframesep. Removing these on both sides of your \highlight macro produces the desired result:

enter image description here

\documentclass{article}  
\usepackage{xcolor,pstricks}% http://ctan.org/pkg/{xcolor,pstricks}
\newrgbcolor{vlgray}{0.87 0.87 0.87}
\newcommand{\highlight}[1]{%
  \kern\dimexpr-\pslinewidth-\psframesep\relax%
  \psframebox[linearc=1.4mm,cornersize=absolute,fillcolor=vlgray, fillstyle=solid,linecolor=white]{#1}%
  \kern\dimexpr-\pslinewidth-\psframesep\relax}%
\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}

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.