
我試圖弄清楚如何用數值(1、2、3等)替換圓圈中的點
請幫助我確定需要編輯哪些程式碼行才能完成此替換。
先致謝!
mwe (h/t 佩庫斯)
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,fit}
\begin{document}
\begin{tikzpicture}
%put some nodes on the left
\foreach \x in {1,2,3}{
\node[fill,circle,inner sep=2pt] (d\x) at (0,\x) {};
}
\node[fit=(d1) (d2) (d3),ellipse,draw,minimum width=1cm] {};
%put some nodes on the center
\foreach \x[count=\xi] in {0.5,1.5,...,4}{
\node[fill,circle,inner sep=2pt] (r\xi) at (2,\x) {};
}
\node[fit=(r1) (r2) (r3) (r4),ellipse,draw,minimum width=1.5cm] {};
%put some nodes on the right
\foreach \x[count=\xi] in {0.75,1.5,...,3}{
\node[fill,circle,inner sep=2pt] (c\xi) at (4,\x) {};
}
\node[fit=(c1) (c2) (c3) (c4) ,ellipse,draw,minimum width=1.5cm] {};
\draw[-latex] (d1) -- (r2);
\draw[-latex] (d2) -- (r2);
\draw[-latex] (d3) -- (r4);
\draw[-latex] (r1) -- (c2);
\draw[-latex] (r2) -- (c3);
\draw[-latex] (d3) -- (r4);
\end{tikzpicture}
\end{document}
答案1
我不知道您想將哪些數字放在哪裡,但一般來說,節點的內容應該放在花括號之間:\node[options] (name) at (0,0) {
... };
。
\foreach
在您的範例中,您可能希望將儲存在巨集中的循環中的數字放入節點中\x
。由於節點被填充為黑色,因此內部的文字將不可見。因此,我建議您使用例如 來更改填滿顏色fill=lightgray
。所以,你可以這樣寫:\node[fill=lightgray,circle,inner sep=2pt] (d\x) at (0,\x) {\x};
。
如果對所有三組節點執行此操作,您會發現節點內容的長度不同,因此圓圈的大小也不同。例如,某些節點僅包含一位數字,而其他節點則包含三位和一個點作為小數點分隔符號。您可能想要為節點新增選項minimum width=2.5em
(或類似選項),以便所有節點獲得相同的大小。
最後,由於節點可能會因尺寸增加而重疊,因此您可以設定y=1.25cm
整體選項tikzpicture
,增加垂直距離,同時保持水平距離不變。
把所有的東西放在一起,你會得到:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,fit}
\begin{document}
\begin{tikzpicture}[y=1.25cm]
%put some nodes on the left
\foreach \x in {1,2,3}{
\node[fill=lightgray,circle,inner sep=2pt,minimum width=2.5em] (d\x) at (0,\x) {\x};
}
\node[fit=(d1) (d2) (d3),ellipse,draw,minimum width=1cm] {};
%put some nodes on the center
\foreach \x[count=\xi] in {0.5,1.5,...,3.5}{
\node[fill=lightgray,circle,inner sep=2pt,minimum width=2.5em] (r\xi) at (2,\x) {\x};
}
\node[fit=(r1) (r2) (r3) (r4),ellipse,draw,minimum width=1.5cm] {};
%put some nodes on the right
\foreach \x[count=\xi] in {0.75,1.5,...,3}{
\node[fill=lightgray,circle,inner sep=2pt,minimum width=2.5em] (c\xi) at (4,\x) {\x};
}
\node[fit=(c1) (c2) (c3) (c4),ellipse,draw,minimum width=1.5cm] {};
\draw[-latex] (d1) -- (r2);
\draw[-latex] (d2) -- (r2);
\draw[-latex] (d3) -- (r4);
\draw[-latex] (r1) -- (c2);
\draw[-latex] (r2) -- (c3);
\draw[-latex] (d3) -- (r4);
\end{tikzpicture}
\end{document}
如果您不希望數字後面有任何背景,您可以完全刪除節點的選項(而且,您不需要設定y=1.25cm
)。您可能想添加一些顏色(類似於您在評論中提供的圖片),因此我向左側橢圓形和其中一個箭頭添加了一些顏色:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,fit}
\begin{document}
\begin{tikzpicture}
%put some nodes on the left
\foreach \x in {1,2,3}{
\node (d\x) at (0,\x) {\x};
}
\node[fit=(d1) (d2) (d3),ellipse,draw=blue,minimum width=1cm] {};
%put some nodes on the center
\foreach \x[count=\xi] in {0.5,1.5,...,3.5}{
\node (r\xi) at (2,\x) {\x};
}
\node[fit=(r1) (r2) (r3) (r4),ellipse,draw,minimum width=1.5cm] {};
%put some nodes on the right
\foreach \x[count=\xi] in {0.75,1.5,...,3}{
\node (c\xi) at (4,\x) {\x};
}
\node[fit=(c1) (c2) (c3) (c4),ellipse,draw,minimum width=1.5cm] {};
\draw[-latex,orange] (d1) -- (r2);
\draw[-latex] (d2) -- (r2);
\draw[-latex] (d3) -- (r4);
\draw[-latex] (r1) -- (c2);
\draw[-latex] (r2) -- (c3);
\draw[-latex] (d3) -- (r4);
\end{tikzpicture}
\end{document}