5

I just want to know how can I create this simple line in tikz. I found many example but they are too complicated for me to understand. enter image description here

\begin{tikzpicture}[xscale=1.2]
\draw (0,0) -- (6,0); 
\foreach \x in {1,2,3,4,5}
\draw[shift={(\x,0)},color=black] (0pt,0pt) -- (0pt, 1pt) node[below] 
{$t_1, t_2, t_3, t_4, t_5$};
\end{tikzpicture}
Neeraj
  • 173

2 Answers2

9

You were almost there; you should only change the node text according to the \foreach index.

\foreach is a cycle for the variable \x from 1 to 5, step 1 (the step is given by the difference between the first and the second element here {1,2,...,5}, you don't need to list all the elements, tikz computes them for you).

shift={(\x,0)} means that the \draw is shifted by \x along the horizontal axis, you could also use xshift=\x cm where cm could be replaced by any unit of measure you prefer.

node[below=2ex] {$t_\x$} write a node, with text $t_\x$ (\x is automatically replaced), 2ex below the path.

\documentclass{article} 
\usepackage{tikz} 
\begin{document} 
    \begin{figure} 
        \centering 
        \begin{tikzpicture}
        \draw (0,0) -- (6,0); 
        \foreach \x in {1,2,...,5}
            \draw[shift={(\x,0)},color=black] (0pt,-4pt) -- (0pt, 4pt) node[below=2ex] 
            {$t_\x$};
        \end{tikzpicture}  
    \end{figure}
\end{document}

enter image description here

Edit: Second OP's request.

Instead of writing the node text while you were drawing the path, you could name the nodes with node (t\x) {}, where t\x is the name (\x is automatically replaced) and, then, refer to these names to position your labels.

The tikzlibrary positioning allows you to decide where and at which distance to position a node w.r.t. another. For example \node[below=2ex of t5] {$t$}; means "position the node 2ex below of the node named t5".

\documentclass{article} 
\usepackage{tikz} 
\usetikzlibrary{positioning}
\begin{document} 
    \begin{figure} 
        \centering 
        \begin{tikzpicture}
        \draw (0,0) -- (6,0); 
        \foreach \x in {1,2,...,5}
        {\draw[shift={(\x,0)},color=black] (0pt,-4pt) -- (0pt, 4pt) node (t\x) {};}
        \node[below=2ex of t5] {$t$};
        \node[below=2ex of t1] {$t- \Delta t$};
        \end{tikzpicture}  
    \end{figure}
\end{document}

enter image description here

CarLaTeX
  • 62,716
  • 1
    As the OP probably did not fully understand the code, you might want to explain what it does. – TeXnician Dec 02 '17 at 10:00
  • @CarLaTeX Thanks. But I wanted to know, can I replace foreach with custom name for every tick like {t_1, t_2, t_3, t_4, t_5} – Neeraj Dec 02 '17 at 10:03
  • @CarLaTeX In next diagram I need to used only two tick one is $t$ and second is $t + \Delta t$. So, I can not use foreach that. – Neeraj Dec 02 '17 at 10:08
  • @Neeraj I'm happy of that, thank you for accepting my answer, I'll go on with the explanations. – CarLaTeX Dec 02 '17 at 10:17
  • @CarLaTex why should i not accept your answer when it provide exactly what I wanted. I try my best to upvote every question and answer that I check on tex.stackexchange. – Neeraj Dec 02 '17 at 12:11
5

Since you asked for means to use a list for the labels, you might use the following for that. I think pgf has a built in mechanism for arrays, but I don't remember the macro names...

\documentclass[tikz, border=3mm]{standalone}

\makeatletter
\newcommand{\setupmylabellist}[1]{%
  \count255 = 0\relax%
  \@for\x:=#1\do{
    \expandafter\edef\csname mylabel\the\count255\endcsname{\x}
    \advance\count255 by 1\relax%
  }
}
\newcommand{\usemylabel}[1]{\csname mylabel#1\endcsname}
\makeatother

\begin{document}
  \begin{tikzpicture}[
    thick,
]
\setupmylabellist{foo1,bar1,baz1,foo2,bar2,baz2}
\foreach \x in {0,1,2,3,4,5}
\draw [shift={(\x,0)}] (0.1,0.2) -- + (0,-0.4) node[below] {\usemylabel{\x}};
%
\draw[thick] (0,0) -- node[below=8mm] {X axis title} + (5.2,0);
%
\end{tikzpicture}
\end{document} 

enter image description here

Skillmon
  • 60,462