How to create a for loop in math mode

enter image description here

\documentclass{article}

\usepackage{tikz}


\begin{document}

\[
F_x = q\left[\foreach \i/\p in {x/, y/+, z/+} {\p\frac{\partial E_\i}{\partial \i}\mathrm{d}\i}  
\right]
\]
\end{document}

Note: loading tikz is overkill.


\documentclass{article}

\usepackage{amsmath}
\newcommand\z[2]{\frac{\partial E_x}{\partial #1}\operatorname{d}#1
             \ifx\z#2+\fi#2}
\begin{document}

\[
  F_x = q\left[\z x\z y\z z\right]
\]
\end{document}

Not sure it’s worth the trouble:

\documentclass{article}
\usepackage{xparse}

\newcommand\diff{\mathop{}\!\mathrm{d}}

\ExplSyntaxOn
\NewDocumentCommand{\totaldiff}{mm}
 {
  \seq_set_split:Nnn \l_opisthofulax_variables_seq { , } { #2 }
  \seq_pop_left:NN \l_opisthofulax_variables_seq \l_opisthofulax_firstvar_tl
  \frac{\partial #1}{\partial\l_opisthofulax_firstvar_tl}\diff\l_opisthofulax_firstvar_tl
  \seq_map_inline:Nn \l_opisthofulax_variables_seq
   {
    +\frac{\partial #1}{\partial##1}\diff ##1
   }
 }
\seq_new:N \l_opisthofulax_variables_seq
\tl_new:N \l_opisthofulax_firstvar_tl
\ExplSyntaxOff

\begin{document}

\[
F_x=q\biggl[\totaldiff{E}{x,y,z}\biggr]
\]

\[
\totaldiff{f}{x}\qquad \totaldiff{g}{u,v}
\]

\end{document}

enter image description here

Note that \operatorname{d}x gives wrong spacing.

A more general version, where a \doloop macro is defined, taking as arguments a comma separated list of items and a template for doing the job. There's also an optional trailing argument for setting the separator between successive applications of the cycle.

Some examples of usage are given.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\newcommand\diff{\mathop{}\!\mathrm{d}}

\ExplSyntaxOn
\NewDocumentCommand{\doloop}{mmO{+}}
 {% #1 = variables for the loop, #2 = template, #3 = separator, default +
  \giutex_doloop:nnn { #1 } { #2 } { #3 }
 }

\clist_new:N \l__giutex_doloop_clist
\tl_new:N \l__giutex_doloop_tl

% initialize
\cs_new:Nn \__giutex_doloop_first:n {}
\cs_generate_variant:Nn \__giutex_doloop_first:n { V }
\cs_new:Nn \__giutex_doloop:n {}

\cs_new_protected:Nn \giutex_doloop:nnn
 {
  \cs_set:Nn \__giutex_doloop_first:n { #2 }
  \cs_set:Nn \__giutex_doloop:n { #3 #2 }
  \clist_set:Nn \l__giutex_doloop_clist { #1 }
  \clist_pop:NN \l__giutex_doloop_clist \l__giutex_doloop_tl
  \__giutex_doloop_first:V \l__giutex_doloop_tl
  \clist_map_function:NN \l__giutex_doloop_clist \__giutex_doloop:n
 }

\ExplSyntaxOff

% if you define a command in terms of \doloop, the variable item is denoted by ##1
\newcommand{\totaldiff}[2]{%
  \doloop{#2}{\frac{\partial #1}{\partial##1}\diff##1}%
}

\begin{document}

\[
F_x=q\Bigl[\doloop{x,y,z}{\frac{\partial E}{\partial#1}\diff#1}\Bigr]
\]

\[
F_x=q\Bigl[\totaldiff{E}{x,y,z}\Bigr]
\]

\[
D(f+g+h)=\doloop{f,g,h}{\frac{\diff #1}{\diff x}}
\]

\[
n!=\doloop{n,(n-1),\dotsb,3,2,1}{#1}[\cdot]
\]

\end{document}

Note that the current item in the loop is denoted by #1; this has to be ##1 if \doloop is used to define another macro such as \totaldiff.

enter image description here