我有以下图表:
\documentclass[border=1mm]{standalone}
\usepackage{siunitx}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
]
\node (n1) at (0,0) [draw=black,auto=left,circle,thick,fill=red!50,scale=1.3] {$\mathbf{X}_1$};
\node (n2) at (2,2) [draw=black,auto=left,circle,thick,fill=red!50,scale=1.3] {$\mathbf{X}_2$};
\node (n3) at (4,4) [draw=black,auto=left,circle,thick,fill=red!50,scale=1.3] {$\mathbf{X}_3$};
\node (n4) at (3,0) [draw=black,auto=left,circle,thick,fill=red!50,scale=1.3] {$\mathbf{X}_4$};
\node (n5) at (5,2) [draw=black,auto=left,circle,thick,fill=red!50,scale=1.3] {$\mathbf{X}_5$};
\node (n6) at (7,4) [draw=black,auto=left,circle,thick,fill=red!50,scale=1.3] {$\mathbf{X}_6$};
\node (n7) at (6,0) [draw=black,auto=left,circle,thick,fill=red!50,scale=1.3] {$\mathbf{X}_7$};
\node (n8) at (8,2) [draw=black,auto=left,circle,thick,fill=red!50,scale=1.3] {$\mathbf{X}_8$};
\node (n9) at (10,4) [draw=black,auto=left,circle,thick,fill=red!50,scale=1.3] {$\mathbf{X}_9$};
\node (n11) at (0,5) [draw=black,auto=left,circle,thick,fill=green!50,scale=1.2] {$\mathbf{Y}_{10}$};
\node (n22) at (2,7) [draw=black,auto=left,circle,thick,fill=green!50,scale=1.2] {$\mathbf{Y}_{11}$};
\node (n33) at (4,9) [draw=black,auto=left,circle,thick,fill=green!50,scale=1.2] {$\mathbf{Y}_{12}$};
\node (n44) at (3,5) [draw=black,auto=left,circle,thick,fill=green!50,scale=1.2] {$\mathbf{Y}_{13}$};
\node (n55) at (5,7) [draw=black,auto=left,circle,thick,fill=green!50,scale=1.2] {$\mathbf{Y}_{14}$};
\node (n66) at (7,9) [draw=black,auto=left,circle,thick,fill=green!50,scale=1.2] {$\mathbf{Y}_{15}$};
\node (n77) at (6,5) [draw=black,auto=left,circle,thick,fill=green!50,scale=1.2] {$\mathbf{Y}_{16}$};
\node (n88) at (8,7) [draw=black,auto=left,circle,thick,fill=green!50,scale=1.2] {$\mathbf{Y}_{17}$};
\node (n99) at (10,9) [draw=black,auto=left,circle,thick,fill=green!50,scale=1.2] {$\mathbf{Y}_{18}$};
\node (n7h) at (9,0) [] {$ $};
\node (n8h) at (11,2) [] {$ $};
\node (n9h) at (13,4) [] {$ $};
\node (n1l) at (-3,0) [] {$ $};
\node (n2l) at (-1,2) [] {$ $};
\node (n3l) at (1,4) [] {$ $};
\node (n1f) at (-2,-2) [] {$ $};
\node (n4f) at (1,-2) [] {$ $};
\node (n7f) at (4,-2) [] {$ $};
\node (n3t) at (6,6) [] {$ $};
\node (n6t) at (9,6) [] {$ $};\draw ($(n1)!1cm!(n1l)$) -- ($(n1)!3cm!(n1l)$);
\node (n9t) at (12,6) [] {$ $};
\foreach \from/\to in {n11/n1,n22/n2,n33/n3,n44/n4,n55/n5,n66/n6,n77/n7,n88/n8,n99/n9}[ultra thin] \draw (\from) -- (\to);
\foreach \from/\to in {n1/n2,n2/n3,n4/n5,n5/n6,n7/n8,n8/n9} \draw[ultra thick] (\from) -- (\to);
\foreach \from/\to in {n1/n4,n4/n7,n2/n5,n5/n8,n3/n6,n6/n9} \draw[ultra thick] (\from) -- (\to);
\foreach \from/\to in {n7/n7h,n8/n8h,n9/n9h} \draw (\from)[dashed] -- (\to);
\foreach \from/\to in {n1/n1l,n2/n2l,n3/n3l} \draw (\from)[dashed] -- (\to);
\foreach \from/\to in {n1/n1f,n4/n4f,n7/n7f} \draw (\from)[dashed] -- (\to);
\foreach \from/\to in {n3/n3t,n6/n6t,n9/n9t} \draw (\from)[dashed] -- (\to);
\end{tikzpicture}
\end{document}
我想画一条半虚线,例如在节点 X_1 和 Y_{13} 之间。我该怎么做?
答案1
“半虚线”的另一种解释:
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations.pathreplacing,calc}
\tikzset{draw half paths/.style 2 args={%
decoration={show path construction,
lineto code={
\draw [#1] (\tikzinputsegmentfirst) --
($(\tikzinputsegmentfirst)!0.5!(\tikzinputsegmentlast)$);
\draw [#2] ($(\tikzinputsegmentfirst)!0.5!(\tikzinputsegmentlast)$)
-- (\tikzinputsegmentlast);
}
}, decorate
}}
\begin{document}
\begin{tikzpicture}[every node/.style={circle, draw}, >=stealth]
\foreach \i in {1,...,5}
\node (n-\i) at (72*\i:2) {\i};
\path [draw half paths={solid}{dashed}] (n-1) -- (n-3);
\path [draw half paths={dotted}{dashed}] (n-2) -- (n-4);
\path [draw half paths={blue, thin}{red, very thick}] (n-3) -- (n-5);
\path [draw half paths={dashed}{draw=none}] (n-4) -- (n-1);
\path [draw half paths={dotted, ->}{draw=none}] (n-5) -- (n-2);
\end{tikzpicture}
\end{document}
答案2
我不太确定,您为什么介意“半虚线”......如果它由两条线组成,一条实线和一条(与实线距离不远)虚线,看看这是否能解决您的问题:
\begin{scope}[on background layer]
% half dashed line
\draw (n1) -- (n13);
\draw[dashed] ($(n1)!2pt!90:(n13)$) edge ($(n13)!2pt!-90:(n1)$);
\end{scope}
此项添加可得出:
这张图片是我用 MWE 生成的,嗯,与你的非常不同......
\documentclass[border=3mm, tikz, preview]{standalone}
\usepackage{siunitx}
\usepackage{tikz}
\usetikzlibrary{calc,backgrounds,scopes}
\begin{document}
\begin{tikzpicture}[
XYnode/.style = {circle, draw=black, thick, fill=#1!50,
minimum size=1em, font=\Large},
]
% red nodes
\foreach \x in {1,2,...,3}
\foreach \y in {1,2,...,3}
{
\pgfmathtruncatemacro{\i}{\y + (\x-1)*3};
\node[XYnode=red] (n\i) at ({3*(\x+\y-2)-\y},2*\y-2) {$\mathbf{X}_{\i}$};
}
%green nodes
\begin{scope}[yshift=50mm]
\foreach \x in {1,2,...,3}
\foreach \y in {1,2,...,3}
{
\pgfmathtruncatemacro{\i}{\y + (\x-1)*3 + 9};
\node[XYnode=green] (n\i) at ({3*(\x+\y-2)-\y},2*\y-2) {$\mathbf{Y}_{\!\!\i}$};
}
\end{scope}
% background lines
\begin{scope}[on background layer]
% grid below red nodes
\draw[ultra thick] (n1) -- (n3) (n4) -- (n6) (n7) -- (n9)
(n1) -- (n7) (n2) -- (n8) (n3) -- (n9);
% half dashed line
\draw (n1) -- (n13);
\draw[dashed] ($(n1)!2pt!90:(n13)$) -- ($(n13)!2pt!-90:(n1)$);
\end{scope}
% links between red and green nodes
\foreach \j [count=\jj from 10] in {1,...,9}
\draw (n\j) -- (n\jj);
% dashed lines
\foreach \k [count=\kk from 7] in {1,...,3}
\draw[dashed] (n\k) -- + (-33mm,0mm) (n\kk) -- + (33mm,0mm);
\foreach \k [evaluate=\k as \kk using int(\k + 2)] in {1,4,...,7}
\draw[dashed] (n\k) -- + (-18mm,-18mm)
(n\kk) -- + ( 22mm, 22mm);
\end{tikzpicture}