5

I'm new with Tikz package and now need to create a diagram like this:

enter image description here

But, I don't know how to make vertical lines in each node.

This is the code:

\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}

Really appreciate any help!Thanks

6

As you have already defined the coordinates (B and C) of the point, a foreach loop seems to be the natural choice:

\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}

Note that I used the calc library, but you have other ways to achieve the same result:

\foreach \x in {B,C}{%
   \draw (\x)--++(0,2pt);
   \draw (\x)--++(0,-2pt);
}

Or also (which is a compact version)

\foreach \x in {B,C}
\draw (\x)--++(0,2pt) (\x)--++(0,-2pt);
| improve this answer | |
  • Woowww. Welcomeeeeee...Claudio. Do you remember me? I'm Sebastiano the professor that a few years ago asked you to certify the use of LaTeX. Obviously I voted +1 your answer. A very strong hug. – Sebastiano Feb 12 '18 at 21:46
  • @Sebastiano Sure, I remember very well:) Hope everything is going on well. Cheers – Claudio Fiandrino Feb 15 '18 at 9:09
5

You could split the path (this way, you can be sure that the ticks are all the same):

\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}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.