![如何在 tikz 中绘制立方体形状的节点和边图](https://linux22.com/image/321709/%E5%A6%82%E4%BD%95%E5%9C%A8%20tikz%20%E4%B8%AD%E7%BB%98%E5%88%B6%E7%AB%8B%E6%96%B9%E4%BD%93%E5%BD%A2%E7%8A%B6%E7%9A%84%E8%8A%82%E7%82%B9%E5%92%8C%E8%BE%B9%E5%9B%BE.png)
我可以画出如下的简单矩形。有人能提供一些提示,如何将其扩展到上述版本吗?(或者,如果上述立方体图形已经有在线模板,如果您可以提供链接或添加到答案中,那就太好了)
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,row sep=3em,column sep=4em,minimum width=2em]
{
C & D \\
G & H \\};
\path[-stealth]
(m-2-1) edge node [left] {10} (m-1-1)
(m-1-1) edge node [above] {4} (m-1-2)
(m-2-2) edge node [right] {12} (m-1-2)
(m-2-1) edge node [below] {7} (m-2-2);
%(m-2-2) edge [dashed] node [below] {$M'_p$} (m-2-1);
\end{tikzpicture}
答案1
您可以使用tikz-cd
:
\documentclass{article}
\usepackage{tikz-cd}
\definecolor{myblue}{RGB}{90,143,222}
\begin{document}
\begin{tikzcd}[
row sep=scriptsize,
column sep=scriptsize,
cells={nodes={top color=myblue!50,bottom color=myblue,draw=myblue!70!black,text=white,rounded corners}},
arrows={myblue,text=black,shorten >=1pt,shorten <= 1pt}
]
A\ar[rr,"1"]\ar[dr,"3"] & & B\ar[dr,"2"]\ar[<-,dd,"12",near end] & \\
& C\ar[rr,"4",near start,crossing over] & & D & \\
E\ar[rr,"5",near start]\ar[dr,"7"]\ar[uu,"9"] & & F\ar[dr,"6"] & \\
& G\ar[rr,"8"]\ar[uu,"10",crossing over,near start,swap] & & H\ar[uu,"11",swap] &
\end{tikzcd}
\end{document}
如果您不想要颜色,请删除cells
和arrows
键:
\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}[
row sep=scriptsize,
column sep=scriptsize,
]
A\ar[rr,"1"]\ar[dr,"3"] & & B\ar[dr,"2"]\ar[<-,dd,"12",near end] & \\
& C\ar[rr,"4",near start,crossing over] & & D & \\
E\ar[rr,"5",near start]\ar[dr,"7"]\ar[uu,"9"] & & F\ar[dr,"6"] & \\
& G\ar[rr,"8"]\ar[uu,"10",crossing over,near start,swap] & & H\ar[uu,"11",swap] &
\end{tikzcd}
\end{document}
答案2
像这样吗?
您可以使用(x,y,z)
坐标来指定节点在 3D 中的位置。我使用了一个包含 2 个变量(坐标和标记它的字母)的循环来在长方体的角上创建命名节点。然后我在这些节点之间绘制边,并使用库提供的简写语法添加标签quotes
。
采用 3 种风格:
every edge quotes
是库提供的标准样式quotes
。这决定了箭头上边标签的格式。blue block
定义角落节点的样式。every edge
是确定用于绘制箭头的边缘外观的标准样式。
代码:
\documentclass[border=20pt,tikz]{standalone}
\usetikzlibrary{quotes}
\begin{document}
\begin{tikzpicture}
[
every edge quotes/.append style={font=\scriptsize\sffamily, midway, auto, inner sep=1pt},
blue block/.style={top color=blue!50!gray!75, bottom color=blue!50!gray, font=\sffamily, text=white, rounded corners, outer sep=2.5pt},
every edge/.append style={color=blue!50!gray, preaction={draw=white, ultra thick}, thick}
]
\foreach \j/\i in {(-2,1,-1)/A,(0,1,-1)/B,(0,1,1)/C,(2,1,1)/D,(-2,-1,-1)/E,(0,-1,-1)/F,(0,-1,1)/G,(2,0-1,1)/H}
\node [blue block] (\i) at \j {\i};
\draw [->] (A) edge ["1"] (B) edge [<-, "9"] (E) edge ["3"] (C);
\draw [->] (F) edge ["11"] (B) edge [<-, "5"] (E) edge ["6"] (H);
\draw [->] (G) edge ["10"] (C) edge [<-, "8"] (E) edge ["7"] (H);
\draw [<-] (D) edge ["2"] (B) edge ["4"] (C) edge ["12"] (H);
\end{tikzpicture}
\end{document}