我是 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}