3

I have a box, myBox, that can either shrink to the content, or make sure to do wrap the contents inside the page boundaries. Is there a way do do both at the same time?

\documentclass{article}

\usepackage[most]{tcolorbox}

\newtcolorbox{myBox}{
enhanced,
colback=black!20,
left=15pt,
right=15pt,
top=15pt,
bottom=15pt,
capture=hbox %  <--- This line
}

\newcommand{\myBoxWrapper}[1]{
\begin{center}
  \begin{myBox}
    \Large #1
  \end{myBox}
\end{center}
}

\begin{document}
  \myBoxWrapper{A short box}
  \myBoxWrapper{This is a long sentence that's so long that it will have to get a new line or else the printer is gonna print past the paper and all over my floor}
\end{document}

produces this. The upper box is right, the bottom one is wrong.

Picture 1

while

\documentclass{article}

\usepackage[most]{tcolorbox}

\newtcolorbox{myBox}{
enhanced,
colback=black!20,
left=15pt,
right=15pt,
top=15pt,
bottom=15pt,
capture=minipage %  <--- This line
}

\newcommand{\myBoxWrapper}[1]{
\begin{center}
  \begin{myBox}
    \Large #1
  \end{myBox}
\end{center}
}

\begin{document}
  \myBoxWrapper{A short box}
  \myBoxWrapper{This is a long sentence that's so long that it will have to get a new line or else the printer is gonna print past the paper and all over my floor}
\end{document}

produces this. The upper box is wrong, the bottom one is right.

Picture 2

0

1 Answer 1

6

I think the option you're looking for is tcbox width=auto limited (tcolorbox manual, p. 100):

\documentclass{article}

\usepackage[most]{tcolorbox}

\newtcolorbox{myBox}{
tcbox width=auto limited,
capture=hbox
}

\newcommand{\myBoxWrapper}[1]{
\begin{center}
    \begin{myBox}
        \Large #1
    \end{myBox}
\end{center}
}

\begin{document}

\myBoxWrapper{A short box}
\myBoxWrapper{This is a long sentence that's so long that it will have to get a new line or else the printer is gonna print past the paper and all over my floor}

\end{document}

screenshot

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .