4

I've looked to another example but struggle to adapt it to my needs. Tikz is new to me.

  \begin{center}
  \begin{tikzpicture}[every edge/.style={shorten <=1pt, shorten >=1pt}]
  \draw (0,0)  node [below] {0} -- (1,0) node [below] {1};
  % draw the tick marks
  \coordinate (p) at (0,2pt);
  \foreach \myprop/\mytext [count=\n] in {0.06/$p_1$,0.06/$p_2$,0.06/$p_3$,0.06/$p_4$,0.06/$p_5$,0.15/$p_6$,0.13/$p_7$,0.14/$p_8$,0.15/$p_9$,0.13/$p_10$}
  \draw [decorate,decoration={brace,amplitude=2}] (p)  edge [draw] +(0,-4pt) -- ++(\myprop,0) coordinate (p) node [midway, above=2pt, anchor=south] {\mytext} ;
  \path (1,2pt) edge [draw]  ++(0,-4pt);
\end{tikzpicture}
\end{center}

enter image description here

Latex squeeze all the values together instead of spreading them over the 0 to 1 axis.

10
  • Please search the site to see if something has been done already before you ask a new question. :) Anyway, see this question: tex.stackexchange.com/q/80041/4778
    – Alenanno
    Commented Apr 9, 2016 at 9:56
  • By the way, why did you ask for a "vertical line"? The line in your image is horizontal.
    – Alenanno
    Commented Apr 9, 2016 at 9:57
  • Horizontal is right, I struggle with the adjective. Thanks for the link to other question, I searched but didn't have the key words.
    – Orongo
    Commented Apr 9, 2016 at 10:04
  • Well, you didn't share any detail as to how you want to do this. Use the code from that question to draw whatever you can do. But for example: how many ticks you want? Is the distance between the ticks regular? Or is it not? Do you want to simply replicate the image?
    – Alenanno
    Commented Apr 9, 2016 at 10:48
  • In my case there is 10 ticks, from 1 to 10. The distances between is not equal.
    – Orongo
    Commented Apr 9, 2016 at 10:51

2 Answers 2

4

Here's one way to modify things:

\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{decorations.pathreplacing,calc,positioning}
\begin{document}
  \begin{tikzpicture}[every edge/.style={shorten <=1pt, shorten >=1pt}]
  \draw (0,0)  node [below] {0} -- (10,0) node [below] {1};
  % draw the tick marks
  \coordinate (p) at (0,2pt);
  \foreach \myprop/\mytext [count=\n] in {1/$p_1$,2/$p_2$,.5/$p_3$,1/$p_4$,5/\dots,.5/$p_n$}
  \draw [decorate,decoration={brace,amplitude=2}] (p)  edge [draw] +(0,-4pt) -- ++(\myprop,0) coordinate (p) node [midway, above=2pt, anchor=south] {\mytext} ;
  \path (10,2pt) edge [draw]  ++(0,-4pt);
\end{tikzpicture}
\end{document}

modified loop

5
  • Thank you for both examples. Its a whole new world Tikz and its quite amazing stuff. THANKS
    – Orongo
    Commented Apr 9, 2016 at 11:57
  • I have tried to adapt the example codes and get a really ugly picture. How can this be changed, I'm sure its a simple matter for someone that knows Tikz.
    – Orongo
    Commented Apr 9, 2016 at 12:31
  • Change {1/$p_1$,2/$p_2$,.5/$p_3$,1/$p_4$,5/\dots,.5/$p_n$} to alter the number, placement and labels. The numbers before the /s should add up to 10. Each .../... creates one segment. To change the amplitude you can alter amplitude=2. For bigger ticks, you can increase 2pt and -4pt. The negative number should be -2 times the positive number. That's about it really. It is hard to know what you want to change just from the information in your comment.
    – cfr
    Commented Apr 9, 2016 at 12:37
  • Sorry the total value of the axis should equal to 1, I've changed this now.
    – Orongo
    Commented Apr 9, 2016 at 12:58
  • That means that the total width of your diagram will be 1 i.e. 1cm. Do you really want to create a miniature diagram? The 10 is just intended as a reasonable default width. If you need something only 1cm wide, you need to rethink your diagram completely because you clearly don't have space for that many labels etc. in such a small space.
    – cfr
    Commented Apr 9, 2016 at 20:49
1

Here is one example. Following your comment after the question, I have put 10 segments (11 ticks).

\documentclass[tikz,border=7mm]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
  \begin{tikzpicture}
    % draw segment and ticks
    \draw[-](0,0)--(10,0) foreach~in{0,...,10}{(~,-2pt)--(~,2pt)};
    % draw braces and labels
    \foreach[count=\j]\i in{0,...,9}
      \draw[decorate,decoration=brace](\i cm + 1pt,3pt) -- node[above,font=\scriptsize]{$p_{\j}$} (\j cm - 1pt,3pt);
  \end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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