![简化重复绘图和节点放置](https://linux22.com/image/384971/%E7%AE%80%E5%8C%96%E9%87%8D%E5%A4%8D%E7%BB%98%E5%9B%BE%E5%92%8C%E8%8A%82%E7%82%B9%E6%94%BE%E7%BD%AE.png)
我使用以下代码绘制重复的线条并将节点放置在特定位置,以便稍后绘制圆圈并用线条连接它们。
有没有更简单的方法来完成这项工作,如使用\foreach
命令或任何其他可能更简单的方法。
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{}
\hspace {-1cm}
\begin{tikzpicture}[scale=.7, transform shape,linecolor/.style={ultra thick, green!40!black},dotline/.style={thin, black, loosely dotted}]
\pgfmathsetmacro{\incrmnt}{.6}
\draw [thick,-latex](-.3,0) -- (0,0) -- (5,0);
\draw [thick,-latex](0,-.3) -- (0,0) -- (0,8);
\draw [thick,-latex](6.,0) -- (7,0) -- (14.4,0);
\draw [thick,-latex](8,-.2) -- (8,0) -- (8,8);
\draw [linecolor] (0,\incrmnt*1) node (yal){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {1/12}([shift={(0:8)}]yal.center) node (ya){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {1/12};
\draw [dotline] (0,\incrmnt*1) -- +(0:14cm);
\draw [linecolor] (0,\incrmnt*2) node (ybl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {2/12}([shift={(0:8)}]ybl.center) node (yb){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {2/12};
\draw [dotline] (0,\incrmnt*2) -- +(0:14cm);
\draw [linecolor] (0,\incrmnt*3) node (ycl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {3/12}([shift={(0:8)}]ycl.center) node (yc){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {3/12};
\draw [dotline] (0,\incrmnt*3) -- +(0:14cm);
\draw [linecolor] (0,\incrmnt*4) node (ydl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {4/12}([shift={(0:8)}]ydl.center) node (yd){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {4/12};
\draw [dotline] (0,\incrmnt*4) -- +(0:14cm);
\draw [linecolor] (0,\incrmnt*5) node (yel){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {5/12}([shift={(0:8)}]yel.center) node (ye){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {5/12};
\draw [dotline] (0,\incrmnt*5) -- +(0:14cm);
\draw [linecolor] (0,\incrmnt*6) node (yfl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {6/12}([shift={(0:8)}]yfl.center) node (yf){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {6/12};
\draw [dotline] (0,\incrmnt*6) -- +(0:14cm);
\draw [linecolor] (0,\incrmnt*7) node (ygl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {7/12}([shift={(0:8)}]ygl.center) node (yg){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {7/12};
\draw [dotline] (0,\incrmnt*7) -- +(0:14cm);
\draw [linecolor] (0,\incrmnt*8) node (yhl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {8/12}([shift={(0:8)}]yhl.center) node (yh){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {8/12};
\draw [dotline] (0,\incrmnt*8) -- +(0:14cm);
\draw [linecolor] (0,\incrmnt*9) node (yhl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {9/12}([shift={(0:8)}]yhl.center) node (yh){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {9/12};
\draw [dotline] (0,\incrmnt*9) -- +(0:14cm);
\draw [linecolor] (0,\incrmnt*10) node (yhl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {10/12}([shift={(0:8)}]yhl.center) node (yh){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {10/12};
\draw [dotline] (0,\incrmnt*10) -- +(0:14cm);
\draw [linecolor] (0,\incrmnt*11) node (yhl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {11/12}([shift={(0:8)}]yhl.center) node (yh){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {11/12};
\draw [dotline] (0,\incrmnt*11) -- +(0:14cm);
\draw [linecolor] (0,\incrmnt*12) node (yhl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {12/12}([shift={(0:8)}]yhl.center) node (yh){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {12/12};
\draw [dotline] (0,\incrmnt*12) -- +(0:14cm);
\draw [linecolor] (\incrmnt*1,0) node (al){} +(90:\incrmnt*1) node [circle, draw, solid, red, fill=red, scale=0.4]{} -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {1} ([shift={(0:8)}]al.center) node (a){} +(90:.25cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {1} ([shift={(90:\incrmnt*1)}]a.center) node (au){} ([shift={(90:\incrmnt*1)}]au.center) node (auu){};
\draw [linecolor] (\incrmnt*2,0) node (bl){} +(90:\incrmnt*2) node [circle, draw, solid, red, fill=red, scale=0.4]{} -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {2} ([shift={(0:8)}]bl.center) node (b){} +(90:.25cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {2} ([shift={(90:\incrmnt*1)}]b.center) node (bu){} ([shift={(90:\incrmnt*2)}]bu.center) node (buu){};
\draw [linecolor] (\incrmnt*3,0) node (cl){} +(90:\incrmnt*2) node [circle, draw, solid, red, fill=red, scale=0.4]{} -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {3} ([shift={(0:8)}]cl.center) node (c){} +(90:.25cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {3} ([shift={(90:\incrmnt*3)}]c.center) node (cu){} ([shift={(90:\incrmnt*2)}]cu.center) node (cuu){};
\draw [linecolor] (\incrmnt*4,0) node (dl){} +(90:\incrmnt*2) node [circle, draw, solid, red, fill=red, scale=0.4]{} -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {4} ([shift={(0:8)}]dl.center) node (d){} +(90:.25cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {4} ([shift={(90:\incrmnt*5)}]d.center) node (du){} ([shift={(90:\incrmnt*2)}]du.center) node (duu){};
\draw [linecolor] (\incrmnt*5,0) node (el){} +(90:\incrmnt*2) node [circle, draw, solid, red, fill=red, scale=0.4]{} -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {5} ([shift={(0:8)}]el.center) node (e){} +(90:.25cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {5} ([shift={(90:\incrmnt*7)}]e.center) node (eu){} ([shift={(90:\incrmnt*2)}]eu.center) node (euu){};
\draw [linecolor] (\incrmnt*6,0) node (fl){} +(90:\incrmnt*2) node [circle, draw, solid, red, fill=red, scale=0.4]{} -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {6} ([shift={(0:8)}]fl.center) node (f){} +(90:.25cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {6} ([shift={(90:\incrmnt*9)}]f.center) node (fu){} ([shift={(90:\incrmnt*2)}]fu.center) node (fuu){};
\draw [linecolor] (\incrmnt*7,0) node (gl){} +(90:\incrmnt*1) node [circle, draw, solid, red, fill=red, scale=0.4]{} -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {7} ([shift={(0:8)}]gl.center) node (g){} +(90:.25cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {7}([shift={(90:\incrmnt*11)}]g.center) node (gu){} ([shift={(90:\incrmnt*1)}]gu.center) node (guu){};
\end{tikzpicture}
\end{frame}
\end{document}
这是生成以下绘图的其余代码
\draw [ultra thick, blue] (au.center) -- (bu.center) (buu.center) -- (cu.center) (cuu.center) -- (du.center) (duu.center) -- (eu.center) (euu.center) -- (fu.center) (fuu.center) -- (gu.center) (guu.center) --+(0:\incrmnt*2) node (inf){};
\draw [thick, black] (inf.center) +(90:\incrmnt*1) --(inf.center|-0,0) node [xshift=0cm, yshift=-.35cm] {$\infty$};
\draw [thick, red, fill=red] (au.center) circle (.1) (buu.center) circle (.1) (cuu.center) circle (.1) (duu.center) circle (.1) (euu.center) circle (.1) (fuu.center) circle (.1) (guu.center) circle (.1);
\draw [thick, red, fill=white] (a.center) circle (.14) (bu.center) circle (.14) (cu.center) circle (.14) (du.center) circle (.14) (eu.center) circle (.14) (fu.center) circle (.14) (gu.center) circle (.14);
答案1
在这个答案中,我专注于用\foreach
循环重现您的输出。我不会重现您的命名节点,因为这些节点在您的代码中没有使用。当然,可以生成自动命名的节点,如果碰巧使用它们,我可以添加它们。
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\begin{document}
\begin{frame}[t]
\frametitle{}
\hspace {-1cm}
\begin{tikzpicture}[scale=.7, transform shape,
linecolor/.style={ultra thick, green!40!black},
dotline/.style={thin, black, loosely dotted}]
\pgfmathsetmacro{\incrmnt}{.6}
\draw [thick,-latex](-.3,0) -- (5,0);
\draw [thick,-latex](0,-.3) -- (0,8);
\draw [thick,-latex](6.,0) -- (14.4,0) coordinate (br);
\draw [thick,-latex](8,-.2) -- (8,8) coordinate(tr);
\foreach \X in {1,...,12}
{
\draw [dotline] (0,\X*7cm/12) -- ++ (14,0);
\draw [linecolor] (-\incrmnt/2,\X*7cm/12)node[left]{\X/12} -- ++ (\incrmnt,0)
(8-\incrmnt/2,\X*7cm/12)node[left]{\X/12}-- ++ (\incrmnt,0);
}
\foreach \X in {1,...,7}
{
\draw[linecolor] (3*\X/5,-\incrmnt/2) node[below]{\X} -- ++ (0,\incrmnt)
(8+3*\X/5,-\incrmnt/2) node[below]{\X} -- ++ (0,\incrmnt);
\pgfmathtruncatemacro{\tst}{abs(\X-4)}
\ifnum\tst=3
\draw[linecolor] (3*\X/5,0) -- ++ (0,7/12)
node[fill,red,circle,inner sep=2pt]{};
\else
\draw[linecolor] (3*\X/5,0) -- ++ (0,14/12)
node[fill,red,circle,inner sep=2pt]{};
\fi
\ifnum\X=7
\draw[blue,thick] (8+3*\X/5,{7*(2*\X-2)/12})
node[fill,red,circle,inner sep=2pt]{} -- ++ (2*7cm/12,0) coordinate(xr);
\else
\draw[blue,thick] (8+3*\X/5,{7*(2*\X-1)/12}) node[fill,red,circle,inner sep=2pt]{}
-- ++ (1*7cm/12,0) node[circle,fill=white,draw=red,inner sep=2pt]{};
\fi
}
\draw (tr-|xr) -- (br-|xr) node[below](ir){$\infty$};
\node at (6.5,0 |-ir) {$-\infty$};
\node[circle,fill=white,draw=red,inner sep=2pt,thick] at (8+3/5,0){};
\end{tikzpicture}
\end{frame}
\end{document}
答案2
编辑:
易于阅读的代码:
编程的基本原则是编写易于阅读代码,尤其是使用像 TeX 这样的编译语言。事实上,代码越容易阅读和理解,在 6 个月、1 年或 10 年后修改就越容易……
因此,我写道(即使代码没有优化)每个功能块的代码。这里:
- 我首先画出左图的坐标轴
- 然后我画出图表本身(我将数据放入其中)
- 我对第二张图也做了同样的操作。
这样构造和注释的代码很容易理解。
重复使用代码:
编程的根本原则就是重复使用代码。为此,我们可以使用macros
或更简单的foreach
循环。
这里有两个图表相同的轴。你所要做的就是建造它,然后重复使用它scope
在向右移动几厘米的环境中。这避免了不必要的计算。
完成后,第二张图的内容就绘制出来了。
第二张图表很适合foreach
循环,因为每次1 unit
向右移动,就会2 units
向上移动,因此这个公式\incrmnt*(2*\x-1)
使得计算变得简单。
由于其余部分不可分解,因此单独绘制。
使用 www.DeepL.com/Translator 翻译
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{}
\hspace {-1cm}
\begin{tikzpicture}[scale=.7, transform shape,linecolor/.style={ultra thick, green!40!black},dotline/.style={thin, black, loosely dotted},
point/.style={circle, draw, red, fill=red, scale=0.4},
cercle/.style={circle,draw,scale=.5, red,thick, fill=white,inner sep=6pt}
]
\pgfmathsetmacro{\incrmnt}{.6}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plot of the abscissa axis (horizontal) and ordinate axis (on the left)
\draw [thick,-latex](-.3,0) -- (5,0);
\draw [thick,-latex](0,-.3) -- (0,8);
\foreach \y in {1,...,12}{
\draw [linecolor] (0,\incrmnt*\y) +(0:.15) -- +(180:.15) node [left] {\y/12};% indexation of ordinate axis (vertical)
\draw [dotline] (0,\incrmnt*\y) -- +(0:8*\incrmnt);% horizontal dotted lines
}
\foreach \x in {1,...,7}{
\draw [linecolor] (\incrmnt*\x,.15) -- (\incrmnt*\x,-.15) node [below] {\x} ;
}
% draw of the left graph
\begin{scope}[every node/.style={circle, draw, solid, red, fill=red, scale=0.4}]
\draw [linecolor] (\incrmnt*1,\incrmnt*1) node{} -- (\incrmnt,.15);
\draw [linecolor] (\incrmnt*2,\incrmnt*2) node{}-- (\incrmnt*2,.15);
\draw [linecolor] (\incrmnt*3,\incrmnt*2) node{} -- (\incrmnt*3,.15);
\draw [linecolor] (\incrmnt*4,\incrmnt*2) node{} -- (\incrmnt*4,.15);
\draw [linecolor] (\incrmnt*5,\incrmnt*2) node{} -- (\incrmnt*5,.15);
\draw [linecolor] (\incrmnt*6,\incrmnt*2) node{} -- (\incrmnt*6,.15);
\draw [linecolor] (\incrmnt*7,\incrmnt*1) node {} -- (\incrmnt*7,.15);
\end{scope}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% graph on the rigth
% plot the same axis by a simple 8cm translation
\begin{scope}[xshift=8cm]
\draw [thick,-latex](-2,0)node [below right] {$-\infty$} -- (6,0);
\draw [thick,-latex](0,-.3) -- (0,8);
\foreach \y in {1,...,12}{
\draw [linecolor] (0,\incrmnt*\y) +(0:.15cm) -- +(180:.15cm) node [left] {\y/12};% indexation of ordinate axis (vertical)
\draw [dotline] (0,\incrmnt*\y) -- +(0:10*\incrmnt);% horizontal dotted lines
}
\foreach \x in {1,...,7}{
\draw [linecolor] (\incrmnt*\x,.15) -- (\incrmnt*\x,-.15) node [below] {\x} ;
}
% draw the graph on the rigth
\draw [thick, black] (\incrmnt*9,0) node [below] {$\infty$} --+(0,\incrmnt*13);
\foreach \x in {1,...,6}{
\draw[ultra thick, blue](\incrmnt*\x,{\incrmnt*(2*\x-1)})node[point]{}--+(\incrmnt,0)node[cercle]{};
}
\node[cercle] at(\incrmnt,0){};
\draw[ultra thick, blue](\incrmnt*7,\incrmnt*12)node[point]{}--+(\incrmnt*2,0);
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}
较早的答案:
要使用\foreach
循环,只需确定每行中并行修改的数字即可。在您的代码中,前几行可以通过循环轻松分解。
以下的因素更难分解,因为没有可以轻易利用的规律。
编辑:修正因式分解的遗漏
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{}
\hspace {-1cm}
\begin{tikzpicture}[scale=.7, transform shape,linecolor/.style={ultra thick, green!40!black},dotline/.style={thin, black, loosely dotted}]
\pgfmathsetmacro{\incrmnt}{.6}
\draw [thick,-latex](-.3,0) -- (0,0) -- (5,0);
\draw [thick,-latex](0,-.3) -- (0,0) -- (0,8);
\draw [thick,-latex](6.,0) -- (7,0) -- (14.4,0);
\draw [thick,-latex](8,-.2) -- (8,0) -- (8,8);
% Factoring the first 12 operations `\draw`:
\foreach \y in {1,...,12}{
\draw [linecolor] (0,\incrmnt*\y) node (yhl){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {\y/12}([shift={(0:8)}]yhl.center) node (yh){} +(180:.15cm) -- +(0:.15cm) node [xshift=-.8cm, yshift=0cm] {\y/12};
\draw [dotline] (0,\incrmnt*\y) -- +(0:14cm);
}
\draw [linecolor] (\incrmnt*1,0) node (al){} +(90:\incrmnt*1) node [circle, draw, solid, red, fill=red, scale=0.4]{} -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {1} ([shift={(0:8)}]al.center) node (a){} +(90:.25cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {1} ([shift={(90:\incrmnt*1)}]a.center) node (au){} ([shift={(90:\incrmnt*1)}]au.center) node (auu){};
\draw [linecolor] (\incrmnt*2,0) node (bl){} +(90:\incrmnt*2) node [circle, draw, solid, red, fill=red, scale=0.4]{} -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {2} ([shift={(0:8)}]bl.center) node (b){} +(90:.25cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {2} ([shift={(90:\incrmnt*1)}]b.center) node (bu){} ([shift={(90:\incrmnt*2)}]bu.center) node (buu){};
\draw [linecolor] (\incrmnt*3,0) node (cl){} +(90:\incrmnt*2) node [circle, draw, solid, red, fill=red, scale=0.4]{} -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {3} ([shift={(0:8)}]cl.center) node (c){} +(90:.25cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {3} ([shift={(90:\incrmnt*3)}]c.center) node (cu){} ([shift={(90:\incrmnt*2)}]cu.center) node (cuu){};
\draw [linecolor] (\incrmnt*4,0) node (dl){} +(90:\incrmnt*2) node [circle, draw, solid, red, fill=red, scale=0.4]{} -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {4} ([shift={(0:8)}]dl.center) node (d){} +(90:.25cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {4} ([shift={(90:\incrmnt*5)}]d.center) node (du){} ([shift={(90:\incrmnt*2)}]du.center) node (duu){};
\draw [linecolor] (\incrmnt*5,0) node (el){} +(90:\incrmnt*2) node [circle, draw, solid, red, fill=red, scale=0.4]{} -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {5} ([shift={(0:8)}]el.center) node (e){} +(90:.25cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {5} ([shift={(90:\incrmnt*7)}]e.center) node (eu){} ([shift={(90:\incrmnt*2)}]eu.center) node (euu){};
\draw [linecolor] (\incrmnt*6,0) node (fl){} +(90:\incrmnt*2) node [circle, draw, solid, red, fill=red, scale=0.4]{} -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {6} ([shift={(0:8)}]fl.center) node (f){} +(90:.25cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {6} ([shift={(90:\incrmnt*9)}]f.center) node (fu){} ([shift={(90:\incrmnt*2)}]fu.center) node (fuu){};
\draw [linecolor] (\incrmnt*7,0) node (gl){} +(90:\incrmnt*1) node [circle, draw, solid, red, fill=red, scale=0.4]{} -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {7} ([shift={(0:8)}]gl.center) node (g){} +(90:.25cm) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {7}([shift={(90:\incrmnt*11)}]g.center) node (gu){} ([shift={(90:\incrmnt*1)}]gu.center) node (guu){};
\end{tikzpicture}
\end{frame}
\end{document}