![不同有向圖](https://rvso.com/image/298811/%E4%B8%8D%E5%90%8C%E6%9C%89%E5%90%91%E5%9C%96.png)
我需要製作一個與上圖類似的圖表,但我不知道如何製作沒有箭頭或垂直箭頭的水平線。我能做的最好的事情如下:
\documentclass{amsart}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
\node (p1) at ( 0, 0) {};
\node (p2) at ( 1, -0.2) {i};
\node (p3) at ( 3,0) {};
\node (p4) at ( 0,1) {};
\node (p5) at ( 2,1.2) {n+j};
\node (p6) at ( 3,1) {};
\begin{scope}[every path/.style={->}]
\draw (p1) -- (p3);
\draw (p4) -- (p6);
\draw (p2) -- (p5);
\end{scope}
\end{tikzpicture}
\end{center}
\end{document}
輸出
答案1
這是想要的結果嗎:
筆記:
- 如果您不希望箭頭出現在行尾,請將樣式變更為
every path/.style={-}
或簡單地刪除該選項,就像我在下面所做的那樣。預設情況下,線條沒有箭頭。 - 您想要的箭頭樣式是
-latex
。因此,只需在需要時添加該選項(如橙色線的情況)。 - 添加
shorten <=
和shorten >=
來延長橙色線。或者,您可以手動選擇點在線。 - 此
calc
函式庫用於計算繪製coordinate
垂直黑線的中點 s。 - 我透過添加
$
. - 添加顏色可以更輕鬆地了解哪個繪圖命令正在做什麼。
代碼:
\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{center}
\begin{tikzpicture}[thick]
\node (p1) at ( 0, 0) {};
\node (p2) at ( 1, -0.2) {$i$};
\node (p3) at ( 3,0) {};
\node (p4) at ( 0,1) {};
\node (p5) at ( 2,1.2) {$n+j$};
\node (p6) at ( 3,1) {};
\coordinate (p1MidwayP3) at ($(p1)!0.5!(p3)$);
\coordinate (p4MidwayP6) at ($(p4)!0.5!(p6)$);
\begin{scope}%[every path/.style={-}]
\draw [red] (p1) -- (p3);
\draw [blue] (p4) -- (p6);
\draw [orange, shorten <=-0.08cm, shorten >=-0.10cm, -latex](p2) -- (p5);
\draw (p1MidwayP3) -- (p4MidwayP6);
\end{scope}
\end{tikzpicture}
\end{center}
\end{document}
答案2
PSTricks 解決方案,結合xfp
:
\documentclass{article}
\usepackage{pstricks-add}
\usepackage{xfp}
\begin{document}
\def\Horizontal{5} % length of the horizontal line segments
\def\Vertical{3} % length of the vertical line segment
\def\Indent{0.5} % indentation of the arrow from both sides
\begin{pspicture}(0,-0.4)(\Horizontal,\fpeval{\Vertical+0.45})
\psline(0,0)(\Horizontal,0)
\psline(0,\Vertical)(\Horizontal,\Vertical)
\psline(\fpeval{\Horizontal/2},0)(\fpeval{\Horizontal/2},\Vertical)
\psline{->}(\Indent,0)(\fpeval{\Horizontal-\Indent},\Vertical)
\uput[270](\Indent,0){$i$}
\uput[90](\fpeval{\Horizontal-\Indent},\Vertical){$n+j$}
\end{pspicture}
\end{document}
您只需選擇\Horizontal
、\Vertical
、 和的值\Indent
,繪圖就會相應調整。