我有一个形状为 的节点rectangle split
。其中,带有“32”的节点部分是主要部分,其余部分是它的注释。
问题:如何让带有“32”的节点部分成为一个正方形?
其余分割部分应保持矩形,并具有尽可能低的高度:
· 所以这就是我使用的原因\scalebox{0.25}{...}
,我不知道是否有更好的方法或更像 TikZ 的方法......
· 顺便说一句:价值0.25
确实不是必须修复。
\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\begin{tikzpicture}[font=\footnotesize\sffamily, very thin,
]
\node [rectangle split, rectangle split parts=7, draw, anchor=center,
draw, anchor=text,
inner ysep=0.5pt, %inner xsep=0.5pt,
minimum width=5mm, minimum height=5mm, % seems to have no effect...
rectangle split part fill={brown, orange, yellow, green, blue!33, red, lightgray},
name=mynode]{
\nodepart{four}\scalebox{0.25}{high (3/9)}
\nodepart{five}\scalebox{0.25}{even (1/9)}
\nodepart{six}\color{white}32
\nodepart{seven}\scalebox{0.225}{1. Place}};
\draw[<-, thick, purple] (mynode.six east) -- +(0.3,0) node[right, align=left, draw, font=\tiny, inner sep=1pt]{How to get a \\ \bfseries squared 'nodepart' \\ here? \\
(The others can \\ stay rectangled.)};
\end{tikzpicture}
\end{document}
答案1
这是一种解决方法,因为毕竟我们正在设置类型:
- 在节点文本中放置一条规则
- 宽度=0pt(或不可见),肉眼可见:高度=1.5cm,向下移动-.58cm
\textbf{\rule[-.58cm]{0pt}{1.5cm}\textcolor{white}{\LARGE{32}}}
不知何故,“通过眼睛”可以通过计算来代替。
\documentclass[10pt,border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\begin{document}
\begin{tikzpicture}
\node [rectangle split, rectangle split parts=4,draw,
rectangle split part fill=
{red!80!blue,yellow,red,white}]
{
high (3/9)
\nodepart{two}
even (1/9)
\nodepart{three}
\textbf{\rule[-.58cm]{0pt}{1.5cm}\textcolor{white}{\LARGE{32}}}
\nodepart{four}
1. Place
};
\end{tikzpicture}
\end{document}
答案2
作为 的替代方案,rectangle split
您可以使用matrix of nodes
。在这种情况下,您可以为矩阵的每个特定部分(如颜色和字体)修复选项。您只需minimum width
为所有节点修复 ,因为每个节点的大小都是独立的。
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (A) [matrix of nodes, nodes in empty cells,
row sep=-\pgflinewidth, nodes={draw, anchor=center, minimum width=2cm}]{
|[fill=brown]|\\
|[fill=orange]|\\
|[fill=yellow]|\\
|[fill=green, font=\sffamily\small]| high (3/9)\\
|[fill=cyan, font=\sffamily\small]| even (1/9)\\
|[fill=red, minimum size=2cm, font=\bfseries\sffamily\Huge, text=white]| 32\\
|[fill=gray, font=\sffamily\small]| 1. Place\\
};
\end{tikzpicture}
\end{document}
答案3
与节点部分的交互将会很棘手。
这里有一个解决方法,使用只有一个单元的矩阵,这样我们就可以使用整个构造,就好像它是一个节点(从技术上讲它也是)。但是,矩阵不能变换,甚至不能旋转。
但是,您可以使用matrix anchor
键来引用矩阵内部节点的锚点来放置矩阵,以便将子节点放置在您需要的位置。
我正在使用我的ext.node-families
图书馆这样所有子节点的宽度都相同。未记录的宏\tikzextnodefamiliesgetwidth
用于将(最小)高度设置为与该矩阵中所有节点的宽度相同的值。(如果方形节点本身的宽度高于所有节点的宽度,则需要做更多工作。)
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{ext.node-families}
\tikzset{
tight matrix/.style={
every outer matrix/.append style={inner sep=+0pt, outer sep=+0pt}},
@Rectangle Split part fill/.code={%
\edef\pgfmathcounter{\the\numexpr\pgfmathcounter+1\relax}%
\typeout{\pgfmathcounter: #1}%
\tikzset{Rectangle Split \pgfmathcounter/.append style={fill=#1}}},
Rectangle Split part fill/.style={%
/utils/exec=\def\pgfmathcounter{0},
@Rectangle Split part fill/.list={#1}},
Rectangle Split/.style={
matrix, tight matrix, path only, shape=rectangle,
node contents={
\coordinate(@);
\foreach[count=\I] \T in {#1}
\node[
name=\tikzmatrixname-\I,
node family/width=\tikzmatrixname,
every Rectangle Split node/.try,
Rectangle Split \I/.try,
alias=@, at=(@.south), anchor=north, yshift=+.5\pgflinewidth]{\T};
\\}}}
\begin{document}
\tikz
\node[
every Rectangle Split node/.style=draw, % probably a good default
Rectangle Split part fill={ % almost the same
brown, orange, yellow, green, blue!33, red, lightgray},
Rectangle Split 6/.append style={
text=white,
minimum height=\tikzextnodefamiliesgetwidth{\tikzmatrixname}},
Rectangle Split={,,,high (3/9), even (1/9), 32, 1. Place}];
\end{document}
输出
答案4
我将带有 的正方形变成rule
,phantom
并用另一个 覆盖文本node
:
\documentclass[]{book}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\begin{document}
\pgfmathsetlengthmacro\InnerSep{0.5pt}
\pgfmathsetlengthmacro\squarelength{10.24mm-2*\InnerSep}
\begin{tikzpicture}[font=\tiny\sffamily, very thin]
\node (mynode)[
rectangle split, rectangle split parts=4,
rectangle split ignore empty parts,
draw, align=center,
text width=\squarelength,
inner sep=\InnerSep,
rectangle split part fill={orange, yellow, red, lightgray}
]{
\nodepart{one} first
\nodepart{two} even \hfill \hspace{-2mm}(2/9)
\nodepart{three} \phantom{\rule[-\squarelength]{\squarelength}{\squarelength}}
\nodepart{four} 1. Place
};
\path[draw=none] (mynode.two split west) -- (mynode.three split east) node[midway, font=\sffamily\Huge\bfseries, text=white]{32};
% Test-Square
\draw[cyan] ([shift={(\pgflinewidth,\pgflinewidth)}]mynode.three split west) rectangle +(\squarelength+2*\InnerSep,\squarelength+2*\InnerSep) node[right]{Test-Square};
\end{tikzpicture}
\end{document}