我用这个巧妙的答案来回答我的问题,使用 \tcbsetmacrotoheightofnode 和 tcolorbox-2 中的维度,使用 tcolorbox 生成环境,我想实现它的变体。更准确地说,在我的旧帖子中,环境标题框通过boxnode
使用命令设置宽度的最大值来“垂直”放置text width=width("#1")*1.2
。现在,我的目标是将标题放在一行上,所以我避免使用此命令。之后,必须在框后安排 parshape,并考虑到宽度。boxtitle
我尝试使用类似于使用命令的命令\tcbsetmacrotoneightofnode
,所以我尝试了以下命令:
\tcbsetmacrotowidthofnode\BBB{tmpbox}
\global\let\myboxwidth\BBB
和命令\pgfmathsetlengthmacro{\parwidth}{\hsize-\myboxwidth}
而不是\pgfmathsetlengthmacro{\parwidth}{\hsize-(width("#2")+0.666em+10mm)}
,但我有一个错误。似乎长度的使用\myboxwidth
在内部被拒绝set parshape/.code={..
,但在underlay unbroken and last={..
例如 内部被接受!
那么,我必须如何定义\myboxwidth
命令才能将其用于 parshape,以便在框后以固定长度缩进文本,即使宽度可变?
我给出使用的文件:
\documentclass{book}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}
\usetikzlibrary{calc} % for the let-syntax
\tcbuselibrary{skins,breakable}
%\usepackage{polyglossia}
%\setmainlanguage{english}
\newlength\LineW
\setlength\LineW{0.5mm}
\tikzset{
frameline/.style={
draw=#1!75!black,line width=\LineW
}
}
\newcommand{\myboxnode}[2]{%
\node[
fill=#2!5!white, draw=#2!75!black, line width=.5pt,
below left, minimum height=2em,
% text width=width("#1")*1.2,
align=center,
font=\bfseries
] (tmpbox) at
([xshift=-1mm-0.5\LineW,yshift=-1mm-0.5\LineW]frame.north east)
{#1 \thetcbcounter}}
\tcbset{
mystyle/.style n args={4}{
enhanced jigsaw,
breakable,
colback=white,
opacityframe=0,
top=2mm, bottom=2mm,
left=2mm, right=2mm,
% drawing corners on top and title
underlay unbroken and first={%
\myboxnode{#2}{#1};
\tcbsetmacrotoheightofnode\HHH{tmpbox}
% \global\let so it's available outside the underlay unbroken and first
\global\let\myheight\HHH
\tcbsetmacrotowidthofnode\BBB{tmpbox}
\global\let\myboxwidth\BBB
%
\ifstrempty{#4}{ % if no title
% draw top right frame
\draw [frameline={#1}]
([shift={(0, -\myheight)}]frame.north east) |-
([shift={(-\textwidth/4,0)}]frame.north east);
}{ % if title
% add node with title
\node [anchor=north east,
inner xsep=2mm,
fill=#1!5!white,
frameline={#1}
]
(titlenode)
at
([xshift=-#3,yshift=0.5\pgflinewidth]frame.north -| tmpbox.west)
{#4};
% draw top right frame
\draw [frameline={#1}]
([yshift=-0.5\pgflinewidth]titlenode.north east) -|
([yshift=-\myheight]frame.north east);
% calculate distance from corner of frame to opposite corner of title
% save in \mywidth
\path let \p1=(frame.north east), \p2=(titlenode.north west), \n1={\x1-\x2} in
\pgfextra{\pgfmathsetlengthmacro{\WWW}{\n1}\global\let\mywidth\WWW}
;
}
% draw top left frame
\draw [frameline={#1}]
([shift={(0,-\myheight)}]frame.north west) |-
([shift={(\textwidth/4,0)}]frame.north west);
},
% if box breaks across pages, draw corners on last part
underlay unbroken and last={%
\ifstrempty{#4}{ % if no title
% draw bottom right frame
\draw [frameline={#1}]
([shift={(0, \myheight)}]frame.south east) |-
([shift={(-\textwidth/4,0)}]frame.south east);
}{
% draw bottom right frame
\draw [frameline={#1}]
([xshift=-\mywidth]frame.south east) -|
([yshift=\myheight]frame.south east);
}
% draw bottom left frame
\draw [frameline={#1}]
([yshift=\myheight]frame.south west) |-
([xshift=\textwidth/4]frame.south west);
},
set parshape/.code={%
% 0.666em is twice the inner sep
% 6mm is 2mm from left, 2mm from right, 1mm right of box, 1mm left of box
\pgfmathsetlengthmacro{\parwidth}{\hsize-(width("#2")+0.666em+10mm)}
\ifstrempty{##1}
{\tcbset{
before upper={\parshape 4
0pt \parwidth
0pt \parwidth
0pt \parwidth
0pt \hsize}
}
}
{\tcbset{
before upper={\vspace*{\baselineskip}\parshape 3
0pt \parwidth
0pt \parwidth
0pt \hsize},
}}
},
set parshape={#4}
}
}
\newtcolorbox[auto counter]{proposition}[2][]{%
mystyle={blue}{Prop}{1mm}{#2},
#1}
\newtcolorbox[auto counter]{definition}[2][]{%
mystyle={green}{Def}{1mm}{#2},
#1}
\begin{document}
\pagestyle{empty}
\begin{definition}{}
\lipsum[1]
\end{definition}
\begin{proposition}{A long title}
\lipsum[1]
\end{proposition}
\end{document}