我正在处理以下文档,并希望红色标题框垂直居中,位于文本框顶部规则的正中间。当然,我可以通过将 y 偏移调整为适当的值来手动执行此操作,但我希望更自动化,因为我可能会决定在某个时候更改框标题(例如,我可能会选择将它们编号为属性 1.1、示例 1.2 等)。我认为也许将节点高度移动一半就可以做到这一点,尽管我可能错了。但是,我不知道如何指定节点的高度并将其应用于 y 偏移。我该如何实现这一点?
\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\newcommand{\eqtitle}[2]{\large \textbf{#1 #2}}
\begin{document}
\begin{tcolorbox}[
enhanced,
colback=white,
colframe=black,
arc=10pt,
top=11pt,
left=11pt,
right=11pt,
boxsep=0pt,
bottom=11pt,
boxrule=1pt,
drop fuzzy shadow,
overlay={
\node[inner sep=3pt,anchor=north west,fill=red,text=white,font=\bfseries, draw=black, line width=1pt, rounded corners=4pt] at ([xshift=11pt,yshift=0pt]frame.north west) {Property};
}
]
This is a property.
\end{tcolorbox}
\end{document}
附言:我知道这是为 tcolorbox 指定框架/轮廓(作为覆盖)和标题的一种不寻常的方式,但我这样做是出于其他(不相关的)原因,我不会深入讨论。
答案1
如果我正确理解了您想要的输出,那么可以通过以下设置轻松实现。将锚点设置为center
节点位置frame.north
:
\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\newcommand{\eqtitle}[2]{\large \textbf{#1 #2}}
\begin{document}
\begin{tcolorbox}[
enhanced,
colback=white,
colframe=black,
arc=10pt,
top=11pt,
left=11pt,
right=11pt,
boxsep=0pt,
bottom=11pt,
boxrule=1pt,
drop fuzzy shadow,
overlay={
\node[inner sep=3pt,anchor=center,fill=red,text=white,font=\bfseries, draw=black, line width=1pt, rounded corners=4pt] at (frame.north) {Property};
}
]
This is a property.
\end{tcolorbox}
\end{document}
更新:为了实现您想要的效果,您可以将锚点设置为
center
并在叠加绘制之前测量属性框的宽度,然后将框宽度的 0.5 添加到该xshift
值。
\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\newcommand{\eqtitle}[2]{\large \textbf{#1 #2}}
\begin{document}
\begin{tcolorbox}[
enhanced,
colback=white,
colframe=black,
arc=10pt,
top=11pt,
left=11pt,
right=11pt,
boxsep=0pt,
bottom=11pt,
boxrule=1pt,
drop fuzzy shadow,
before upper=\global\sbox0{\tikz{\node[inner sep=3pt,font=\bfseries, draw=black, line width=1pt, rounded corners=4pt] at (0,0) {Property have longer title};}},
overlay={
\node[inner sep=3pt,anchor=center,fill=red,text=white,font=\bfseries, draw=black, line width=1pt, rounded corners=4pt] at ([xshift=11pt+0.5\wd0,yshift=0pt]frame.north west) {Property have longer title};
}
]
This is a property.
\end{tcolorbox}
\end{document}