我想制作一个 tikzpicture,显示多次移位的函数,每次移位的颜色和线宽都更浅。使用如何制作循环?和在 foreach 中改变颜色作为参考,我创建了以下代码:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=1]
\begin{axis}[
axis lines=center,
grid=major,
xmin=-0.25,
xmax=10,
ymin=-0.25,
ymax=1.25,
width=15cm,
height=5cm,
]
\addplot[smooth,densely dashed,black] coordinates {(5,-0.5)(5,1.5)};
% https://tex.stackexchange.com/questions/218704/how-to-make-loop
% https://tex.stackexchange.com/questions/251300/changing-color-in-foreach
\foreach \n in {0, ..., 8}
{
\pgfmathsetmacro\clr{\n*10} % color
\pgfmathsetmacro\lnwdth{10-\n} % color
\addplot[smooth, blue!\clr, line width=\lnwdth pt,domain={{8-\n}:10},->] {1/((x-6+\n)*ln(x-6+\n))};
\addplot[smooth,blue!\clr,line width=\lnwdth pt, domain={-0.25:8-\n},<-] {0};
\addplot[mark=*,solid,blue!\clr] coordinates {(8-\n, {1/(2*ln(2))} )};
\addplot[mark=*,solid,blue!\clr,fill=white] coordinates {(8-\n, 0)};
}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
但是,有一些错误(当前错误是undefined control sequence \pgfkeyscurrentkey ->blue!\clr
)。链接帖子中的一个评论提到了\foreach
无法使用axis
(但我链接的第一篇帖子确实使用了这一点……),所以我尝试查看\foreach 在轴环境中不起作用并替换\foreach
为pgfplotsinvokeforeach
,但这不起作用(undefined control sequence \pgfplotsinvokeforeach
)
答案1
尝试使用\pgfplotsinvokeforeach
下面的方法,虽然它不应该引发“未定义的控制序列”,但在这里会给出错误的输出......
%! TEX program = lualatex
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
123
\begin{center}
\begin{tikzpicture}[scale=1]
\begin{axis}[
axis lines=center,
grid=major,
xmin=-0.25,
xmax=10,
ymin=-0.25,
ymax=1.25,
width=15cm,
height=5cm,
]
\addplot[smooth,densely dashed,black] coordinates {(5,-0.5)(5,1.5)};
% https://tex.stackexchange.com/questions/218704/how-to-make-loop
% https://tex.stackexchange.com/questions/251300/changing-color-in-foreach
\pgfplotsinvokeforeach {0, ..., 8}
{
\pgfmathsetmacro\clr{#1*10} % color
\pgfmathsetmacro\lnwdth{10-#1} % color
\addplot[smooth, blue!\clr, line width=\lnwdth pt,domain={{8-#1}:10},->] {1/((x-6+#1)*ln(x-6+#1))};
\addplot[smooth,blue!\clr,line width=\lnwdth pt, domain={-0.25:8-#1},<-] {0};
\addplot[mark=*,solid,blue!\clr] coordinates {(8-#1, {1/(2*ln(2))} )};
\addplot[mark=*,solid,blue!\clr,fill=white] coordinates {(8-#1, 0)};
}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
...因为\clr
和\lnwdth
它们本身都是宏,所以存在同样的问题。
使用 edef 技巧:
%! TEX program = lualatex
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
123
\begin{center}
\begin{tikzpicture}[scale=1]
\begin{axis}[
axis lines=center,
grid=major,
xmin=-0.25,
xmax=10,
ymin=-0.25,
ymax=1.25,
width=15cm,
height=5cm,
]
\addplot[smooth,densely dashed,black] coordinates {(5,-0.5)(5,1.5)};
% https://tex.stackexchange.com/questions/218704/how-to-make-loop
% https://tex.stackexchange.com/questions/251300/changing-color-in-foreach
\foreach \n in {0, ..., 8}
{
\pgfmathsetmacro\clr{\n*10} % color
\pgfmathsetmacro\lnwdth{10-\n} % color
\edef\temp{
\noexpand\addplot[smooth, blue!\clr, line width=\lnwdth pt,domain={{8-\n}:10},->] {1/((x-6+\n)*ln(x-6+\n))};
\noexpand\addplot[smooth,blue!\clr,line width=\lnwdth pt, domain={-0.25:8-\n},<-] {0};
\noexpand\addplot[mark=*,solid,blue!\clr] coordinates {(8-\n, {1/(2*ln(2))} )};
\noexpand\addplot[mark=*,solid,blue!\clr,fill=white] coordinates {(8-\n, 0)};
}\temp
}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}