# How to create a for loop in math mode

I'm looking for a way to iterate some process in order to write less in math mode. Let's take an example.

$F_x = q\left[\frac{\partial E_x}{\partial x}\operatorname{d}x + \frac{\partial E_x}{\partial y}\operatorname{d}y + \frac{\partial E_z}{\partial x}\operatorname{d}z\right]$


produces the output Now what I would like were to automatise the iteration of writing with something loop-like, as below:

$F_x = q\left[for \i in {x, y, z} +\frac{\partial E_x}{\partial \i}\operatorname{d}\i \right]$


something similar to the for loop in TikZ to avoid repetition in writing. is it someway possible?

• Do you think your code would be easy to read, in case of a compilation problem? – Bernard Jul 19 '17 at 15:55
• \usepackage{pgffor} and then \foreach \i in {x,y,z} {+\frac{\partial E_x}{\partial\i} \dd \i (with \newcommand*\dd{\mathop{}\!\mathrm{d}}. But you get an extra +. To take care of the + you could do \if x\i\else+\fi instead of +, but think about code readability. – Manuel Jul 19 '17 at 15:55
• @Bernard my code is almost 2000 lines, so I prefer to shorten what I can, even giving up some readability, as a so long code it's not readable though. – GiuTeX Jul 19 '17 at 15:58
• @Manuel could you provide a more clear example, I cannot understand how to deal with those pieces of code, thanks! P.S: I don't care about the +, as long as I can compress lines :-) – GiuTeX Jul 19 '17 at 15:58
• You might be able to shorten more by defining macros with repeated expressions. – John Kormylo Jul 19 '17 at 16:04

\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 the last differentiated function is not $E_x$, but $E_z$… – Bernard Jul 19 '17 at 16:36
• I suspect that there is a typo in the original question. But I'll leave it as is, now. Refinements should be clear to the OP. – JPi Jul 19 '17 at 16:42
• You're probably right. – Bernard Jul 19 '17 at 16:43
• @JPi thanks, no typo in the source , is the $x$ component of the electric field everywhere as i'm looking for the $x$ component of the force filed. This solution seems to be the best, as I already load TikZ package for other reasons :-) – GiuTeX Jul 19 '17 at 16:58
• You can just load pgffor to get access to the loop without loading all of TikZ. – David Z Jul 19 '17 at 21:50
\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}


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.

• Thanks, but why that \mathop{}\! before the \mathrm? – GiuTeX Jul 19 '17 at 16:53
• @opisthofulax In order to get the desired thin space in front of the ‘d’. – egreg Jul 19 '17 at 16:57
• Nice! Anyway you're right by saying "Not sure it’s worth the trouble", I was looking for something to shorten my code... :-) – GiuTeX Jul 19 '17 at 17:01
• @opisthofulax The document code is dramatically shorter, actually. – egreg Jul 19 '17 at 17:14
• Yes, of course, but it's very specific to the case I have partial derivatives, while I was looking more for something general (the derivatives one was only an example, but I need this for a bunch of other things), more like the the answer of JPi. – GiuTeX Jul 19 '17 at 17:35

If you use a macro for the differential symbol and the esdiff package, you can have a shorter code and a better spacing. Compare:

\documentclass{article}
\usepackage{mathtools}
\usepackage{esdiff}

\newcommand*{\dd}{\mathop{\kern0pt\mathrm{d}}\mkern-2mu{}}

\begin{document}
$F_x = q\left[\frac{\partial E_x}{\partial x}\operatorname{d}x + \frac{\partial E_x}{\partial y}\operatorname{d}y + \frac{\partial E_z}{\partial x}\operatorname{d}z\right]$

$F_x = q\left[\diffp{E_x}{x}\dd x + \diffp{E_x}{y}\dd y + \diffp{E_z}{x}\dd z \right]$

\end{document}


• Why \mkern-2mu? when \! is precisely the space added by \mathop? (Which is 3mu IIRC) – Manuel Jul 19 '17 at 16:14
• I prefer a very thin distance between the differential operator and the variable, but that's my personal taste. – Bernard Jul 19 '17 at 16:19
• I prefer without space too... as a matter of facts in my code I don't use \operatorname. Thanks anyway. – GiuTeX Jul 19 '17 at 16:55