\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}
答案1
在循环内使用\global\let
而不是。循环是组有限的,因此使用 只会进行局部更改,该更改会在每次迭代结束时丢失。\let
\foreach
\let
\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}
答案2
循环中的赋值\foreach
仅限于当前循环周期。您可以使用索引\k
。
您确实要加载吗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}