In the code posted below, I am using varwidth to shrink my tcolorbox to the text width for short text lines. However, I also have some wraparound text lines and the tcolorbox is too big in that case. How can I retain the shrink-to-fit behavior on short text snippets while changing the maximum margins of the tcolorbox? I would like the maximum margins of the gray area to be slightly less than the regular paragraph margins on both sides, but I also want to keep is shrinking to fit when the text length is smaller than that.
\documentclass[preprint]{revtex4-2} \usepackage{lipsum} \usepackage{setspace} \usepackage{varwidth} \usepackage{tcolorbox}\tcbuselibrary{breakable}\newsavebox{\myboxbox}\NewDocumentEnvironment{mybox}{+b}% find a better name{% \centering \setlength{\parindent}{0pt}% % measure the text width \sbox{\myboxbox}{\begin{varwidth}{\columnwidth}#1\end{varwidth}}% \begin{tcolorbox}[ breakable, width=\dimexpr\wd\myboxbox+8mm, top=3mm, bottom=4mm, ] #1 \end{tcolorbox}%}{}\begin{document}\lipsum[1]\\\begin{mybox} \begin{singlespace} \lipsum[1] \end{singlespace}\end{mybox}\begin{mybox} \begin{singlespace} Just a few words here. Just a few words here. \end{singlespace}\end{mybox}\end{document}