# Easy command or package for properly formatted integrals?

I like to format my integrals so that there is less space between the integral sign and the integrand, more space between the integrand and differential, and so that the differential is upright. However, it is tedious to type out the formatting repeatedly. I wrote a command,

\newcommand{\intf}[4]{\int_{#1}^{#2} \! #3 \, \mathrm{d}#4}


So that the properly-formatted integral is easier to type, e.g. typing in $\intf{a}{b}{x^2}{x}$ gives

However, this command requires 4 inputs. If I didn't want the upper or lower limits, I could omit their inputs but I would still need to write out the brackets, e.g. \intf{}{}{x^2}{x}. In the long run, however, this gets tedious as well, and looks messy in the code! It would be super if I had some command where I could just write ^ and _ as necessary, e.g. \intf^{b}{x^2}{x}

So, I'm wondering two things: (a) Is there some preexisting package that has a pre-formatted integral command? (b) Is there an easy way to change the above command so that it can take modifications such as ^ and _ as required?

Thanks!

-
Welcome to TeX.SX! –  Heiko Oberdiek Apr 29 '14 at 3:32

LaTeX's kernel macro \@ifnextchar can be used to peek at the next token, syntax:

\@ifnextchar<token>{<yes>}{<no>}


Example for \intf with the following syntax:

\intf _<subscript> ^<superscript> <term> <variable>

• The order of the subscript and superscript does not matter. Curly argument braces are needed for some <...>, if it contains more than one token.

Full example file:

\documentclass{article}

\makeatletter
\newcommand*{\intf}{%
\@ifnextchar_{\intf@sub}{%
\@ifnextchar^{\intf@sup}{%
\intf@{}{}%
}%
}%
}
\def\intf@sub_#1{%
\@ifnextchar^{%
\intf@sub@sup{#1}%
}{%
\intf@{#1}{}%
}%
}
\def\intf@sup^#1{%
\@ifnextchar_{%
\intf@sup@sub{#1}%
}{%
\intf@{}{#1}%
}%
}
\def\intf@sub@sup#1^#2{\intf@{#1}{#2}}
\def\intf@sup@sub#1_#2{\intf@{#2}{#1}}
\newcommand*{\intf@}[4]{%
\int
\ifx\\#1\\\else _{#1}\fi
\ifx\\#2\\\else ^{#2}\fi
\!#3\,\mathrm{d}#4%
}
\makeatother

\begin{document}
$\intf_a^b{x^2}{x}, \intf^b_a{x^2}{x}, \intf_a{x^2}{x}, \intf^b{x^2}{x}, \intf{x^2}{x}$
\end{document}


A little more fun with delimited arguments: The d can be used as delimiter. Since the integration variable is usually one letter, some curly braces can be saved with following syntax:

\intf _<subscript> ^<superscript> <term> d<variable>


Braces are needed, if <superscript>, <subscript>, or <variable> consists of more than one token. And <term> needs curly braces, if it contains d. Example:

\documentclass{article}

\makeatletter
\newcommand*{\intf}{%
\@ifnextchar_{\intf@sub}{%
\@ifnextchar^{\intf@sup}{%
\intf@{}{}%
}%
}%
}
\def\intf@sub_#1{%
\@ifnextchar^{%
\intf@sub@sup{#1}%
}{%
\intf@{#1}{}%
}%
}
\def\intf@sup^#1{%
\@ifnextchar_{%
\intf@sup@sub{#1}%
}{%
\intf@{}{#1}%
}%
}
\def\intf@sub@sup#1^#2{\intf@{#1}{#2}}
\def\intf@sup@sub#1_#2{\intf@{#2}{#1}}
\def\intf@#1#2#3d#4{%
\int
\ifx\\#1\\\else _{#1}\fi
\ifx\\#2\\\else ^{#2}\fi
\!#3\,\mathrm{d}#4%
}
\makeatother

\begin{document}
$\intf_a^b x^2 dx, \intf^b_a x^2 dx, \intf_a x^2 dx, \intf^b x^2 dx, \intf x^2 dx$
$\intf_{xy}^z {d^2-3d} dd$

\end{document}


-
This is a fantastic implementation, and I've already added it to my arsenal! As you're likely aware, it can be easily used for double integrals with multiple differentials, e.g. \intf_0^{\infty}{\!\intf_0^{\infty} e^{-x-y} dx} dy. Brilliant! –  user31360 Apr 29 '14 at 16:10

I would avoid having the differential symbol included in the workings, because you'll have problems as soon as double or line integrals are concerned.

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

\NewDocumentCommand{\Int}{s >{\SplitArgument{1}{;}}o}{%
\int
\IfBooleanT{#1}{\limits}%
\IfValueT{#2}{\IntAux#2}%
}

\NewDocumentCommand{\IntAux}{mm}{%
_{#1}\IfNoValueTF{#2}{\,}{^{#2}}\!
}

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

\begin{document}
\begin{gather*}
\Int[a;b] x^2 \diff x \\
f(t)=\Int[t] x^2 \diff x \\
g(t)=\Int[;t] x^2 \diff x \\
\Int x^2 \diff x \\
\Int*[\Omega]f(x,y)\diff x\diff y
\end{gather*}
\end{document}


If only one component in the optional argument is given, it will be typeset as lower limit. The *-variant uses \limits, as seen in the last example.

-
I like this implementation, especially since it can handle \limits. However, using the differential in the workings (as in Heiko's implementation of \intf) doesn't preclude using a double differentiation, as you may nest \intf (\intf{\intf f(x) dx}dy) or apply \diff as you have it defined (\intf_A} f dx \,\diff y) –  user31360 Apr 29 '14 at 16:24
@user31360 Do you think to having a good interface that way? What about $\Int[\gamma]f(z)\lvert\diff z\rvert$? –  egreg Apr 29 '14 at 16:27
that's a good point, though you could work around it in @heiko-oberdiek 's implementation as \intf_{\gamma} f(z) \,\lvert\!dz\rvert (which, admittedly, is only a little better than using \int and formatting everything). To me, it seems that \Int is quite versatile while I find \intf easier to type in most cases. Both are useful in their own way, which is why both are now in my toolbox! –  user31360 Apr 29 '14 at 19:53

There is the cool package that allows to use \Int{f}{x} for the case where there are no limits, or \Int{f}{x,a,b} when there are limits:

## Code:

\documentclass{article}
\usepackage{cool}

\Style{IntegrateDifferentialDSymb=\mathrm{d}}% Use an upright d

\begin{document}
$\Int{x^2}{x} \quad \Int{x^2}{x,a,b}$
\end{document}

-