1
 \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}
3
  • misprint: header should say interfere with \let code............
    – user34241
    Commented Jul 28, 2020 at 18:13
  • I fixed the header. You should be able to edit your question, as well. Commented Jul 28, 2020 at 18:18
  • Each iteration of a \foreach loop is executed within a group, so your \let is reverted at the end of each iteration.
    – Skillmon
    Commented Jul 28, 2020 at 18:20

2 Answers 2

1

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}

enter image description here

0

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}

enter image description here

You must log in to answer this question.

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