我正在尝试学习 TikZ。我想要两个节点之间的边,但垂直向下移动一点。
为什么这不起作用?似乎只能在一侧向下移动。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations, arrows}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (10,0);
\foreach \x in {0,2,4,7,9}
\draw (\x cm,3pt) -- (\x cm,-3pt);
% draw nodes
\draw (0,0) node[below=3pt](A) {$ 0 $} node[above=3pt] {$ $};
\draw (1,0) node[below=3pt](B) {$ $} node[above=3pt] {$ Y_{i1} $};
\draw (2,0) node[below=3pt](C) {$ t_{i1} $} node[above=3pt] {$ $};
\draw (3,0) node[below=3pt](D) {$ $} node[above=3pt] {$ Y_{i2} $};
\draw (4,0) node[below=3pt](E) {$ t_{i2} $} node[above=3pt] {$ $};
\draw (5.5,0) node[below=3pt](F) {$ $} node[above=3pt] {$ Y_{i3} $};
\draw (7,0) node[below=3pt](G) {$ t_{i3} $} node[above=3pt] {$ $};
\draw (8,0) node[below=3pt](H) {$ $} node[above=3pt] {$ Y_{i3} $};
\draw (9,0) node[below=3pt](I) {$ t_{i4} $} node[above=3pt] {$ $};
%\draw[latex'-latex'] (A) -- (C);
\draw[latex'-latex'] ([yshift=-5cm]A) -- ([yshift=-5cm]C);
\draw[latex'-latex'] (C) -- (E);
\draw[latex'-latex'] (E) -- (G);
\end{tikzpicture}
\end{document}
答案1
例如,箭头可以按以下方式移动。示例使用锚点作为移动的锚点north *
,以避免由于节点高度不同而导致线条倾斜。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc, arrows}
\begin{document}
\begin{tikzpicture}[
>=latex',
]
\draw
(0,0) -- (10,0)
\foreach \x in {0,2,4,7,9} {
(\x cm,3pt) -- (\x cm,-3pt)
}
% draw nodes
(0,0) node[below=3pt](A) {$ 0 $}
(1,0) node[above=3pt] {$ Y_{i1} $}
(2,0) node[below=3pt](C) {$ t_{i1} $}
(3,0) node[above=3pt] {$ Y_{i2} $}
(4,0) node[below=3pt](E) {$ t_{i2} $}
(5.5,0) node[above=3pt] {$ Y_{i3} $}
(7,0) node[below=3pt](G) {$ t_{i3} $}
(8,0) node[above=3pt] {$ Y_{i3} $}
(9,0) node[below=3pt](I) {$ t_{i4} $}
;
%\draw[latex'-latex'] (A) -- (C);
\draw[<->] ($(A.north east) - (0, 1em)$) -- ($(C.north west) - (0, 1em)$);
\draw[<->] (C) -- (E);
\draw[<->] (E) -- (G);
\end{tikzpicture}
\end{document}
如果您想要校正第一个箭头并将左侧向下移动以获得水平线,那么可以通过以下方式更轻松地实现\vphantom
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[
>=latex',
]
\draw
(0,0) -- (10,0)
\foreach \x in {0,2,4,7,9} {
(\x cm,3pt) -- (\x cm,-3pt)
}
% draw nodes
(0,0) node[below=3pt](A) {$ 0\vphantom{t_{i1}} $}
% Now node A has the same height and depth as node C
(1,0) node[above=3pt] {$ Y_{i1} $}
(2,0) node[below=3pt](C) {$ t_{i1} $}
(3,0) node[above=3pt] {$ Y_{i2} $}
(4,0) node[below=3pt](E) {$ t_{i2} $}
(5.5,0) node[above=3pt] {$ Y_{i3} $}
(7,0) node[below=3pt](G) {$ t_{i3} $}
(8,0) node[above=3pt] {$ Y_{i3} $}
(9,0) node[below=3pt](I) {$ t_{i4} $}
;
\draw[<->] (A) -- (C);
\draw[<->] (C) -- (E);
\draw[<->] (E) -- (G);
\end{tikzpicture}
\end{document}
答案2
我最终使用了这个代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations, arrows}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (10,0);
\foreach \x in {0,2,4,7,9}
\draw (\x cm,3pt) -- (\x cm,-3pt);
% draw nodes
\node[below=3pt](A) at (0,0){$ 0 $};
\node[above=3pt](B) at (1,0){$ Y_{i1} $};
\node[below=3pt](C) at (2,0){$ t_{i1} $};
\node[above=3pt](D) at (3,0){$ Y_{i2} $};
\node[below=3pt](E) at (4,0){$ t_{i2} $};
\node[above=3pt](F) at (5.5,0){$ Y_{i3} $};
\node[below=3pt](G) at (7,0){$ t_{i3} $};
\node[above=3pt](H) at (8,0){$ Y_{i4} $};
\node[below=3pt](L) at (9,0){$ t_{i4} $};
\draw[<->, transform canvas={yshift=-0.5cm}] (A) -- (C) node [midway, below] {$S_1$};
\draw[<->, transform canvas={yshift=-0.5cm}] (C) -- (E) node [midway, below] {$S_2$};
\draw[<->, transform canvas={yshift=-0.5cm}] (E) -- (G) node [midway, below] {$S_3$};
\draw[<->, transform canvas={yshift=-0.5cm}] (G) -- (L) node [midway, below] {$S_4$};
\end{tikzpicture}
\end{document}