# colorbox inside texttt, keep alignment of text

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.

\texttt{
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:

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

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
\setlength{\fboxsep}{2pt}