我使用以下代码来绘制具有确定距离的线条。
我如何设置 (1.2) 和 (36) 数字的值以及线条定义(超粗,绿色!40!黑色);以便我可以稍后更改它们而无需手动更改每一个。
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{}
\begin{tikzpicture}[scale=.58, transform shape]
\draw [thick,-latex](0,0) -- (9,0);
\draw [thick,-latex](0,0) -- (0,8);
\draw [ultra thick, green!40!black] (0,1.2) node (yal){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {2/36};
\draw [ultra thick, green!40!black] (0,1.2*2) node (ybl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {4/36};
\draw [ultra thick, green!40!black] (0,1.2*3) node (ycl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {6/36};
\draw [ultra thick, green!40!black] (0,1.2*4) node (ydl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {8/36};
\draw [ultra thick, green!40!black] (0,1.2*5) node (yel){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {10/36};
\draw [ultra thick, green!40!black] (1.2,0) node (al){} +(90:1.2*3cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {0};
\draw [ultra thick, green!40!black] (1.2*2,0) node (bl){} +(90:1.2*5cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {1};
\draw [ultra thick, green!40!black] (1.2*3,0) node (cl){} +(90:1.2*4cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {2};
\draw [ultra thick, green!40!black] (1.2*4,0) node (dl){} +(90:1.2*3cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {3};
\draw [ultra thick, green!40!black] (1.2*5,0) node (el){} +(90:1.2*2cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {4};
\draw [ultra thick, green!40!black] (1.2*6,0) node (fl){} +(90:1.2cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {5};
\end{tikzpicture}
\end{frame}
\end{document}
答案1
一种非常简单的方法是定义一个包含这些定义的样式,并定义包含这些因素的宏。然后,您只需更改这些宏,即可在任何地方进行更改。请注意,我是“本地”定义的,即在 tikzpicture 内部,因此它们不会被外部知道。您\tikzset
还可以定义一个全局样式,其他 tikzpicture 也会知道该样式。
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{Styles can be really useful}
\begin{tikzpicture}[scale=.58, transform shape,Hany/.style={ultra thick,green!40!black}]
\pgfmathsetmacro{\myscale}{1.2}
\pgfmathsetmacro{\mydenominator}{36}
\draw [thick,-latex](0,0) -- (9,0);
\draw [thick,-latex](0,0) -- (0,8);
\draw [Hany] (0,\myscale) node (yal){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {2/\mydenominator};
\draw [Hany] (0,\myscale*2) node (ybl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {4/\mydenominator};
\draw [Hany] (0,\myscale*3) node (ycl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {6/\mydenominator};
\draw [Hany] (0,\myscale*4) node (ydl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {8/\mydenominator};
\draw [Hany] (0,\myscale*5) node (yel){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {10/\mydenominator};
\draw [Hany] (\myscale,0) node (al){} +(90:\myscale*3cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {0};
\draw [Hany] (\myscale*2,0) node (bl){} +(90:\myscale*5cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {1};
\draw [Hany] (\myscale*3,0) node (cl){} +(90:\myscale*4cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {2};
\draw [Hany] (\myscale*4,0) node (dl){} +(90:\myscale*3cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {3};
\draw [Hany] (\myscale*5,0) node (el){} +(90:\myscale*2cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {4};
\draw [Hany] (\myscale*6,0) node (fl){} +(90:\myscale*1cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {5};
\end{tikzpicture}
\end{frame}
\end{document}