![平均能量损失](https://linux22.com/image/346093/%E5%B9%B3%E5%9D%87%E8%83%BD%E9%87%8F%E6%8D%9F%E5%A4%B1.png)
基本上,我将使用许多非常相似的图,但 2 个边界的位置和 2 个标签的位置有所不同,因此我希望能够只输入这两个数字作为参数,然后让 LaTeX 来处理它。我的失败尝试:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{xparse}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
%zc,zt
\NewDocumentCommand{\hypplot}{ m m }
{%
\begin{tikzpicture}
\begin{axis}[
no markers, domain=-4:4,
samples=100, axis lines*=left,
every axis x label/.style={at=(current axis.right of origin),anchor=west},
height=5cm, width=12cm,
ytick=\empty, xtick=\empty,
enlargelimits=false, clip=false, axis on top,
y axis line style={draw=none},
]
%%Shade the area for alpha
\addplot [fill=cyan!20, draw=none, domain=-4:#1] {gauss(0,1)} \closedcycle;
%%Shade the area for p-value
\addplot [fill=orange!20, draw=none, domain=#1:#2] {gauss(0,1)} \closedcycle;
%%draw the gaussian
\addplot [very thick,cyan!50!black] {gauss(0,1)};
%%draws a double-headed arrow for Zc
\draw [latex-latex] (axis cs:#1,-0.07) -- node [fill=white] {$\displaystyle \zcl{c}\frac{\sigma}{\sqrt{n}}$} (axis cs:0,0.-0.07);
%%draws a double-headed arrow for Zt
\draw [yshift=-0.6cm, latex-latex] (axis cs:#2,0.16) -- (axis cs:0,0.16);
%%Label Zt
\draw [yshift=-0.6cm](axis cs:(#1+#2)/2,0.22) node {$\displaystyle \zcl{T}\frac{\sigma}{\sqrt{n}}$};
%%label the mean
\draw (axis cs:0,.43) node {$\mu=\mu_0^{}$};
\addplot +[mark=none] coordinates {(0, 0) (0, 0.3989)};
%%add alpha
\draw (axis cs:#1*1.071,0.02) node {$\alpha$};
\addplot +[mark=none] coordinates {(#1, -.01) (#1, 0.08)};
\draw (axis cs:#1*1.02,0.10) node {$\bar{x}_c^{}$};
%add p-value border
\addplot +[mark=none] coordinates {(#2, -.01) (#2, 0.22)};
\draw (axis cs:#2*1.02,0.25) node {$\bar{x}$};
\end{axis}
\end{tikzpicture}%
}
\begin{document}
\hypplot{-1.96}{-1.08}
\end{document}
我想我总是可以复制粘贴并更改它们每一个,但如果有一个命令来执行此操作不是很好吗?
答案1
当然会很好。如果你从\hypplot
命令中复制并粘贴代码,并且只替换和#1
,#2
你-1.96
仍然-1.08
会得到一个错误。
问题是 PFGPlots 在解析坐标时不接受表达式(请参阅有关此问题)因此如果您只是解析表达式,然后在坐标内有一个表达式时\pgfmathparse
传递结果,它就可以正常工作:\pgfmathresult
平均能量损失
\documentclass{article}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{xparse}
\pgfplotsset{compat=1.14}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
%zc,zt
\NewDocumentCommand{\hypplot}{ m m }
{%
\begin{tikzpicture}
\begin{axis}[ no markers, domain=-4:4,
samples=100, axis lines*=left,
every axis x label/.style={at={(current axis.right of origin)}, anchor=west},
height=5cm, width=12cm,
ytick=\empty, xtick=\empty,
enlargelimits=false, clip=false, axis on top,
y axis line style={draw=none}
]
%%Shade the area for alpha
\addplot [fill=cyan!20, draw=none, domain=-4:#1] {gauss(0,1)} \closedcycle;
%%Shade the area for p-value
\addplot [fill=orange!20, draw=none, domain=#1:#2] {gauss(0,1)} \closedcycle;
%%draw the gaussian
\addplot [very thick,cyan!50!black] {gauss(0,1)};
%%draws a double-headed arrow for Zc
\draw [latex-latex] (axis cs:#1,-0.07) -- node [fill=white] {$\displaystyle \frac{\sigma}{\sqrt{n}}$} (axis cs:0,0.-0.07);
%%draws a double-headed arrow for Zt
\draw [yshift=-0.6cm, latex-latex] (axis cs:#2,0.16) -- (axis cs:0,0.16);
%%Label Zt
\pgfmathparse{#1+#2)/2}
\draw [yshift=-0.6cm](axis cs:(\pgfmathresult,0.22) node {$\displaystyle \frac{\sigma}{\sqrt{n}}$};
%%label the mean
\draw (axis cs:0,.43) node {$\mu=\mu_0^{}$};
\addplot +[mark=none] coordinates {(0, 0) (0, 0.3989)};
%%add alpha
\pgfmathparse{#1*1.071}
\draw (axis cs:\pgfmathresult,0.02) node {$\alpha$};
\addplot +[mark=none] coordinates {(#1, -.01) (#1, 0.08)};
\pgfmathparse{#1*1.02}
\draw (axis cs:\pgfmathresult,0.10) node {$\bar{x}_c^{}$};
%add p-value border
\pgfmathparse{#2*1.02}
\addplot +[mark=none] coordinates {(#2, -.01) (#2, 0.22)};
\draw (axis cs:\pgfmathresult,0.25) node {$\bar{x}$};
\end{axis}
\end{tikzpicture}%
}
\begin{document}
\hypplot{-1.96}{-1.08}
\end{document}