# Problem using variables with feynmp fmffor

This is a follow-up to this question. The solution provided by egreg worked, but the same solution doesn't work when trying to update a variable within an fmffor loop. A complete example is shown below. The expected behavior is to produce two photons each bending in the opposite direction (right=+0.5, and right=-0.5), but instead \temp is always set to +0.5 in the simple.mp file.

\documentclass[border=2cm]{standalone}
\usepackage{feynmp}
\usepackage[pdftex]{graphicx}
\usepackage{pgf}

\DeclareGraphicsRule{*}{mps}{*}{}

\newcommand{\efmf}[1]{
\begingroup\edef\x{\endgroup\noexpand\fmf{#1}}\x
}

\begin{document}

\begin{fmffile}{simple}
\begin{fmfgraph*}(450,320)
\fmftopn{t}{3}    \fmfn{plain}{t}{3}
\fmfbottomn{b}{3} \fmfn{plain}{b}{3}
\pgfmathsetmacro{\temp}{-1.5}
\begin{fmffor}{n}{1}{1}{2}
\pgfmathsetmacro{\temp}{\temp+1}
\efmf{photon, foreground=blue, tension=0.1, right=\temp}{t2,b2}
\end{fmffor}
\end{fmfgraph*}
\end{fmffile}

\end{document}


Here is another complete example that includes the needed functionality of fmffor (it should display the photon loop bent to the left on the left, and to the right on the right):

\documentclass[border=2cm]{standalone}
\usepackage{feynmp}
\usepackage{graphicx}
\usepackage{xparse}
\usepackage{pgf}

\DeclareGraphicsRule{*}{mps}{*}{}

\ExplSyntaxOn
\NewDocumentCommand{\efmf}{mm}
{
\xfmf_fmf:xn { #1 } { #2 }
}
\cs_new_protected:Nn \xfmf_fmf:nn { \fmf{#1}{#2} }
\cs_generate_variant:Nn \xfmf_fmf:nn { x }
\NewDocumentCommand{\fmfcycle}{mmmm}
{
\cs_set:Nn \__xfmf_fmfcycle:n { #4 }
\int_step_function:nnnN { #1 } { #2 } { #3 } \__xfmf_fmfcycle:n
}
\ExplSyntaxOff

\begin{document}

\begin{fmffile}{simple}
\begin{fmfgraph*}(450,320)
\fmftopn{t}{3}    \fmfn{plain}{t}{3}
\fmfbottomn{b}{3} \fmfn{plain}{b}{3}
\pgfmathsetmacro{\temp}{-2}
\begin{fmffor}{n}{1}{1}{3}
\pgfmathsetmacro{\temp}{\temp+1}
\efmf{photon, foreground=blue, tension=0.1, right=\temp/5}{t[n],b[n]}
\end{fmffor}
\end{fmfgraph*}
\end{fmffile}

\end{document}

• If \temp is -0.5, then \temp+1 is 0.5. What's the problem? You're simply doing the vconnect operation twice with the same data. – egreg Apr 14 at 22:39
• Note that fmffor doesn't execute the body multiple times, but only writes code for a Metapost loop. – egreg Apr 14 at 22:45
• I edited with your suggested modification, but I still have the same problem with the updated code. Note that the temp variable should be -0.5 and +0.5. There should be two photon loops, not one, because the \temp variable should be updated. – user1247 Apr 14 at 23:03

The fmffor environment doesn't execute the body multiple times.

Here I propose a different loop macro. The first three arguments are the initial point, the step and the final point. You refer to the current value during the loop as #1 in the fourth argument.

\documentclass[border=2cm]{standalone}
\usepackage{feynmp}
\usepackage{graphicx}
\usepackage{xparse}

\DeclareGraphicsRule{*}{mps}{*}{}

\ExplSyntaxOn
\NewDocumentCommand{\efmf}{mm}
{
\xfmf_fmf:xn { #1 } { #2 }
}
\cs_new_protected:Nn \xfmf_fmf:nn { \fmf{#1}{#2} }
\cs_generate_variant:Nn \xfmf_fmf:nn { x }
\NewDocumentCommand{\fmfcycle}{mmmm}
{
\cs_set:Nn \__xfmf_fmfcycle:n { #4 }
\int_step_function:nnnN { #1 } { #2 } { #3 } \__xfmf_fmfcycle:n
}
\ExplSyntaxOff

\begin{document}

\begin{fmffile}{simple}
\begin{fmfgraph*}(450,320)
\fmftopn{t}{3}    \fmfn{plain}{t}{3}
\fmfbottomn{b}{3} \fmfn{plain}{b}{3}
\fmfcycle{0}{1}{1}{
\efmf{photon, foreground=blue, tension=0.1, right=-0.5+#1}{t2,b2}
}
\end{fmfgraph*}
\end{fmffile}

\end{document}


Like in my previous code I removed the pdftex option for graphicx and the useless \begin{fmffile}{fgraphs} and the matching \end{fmffile} lines.

• OK, this looks like you and I are now on the same page. Thanks a bunch. I will test it and get back to you. – user1247 Apr 14 at 23:07
• @user1247 I fixed the code to have -0.5 and 0.5. – egreg Apr 14 at 23:08
• How would this work if it was already inside a macro that was using the "#1"? – user1247 Apr 14 at 23:32
• Looking more deeply at this -- I don't think this works as a general solution, because it does not reproduce the functionality of "fmffor". I can provide a more detailed example if necessary. For fmffor, the loop variable is used to index vertices, as in "v[n]", which is necessary functionality for it to be useful. Or maybe I don't understand your solution. – user1247 Apr 14 at 23:40
• I've added another example that I don't think your solution works with – user1247 Apr 14 at 23:54