TransWikia.com

Why does foreach loop seem to interfere with let code????? See the code below. Outside a loop, repeating the same code twice works fine

TeX - LaTeX Asked on December 10, 2020

 documentclass[ aspectratio = 1610 ]{beamer} 
 
 usepackage{psfrag}
 
 usepackage{sidecap}
 
 usepackage{tikz}
 
 usepackage{pgfplots}
 
 usepackage{amsmath}
 
  begin{document}
  
 begin{frame}
 
 begin{tikzpicture} [scale = 0.6]
 
 defscore{10}
 
  draw (0,3) node [right] {Score is initialized to 10 and will be augmented  by 3 twice by repeating code};

  pgfmathparse{score + 3 } letscorepgfmathresult

 draw  (0,2)  node [right] { New score quad    $score$ correct};

  pgfmathparse{score + 3 } letscorepgfmathresult

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

 defscore{10}

  foreach k in { 1,2 }  {

  pgfmathparse{score + 3 } letscorepgfmathresult

  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 globallet 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]
 
 defscore{10}
 
  draw (0,3) node [right] {Score is initialized to 10 and will be augmented  by 3 twice by repeating code};

  pgfmathparse{score + 3 } letscorepgfmathresult

 draw  (0,2)  node [right] { New score quad    $score$ correct};

  pgfmathparse{score + 3 } letscorepgfmathresult

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

 defscore{10}

  foreach k in { 1,2 }  {

  pgfmathparse{score + 3 } globalletscorepgfmathresult

  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

Answered by Steven B. Segletes on December 10, 2020

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]
defscore{10}
draw (0,3) node [right] {Score is initialized to 10 and will be 
                          augmented  by 3 twice by repeating code};
pgfmathsetmacroscore{score + 3 }
draw  (0,2)  node [right] { New score quad    $score$ correct};
pgfmathsetmacroscore{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};
defscore{10}
foreach k in { 1,2 } {
  pgfmathsetmacroscore{score + 3*k}
  draw (0,-k) node [right] {New score $score$};
}
end{tikzpicture}
 
end{frame}
  
end{document}

enter image description here

Answered by egreg on December 10, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP