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.

I've made the command

\newcommand{\pink}[1]{\colorbox{red!20}{#1}}

to highlight in pink. It works fine for words and sentences, but if I want to highlight a subpart of a word, e.g.

laugh\pink{ing}

it adds a space before ing (this is for a linguistics lecture)

Is there any way to override this?

share|improve this question
    
Thanks - works a treat. –  Nick Riches Sep 28 '12 at 8:12

2 Answers 2

up vote 6 down vote accepted

First of all, TeX language is not "free form", so there's a big difference between

\newcommand{\pink}[1]{
\colorbox{red!20}{#1}
}

and

\newcommand{\pink}[1]{\colorbox{red!20}{#1}}

In the first case two spaces are added before and after the \colorbox, because end-of-lines are converted into spaces and they become part of the replacement text. So your first attempt is equivalent to do

\newcommand{\pink}[1]{ \colorbox{red!20}{#1} }

and the source of the spacing is now evident.

However, there's also another catch: \colorbox adds a padding around the text; its thickness is controlled by the parameter \fboxsep.

Here is a more complex definition for taking into account a problem that will be clear from the last example:

\documentclass{article}
\usepackage{xcolor}
\newcommand{\reducedstrut}{\vrule width 0pt height .9\ht\strutbox depth .9\dp\strutbox\relax}
\newcommand{\pink}[1]{%
  \begingroup
  \setlength{\fboxsep}{0pt}%  
  \colorbox{red!20}{\reducedstrut#1\/}%
  \endgroup
}

\begin{document}
laugh\pink{ing}

laugh\pink{ing}

lau\pink{f}ing

lau\begingroup\fboxsep=0pt\colorbox{red!20}{\reducedstrut f}\endgroup ing
\end{document}

I've used \reducedstrut in order that the colored box does not touch possible other colored boxes in the lines above or below. With \strut, instead of \reducedstrut, the colored boxes in the first two lines would merge. Finally, the "italic correction" \/ ensures that the whole contents is covered by the pink background, as it's clear from the last two lines (the last one has the same construction, but without \/).

enter image description here

share|improve this answer

Okay, I've answered my own question. In the original code I'd used some new lines to make the macro more readable;

\newcommand{\pink}[1]{
\colorbox{red!20}{#1}
}

I didn't realise these would affect the final formatting.

The new version;

\newcommand{\pink}[1]{\colorbox{red!20}{#1}}

removes the gap before the colorbox. However, there is still some extra space within the colorbox before the first letter, which I'd quite like to trim.

share|improve this answer
    
I have gotten in the habit of adding a % after an open { as I got bit with this before. See Tex Capacity Exceeded (if remove % after use of macro). Took me quite some time to get the MWE. –  Peter Grill Sep 28 '12 at 8:23

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.