5

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!

4

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

4

A TikZy alternative to David Koala Purton's nice answer.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\tikzmarknode[fill=gray!30,inner xsep=-0.1em]{X}{[\textsubscript{CP} laa3 [\textsubscript{TP} \textit{t} zau2-zo2]}]
\end{document}

enter image description here

4

You can use adjustbox as a replacement for \colorbox and use the margin and bgcolor keys to get the frame separation only on top and bottom but not (or less) on left and right.

The following sets the left/right margin to 0pt and the top/bottom margin to the same as with \colorbox.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{times}
\usepackage{color}
\usepackage{adjustbox}
\definecolor{shadecolor}{gray}{0.85}

\newcommand\mycolorbox[1]{\adjustbox{margin=0pt {\fboxsep},bgcolor=#1}}

\begin{document}
\section{Example}

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

\end{document}

enter image description here

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.