![TikZ:节点两侧的项目符号](https://linux22.com/image/470081/TikZ%EF%BC%9A%E8%8A%82%E7%82%B9%E4%B8%A4%E4%BE%A7%E7%9A%84%E9%A1%B9%E7%9B%AE%E7%AC%A6%E5%8F%B7.png)
这是我正在为论文研究的图表的 MWE:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,backgrounds}
\begin{document}
\begin{tikzpicture}
[
item/.style={circle, fill, minimum width=2pt, inner sep=0pt},
group/.style={rectangle, draw},
]
% group 1
\node (item1) [item, label=left:item 1] {};
\node (item2) [item, label=left:item 2, below=12pt of item1] {};
% group 2
\node (item3) [item, label=right:item 3, right=60pt of item1] {};
\node (item4) [item, label=right:item 4, below=12pt of item3] {};
\begin{scope}[on background layer]
% groups
\node (group1) [fit=(item1) (item2), group, label=above:group 1, xshift=-27pt, minimum width=80pt, inner sep=12pt] {};
\node (group2) [fit=(item3) (item4), group, label=above:group 2, xshift=27pt, minimum width=80pt, inner sep=12pt] {};
\end{scope}
\end{tikzpicture}
\end{document}
有几组(画成矩形),每组包含一些项目。每个项目旁边都有一个要点。
我的问题:我怎样才能在项目的两侧复制项目要点(即,当它们在右侧时,我希望它们也同时在左侧)?谢谢,
查理
答案1
将每条线放在 80pt 节点中lineitem
,并用来path picture
创建两个圆圈。
([xshift=-2pt]item1.east)
编辑:我们可以通过将 修改outer sep=2pt
为 来定位具有 和 的点
outer sep=0pt
,
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,backgrounds}
\begin{document}
\begin{tikzpicture}
[
%item/.style={circle, fill,red, minimum width=2pt, inner sep=0pt},
lineitem/.style={
%draw,
minimum width=80pt,
inner sep=0pt,
outer sep=0pt,%<-- modif
path picture = {
\draw [fill] ([xshift=2pt]path picture bounding box.west) circle (1pt);
\draw [fill] ([xshift=-2pt]path picture bounding box.east) circle (1pt);
}
},
group/.style={rectangle, draw},
]
% group 1
\node [lineitem] (item1) {item 1};
\node [lineitem,below=12pt of item1] (item2) {item 2};
% group 2
\node [lineitem, right=60pt of item1] (item3) {item 3};
\node [lineitem, below=12pt of item3] (item4) {item 4};
\begin{scope}[on background layer]
% groups
\node (group1) [fit=(item1) (item2), group, label=above:group 1minimum,inner sep=12pt] {};
\node (group2) [fit=(item3) (item4), group, label=above:group 2,inner sep=12pt] {};
\end{scope}
\draw ([xshift=-2pt]item1.east)--([xshift=2pt]item4.west);
\end{tikzpicture}
\end{document}
答案2
mid west
我将使用和锚点为项目符号添加标签,mid east
使其垂直对齐。通过使这些标签的名称取决于\tikzlastnode
,它们将自动根据其父节点的名称进行命名。
我在下面的例子中使用矩阵来命名节点<matrix name>-<row>-<column>
,因此项目符号被命名<matrix name>-<row>-<column>-w
为“---e”,但上面的内容适用于每个命名节点。
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{matrix, positioning, quotes}
\tikzset{
group with itemize/.style={
matrix of nodes, draw, nodes={itemize, text depth=+0pt}},
itemize/.style={
append after command={
[every node/.code=] % empty “every node”
nodealso[
label={[every item=weast,xshift=+-.15em]mid west:},
label={[every item=ewest,xshift=+ .15em]mid east:}](\tikzlastnode)}},
every item/.style 2 args={
shape=circle, inner sep=+0pt, outer sep=+0pt,
minimum size=+.6ex, fill, name=\tikzlastnode-#1, anchor=#2}}
\begin{document}
\tikz
\node[group with itemize, "group 1"] (group1) {
item 1 \\
item 2 \\
}
node[group with itemize, "group 2", right=of group1] (group2) {
Foo Long Text Bar \\
item 4 \\
}
(group1-1-1-e) edge[red] (group2-2-1-w)
;
\end{document}
输出
答案3
像这样:
代码(仅 tikz - 没有库):
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node at(4,3) () {group1};
\node at(9,3) () {group2};
\draw (2,1) rectangle (6,2.5);
\draw (7,1) rectangle (11,2.5);
\filldraw[draw=white] (2.5,2) circle(2pt) -- (5.5,2) circle(2pt) node[pos=.5,fill=white] () {item 1};
\filldraw[draw=white] (2.5,1.5) circle(2pt) -- (5.5,1.5) circle(2pt) node[pos=.5,fill=white] () {item 2};
\filldraw[draw=white] (7.5,2) circle(2pt) -- (10.5,2) circle(2pt) node[pos=.5,fill=white] () {item 3};
\filldraw[draw=white] (7.5,1.5) circle(2pt) -- (10.5,1.5) circle(2pt) node[pos=.5,fill=white] () {item 4};
\end{tikzpicture}
\end{document}
您可以使图片更紧凑——更改坐标的节点。