0

I am currently trying to draw this graph using Tikz : enter image description here

The fact is that I am a bit lost with this process since it's my first time trying to do so. I am reading a documentation but this is only about listing examples so I am proceeding by mimicry...

\documentclass{article}

\usepackage{amsmath}
\usepackage{bm}
\usepackage{xcolor}
 
\usepackage{pgf, tikz}
    
\begin{document}


\begin{center}
\begin{tikzpicture}

\draw[step = 1cm, gray, very thin](-6,-2)grid(6, 2);
\draw[very thick, ->](-6,0)--(6,0)node[above]{x};
\foreach \x in {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}
\draw(\x, 1pt) -- (\x, -1pt) node[below]{$\x$};


\end{tikzpicture}
\end{center}

\end{document}

Obtaining this :

enter image description here For the moment :

  • I don't know how to use letters instead of numbers in order to graduate my line : how to do so ?
  • Furthermore, I don't know how to plot the rank of each interval above every "cell" : (1,...,(i-1),I,(i+1),...,N).

Could anyone help me to proceed please ?

Thank you in advance.

1 Answer 1

6

Welcome to TeX.:SE!

Reproduction of your image:

enter image description here

is done by:

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}

\begin{document}
 \begin{tikzpicture}
\draw[very thick](-6,0) -- (-3.84,0) -- ++ (1mm,2mm) -- ++ (2mm,-4mm) -- ++ (1mm,2mm)
                        -- ( 3.36,0) -- ++ (1mm,2mm) -- ++ (2mm,-4mm) -- ++ (1mm,2mm)
                        -- (6,0);
\foreach \x/\i in {-5/x_{{1}{2}}, -4/x_{\frac{3}{2}},-2/, -1/x_{i-\frac{3}{2}}, 
                    1/x_{i+\frac{3}{2}}, 2, 4/x_{N_x-\frac{1}{2}}, 5/x_{N_x+\frac{1}{2}}}
\draw(1.2*\x, 2mm) -- ++ (0,-4mm) node[below]{$\i$};
%
\foreach \x/\i [evaluate=\x as \xx using \x+1] in {-5/1, -2/i-1, -0.5/i, 1/i+1, 4/N_x}
\path (1.2*\x,0) -- node[above, font=\small] {$\i$} (1.2*\xx,0);

\end{tikzpicture}
\end{document}

Note:

  • In above MWE are used elementary commands for drawing of TikZ pictures-
  • For the first line is used combination of absolute and relative coordinates. Latter are used zig-zag part of line.
  • Vertical lines, which divide main line into segments, are drawn in the \foraech loops. To this lines are added labels below of them.
  • Description for loops you can find in chapter 88 Repeating Things: The Foreach Statement, where see:
    • for the first see paragraph Multiple variables(page 1003) for general principle of use multiple variables. IN your case are \x for positions of bars and \i for labels below bars. Values are collected in pairs \x/\i in the list in curly braces. In your case first such pair is -5/x_{{1}{2}},, the second is -4/x_{\frac{3}{2}}, etc
    • for the second loop Options to customize the foreach-statement om pages 1004-1005. Option evaluate is used for determination of segments with labels above. At this \x determine start of segment, evaluated \xxend of segment, and variable\i` contain labels of segment.
2
  • Thank you very much for your answer. But please : would you mind take a bit of time to explain some lines ? For instance : I don't understand how this line is written : \foreach \x/\i in {-5/x_{{1}{2}}, -4/x_{\frac{3}{2}},-2/, -1/x_{i-\frac{3}{2}}, 1/x_{i+\frac{3}{2}}, 2, 4/x_{N_x-\frac{1}{2}}, 5/x_{N_x+\frac{1}{2}}}
    – Waxler
    May 15, 2022 at 8:47
  • @Wiss, see added note in answer.
    – Zarko
    May 15, 2022 at 11:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .