我有一个脚本可以生成一条具有 10 个间隔的线。我现在想要相同的图形,但用于一般情况。它应用了 tikzpicture,这是我不熟悉的东西。
\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}
答案1
\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}
答案2
TikZ 的替代方案:作为练习,使用随机间隔宽度、修改括号、正确使用centering
并考虑(根据图片标题)间隔数为 10:
\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}
答案3
PSTricks 解决方案:
\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}
PS:我不知道为什么我们必须乘以因子 0.996 才能正确获得括号的开始和结束位置。