我正在尝试使用 Tikz 制作数组图。到目前为止,我的结果如下所示:
如您所见,我有两个问题:框与节点的内容成比例,并且我没有整体框大小。(以最后三个框为例)此外,文本“k-elements”位于数组图和括号之间。它应该在括号下方。
代码:
\begin{tikzpicture}
[
-{Stealth[length=2.5pt]},
MyStyle/.style={draw, text width=25pt, text height=10pt, text centered},
my arrow/.style={shape=single arrow, rotate=90, inner sep=5pt, outer sep=0pt, single arrow head extend=0pt, minimum height=7.5pt, text width=0pt, draw=blue!50, fill=blue!25}
]
\begin{scope} [start chain, node distance=0pt]
\node [MyStyle,on chain] (1) {$1$};
\node [MyStyle,on chain] (2) {$2$};
\node [MyStyle,on chain] (3) {$3$};
\node [MyStyle,on chain] (4) {$4$};
\node [MyStyle,on chain] (5) {$\cdots$};
\node [MyStyle,on chain] (6) {$k$};
\node [MyStyle,on chain] (7) {$\cdots$};
\node [MyStyle,on chain] (8) {$n-2$};
\node [MyStyle,on chain] (9) {$n-1$};
\node [MyStyle,on chain] (10) {$n$};
\end{scope}
\draw (1.north) [out=25, in=155] to (2.north);
\draw (1.north) [out=30, in=155] to (3.north);
\draw (1.north) [out=35, in=155] to (4.north);
\draw (1.north) [out=40, in=155] to (6.north);
\draw [decorate,decoration={brace,amplitude=10pt,mirror},xshift=-4pt,yshift=0pt]
(0.7,-0.27) -- (6.6,-0.27)node[black,midway,xshift=9pt] {$k$-elements} ;
\end{tikzpicture}
(代码基于带箭头且外部有单元格的数组和在 TikZ 中绘制花括号)
还有其他问题:有没有关于 Tikz 的好的介绍?或者文档是最好的起点?
谢谢大家的帮助!
答案1
测试新的 LaTeX 编辑器 我的答案太迟了。它几乎与 @ferahfeza 的答案相同。细微的差别在于箭头和括号的定义:
\documentclass[tikz,margin=10pt]{standalone}
\usetikzlibrary{arrows.meta,chains,%
decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[
% -{Stealth[length = 2.5pt]},
start chain = going right,
node distance = 0pt,
MyStyle/.style={draw, minimum width=2em, minimum height=2em,
outer sep=0pt, on chain},
]
\node [MyStyle] (1) {$1$};
\node [MyStyle] (2) {$2$};
\node [MyStyle] (3) {$3$};
\node [MyStyle] (4) {$4$};
\node [MyStyle] (5) {$\cdots$};
\node [MyStyle] (6) {$k$};
\node [MyStyle] (7) {$\cdots$};
\node [MyStyle] (8) {$n-2$};
\node [MyStyle] (9) {$n-1$};
\node [MyStyle] (10) {$n$};
\begin{scope}[-{Stealth[length = 2.5pt]}]
\draw (1.north) [out=25, in=155] to (2.north);
\draw (1.north) [out=30, in=155] to (3.north);
\draw (1.north) [out=35, in=155] to (4.north);
\draw (1.north) [out=40, in=155] to (6.north);
\end{scope}
\draw[decorate,decoration={brace, amplitude=10pt, raise=5pt, mirror}]
(2.south west) to node[black,midway,below= 15pt] {$k$-elements} (7.south east);%
\end{tikzpicture}
\end{document}
用数字命名节点不是一个好主意,在某些情况下可能会导致意外错误。
要学习 TikZ,请阅读 TikZ 手册中的“TikZ is kein Zeichenprogramm”一章。其中有一些(非常冗长的)TikZ 手册的摘要。还提供了一些有用的示例http://www.texample.net/
答案2
对于这种方案,matrix
可以提供帮助。至少它可以节省您的一些输入。
\documentclass[tikz,margin=10pt]{standalone}
\usetikzlibrary{arrows.meta,matrix,%
decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[
MyStyle/.style={draw, minimum width=2em, minimum height=2em,
outer sep=0pt},
]
\matrix (A) [matrix of math nodes, nodes={MyStyle, anchor=center}, column sep=-\pgflinewidth]
{1 & 2 & 3 & 4 & \cdots & k & \cdots & n-2 & n-1 & n\\};
\begin{scope}[-{Stealth[length = 2.5pt]}]
\draw (A-1-1.north) [out=25, in=155] to (A-1-2.north);
\draw (A-1-1.north) [out=30, in=155] to (A-1-3.north);
\draw (A-1-1.north) [out=35, in=155] to (A-1-4.north);
\draw (A-1-1.north) [out=40, in=155] to (A-1-5.north);
\end{scope}
\draw[decorate,decoration={brace, amplitude=10pt, raise=5pt, mirror}]
(A-1-1.south west) to node[black,midway,below= 15pt] {$k$-elements} (A-1-6.south east);%
\end{tikzpicture}
\end{document}
答案3
这里添加一个略有不同的解决方案。您可以将其与内容最大的节点对齐,而不是手动设置节点大小。您可以通过将以下内容添加到节点样式来实现此目的。
minimum height=\heightof{$n-2$}+2*2*1mm)
您还可以使用 foreach 命令来减少代码量。您可以通过阅读Tikz 手册(v 3.0.0)。
输出
代码
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usepackage{graphics}
\usetikzlibrary{arrows.meta,decorations,decorations.pathreplacing,calc,bending,positioning, chains}
\tikzset{
MyStyle/.style={draw, text width=25pt, text height=10pt, text centered,minimum height=\heightof{$n-2$}+2*2*1mm)},
myarrow/.style={shape=single arrow, rotate=90, inner sep=5pt, outer sep=0pt,
single arrow head extend=0pt, minimum height=7.5pt, text width=0pt, draw=blue!50, fill=blue!25}
}
\begin{document}
\begin{tikzpicture}[
-{Stealth[length=2.5pt]}
]
\begin{scope} [start chain, node distance=-.5pt]
\foreach \name [count=\xi] in {1,2,3,4,\cdots,k,\cdots,n-2,n-1,n}{
\node[MyStyle, on chain] (n\xi) {$\name$};
}
\end{scope}
\draw (n1.north) [out=25, in=155] to (n2.north);
\draw (n1.north) [out=30, in=155] to (n3.north);
\draw (n1.north) [out=35, in=155] to (n4.north);
\draw (n1.north) [out=40, in=155] to (n6.north);
\draw [decorate,decoration={brace,amplitude=10pt,mirror}]
(n1.south east) -- (n7.south west) node[black,midway,below=8pt] {$k$-elements};
\end{tikzpicture}
\end{document}