I would like to print some hex code and annotate it with colors. I used \texttt and \colorbox, but the \colorbox adds half a space before and half a space after the text, thus the column alignment get's messed up.

AA E2 8E A2 E2 8E 9C 20 E2 8E B3 61 E2 81 B1 2D\\
62 E2 81 B1 E2 8E 9F \colorbox{red}{E2 8E A5 E2 8E AA 0A}20 20\\
32 48 E2 82 82 20 2B 20 4F E2 82 82 20 E2 87 8C\\
20 32 \colorbox{green}{48 E2 82 82 4F 2C 20}52 20 3D 20 34 2E 37\\
20 6B CE A9 2C 20 E2 8C 80 20 32 30 30 20 6D 6D\\

Here is the example:

the colorbox starts at the beginning of the next word, but half a space is inserted afterwards

Is it possible to have the columns correctly? I removed the space after the text, so the following columns are correctly aligned, but the text inside the colorbox is shifted in any case. Or before this is a XY problem, is there a method in latex how one would actually do this?

  • Maybe a dupe of tex.stackexchange.com/questions/23681/…?
    – wilx
    Feb 2 '17 at 14:50
  • 1
    Whatever those colors mean, you better define a semantic command, that way you can program easily all. Taking David's answer, you can do \newcommand*\hexcoloraux[2]{\hspace*{-\fboxsep}\textcolor{#1}{#2}\hspace*{-\fboxsep}} and then \newcommand*\hexpositive[1]{\hexcoloraux{green}{#1}} and \newcommand*\hexnegative[1]{\hexcoloraux{red}{#1}}.
    – Manuel
    Feb 2 '17 at 15:06

There is \fboxsep padding around the box, in the answers in the question linked in comments there are suggestions for locally setting \boxsep to zero but here I suspect that you want the padding but to account for it so

\hspace*{-\fboxsep}\colorbox{red}{E2 8E A5 E2 8E AA 0A}\hspace*{-\fboxsep}

You might want to reduce \fboxsep a bit as well, or not, depending


for example (the default is 3pt)

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.