How to typeset properly products of glyphs with diacritics in mathmode?

For the d/v hat perhaps

         \hat{d}\hat{v\vphantom{d}\,}\!_{ap}

does what you want:

enter image description here

It could perhaps be incorporated with a general macro that makes each accent macro look-ahead and insert horizontal kerns or vertical struts on a case-by-case basis.


Here's a solution to the d/v-problem that automatically inserts the correct kerns for the accent. The idea is taken from another answer of mine, which in turn is based on what I learned from TH. The command \ra ("raise accent") takes two arguments, the accent and the letter. The accent is then put at the same height as it would be over the letter d. This works equally well for normal text and for sub-/superscripts:

example with accents

The solution also works if you replace pxfonts with mathpazo – I strongly support this suggestion of egreg (see comment to the question). However, let me point out that this only works for latin letters, so \ra \hat \gamma won't work. Note that \ra \hat d has the same effect as \hat d since the accent doesn't have to be raised.

Personally, I wouldn't raise the accents; I think it's substituting one eyesore for another.

\documentclass{article}

\usepackage{pxfonts}

\makeatletter
\newcommand*\ra[2]{%
        \begingroup
        \mathchoice{\ra@helper{#1}{#2}{\displaystyle}{\textfont}}
                   {\ra@helper{#1}{#2}{\textstyle}{\textfont}}
                   {\ra@helper{#1}{#2}{\scriptstyle}{\scriptfont}}
                   {\ra@helper{#1}{#2}{\scriptscriptstyle}{\scriptscriptfont}}%
        \endgroup
        #2%
}
\newcommand*\ra@helper[4]{%
        \def\currentfont{\the#41}%
        \def\currentskewchar{\char\the\skewchar\currentfont}%
        \setbox\tw@\hbox{\currentfont#2\currentskewchar}%
        \dimen@ii\wd\tw@
        \setbox\tw@\hbox{\currentfont#2{}\currentskewchar}%
        \advance\dimen@ii-\wd\tw@
        \rlap{$\m@th#3\kern\dimen@ii#1{\vphantom{d}\phantom{#2}}$}%
}
\makeatother

\begin{document}
\Huge
$\ra\hat d\ra\hat v_{ap}$
$A_{\ra\hat d\ra\hat v_{ap}}$
\end{document}