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?
Best Answer
First of all, TeX language is not "free form", so there's a big difference between
and
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 doand 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:
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\/
).