我正在嘗試為論文繪製網路圖,使用 TikZ 將各個頁面繪製為節點。然而,當我排版時,圖形位於線條的一個小角落,並且節點靠得太近。node distance
不起作用,只是minimum size
擴展節點以使它們彼此重疊。如何設定節點之間的最小距離?謝謝。
未完成的(還沒有線)圖如下:
理想情況下,它應該是頁面的寬度,節點應該分佈得彼此遠離。
我的程式碼如下所示:
\documentclass[12pt, oneside]{amsart}
\usepackage{geometry}
\geometry{a4paper}
\usepackage{tikz}
\begin{document}
\tikzstyle{node}=[circle, draw=black!100, thick]
\tikzstyle{dangling node}=[circle, draw=black!100, fill=black!30, thick]
\begin{tikzpicture}[minimum size=5mm][node distance=5.3cm,>=stealth?,bend angle=45,auto]
\node[node](page 1){1};
\node[node](page 2)[right of=page 1]{2};
\node[node](page 3)[below of=page 1]{3};
\node[node](page 4)[below of=page 2]{4};
\node[dangling node](page 5)[below of=page 3, xshift=5mm]{5};
\end{tikzpicture}
\end{document}
答案1
環境tikzpicture
只需要一可選參數。也就是說,如果 LaTeX 看到[
after \begin{tikzpicture}
,它會抓取它看到的第一個之前的所有內容]
,這就是環境的選項。當你有 時\begin{tikzpicture}[..][..]
,我認為第二個括號對實際上只是作為普通文本讀取,而 TiKZ 通常會嘗試抑制任何不是pgf
/TikZ 命令的內容,因此它基本上被忽略。
所以總而言之,a的所有選項tikzpicture
都必須放在同一個括號對中。
如果您相應地更改程式碼(並刪除?
後面的錯誤位置stealth
),您會發現您的node distance
設定按預期工作。
也就是說,您可能需要遵循 TeXnicians 的建議來使用該positioning
程式庫,並說 例如right=of
而不是right of=
。關於此的一些討論可以在PGF/TikZ 中「right of=」與「right=of」的區別。使用該positioning
庫的另一個原因是您可以分別設定水平和垂直節點距離,透過編寫node distance=Ycm and Xcm
,在下面的程式碼中可以看到一個例子。正如 gernot 在評論中提到的那樣,在這種情況下,人們可能還想添加on grid
節點樣式。on grid
意味著距離是從節點的中心點開始計算的。
最後注意:您可以將一種樣式放入另一種樣式中,因此如果dangling node
具有與 相同的樣式node
,並且添加了fill
,那麼您可以說dangling node/.style={node,fill=black!30}
。這減少了程式碼重複,並且更容易修改。
\documentclass[12pt, oneside]{amsart}
\usepackage{geometry}
\geometry{a4paper}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{
node/.style={circle, draw=black!100, thick, on grid}, % on grid added
dangling node/.style={node, fill=black!30}
}
\begin{document}
\begin{center}
\begin{tikzpicture}[
minimum size=5mm,
node distance=4cm and 7cm,
>=stealth,
bend angle=45,
auto
]
% grid to easier see that the node centers line up
\draw [help lines] (-1,-9) grid (9,1);
\node[node](page 1){1};
\node[node](page 2)[right=of page 1]{22};
\node[node](page 3)[below=of page 1]{3333};
\node[node](page 4)[below=of page 2]{4555555};
\node[dangling node](page 5)[below=of page 3, xshift=5mm]{5};
\end{tikzpicture}
\end{center}
\end{document}