tcolorbox 框标题中的换行符

tcolorbox 框标题中的换行符

我有一个类似于以下的文档:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}
\usepackage[most]{tcolorbox}

\newtcolorbox{definition}[2][]{
    arc=5mm,
    lower separated=false,
    fonttitle=\bfseries,
    colbacktitle=green!10,
    coltitle=green!50!black,
    enhanced,
    attach boxed title to top left={xshift=0.5cm,
            yshift=-3mm},
    colframe=green!50!black,
    colback=green!10,
    title=#2}

\begin{document}
\begin{definition}{Assume we have a title that is waaaaaaay too long to fit in a single line and therefore requires a line break}
\blindtext
\end{definition}
\end{document}

问题截图 如您所见,标题太长,无法写成一行。如何在方框标题内创建换行符?我尝试使用\\,但不起作用。

答案1

作为一种解决方法,您可以将标题放在\parbox相应大小中:

\documentclass{article}
\usepackage{blindtext}
\usepackage[most]{tcolorbox}

\newtcolorbox{definition}[2][]{
    arc=5mm,
    lower separated=false,
    fonttitle=\bfseries,
    colbacktitle=green!10,
    coltitle=green!50!black,
    enhanced,
    attach boxed title to top left={xshift=0.5cm,
            yshift=-3mm},
    colframe=green!50!black,
    colback=green!10,
    title=#2}

\begin{document}

\begin{definition}{\parbox{10.45cm}{Assume we have a title that is waaaaaaay too long to fit in a single line and therefore requires a line break}}
\blindtext
\end{definition}

\end{document} 

在此处输入图片描述

答案2

tcolorbox为长标题定义了几种解决方案:minipage boxed title,,varwidth boxed titletikznode boxed title

以下是第一个示例:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}
\usepackage[most]{tcolorbox}

\newtcolorbox{definition}[2][]{
    arc=5mm,
    lower separated=false,
    fonttitle=\bfseries,
    colbacktitle=green!10,
    coltitle=green!50!black,
    enhanced,
    minipage boxed title*=-3cm,
    attach boxed title to top left={xshift=.5cm, yshift*=-\tcboxedtitleheight/2},
    colframe=green!50!black,
    colback=green!10,
    title=#2}

\begin{document}
\begin{definition}{Assume we have a title that is waaaaaaay too long to fit in a single line and therefore requires a line break}
\blindtext
\end{definition}
\end{document}

在此处输入图片描述

相关内容