Tcolorbox 可缩小到内容,但仍提供文本换行

Tcolorbox 可缩小到内容,但仍提供文本换行

我有一个盒子,myBox,它可以缩小到内容,也可以确保将内容包装在页面边界内。有没有办法同时做到这两点?

\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}

产生这个。上面的框是正确的,下面的框是错误的。

图片1

尽管

\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}

产生这个。上面的框是错误的,下面的框是正确的。

图片2

答案1

我认为您正在寻找的选项是tcbox width=auto limitedtcolorbox手册,第 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}

截屏

相关内容