[Tex/LaTex] width of colorbox

colorbox

I was wondering if there's a way to set the length of a colorbox. For example:

enter image description here

As you can see, the right edge of the box is a bit too much (and the left edge too), and I just want the edges to exactly fit the text.

The two right square brackets have too much of a space in between them (looks like ] ]),but my hope would be something more like ]]).

This is my current code.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{times}
\usepackage{framed, color}
\definecolor{shadecolor}{gray}{0.85}
\title{}
\author{}
\date{}

\begin{document}
\section{Example}

[\textsubscript{CP\textsubscript{2}} Ming4zai2 \colorbox{shadecolor}{[\textsubscript{CP} laa3 [\textsubscript{TP} \textit{t} zau2-zo2]}]

\end{document}

Can anyone help improve this? Thanks!

Best Answer

You can set \fboxsep to 0pt and then increase the height of the material inside manually if you need.

\documentclass{article}
\usepackage{xcolor}
\setlength{\fboxsep}{0pt}
\begin{document}
\colorbox{black!15}{[\textsubscript{CP} laa3 [\textsubscript{TP} \textit{t}
zau2-zo2]\strut}]

\bigskip

\colorbox{black!15}{[\textsubscript{CP} laa3 [\textsubscript{TP} \textit{t}
zau2-zo2]\vrule height 4mm depth 2.5mm width 0mm}]
\end{document}

output

Related Question