![使用 \tcbsetmacrotoheightofnode 和 tcolorbox 中的维度](https://linux22.com/image/363959/%E4%BD%BF%E7%94%A8%20%5Ctcbsetmacrotoheightofnode%20%E5%92%8C%20tcolorbox%20%E4%B8%AD%E7%9A%84%E7%BB%B4%E5%BA%A6.png)
根据我关于尺寸问题的巧妙回答tcolorbox
,tcolorbox-4 中的精确尺寸,我想获得一个改进,在我看来,这对于可读性很重要,它使垂直条与框节点的高度一样长......我记得.tex
答案中给出的文件:
\documentclass{book}
\usepackage[most]{tcolorbox}
\usepackage{varwidth}
\tcbuselibrary{skins,breakable}
\usepackage{polyglossia}
\setmainlanguage{english}
\newcommand{\myboxnode}[2]{%
\node[fill=#2!5!white, draw=#2!75!black, line width=.5pt,
below right, minimum height=2em,
text width=.8cm, align=center,
font=\bfseries] at ([xshift=3mm,yshift=-1mm]frame.north east) {#1 \thetcbcounter}}
\tcbset{
mystyle/.style 2 args={
enhanced jigsaw, breakable,
colback=white, colframe=white,
sharp corners,
boxrule = 0mm,
top=2mm, bottom=2mm, left=2mm, right=2mm,
% varwidth boxed title,
tikznode boxed title={minimum width=\textwidth/4-15mm-4.5mm},
attach boxed title to top right={%
xshift=.5mm,yshift=- \tcboxedtitleheight+.5mm},
boxed title style={%
enhanced,
sharp corners,
colframe=#1!75!black,
left=1mm, right=1mm,
boxrule=.5mm},
colbacktitle=#1!5!white,
coltitle=black,
extrude right by=-15mm,
underlay unbroken and first={%
\myboxnode{#2}{#1};
\draw [#1!75!black,line width=.5mm]([shift={(15mm-.5\pgflinewidth,-15mm+.5\pgflinewidth)}]frame.north east)|-([shift={(-\textwidth/4+15mm,+.5\pgflinewidth)}]frame.north east);
\draw [#1!75!black,line width=.5mm]([shift={(.5\pgflinewidth,-15mm+.5\pgflinewidth)}]frame.north west)|-([shift={(\textwidth/4,+.5\pgflinewidth)}]frame.north west); },
underlay unbroken and last={%
\draw [#1!75!black,line width=.5mm]([yshift=.25mm]frame.south west)(\textwidth/4,0)--(0,0);
\draw [#1!75!black,line width=.5mm]([xshift=.25mm,yshift=.25mm]frame.south west)--+(0,15mm);
\draw [#1!75!black,line width=.5mm]([xshift=14.75mm,yshift=.25mm]frame.south east)--+(0,15mm);
\draw [#1!75!black,line width=.5mm]([yshift=.25mm]frame.south east)(3*\textwidth/4,0)--(\textwidth,0);
},
},
mytitle/.style 2 args={
code={%
\ifstrempty{#1}
{\tcbset{
before upper={\parshape 4
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \hsize}}}
{\tcbset{
title={#1},
before upper={\vspace*{\baselineskip}\parshape 3
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \hsize},
underlay unbroken and last={
\draw [#2!75!black,line width=.5mm](frame.south east)--++(180:\tcboxedtitlewidth-\pgflinewidth);
}}}
},
}
}
\newtcolorbox[auto counter]{proposition}[2][]{%
mystyle={blue}{Prop},
mytitle={#2}{blue},
#1}
\newtcolorbox[auto counter]{definition}[2][]{%
mystyle={green}{Def},
mytitle={#2}{green},
#1}
\begin{document}
\pagestyle{empty}
\begin{definition}{}
A non-titled definition
\vspace{2.2cm}
\end{definition}
\begin{proposition}{A long title}
A titled proposition
\vspace{2cm}
\end{proposition}
\begin{definition}[height=2cm,label=Exlab]{A}
A titled definition
\end{definition}
\ref{Exlab}
\end{document}
以及使用 xelatex 编译的结果:
现在我想让长度为 15 毫米的垂直条具有 boxnode 的高度。我在 tcolorbox 的文档中找到了12.9 提取节点维度,命令\tcbsetmacrotoheightofnode
,似乎可以完成这项工作,但我不能使用它。经过一番谷歌搜索,我发现了这个问题如何获取 tcolorbox 中框的自然高度,其中的答案谈到了它的用途,但我无法将它应用于我的问题。所以我的问题是:如何用 boxnode(在程序中称为 myboxnode)的高度替换盒子中的 15mm 尺寸?
我在代码中尝试了类似这样的片段,但编译没有任何结果...它无限期地运行!
\tcbsetmacrotoheightofnode\myheight{\myboxnode{#2}{#1}},
underlay unbroken and first={%
\myboxnode{#2}{#1};
\draw [#1!75!black,line width=.5mm]([shift={(15mm-.5\pgflinewidth,-\dimexpr\myheight+.5\pgflinewidth)}]frame.north east)|-([shift={(-\textwidth/4+15mm,+.5\pgflinewidth)}]frame.north east);
\draw [#1!75!black,line width=.5mm]([shift={(.5\pgflinewidth,-\dimexpr\myheight+.5\pgflinewidth)}]frame.north west)|-([shift={(\textwidth/4,+.5\pgflinewidth)}]frame.north west); },
underlay unbroken and last={%
\draw [#1!75!black,line width=.5mm]([yshift=.25mm]frame.south west)(\textwidth/4,0)--(0,0);
\draw [#1!75!black,line width=.5mm]([xshift=.25mm,yshift=.25mm]frame.south west)--+(0,\dimexpr\myheight);
\draw [#1!75!black,line width=.5mm]([xshift=14.75mm,yshift=.25mm]frame.south east)--+(0,\dimexpr\myheight);
\draw [#1!75!black,line width=.5mm]([yshift=.25mm]frame.south east)(3*\textwidth/4,0)--(\textwidth,0);
},
答案1
\tcbsetmacrotoheightofnode
需要节点的名称,而不是节点本身的整个定义。例如,如果您执行 \node (A) at (1,1) {Foobar};
或\node [name=A] at (1,1) {Foobar};
,则A
是节点的名称,并且
\tcbsetmacrotoheightofnode\myheight{A}
将给出节点的高度。
因此你需要修改你的\myboxnode
宏,例如
\newcommand{\myboxnode}[2]{%
\node[fill=#2!5!white, draw=#2!75!black, line width=.5pt,
below right, minimum height=2em,
text width=.8cm, align=center,
font=\bfseries] (tmpbox) at ([xshift=3mm,yshift=-1mm]frame.north east) {#1 \thetcbcounter}}
注意添加(tmpbox)
。然后您可以添加
\tcbsetmacrotoheightofnode\myheight{tmpbox}
之后立即上线\myboxnode{#2}{#1}
,稍后使用\myheight
。
完整代码(请注意,\dimexpr
您不再需要):
\documentclass{book}
\usepackage[most]{tcolorbox}
\usepackage{varwidth}
\tcbuselibrary{skins,breakable}
%\usepackage{polyglossia}
%\setmainlanguage{english}
\newcommand{\myboxnode}[2]{%
\node[fill=#2!5!white, draw=#2!75!black, line width=.5pt,
below right, minimum height=2em,
text width=.8cm, align=center,
font=\bfseries] (tmpbox) at ([xshift=3mm,yshift=-1mm]frame.north east) {#1 \thetcbcounter}}
\tcbset{
mystyle/.style 2 args={
enhanced jigsaw, breakable,
colback=white, colframe=white,
sharp corners,
boxrule = 0mm,
top=2mm, bottom=2mm, left=2mm, right=2mm,
% varwidth boxed title,
tikznode boxed title={minimum width=\textwidth/4-15mm-4.5mm},
attach boxed title to top right={%
xshift=.5mm,yshift=- \tcboxedtitleheight+.5mm},
boxed title style={%
enhanced,
sharp corners,
colframe=#1!75!black,
left=1mm, right=1mm,
boxrule=.5mm},
colbacktitle=#1!5!white,
coltitle=black,
extrude right by=-15mm,
underlay unbroken and first={%
\myboxnode{#2}{#1};
\tcbsetmacrotoheightofnode\myheight{tmpbox}
\draw [#1!75!black,line width=.5mm]
([shift={(15mm-.5\pgflinewidth, -\myheight+.5\pgflinewidth)}]frame.north east) |-
([shift={(-\textwidth/4+15mm,+.5\pgflinewidth)}]frame.north east);
\draw [#1!75!black,line width=.5mm]
([shift={(.5\pgflinewidth,-\myheight+.5\pgflinewidth)}]frame.north west) |-
([shift={(\textwidth/4,+.5\pgflinewidth)}]frame.north west);
\draw [#1!75!black,line width=.5mm] ([yshift=.25mm]frame.south west)(\textwidth/4,0) -- (0,0);
\draw [#1!75!black,line width=.5mm] ([xshift=.25mm,yshift=.25mm]frame.south west) -- +(0,\myheight);
\draw [#1!75!black,line width=.5mm] ([xshift=14.75mm,yshift=.25mm]frame.south east) -- +(0,\myheight);
\draw [#1!75!black,line width=.5mm] ([yshift=.25mm]frame.south east)(3*\textwidth/4,0) -- (\textwidth,0);
},
},
mytitle/.style 2 args={
code={%
\ifstrempty{#1}
{\tcbset{
before upper={\parshape 4
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \hsize}}}
{\tcbset{
title={#1},
before upper={\vspace*{\baselineskip}\parshape 3
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \hsize},
underlay unbroken and last={
\draw [#2!75!black,line width=.5mm](frame.south east)--++(180:\tcboxedtitlewidth-\pgflinewidth);
}}}
},
}
}
\newtcolorbox[auto counter]{proposition}[2][]{%
mystyle={blue}{Prop},
mytitle={#2}{blue},
#1}
\newtcolorbox[auto counter]{definition}[2][]{%
mystyle={green}{Def},
mytitle={#2}{green},
#1}
\begin{document}
\pagestyle{empty}
\begin{definition}{}
A non-titled definition
\vspace{2.2cm}
\end{definition}
\begin{proposition}{A long title}
A titled proposition
\vspace{2cm}
\end{proposition}
\begin{definition}[height=2cm,label=Exlab]{A}
A titled definition
\end{definition}
\ref{Exlab}
\end{document}