2

I have a beamer frame with an axis environment that contains a foreach loop. I would like to use the count index deriving from such loop as a variable within the loop. Specifically, I need such variable to affect the visibility and label of the plot. I cannot manage to adapt accordingly neither this nor this chunk of code. I provide hereby a minimum (non) working example. The related picture is obtained by running the currently commented line and while not running (commenting) the line above.

\documentclass[beamer]{standalone}
\usepackage{pgfplots}
\tikzset{
    invisible/.style={opacity=0},
    visible on/.style={alt={#1{}{invisible}}},
    alt/.code args={<#1>#2#3}{%
        \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}
    },
}

\begin{document} \begin{frame} \begin{center} \begin{tikzpicture} \begin{axis}[width=7cm] \foreach \p [count=\n from 0] in {0.15, 1.15, 2.15} { \def\pline{(1-(\p*(\x-3))}; \addplot [mark=none, draw=gray, domain=0:4,visible on=<\n>] {\pline} node [pos=0.25] {\n}; %\addplot [mark=none, draw=gray, domain=0:4] {\pline} node [pos=0.25, fill=white] {L}; } \end{axis} \end{tikzpicture} \end{center} \end{frame} \end{document}

Picture obtain with currently commented line

1 Answers1

3

You can use the same trick as in https://tex.stackexchange.com/a/667486/36296 to uncover your curves.

For the label, one could use something based on the current slide in frame number instead of the loop counter. If there are other overlays on your page, adjust the value of -1 to match what labels you want.

\documentclass[beamer]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{overlay-beamer-styles}

\makeatletter \newcommand*{\foo}{\number\numexpr\beamer@slideinframe-1} \makeatother

\begin{document}

\begin{frame} \begin{center} \begin{tikzpicture} \begin{axis}[width=7cm] \foreach \p [count=\n from 1] in {0.15, 1.15, 2.15} { \def\pline{(1-(\p*(\x-3))}; \xdef\ADDPLOT{% \noexpand \addplot [mark=none, draw=gray, domain=0:4,visible on=<\n>] } \ADDPLOT {\pline} node [pos=0.25,fill=white] {\foo}; } \end{axis} \end{tikzpicture} \end{center} \end{frame} \end{document}

enter image description here