
我的程式碼是:
\begin{center}
\vspace{-20pt}
\begin{tikzpicture}[mybox/.style={rectangle, draw, minimum width=10mm, minimum height=10mm}]
\foreach \addr/\val [count=\x] in {$0$,$1$/$28$,$6$/$\infty$,$6$/$\infty$,$6$/$25$,$0$,$6$/$\infty$} {
\node (node\x) [mybox] at (\x,0) {\addr};
\node[above=5mm] at (\x,0) {\footnotesize \x};
\node[below=5mm] at (\x,0) {\val};
}
\node at (node1.south west-100mm,0) [below=5mm] {Edge Weight:};
\draw[ultra thick] (node1.south west) rectangle (node7.north east);
\end{tikzpicture}
\end{center}
我在與使用錨點作為座標的\node (lab) at (node1.south west-100mm,0) [below=5mm] {Edge Weight:};
部分相關的行上收到錯誤。(node1.south west-100mm,0)
錯誤:
包 PGF 數學錯誤:未知函數“node1”(在“node1.south west-100mm”中)。 \node at (node1.south west-100mm,0)
包 PGF 數學錯誤:未知函數“node1”(在“node1.south west-100mm”中)。 ...est-100mm,0) [低於=5mm] {邊緣重量:};
我希望節點的位置lab
與node0 的西南錨點的距離為(-100pt,0)。我該怎麼做呢?
我甚至嘗試寫:
(node1.south west)+(-1,0)
但這也行不通。我該如何解決這個問題?
PS我知道寫作\path (node1.south west) node at +(-35pt,-8pt) {Edge Weight:};
可以解決問題,但是有沒有辦法直接做到這一點,而不使用路徑命令,而簡單地使用節點命令?
答案1
您可以使用xshift
:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[mybox/.style={rectangle, draw, minimum width=10mm, minimum height=10mm}]
\foreach \addr/\val [count=\x] in {$0$,$1$/$28$,$6$/$\infty$,$6$/$\infty$,$6$/$25$,$0$,$6$/$\infty$} {
\node (node\x) [mybox] at (\x,0) {\addr};
\node[above=5mm] at (\x,0) {\footnotesize \x};
\node[below=5mm] at (\x,0) {\val};
}
\node at ([xshift=-10]node1.south west) [below=5mm] {Edge Weight:};
\draw[ultra thick] (node1.south west) rectangle (node7.north east);
\end{tikzpicture}
\end{document}
透過這個小調整,您的程式碼會產生:
就其價值而言,為了使程式碼更易於閱讀,我會將 for 迴圈替換為
\foreach \addr/\val [count=\x] in {0,1/28,6/\infty,6/\infty,6/25,0,6/\infty} {
\node (node\x) [mybox] at (\x,0) {$\addr$};
\node[above=5mm] at (\x,0) {\footnotesize \x};
\node[below=5mm] at (\x,0) {$\val$};
}
答案2
如果要使用該calc
庫,則需要將節點操作編寫為:
\node at ($(node1.south west)+(-1,0)$)
你將擁有:
完整程式碼:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[mybox/.style={rectangle, draw, minimum width=10mm, minimum height=10mm}]
\foreach \addr/\val [count=\x] in {$0$,$1$/$28$,$6$/$\infty$,$6$/$\infty$,$6$/$25$,$0$,$6$/$\infty$} {
\node (node\x) [mybox] at (\x,0) {\addr};
\node[above=5mm] at (\x,0) {\footnotesize \x};
\node[below=5mm] at (\x,0) {\val};
}
\node at ($(node1.south west)+(-1,0)$) [below=5mm] {Edge Weight:};
\draw[ultra thick] (node1.south west) rectangle (node7.north east);
\end{tikzpicture}
\end{document}