![将 newtcolorbox 调整为一行或多行文本宽度](https://linux22.com/image/422654/%E5%B0%86%20newtcolorbox%20%E8%B0%83%E6%95%B4%E4%B8%BA%E4%B8%80%E8%A1%8C%E6%88%96%E5%A4%9A%E8%A1%8C%E6%96%87%E6%9C%AC%E5%AE%BD%E5%BA%A6.png)
我正在尝试制作一个tcolorbox
可调整文本宽度的“示例”。我试过了,hbox
但当有多行时就会失败。
使用hbox
无需使用hbox
预期的:
是的,我设法得到了我期望的解决方案,但创建了另一个“示例” tcolorbox
。我只想使用一个。
这是我的可编译示例(第一张图片):
\documentclass{article}
\usepackage{lipsum}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\tcbuselibrary{skins}
\newtcbtheorem[]{exa}{EXAMPLE}{
hbox,
fonttitle=\bfseries, fontupper=\normalsize,
theorem style=standard,
enhanced,frame hidden,
boxrule=0pt,
left=0.2cm,top=0.2cm, toptitle=0.1cm+1pt,bottomtitle=-0.1cm+0.5em,
bottomrule=1pt,
colback=white,coltitle=orange,
title style=white,
titlerule=1pt, titlerule style=orange,
borderline south={1pt}{0pt}{orange}
}{exa}
\begin{document}
\begin{exa}{}{}
hello
\end{exa}
\begin{exa}{}{}
\lipsum[1]
\end{exa}
\end{document}
答案1
这里
- 提供了一个
\newtcboxtheorem
基于 的新命令,并且\tcbox
- 选择键
tcbox width
扩展为接受值auto limited with title counted
,这将使输出的宽度\tcbox
为min("/tcb/width", max(upper, title))
。
默认情况下,\tcbox
定义一个新命令(\exabox
在下面的例子中),因此还提供了环境版本,这使得带星号/未编号定理的输入更容易。
\documentclass{article}
\usepackage{lipsum}
\usepackage{tcolorbox}
\tcbuselibrary{skins, theorems}
\makeatletter
\def\newtcboxtheorem{%
\let\@@newtcolorbox\newtcbox
\new@tcbtheorem}
\tcbset{
tcbox width/auto limited with title counted/.code={\tcb@set@embed@tcbox{%
\setbox\z@=\color@hbox##1\color@endbox
\setbox\@tempboxa=\color@hbox\kvtcb@fonttitle\kvtcb@before@title\kvtcb@title\kvtcb@after@title\color@endbox
\@tempdima=\wd\z@
\@tempdimb=\wd\@tempboxa
\ifdim\@tempdima<\tcb@w@upper\relax
\ifdim\@tempdima<\@tempdimb
\begin{minipage}{\@tempdimb}##1\end{minipage}%
\else
\box\z@
\fi
\else
\begin{minipage}{\tcb@w@upper}##1\end{minipage}%
\fi}}
}
\makeatother
\newtcboxtheorem{exabox}{EXAMPLE}{
tcbox width=auto limited with title counted,
fonttitle=\bfseries, fontupper=\normalsize,
theorem style=standard,
enhanced,frame hidden,
boxrule=0pt,
left=0.2cm,top=0.2cm, toptitle=0.1cm+1pt,bottomtitle=-0.1cm+0.5em,
bottomrule=1pt,
colback=white,coltitle=orange,
title style=white,
titlerule=1pt, titlerule style=orange,
borderline south={1pt}{0pt}{orange}
}{exa}
\NewDocumentEnvironment{exa}{ O{} m m +b }{%
\exabox[#1]{#2}{#3}{#4}%
}{}
\NewDocumentEnvironment{exa*}{ O{} m +b }{%
\csname exabox*\endcsname[#1]{#2}{#3}%
}{}
\begin{document}
\def\dummy{%
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
}
\subsection*{Using command \texttt{\textbackslash exabox[*]}}
\exabox{}{}{short}
\exabox{}{}{long long longer than title}
\exabox{}{}{\dummy}
\csname exabox*\endcsname{}{unnumbered}
\subsection*{Using environment \texttt{exa[*]}}
\begin{exa}{}{}
short
\end{exa}
\begin{exa}{}{}
long long longer than title
\end{exa}
\begin{exa}{}{}
\dummy
\end{exa}
\begin{exa*}{}
unnumbered
\end{exa*}
\end{document}
答案2
一种可能的方法是创建一个新的定理命令,\tcbox
然后可在此基础上使用该varwidth upper
选项。
像这样,我创建了一个新的\tcboxtheorem
宏,它可用于定义编号\exa
和\exastar
未编号定理:
\documentclass{article}
\usepackage{lipsum}
\usepackage{varwidth}
\usepackage{tcolorbox}
\tcbuselibrary{theorems,skins}
\makeatletter
% \newtcboxtheorem[init options]{name}{display name}{options}{prefix}
\newcommand{\newtcboxtheorem}[5][]{%
\newtcbox[auto counter,#1]{#2}[3][]{%
#4,
varwidth upper,
title={\tcb@theo@title{#3}{\thetcbcounter}{##2}},
list entry={\protect\numberline{\thetcbcounter}##2},
nameref={##2},
code={\tcb@theo@label{#5}{##3}},
##1
}%
\newtcbox[#1,no counter,list inside=]{#2star}[2][]{%
#4,
varwidth upper,
title={\tcb@theo@title{#3}{\@empty}{##2}},
##1}%
}
\makeatother
\newtcboxtheorem[]{exa}{EXAMPLE}{
fonttitle=\bfseries, fontupper=\normalsize,
theorem style=standard,
enhanced, frame hidden,
boxrule=0pt,
left=0.2cm, top=0.2cm, toptitle=0.1cm+1pt, bottomtitle=-0.1cm+0.5em,
bottomrule=1pt,
colback=white, coltitle=orange,
title style=white,
titlerule=1pt, titlerule style=orange,
borderline south={1pt}{0pt}{orange}
}{exa}
\begin{document}
\exa{Title}{label}{hello}
\exastar{Title}{\lipsum[1]}
\end{document}