0

I want to make a tikzpicture displaying a function shifted multiple times, each time having a lighter color and line width. Using How to make Loop? and Changing color in foreach as references, I created the following code:

\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}

However, there are some errors (current one being undefined control sequence \pgfkeyscurrentkey ->blue!\clr). A comment in one of the linked posts mentioned something about \foreach not working in axis (but the 1st post I linked does use that...), and so I tried looking at \foreach not behaving in axis environment and replacing \foreach with pgfplotsinvokeforeach, but that didn't work (undefined control sequence \pgfplotsinvokeforeach)

1 Answer 1

2

Attempt using \pgfplotsinvokeforeach below, while it should not raise "undefined control sequence", will give the wrong output here...

%! 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}

... because \clr and \lnwdth themselves are macros, which suffers from the same problem.

Use the edef trick:

%! 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}
5

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .