\documentclass[ aspectratio = 1610 ]{beamer}
\usepackage{psfrag}
\usepackage{sidecap}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{frame}
\begin{tikzpicture} [scale = 0.6]
\def\score{10}
\draw (0,3) node [right] {Score is initialized to 10 and will be augmented by 3 twice by repeating code};
\pgfmathparse{\score + 3 } \let\score\pgfmathresult
\draw (0,2) node [right] { New score \quad $\score$ correct};
\pgfmathparse{\score + 3 } \let\score\pgfmathresult
\draw (0,1) node [right] {New score \quad $\score$ correct };
\draw [dashed](0, 0.5)--(20,.5);
\draw(0,0) node[right] {Score re-initialized to 10 and will be augmented by 3 in a foreach loop , again twice};
\def\score{10}
\foreach \k in { 1,2 } {
\pgfmathparse{\score + 3 } \let\score\pgfmathresult
\draw (0, - \k) node [right] { New score $\score$ \ifthenelse{\k=1} { Correct} {should be 16 Wrong. Why?}};
}
\end{tikzpicture}
\end{frame}
\end{document}
2 Answers
Use \global\let
instead of \let
inside the \foreach
loop. The loop is group limited, and so using just \let
makes a local change, which is lost at the end of each iteration.
\documentclass[ aspectratio = 1610 ]{beamer}
\usepackage{psfrag}
\usepackage{sidecap}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{frame}
\begin{tikzpicture} [scale = 0.6]
\def\score{10}
\draw (0,3) node [right] {Score is initialized to 10 and will be augmented by 3 twice by repeating code};
\pgfmathparse{\score + 3 } \let\score\pgfmathresult
\draw (0,2) node [right] { New score \quad $\score$ correct};
\pgfmathparse{\score + 3 } \let\score\pgfmathresult
\draw (0,1) node [right] {New score \quad $\score$ correct };
\draw [dashed](0, 0.5)--(20,.5);
\draw(0,0) node[right] {Score re-initialized to 10 and will be augmented by 3 in a foreach loop , again twice};
\def\score{10}
\foreach \k in { 1,2 } {
\pgfmathparse{\score + 3 } \global\let\score\pgfmathresult
\draw (0, - \k) node [right] { New score $\score$ \ifthenelse{\k=1} { Correct} {should be 16 Wrong. Why?}};
}
\end{tikzpicture}
\end{frame}
\end{document}
Assignments in \foreach
loops are local to the current loop cycle. You can use the index \k
.
Are you really wanting to load psfrag
?
\documentclass[ aspectratio = 1610 ]{beamer}
% \usepackage{psfrag} % really?
%\usepackage{sidecap}
\usepackage{tikz}
\usepackage{pgfplots}
%\usepackage{amsmath}% already loaded by beamer
\begin{document}
\begin{frame}
\begin{tikzpicture} [scale = 0.6]
\def\score{10}
\draw (0,3) node [right] {Score is initialized to 10 and will be
augmented by 3 twice by repeating code};
\pgfmathsetmacro\score{\score + 3 }
\draw (0,2) node [right] { New score \quad $\score$ correct};
\pgfmathsetmacro\score{\score + 3 }
\draw (0,1) node [right] {New score \quad $\score$ correct };
\draw [dashed](0, 0.5)--(20,.5);
\draw(0,0) node[right] {Score re-initialized to 10 and will be
augmented by 3 in a foreach loop, again twice};
\def\score{10}
\foreach \k in { 1,2 } {
\pgfmathsetmacro\score{\score + 3*\k}
\draw (0,-\k) node [right] {New score $\score$};
}
\end{tikzpicture}
\end{frame}
\end{document}
\foreach
loop is executed within a group, so your\let
is reverted at the end of each iteration.