
我有以下 tikz 圖:
\documentclass{book}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node(x1) {$x_1$};
\node(x2) [right of = x1] {$x_2$};
\node(x3) [right of = x2]{$x_3$};
\node(x4) [right of = x3]{$x_4$};
\draw [->] (x1) -- (x2) ;
\draw [->] (x2) -- (x3) ;
\draw [->] (x3) -- (x4) ;
\node(y1) [below of = x1]{$y_1$};
\node(y2) [right of = y1] {$y_2$};
\node(y3) [right of = y2] {$y_3$};
\node(y4) [right of = y3] {$y_4$};
\draw [->] (y1) -- (x1) ;
\draw [->] (y2) -- (x2) ;
\draw [->] (y3) -- (x3) ;
\draw [->] (y4) -- (x4) ;
\end{tikzpicture}
\end{document}
我希望箭頭更長一些。例如,當我在方括號中的繪圖命令中使用“shorten >= -5pt”時,箭頭會變長,但它們會越過節點。除了加長箭頭之外,有沒有辦法讓一切變得更寬敞?
另外,有沒有辦法在垂直箭頭的左側插入另一個節點? (或更具體地說,如何為箭頭而不是節點聲明“下方”或“左側”等?)我嘗試 \draw(arr1) 來標記箭頭,然後將一個節點添加到arr1 的左側,但出現錯誤。
答案1
你不應該使用below of = node
.它已被棄用(參見PGF/TikZ 中「right of=」與「right=of」的區別)。相反,使用positioning
tikz 的函式庫。它允許你寫right = 1.5cm of node
。您可以改變或省略距離。
要標記路徑,只需node[left] {label 1}
在後面添加--
.您可以pos=value
為節點選項新增 0 到 1 之間的值來指定路徑上的位置。
例子
(您修改後的程式碼)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node(x1) {$x_1$};
\node(x2) [right = 1.5cm of x1] {$x_2$};
\node(x3) [right = 1.5cm of x2]{$x_3$};
\node(x4) [right = 1.5cm of x3]{$x_4$};
\draw [->] (x1) -- (x2) ;
\draw [->] (x2) -- (x3) ;
\draw [->] (x3) -- (x4) ;
\node(y1) [below = 2 of x1]{$y_1$};
\node(y2) [below = 2 of x2] {$y_2$};
\node(y3) [below = 2 of x3] {$y_3$};
\node(y4) [below = 2 of x4] {$y_4$};
\draw [->] (y1) -- node[left, pos=0.2] {beginning} (x1) ;
\draw [->] (y2) -- node[left, pos=.8] {ending} (x2) ;
\draw [->] (y3) -- node[left] {label 3} (x3) ;
\draw [->] (y4) -- node[left] {label 4} (x4) ;
\end{tikzpicture}
\end{document}
結果是:
答案2
評論
您可以指定節點之間的預設距離,使用right of=
、left of=
等鍵放置節點node distance=<length>
。
要沿著路徑放置節點,只需將其插入到 lineto-operation 之後,即
\draw (0,0) -- node {on the path} (4,0);
如果您希望文字隨路徑傾斜,請使用sloped
鍵作為節點的選項。
閱讀有關節點放置的更多信息TikZ 文檔第 190 頁第 16.8 節。
執行
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[node distance=2.5cm]
\node(x1) {$x_1$};
\node(x2) [right of = x1] {$x_2$};
\node(x3) [right of = x2] {$x_3$};
\node(x4) [right of = x3] {$x_4$};
\draw [->] (x1) -- (x2) ;
\draw [->] (x2) -- (x3) ;
\draw [->] (x3) -- (x4) ;
\node(y1) [below of = x1]{$y_1$};
\node(y2) [right of = y1] {$y_2$};
\node(y3) [right of = y2] {$y_3$};
\node(y4) [right of = y3] {$y_4$};
\draw [->] (y1) -- node[sloped,below] {(y1) $\to$ (x1)} (x1) ;
\draw [->] (y2) -- node[sloped,below] {(y2) $\to$ (x2)} (x2) ;
\draw [->] (y3) -- node[sloped,below] {(y3) $\to$ (x3)} (x3) ;
\draw [->] (y4) -- node[sloped,below] {(y4) $\to$ (x4)} (x4) ;
\end{tikzpicture}
\end{document}