# Slanted letter in mathmode keeping current style

I'm trying to make a slanted roman d to use in math mode. Using How can I use the slanted variant of the libertine font? I've managed to get it to mostly work, but I don't know how to account for changes in math styles (normal/subscript etc.).

You can see the issue in the extra space after the \dd in the subscript

I've tried using \count0=\fam then \sbox{\diffdbox}{$\fam=\count0 \mathrm{d}$} however that didn't change anytning. Would anybody have any ideas?

\documentclass{article}

\usepackage{mathtools}

\newsavebox\diffdbox
\newcommand{\dd}{%
\sbox{\diffdbox}{$\mathrm{d}$}%
\hskip\wd\diffdbox
\pdfsave%
\pdfsetmatrix{1 0 0.2 1}%
\mathllap{\mathrm{d}}%
\pdfrestore}

\begin{document}

$d \mathrm{d} d$
$d \dd d$
$x_{d \mathrm{d} d}$
$x_{d \dd d}$

\end{document}


## 1 Answer

I'm afraid to ask what would be the usage of the symbol.

There's a much easier way: define a slanted math alphabet.

\documentclass{article}

\usepackage{mathtools}

\DeclareMathAlphabet{\mathsl}{\encodingdefault}{\familydefault}{m}{sl}
\SetMathAlphabet{\mathsl}{bold}{\encodingdefault}{\familydefault}{bx}{sl}

\newcommand{\dd}{\mathsl{d}}

\begin{document}

\begin{gather*}
d \mathrm{d} d
\\
d \dd d
\\
x_{d \mathrm{d} d}
\\
x_{d \dd d}
\end{gather*}
\boldmath
\begin{gather*}
d \mathrm{d} d
\\
d \dd d
\\
x_{d \mathrm{d} d}
\\
x_{d \dd d}
\end{gather*}

\end{document}


If you really want to manually slant the symbol (I can't understand why not using italics), use \mathpalette:

\documentclass{article}

\usepackage{mathtools}

\newsavebox\diffdbox
\newcommand{\dd}{{\mathpalette\makesl{d}}}
\newcommand{\makesl}[2]{%
\begingroup
\sbox{\diffdbox}{$\mathsurround=0pt#1\mathrm{#2}$}%
\pdfsave
\pdfsetmatrix{1 0 0.2 1}%
\rlap{\usebox{\diffdbox}}%
\pdfrestore
\hskip\wd\diffdbox
\endgroup
}

\begin{document}

$d \mathrm{d} d$
$d \dd d$
$x_{d \mathrm{d} d}$
$x_{d \dd d}$

\end{document}


• This is for a differential d. Yes it's unusual but I like it. The reason why I'm doing it this way is the the font doesn't actually have a slanted variant. Hence the bad hacky approach. – tecosaur Jul 25 '19 at 12:15
• @tecosaur I added the proper hack. Don't use it. – egreg Jul 25 '19 at 12:54
• Thanks. I'm just having trouble finding a font with the full feature set I want, so I'm trying to make do. – tecosaur Jul 25 '19 at 16:48
• @tecosaur I realized that \mathllap is not needed. One can just use the box built for measuring. – egreg Jul 25 '19 at 16:58