![修改 \foreach 循环中的计算节点](https://linux22.com/image/387348/%E4%BF%AE%E6%94%B9%20%5Cforeach%20%E5%BE%AA%E7%8E%AF%E4%B8%AD%E7%9A%84%E8%AE%A1%E7%AE%97%E8%8A%82%E7%82%B9.png)
使用以下代码从答案这个问题,如何用+1sigma +2sigma +3sigma 替换 0sigma 节点\mu
,以及如何用+1sigma +2sigma +3sigma 替换 1sigma 2sigma 3sigma 节点?
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\tikzset{pics/.cd,
curvea/.style n args={2}{code={
\node [red] at (#1,#2) (begin){}
([shift={(0.:.2cm)}]begin.center) node (beginr){}
([shift={(30:4.cm)}]beginr.center) node (midtop){}
([shift={(-30:4.cm)}]midtop.center) node (end){}
([shift={(0.:.2cm)}]end.center) node (endr){}
;
}}}
\begin{frame}[t]
\frametitle{mu +sigma}
\vspace {.6cm}
\begin{tikzpicture}[scale=.86, transform shape]
\draw [thick,-latex](0,0) -- (8.4,0) node [black, xshift=.3cm, yshift=0cm] {Z};
\draw [thick,-latex](0,-2.) -- (8.4,-2.) node [black, xshift=.3cm, yshift=0cm] {Z};
\draw [thick,-latex](0,-2.5) -- (0,4);
\pic at (0,0) {curvea={.246}{.2}};
\draw[blue, very thick] ([shift={(-150.:.14cm)}]begin.center) to [out=10, in=10, looseness=.68] (beginr.center) to [out=10, in=180, looseness=.6] ([shift={(90:1.2cm)}]midtop.center) node (midtopnew){} to [out=0, in=170, looseness=.6] (end.center) to [out=170, in=170, looseness=.68] ([shift={(-30.:.14cm)}]endr.center) ([shift={(180:1.2cm)}]midtopnew.center) node (ml){} ([shift={(0:1.22cm)}]midtopnew.center) node (mr){};
\draw [very thick, black] (midtopnew.center) -- (midtopnew.center|-0,0);
\begin{scope}[xshift=-1.cm, yshift=-2.cm]
\foreach[count=\j] \i in {-3,...,3}
\draw[very thick] (\j*1.227,.2) -- ++(0,-.4) node[yshift=-.35cm]{\i$\sigma$}coordinate(\i);
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}
答案1
如果您不想深入研究\pgfmathprintnumber
,您可以只使用一些\ifnum
语句。
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\tikzset{pics/.cd,
curvea/.style n args={2}{code={
\node [red] at (#1,#2) (begin){}
([shift={(0.:.2cm)}]begin.center) node (beginr){}
([shift={(30:4.cm)}]beginr.center) node (midtop){}
([shift={(-30:4.cm)}]midtop.center) node (end){}
([shift={(0.:.2cm)}]end.center) node (endr){}
;
}}}
\begin{frame}[t]
\frametitle{mu +sigma}
\vspace {.6cm}
\begin{tikzpicture}[scale=.86, transform shape]
\draw [thick,-latex](0,0) -- (8.4,0) node [black, xshift=.3cm, yshift=0cm] {Z};
\draw [thick,-latex](0,-2.) -- (8.4,-2.) node [black, xshift=.3cm, yshift=0cm] {Z};
\draw [thick,-latex](0,-2.5) -- (0,4);
\pic at (0,0) {curvea={.246}{.2}};
\draw[blue, very thick] ([shift={(-150.:.14cm)}]begin.center) to [out=10, in=10, looseness=.68] (beginr.center) to [out=10, in=180, looseness=.6] ([shift={(90:1.2cm)}]midtop.center) node (midtopnew){} to [out=0, in=170, looseness=.6] (end.center) to [out=170, in=170, looseness=.68] ([shift={(-30.:.14cm)}]endr.center) ([shift={(180:1.2cm)}]midtopnew.center) node (ml){} ([shift={(0:1.22cm)}]midtopnew.center) node (mr){};
\draw [very thick, black] (midtopnew.center) -- (midtopnew.center|-0,0);
\begin{scope}[xshift=-1.cm, yshift=-2.cm]
\foreach[count=\j] \i in {-3,...,3}
{\ifnum\i=0
\draw[very thick] (\j*1.227,.2) -- ++(0,-.4) node[yshift=-.35cm]{$\mu$}coordinate(\i);
\else
\ifnum\i>0
\def\mysign{+}
\else
\def\mysign{}
\fi
\draw[very thick] (\j*1.227,.2) -- ++(0,-.4) node[yshift=-.35cm]{$\mysign\i\sigma$}coordinate(\i);
\fi
}
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}
答案2
只需使用 pgf 键\pgfkeys{/pgf/number format/showpos}
并显示数字\pgfmathprintnumber{\i}
(参见 3.0.1a 手册第 952 页)。
更新:
为了用符号 \mu 代替 0sigma,我们可以简单地重写循环中写入的符号。
\draw (4*1.227,-.2)node[yshift=-.35cm,fill=white,minimum width=7mm]{$\mu$};
Foreach代码:
\begin{scope}[xshift=-1.cm, yshift=-2.cm]
\pgfkeys{/pgf/number format/showpos}
\foreach[count=\j] \i in {-3,...,3}
\draw[very thick] (\j*1.227,.2) -- ++(0,-.4) node[yshift=-.35cm]{\pgfmathprintnumber{\i}$\sigma$}coordinate(\i);
\draw (4*1.227,-.2)node[yshift=-.35cm,fill=white,minimum width=7mm]{$\mu$};
\end{scope}
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\tikzset{pics/.cd,
curvea/.style n args={2}{code={
\node [red] at (#1,#2) (begin){}
([shift={(0.:.2cm)}]begin.center) node (beginr){}
([shift={(30:4.cm)}]beginr.center) node (midtop){}
([shift={(-30:4.cm)}]midtop.center) node (end){}
([shift={(0.:.2cm)}]end.center) node (endr){}
;
}}}
\begin{frame}[t]
\frametitle{mu +sigma}
\vspace {.6cm}
\begin{tikzpicture}[scale=.86, transform shape]
\draw [thick,-latex](0,0) -- (8.4,0) node [black, xshift=.3cm, yshift=0cm] {Z};
\draw [thick,-latex](0,-2.) -- (8.4,-2.) node [black, xshift=.3cm, yshift=0cm] {Z};
\draw [thick,-latex](0,-2.5) -- (0,4);
\pic at (0,0) {curvea={.246}{.2}};
\draw[blue, very thick] ([shift={(-150.:.14cm)}]begin.center) to [out=10, in=10, looseness=.68] (beginr.center) to [out=10, in=180, looseness=.6] ([shift={(90:1.2cm)}]midtop.center) node (midtopnew){} to [out=0, in=170, looseness=.6] (end.center) to [out=170, in=170, looseness=.68] ([shift={(-30.:.14cm)}]endr.center) ([shift={(180:1.2cm)}]midtopnew.center) node (ml){} ([shift={(0:1.22cm)}]midtopnew.center) node (mr){};
\draw [very thick, black] (midtopnew.center) -- (midtopnew.center|-0,0);
\begin{scope}[xshift=-1.cm, yshift=-2.cm]
\pgfkeys{/pgf/number format/showpos}
\foreach[count=\j] \i in {-3,...,3}
\draw[very thick] (\j*1.227,.2) -- ++(0,-.4) node[yshift=-.35cm]{\pgfmathprintnumber{\i}$\sigma$}coordinate(\i);
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}