15
$\begingroup$

A minor thing, perhaps, but it has bothered me for a long time:

Mathematica, by default, inserts a full space after \partial and \dd, perhaps so that things like \dd x do not end up being typeset as a two-character symbol (with both rendered as standard characters, rather than italics). That's well-intentioned, I suppose, but the typographical appearance is just wrong: The space between \partial and \dd and the following symbol should be small, just like for a two-character symbol. Thus, if I am anal about typography, I'll end up nudging my variables to the left (three times Alt-Left will do it) every time I use these constructs.

So my question is, is there a way to automate this, and teach Mathematica to construct the right spacing every time? Here's hoping against all hope...

This one still bothers the heck out of me, so I'll ask more succinctly, and offer a bounty, too:

Is it possible to define an input alias (that I can later invoke with Esc-somecode-Esc) such that I can construct a combination of a DifferentialD or PartialD character with another symbol (single or multi-letter, ideally, or a subscripted variable after I have declared such variables as symbols via Symbolize) such that the symbol follows the differential symbols with no extra space, and properly italicized as prescribed by the TraditionalForm settings if those are used. So, if I type, say, Escdddescx, I see this in TraditionalForm:

enter image description here

rather than crap like this:

enter image description here

$\endgroup$

2 Answers 2

6
+50
$\begingroup$

If you want to alter the typography AND create functionality than I think you'll need the Notation Package. However, if you just want to alter the typography, you can do that using Boxes. I like to use the InputAutoReplacements option in the Inspector to create my shortcuts. It's less keystrokes to use them... so I'm more productive.

You can create whatever construct you want including font types, placeholders, nudging, using boxes. Below is an example of some code you can try. Open a new notebook and execute the code below.

SetOptions[$FrontEndSession,
 InputAutoReplacements -> {
   "dx" -> 
    Cell[BoxData[
      FormBox[StyleBox["\[DifferentialD]\[NegativeMediumSpace]x"], 
       TraditionalForm]], FormatType -> "TraditionalForm"]}] 

This has created a temporary shortcut which will continue to work until you close your current session in Mathematica. Create a text cell and then type dx and then press the space bar. You will get the following typeset notation with the correct spacing you are looking for.

dx

You can alter the spacing between the d and x using something other than \[NegativeMediumSpace]. You can find other spacings here. You can also change the font if you like something a little different or make it italic or bold or change other characteristics of the font. There's many ways to make it look different ... but your question was specifically about spacing so that's what I addressed.

You can add inputautoreplacements using the option inspector but it's a real pain to do. Executing the code is far more efficient. Also if you change $FrontEndSession to $FrontEnd in the code above, the shortcut created will become permanent so that when you close and reopen Mathematica it still works.

You can list your current inputautoreplacements by executing the following code in a notebook.

Options[EvaluationNotebook[], InputAutoReplacements]

Copy and paste this code into a separate notebook and save it so you have a backup.

$\endgroup$
5
  • $\begingroup$ I changed code to address typing "dx". You can add a larger or smaller nudge between the d and x by using the shortcut for the nudge or copy and pasting inside the quotes between the "[DoubleStruckD]" and the "x" $\endgroup$
    – B flat
    Commented Sep 27, 2016 at 23:48
  • $\begingroup$ Hmm, this kind of gets closer, but it doesn't work quite right yet: The issue is that \[DoubleStruckD] is a different character. What I need is \[DifferentialD]. Trouble is, if I replace the \[DoubleStruckD] in your input replacement with the \[DifferentialD] I need, then I get that silly space between the \[DifferentialD] and the x back. You mentioned I can put nudges in there, too. How do I do that? Simply doing StyleBox["\[DoubleStruckD]x" inserts that spurious half space I don't want. $\endgroup$
    – Pirx
    Commented Sep 28, 2016 at 0:49
  • $\begingroup$ Easy fix! Change as you said to "[DifferentialD]" and then also add "[NegativeMediumSpace]" between it and the "x". Updated. You can vary the spacing by using "[NegativeThinSpace]" or "[NegativeVeryThinSpace]" or "[NegativeThickSpace]". $\endgroup$
    – B flat
    Commented Sep 28, 2016 at 0:53
  • 2
    $\begingroup$ There is a great Calculus book coded in Mathematica by Eric Schulz. here is a link to a talk he did about the making coding the book: youtu.be/iUobpMR04Kw. Start the video at 8:30 to see some of his autoinputreplacements. It addresses typography issues as well as speeding up the writing process. I used his code to base mine on. You can download his presentation notebook on the mathematica website. Saved me a lot of time learning how its done. $\endgroup$
    – B flat
    Commented Sep 28, 2016 at 1:09
  • $\begingroup$ Excellent, that does it, thanks! I have seen Schulz' book a while ago, but had forgotten about it. I'll be sure to watch that video. $\endgroup$
    – Pirx
    Commented Sep 28, 2016 at 11:12
6
$\begingroup$

If I type a space between "d" and "u" and compare it to Esc dd Esc u and "du" with no space it appears to my eye that Mathematica adds about a half a space.

Mathematica graphics

If you are willing to use strings (only for printing) you can group the Esc dd Esc and "y" together using StringExpression and they will display closer.

Below are the results of these expressions:

Style["\[DifferentialD]" ~~ "u", 16]/Style["\[DifferentialD]" ~~ "t", 16];
Style[\[DifferentialD]u, 16]/Style[\[DifferentialD]t, 16];

Mathematica graphics

If you look carefully you will see that using StringExpression (i.e., ~~) makes it appear slightly closer.

You can define functions to make your life easier

ddC[x_, size_: 14] := Style["\[DifferentialD]" ~~ ToString[x], size]
pdC[x_, size_: 14] := Style["\[PartialD]" ~~ ToString[x], size]

and then

ddC[u, 16]/ddC[t, 16]
pdC[u],pdC[t]

gives you image with

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ To fine-tune further, one could use in the StringExpression of ddC and pdC the named characters \[VeryThinSpace], \[ThinSpace], \[MediumSpace], \[ThickSpace], and their negative counterparts \[NegativeVeryThinSpace], \[NegativeThinSpace], \[NegativeMediumSpace], \[NegativeThickSpace]. $\endgroup$
    – user31159
    Commented Sep 9, 2016 at 21:39
  • $\begingroup$ The problem with the above is that this interferes with the processing as TraditionalForm which is my default output style and the style I use for inline and displayed equations. Thus, the "u", and "t" in your examples appear upright when they should be in italics. Now, I know this can be fixed also by defining ddC and pdC accordingly, but what I was really looking for was a global way (via Options Inspector?) to tell Mathematica how to properly typeset these constructs (namely, with no extra space between the differential characters and the rest. $\endgroup$
    – Pirx
    Commented Sep 10, 2016 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.