7

In a formula, the space between variables often corresponds implicitly to a multiplication operator. I find LaTeX's default spacing not so readable: symbols are too close to each other, and I would like to see a bit more space between them, automatically (i.e. without manually inserting it). Admittedly, it is a matter of taste, but also imho of legibility. As MWE:

$a x + b$ vs $a \hspace{0.07em} x + b$ and $y dx$ vs $y \hspace{0.07em} dx$   

ax+b and y dx with standard and extra spacing

Globally changing the math spacing might (i) be a bit complicated and (ii) mess up the fine tuned LaTeX compilation. But TeXmacs offers to do it by deciding that the symbol * should be typeset as a (thin) space. That way, one preserves the meaning in the source (a multiplication), which is still readable ( x * y vs x \, y), while obtaining a visually pleasing output.
I am not familiar with catcode, but could it be used to replace all * with a predetermined space? (I can do without the * symbol anyway.) I am aware of the previous post on this topic, but the answers have failed to convince me, and I would like to go ahead.

  • 1
    There is no need to space ax+b; actually I consider such spacing wrong. – egreg Jun 23 '15 at 20:34
  • @egreg I have seen your point, and I respect it. There are strong arguments in its favor. Nevertheless, I beg to differ, and wish to experiment something else. – Pascal Romon Jun 24 '15 at 11:27

2 Answers2

9
\begingroup\lccode`\~=`\*\lowercase{\endgroup\def~}{\,}
\AtBeginDocument{\mathcode`\*="8000 }

But, regarding the differential, I would use a macro

\newcommand*\dif{\mathop{}\!d}

So you would write

$a x + b$ vs $a * x + b$ and $y dx$ vs $y \dif x$
Manuel
  • 27,118
  • Beat me here by a hair. +1 :) – Sean Allred Jun 23 '15 at 20:14
  • @SeanAllred and me by half that:-) – David Carlisle Jun 23 '15 at 20:14
  • Regarding your macro, when possible, I like to use \newcommand\d[1]{\,d#1}, myself. Keeps the code very readable (\int f(x) \d x). My macro is very naive, though :) – Sean Allred Jun 23 '15 at 20:16
  • 3
    @SeanAllred I don't really mind renewing commands, and I use myself \d too (although with a different definition \renewcommand*\d{\mathop{}\!d}). If you really want it to have an argument \renewcommand*\d[1]{\mathop{}\!d#1} but you should take care of ^{<superscripts>}. – Manuel Jun 23 '15 at 20:18
  • 1
    @Manuel Superscripts... good point :) – Sean Allred Jun 23 '15 at 20:19
  • +1 for the solution... although it would be nice a bit of comments for the non-magic crowd out there :-)... ¿\lowercase? – Rmano Jun 23 '15 at 20:20
  • @Rmano Just searching for the \lowercase trick gives you this :) – Manuel Jun 23 '15 at 20:22
  • @SeanAllred http://tex.stackexchange.com/questions/250674/how-to-get-rid-of-c-cedilla-in-math-mode/250676#comment597358_250676 I would personally use the version without arguments, and, if you prefer, use \d{x} or \d^{2}{x}, which would still give the correct spacing. – Manuel Jun 23 '15 at 20:26
  • @Manuel thanks --- it seems it's almost time, (censored) years after, to reread the TeXbook... – Rmano Jun 23 '15 at 20:27
  • I use this, borrowed from an answer on this site: \renewcommand*{\d}{\mathop{\kern0pt\mathrm{d}}\!{}} – Bernard Jun 23 '15 at 20:30
  • @Bernard This is equivalent to, but more complicated than, \newcommand{\diff}{\mathop{}\!\mathrm{d}}; don't do \renewcommand{\d}, you'll regret it, once or later. – egreg Jun 23 '15 at 20:33
  • @Bernard It's exactly the same. But that solution is over difficult: (1) \kern0pt is there just so \mathop{..} doesn't vcenter its argument; (2) {} is there just to add an ordinary atom after the \mathop. With the other definition (1) you don't have to worry of the vcentering, because the argument is empty; and (2) you don't need to add an atom because the d is itself an ord atom (or the \mathrm{d}, which is the same). – Manuel Jun 23 '15 at 20:33
  • @egreg: I know it's dangerous, but I also use \diffp from the esdiff package (from time to time only, as I'm an algebraist…), which is very close. I know I should think of a solution :o) – Bernard Jun 23 '15 at 21:03
  • @Manuel: Doesn't \mathop add some space between what's before and the d? This a code I do not fully understand, I must say. Your explanations shed some light on it. – Bernard Jun 23 '15 at 21:07
  • @Bernard Which is removed by \! exactly the same as your solution. – Manuel Jun 23 '15 at 21:10
  • @Manuel:??? I thought \! was there to remove the extra spacing (due to \mathop) between the d and what follows. – Bernard Jun 23 '15 at 21:15
  • @Bernard Well not exactly (but yeah, I did not explain correctly) the space is between \mathop and the next ord atom. In your definition the d is inside \mathop so, as you say, \! is there to remove the space between the d and the following {} (ord atom). (I will remove all this comments of the discussion) – Manuel Jun 23 '15 at 21:18
  • Thanks for the answer and all the comments. Though the issue of the differential command is a bit off-topic (I meant it only as an example), it is interesting per se. To keep it connected to the original, I noticed that defining d as a \mathop seems to insert an extra space in front which defeats the original purpose. Try y \diff x and y*\diff x; the former is good enough, while the latter is now too "spaced". By the way, I would change in @Manuel's definition \, into \hspace{0.07em} (or whatever looks best), since the classical thin space is a bit too large for my taste. – Pascal Romon Jun 24 '15 at 11:42
  • @PascalRomon Well, the main idea of using a \diff command is that you don't have to worry about spacing, it's taken care by itself. So * there wouldn't be necessary. I mean \int f(x) \dif x is readable enough. – Manuel Jun 24 '15 at 12:24
6

You might be interested in my answer to Automatic spacing of symbols . Here is it again:

\mathcode`\*="8000
{\catcode`\*=\active
\gdef*{\mathclose{}\,\mathopen{}}}

Compare

$a*b$ 
$a b$ 
$a\,b$
$a*\cos b$
$a \cos b$
$a\,\cos b$

The \mathopen{} avoids inserting two thin spaces if * is in front of a \mathop.

enter image description here

Michel Fioc
  • 1,257
  • 9
  • 14
  • All right I'm convinced. I had not thought that a \mathop would tamper with the above solution, but it does, and your solution smartly prevents this. I'll stick with my preferred length though: \hspace{0.07em}. – Pascal Romon Jun 24 '15 at 19:00