2

I have a script provided to me to generate a line with 10 intervals. I now want the same figure but for a general case. It applies tikzpicture, something that I'm not familiar with.

\begin{figure}
\begin{centering}
\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 {0.6/$p_1$,0.6/$p_2$,0.6/$p_3$,0.6/$p_4$,0.6/$p_5$,1.5/$p_6$,1.3/$p_7$,1.4/$p_8$,1.5/$p_9$,1.3/$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 (10,2pt) edge [draw]  ++(0,-4pt);
\end{tikzpicture}
\caption{Possible values for $X$ with probability $p_i, i=1,2, \dots 10$}
\end{centering}
\end{figure}

What I want is this enter image description here

3 Answers 3

4
\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}
\begin{figure}
\begin{centering}
    \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.6/$p_1$,1.6/$p_2$,1.6/$p_3$,1.6/$p_4$,2.0/$\dots \dots$,1.6/$p_{n}$}
    \draw [decorate,decoration={brace,amplitude=4}] (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}
    \caption{Possible values for $X$ with probability $p_i, i=1,2, \dots 10$}
\end{centering}
\end{figure}
\end{document}

enter image description here

0
2

An alternative with TikZ: as exercise with random width of intervals, modified braces, correct use of centering and considering that (according to figure caption) number of intervals is 10:

enter image description here

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}
    \begin{frame}{Intervals of equal probability}
\begin{figure}
\centering
    \begin{tikzpicture}
% probability axes 
\draw[|-] (0,0)  node [below=1mm] {0} -- (10,0) node [below=1mm] {1};
% braces and ticks
\foreach \i [evaluate=\i as \j using \i+random,
             remember=\j as \ix (initially 0)] in {1,2,3,4,8,10}
{
\ifnum\i=10 \pgfmathsetmacro{\j}{int(\j)} \fi;% limiter of probability to 1 
\ifnum\i=8
    \draw (\j,1mm) -- + (0,-2mm);
    \draw[very thick,loosely dotted,shorten >=5.5mm,shorten <=5.5mm] 
            (\ix,5.5mm) -- (\j,5.5mm);
\else
    \draw [decorate, decoration={brace, amplitude=1mm, 
           pre=moveto,pre length=1pt,post=moveto,post length=1pt,
           raise=1.5mm}]  (\ix,0) -- node (n\i) [above=3mm] {$p_{\i}^{}$} (\j,0);
    \draw (\j,1mm) -- + (0,-2mm);
\fi
}
    \end{tikzpicture}
    \caption{Possible values for $X$ with probability $p_i, i=1,2, \dots 10$}
\end{figure}
    \end{frame}
\end{document}
2

A PSTricks solution:

\documentclass{article}

\usepackage{pstricks-add}
\def\interval(#1)(#2)#3{%
  \psline(!#1 0.05 add 0.4)(!#1 0.05 add 0.7)
  \psline(!#2 0.05 add 0.4)(!#2 0.05 add 0.7)
  \psbrace[
    nodesepA = -4pt,
    nodesepB = -2pt,
    braceWidth = 0.75\pslinewidth
  ](!#2 0.05 add 0.996 mul 0.7)(!#1 0.05 add 0.996 mul 0.7){$p_{#3}$}}

\begin{document}

\begin{pspicture}(8.08,1.55)
  \psline(0.05,0.55)(8.05,0.55)
  \uput[270](0.05,0.4){$0$}
  \uput[270](8.05,0.4){$1$}
  \interval(0)(2){1}
  \interval(2)(3.2){2}
  \interval(3.2)(4.5){3}
  \interval(4.5)(6){4}
  \rput(6.55,0.8){$\cdots$}
  \interval(7)(8){n}
\end{pspicture}

\end{document}

output

P.S. I have no idea why we have to multiply by the factor 0.996 in order to to get the beginning and end of the brace position right.

3
  • Did you solve the problem with the post you've deleted a few minutes ago (about a rotated polygon)? I was about to post the correct parameters.
    – Bernard
    Jun 6, 2016 at 19:05
  • @Bernard Actually I did, using the \pstRotation command from the pst-eucl, but I'll un-delete the question since I would be happy to see your answer. Jun 6, 2016 at 19:07
  • pst-eucl was one of the possibilities I considered, but I finally chose a closer approach to your code.
    – Bernard
    Jun 6, 2016 at 19:10

You must log in to answer this question.

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