Math & ScienceQuestion regarding integrals and differential d's

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
Sleort
Posts: 5
Joined: Thu Sep 02, 2010 9:34 am

Question regarding integrals and differential d's

Post by Sleort »

As a physicist I often use both the

Code: Select all

$\int f(x) \mathrm{d}x$
and the

Code: Select all

$\int \mathrm{d}x f(x)$
convention when writing integrals, i.e. I sometimes write the differential after the function I want to integrate, and sometimes in front of it (right after the integral symbol).

The code written above is of course not very pretty; I want some extra space between the function and the differential in order to separate what's what. In addition I would like to adjust the whitespace between the integral sign and what follows (the space is often too big in my opinion). In code I want something like

Code: Select all

$\int \! f(x) \, \mathrm{d}x \,$
and

Code: Select all

$\int \!\mathrm{d}x \, f(x)$
And, by the way, when there are more differentials I want there to be less space between them than if one of the differentials had been a function. I want something like

Code: Select all

\mathrm{d}x \mathrm{d}y
not

Code: Select all

\mathrm{d}x \,\mathrm{d}y
So my question boils down to: Is there some way to make a macro/new symbol that follow these rules automatically, i.e. it is possible to make the differential's behavior neighbor symbol dependent? (I guess there is if you are into low level Tex programming, but unfortunately I'm not :( ) Alternatively: Do any of you know if there exists a package where this is implemented? (I haven't found any despite extensive searches)

Thanks!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

php1ic
Posts: 192
Joined: Wed Jan 28, 2009 8:17 pm

Question regarding integrals and differential d's

Post by php1ic »

Hi Sleort,

This may not be exactly what you want, but can edit it.

Code: Select all

\documentclass[a4paper,11pt]{article}

\newcommand{\myint}[2]{$\int \; #1 \, \mathrm{d}#2\,$}

\begin{document}

\myint{f(x)}{x}

\end{document}
There is a good explanation here
http://en.wikibooks.org/wiki/LaTeX/Customizing_LaTeX
Sleort
Posts: 5
Joined: Thu Sep 02, 2010 9:34 am

Question regarding integrals and differential d's

Post by Sleort »

php1ic wrote:Hi Sleort,

This may not be exactly what you want, but can edit it.

Code: Select all

\documentclass[a4paper,11pt]{article}

\newcommand{\myint}[2]{$\int \; #1 \, \mathrm{d}#2\,$}

\begin{document}

\myint{f(x)}{x}

\end{document}
There is a good explanation here
http://en.wikibooks.org/wiki/LaTeX/Customizing_LaTeX
Thanks for the reply! I am aware of that possibility, but no, that wasn't what I wanted. First of all this macro/newcommand does not open up for the other way (the "\int dx f(x)" way --where the differential is before the integrand) of writing the integrals. Then I could of course define another macro (e.g. "\myint2") where this is the case, like

Code: Select all

\newcommand{\myint2}[2]{\int \mathrm{d}#2 \, #1 \,}
, but I don't think that is a very elegant (or smart) solution. And what if I would like to write a multidimensional integral? Or include explicit limits of integration? No, this simple \newcommand way of doing it isn't, unfortunately, good enough.

As I said: I think one has to focus on the differential d (the differential operator) and somehow make it dependent on the neighbor character. I don't know if it is possible, but the (pseudo)code should be something like:

if(character (excluding _{} and ^{}) to the right = integral) then (\! \mathrm{d}{#1} \,)
else if(character to the right = differential) then (\mathrm{d}{#1} \,)
else (\, \mathrm{d}{#1} \,)

Is this possible in Latex? (Or Tex...) If yes: how do I do it?
Montag
Posts: 340
Joined: Wed Jul 15, 2009 9:25 am

Question regarding integrals and differential d's

Post by Montag »

php1ic wrote:Hi Sleort,

This may not be exactly what you want, but can edit it.

Code: Select all

\documentclass[a4paper,11pt]{article}

\newcommand{\myint}[2]{$\int \; #1 \, \mathrm{d}#2\,$}

\begin{document}

\myint{f(x)}{x}

\end{document}
There is a good explanation here
http://en.wikibooks.org/wiki/LaTeX/Customizing_LaTeX
Sorry for hijacking, but what exactly is the

Code: Select all

\,
good for in

Code: Select all

\mathrm{d}#2\,$}
of your new command definition? :) AFAIK, there is always a normal space after math mode ends, isn't there? :?:
OS: Win 7 64-bit LaTeX: MikTeX 2.9 64-bit Editor: TXC 1 RC1
pizzocaro
Posts: 13
Joined: Mon Jun 14, 2010 2:26 pm

Question regarding integrals and differential d's

Post by pizzocaro »

Hi,
try to have a look at C. Beccari. TUGBoat 18 (1997) No. 1.

In this article, the author also addresses how to typeset differential d's. He came up with a quite complicated, definition (that, btw, I do not understand).
I report it here for convenience (hoping there will be no typos)

Code: Select all

\makeatletter
\providecommand*{\diff}%
	{\@ifnextchar^{\DIfF}{\DIfF^{}}}
\def\DIfF^#1{%
	\mathop{\mathrm{\mathstrut d}}%
		\nolimits^{#1}\gobblespace
}
\def\gobblespace{%
	\futurelet\diffarg\opspace}
\def\opspace{%
	\let\DiffSpace\!%
	\ifx\diffarg(%
		\let\DiffSpace\relax
	\else
		\ifx\diffarg\[%
			\let\DiffSpace\relax
		\else
			\ifx\diffarg\{%
				\let\DiffSpace\relax
			\fi\fi\fi\DiffSpace}	
It defines the d as an operator (I believe it require the amsmath package) so that spaces are handled correctly. Moreover it handles the right space with special care.

Use it as in

Code: Select all

\int f(x)\diff x
Can this help?
Regards,
Marco
Sleort
Posts: 5
Joined: Thu Sep 02, 2010 9:34 am

Question regarding integrals and differential d's

Post by Sleort »

Now we're talking! :D Thanks!

BUT: Although

Code: Select all

\iint f(x,y) \diff x \diff y
looks (very) nice,

Code: Select all

\int \diff x \int \diff y f(x,y)
doesn't. There is too much space between the integral signs and the differentials and there should be some space between the differential and the function,

Code: Select all

\diff y f(x,y)
Maybe it is possible to extend Beccari's code with an extra if-then statement? Unfortunately I do not know how to do this, but maybe there are someone out there who does (and would like to help ;) )?
pizzocaro
Posts: 13
Joined: Mon Jun 14, 2010 2:26 pm

Question regarding integrals and differential d's

Post by pizzocaro »

Regarding the space between the differentials and the function, it needs probably to be put at hand: how does LaTeX knows that the function is indeed the integral argument?

Regarding the space between the integral sign and the differential... I did not find any difference between \diff and \mathrm{d}...

Code: Select all

\[\int \diff x \diff y \, f(x,y) = \int \mathrm{d} x \mathrm{d} y f(x,y)\]
and I do not think it looks bad.

Regards,
Marco
Sleort
Posts: 5
Joined: Thu Sep 02, 2010 9:34 am

Question regarding integrals and differential d's

Post by Sleort »

pizzocaro wrote:Regarding the space between the differentials and the function, it needs probably to be put at hand: how does LaTeX knows that the function is indeed the integral argument?
You mean the space between the differentials and the function like in this case:

Code: Select all

\diff x f(x)
? Well, that's easily solved. The "\diff" operator should just pick the first character following it as the integral variable (the "x" in this case), and then consider what is following (here "f(x)") as the integrand. If the variable should contain more characters one could just use some brackets, like

Code: Select all

\diff{cos(x)}f(x)
... Or wasn't that what you meant?
pizzocaro wrote:Regarding the space between the integral sign and the differential... I did not find any difference between \diff and \mathrm{d}...

Code: Select all

\[\int \diff x \diff y \, f(x,y) = \int \mathrm{d} x \mathrm{d} y f(x,y)\]
and I do not think it looks bad.
I don't see any difference either (strange) but I do think it looks bad ;) ... The space is too wide. I'd like to think of

Code: Select all

\int \diff x
(as a whole) as an operator, and then there is too much space... It should rather be something like

Code: Select all

\int \! \diff x
...and this is what I would like to see be done automatically - but (of course) only when there is an integral sign in front of the differential.

Summarized:
  • Need more space between "\diff x" and "f(x)"
    Need less space between "\int" and "\diff x"
    ... All this while the old/alternative "\int f(x) \diff x" functionality is preserved/unchanged.
It shouldn't be impossible...
pizzocaro
Posts: 13
Joined: Mon Jun 14, 2010 2:26 pm

Question regarding integrals and differential d's

Post by pizzocaro »

In my humble opinion, it is worth to define something like

Code: Select all

\newcommand{\idiff}{\int \! \diff}
You have already argued that in this way you can not put limits to the integral. However, where there are limits you do not want the space changed, as in

Code: Select all

\[ \int_V \diff^3 r f(r) \]
Sleort
Posts: 5
Joined: Thu Sep 02, 2010 9:34 am

Question regarding integrals and differential d's

Post by Sleort »

pizzocaro wrote: You have already argued that in this way you can not put limits to the integral. However, where there are limits you do not want the space changed, as in

Code: Select all

\[ \int_V \diff^3 r f(r) \]
Yes, but in this example there should be some space between \diff^3 r and f(r), like

Code: Select all

\[ \int_V \diff^3 r \, f(r) \]
and then it starts getting complicated... Maybe I'm too demanding, but it would be really cool if everything could be included in just one symbol/command ("\diff") instead several (\idiff, \diff etc.). And I'm pretty shure it is possible if one uses some lover level Tex programming. (My problem is just that I don't know how... I cross my fingers that some of the forum members do! :) )
Post Reply