我是 Tikz 包的新手,现在需要创建这样的图表:
但是,我不知道如何在每个节点上制作垂直线。
这是代码:
\begin{center}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (1,0);
\coordinate (C) at (2,0);
\coordinate (D) at (10,0);
\draw[|-|]
(A)
node at (A) [above=5pt] {$P_{t,M}$}
node at (A) [below=5pt] {$t$}
--
(B)
node at (B) [above=5pt] {$C$}
node at (B) [below=5pt] {$t+1$}
--
(C)
node at (C) [above=5pt] {$C$}
node at (C) [below=5pt] {$t+2$}
--
(D)
node at (D) [above=5pt] {$C+N$}
node at (D) [below=5pt] {$t + M$};
\end{tikzpicture}
\end{center}
非常感谢您的帮助!谢谢
答案1
由于您已经定义了点的坐标(B 和 C),因此循环foreach
似乎是自然的选择:
\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (1,0);
\coordinate (C) at (2,0);
\coordinate (D) at (10,0);
\draw[|-|]
(A)
node at (A) [above=5pt] {$P_{t,M}$}
node at (A) [below=5pt] {$t$}
--
(B)
node at (B) [above=5pt] {$C$}
node at (B) [below=5pt] {$t+1$}
--
(C)
node at (C) [above=5pt] {$C$}
node at (C) [below=5pt] {$t+2$}
--
(D)
node at (D) [above=5pt] {$C+N$}
node at (D) [below=5pt] {$t + M$};
\foreach \x in {B,C}
\draw ($(\x)+(0,2pt)$)--($(\x)+(0,-2pt)$);
\end{tikzpicture}
\end{document}
请注意,我使用了该calc
库,但您可以使用其他方法来实现相同的结果:
\foreach \x in {B,C}{%
\draw (\x)--++(0,2pt);
\draw (\x)--++(0,-2pt);
}
或者(紧凑版)
\foreach \x in {B,C}
\draw (\x)--++(0,2pt) (\x)--++(0,-2pt);
答案2
您可以分割路径(这样,您可以确保所有的刻度都是相同的):
\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}
%\begin{center} % not really useful in standalone class
\begin{tikzpicture}
[every node/.style={text depth=0pt}] % align node text
\coordinate (A) at (0,0);
\coordinate (B) at (1,0);
\coordinate (C) at (2,0);
\coordinate (D) at (10,0);
\draw[|-|]
(A)
node at (A) [above=5pt] {$P_{t,M}$}
node at (A) [below=5pt] {$t$}
--
(B);
\draw[-|]
(B)
node at (B) [above=5pt] {$C$}
node at (B) [below=5pt] {$t+1$}
--
(C);
\draw[-|]
(C)
node at (C) [above=5pt] {$C$}
node at (C) [below=5pt] {$t+2$}
--
(D)
node at (D) [above=5pt] {$C+N$}
node at (D) [below=5pt] {$t + M$};
\end{tikzpicture}
%\end{center}
\end{document}