# Specific alignment of subscripts in math mode joint with overleftarrow. How to avoid phantom space fiddling?

The oveleft/rightarrow-command is not optimal because it shiftes indices to much to the right in formulas like:

$\overleftarrow{D}_v$


Therefore, I use the following macro which I have adapted from some answer on this site a while ago. (Sorry, I cannot give credit to the author of the code because I forgot where I read it.)

\usepackage{stackengine}
\newcommand\constantoverleftarrow[1]{\stackengine{0pt}{$\overleftarrow{\hphantom{D}\vphantom{#1}}$}{$#1$}{O}{l}{F}{F}{L}}


The command

$\constantoverleftarrow{D_v}$


then gives the desired result. So far so good. Now, I want to align the subscripts as in the follwing code:

$\constantoverleftarrow{D_{v'\vphantom{\scalebox{0.73}{$\int$}}}}x_{v,v'}^d$


If you use

$\constantoverleftarrow{D_{v'}}x_{v,v'}^d$


instead, you will see that the v's and the v''s in the subscripts are not properly aligned. With the naked eye, I chose the value 0.73 in the scalebox to more or less achieve what I want.

Can someone please tell me how I do this properly? I hardly want to choose for each formula a value in the scalebox, as you can imagine. Because this takes incredibly much time. Also, I want the alignement to be perfect and not only approximative. How can I achieve this?

I tried several things now and was not able to do it. There are many questions like this on the site. But I was not able to find a solution. Note, I want the arrow over the D not shifted upwards or anything like this.

Related.

MWE.

\documentclass[12pt]{amsart}
\usepackage{stackengine}
\newcommand\constantoverleftarrow[1]{\stackengine{0pt}{$\overleftarrow{\hphantom{D}\vphantom{#1}}$}{$#1$}{O}{l}{F}{F}{L}}
\begin{document}
$\constantoverleftarrow{D_{v'}}x_{v,v'}^d$
\end{document}


This MWE repdocues indeed the error. You see, the indices are not aligned. Only if you add the integral. The problem is caused by the d in the superscript as I noticed now while playing around. I am sorry that I forgot the d in my initial post.

Possible solution for the right result.

\documentclass[12pt]{amsart}
\usepackage{stackengine}
\newcommand\constantoverleftarrow[1]{\stackengine{0pt}{$\overleftarrow{\hphantom{D}\vphantom{D}}$}{$#1$}{O}{l}{F}{F}{L}}
\newcommand{\nhphantom}[1]{\setbox0=\hbox{#1}\hspace{-\the\wd0}}
\begin{document}
%This is not perfect because the space between $D$ and the subscript $v'$ is not exactly like in $D_{v'}$ but almost and slightly smaller. The indices are however aligned. The best I can do.
$\constantoverleftarrow{D\nhphantom{\displaystyle x_{}^{}}\phantom{x}_{\vphantom{,}v'}^{\vphantom{d}}}x_{v,v'}^d$
\end{document}

• It is a little difficult to make out what you're asking. Could you turn your code fragments into a proper MWE? It should start with \documentclass, and end with \end{document} and should load the minimum number of packages that you need to illustrate the problem. (And it should compile.) – Circumscribe Jul 6 '18 at 13:55
• Yes, I should do this later because in the pictures below it looks great but in my file the results are not ok. @Circumscribe – user66288 Jul 6 '18 at 14:11
• Ah, I see. The different depths of the subscript is due to the presence of a superscript (the ^d). I believe the supdepth package "fixes" this. (See this answer.) – Circumscribe Jul 6 '18 at 14:36
• subdepth with option low-sup does the job. Strangely enough, it only work inbetween $...$ but not inbetween $...$. – user66288 Jul 6 '18 at 14:53
• The low-sup option is buggy, I think it doesn't work in inline equations with superscript that stick out below the baseline. The only fix I know involves redefining ^, and I'm not sure I should be recommending that since it is a rather drastic thing to do. I don't really know how the package works though, so there might very well be better ways to fix that problem. – Circumscribe Jul 6 '18 at 16:22

Because an MWE has not been provided, it is not exactly clear what the issue is. But here is a stab...at something.

\documentclass{article}
\usepackage{stackengine,graphicx}
\newcommand\constantoverleftarrow[1]{\cola#1\endcola}
\def\cola#1#2\endcola{\stackengine{0pt}{}{$\overleftarrow{#1}$}{O}{l}{F}{T}{L}%
\phantom{#1}#2}
\begin{document}
$\overleftarrow{D}_v$

$\constantoverleftarrow{D_v}$

$\constantoverleftarrow{D_{v'}}x_{v,v'}$
\end{document}


• @John Kormylo and @Seven B. Segletes Thank you a lot! I appreciate your help! This is really great. I think you understood the issue very well. I can see that is working. But I don't understand why. Where do you set the horizontal length of the arrow to be equal to D? I ask so that I can modify in other formulas in case I need it. – user66288 Jul 6 '18 at 13:58
• @user66288 The $\overleftarrow{#1}$ is what typesets the overarrowed D, since #1 is the first token of your argument. Later in the definition, the \phantom{#1} skips by the width of a D, so that the subscript can be set as if were next to the plain letter. – Steven B. Segletes Jul 6 '18 at 14:16

I suggest using smaller arrows (like in https://tex.stackexchange.com/a/248297/4427)

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\overleftsmallarrow}{\mathpalette{\overarrowsmall@\leftarrowfill@}}
\newcommand{\overrightsmallarrow}{\mathpalette{\overarrowsmall@\rightarrowfill@}}
\newcommand{\overleftrightsmallarrow}{\mathpalette{\overarrowsmall@\leftrightarrowfill@}}
\newcommand{\overarrowsmall@}[3]{%
\vbox{%
\ialign{%
##\crcr
#1{\smaller@style{#2}}\crcr
\noalign{\nointerlineskip\vskip0.4pt}%
$\m@th\hfil#2#3\hfil$\crcr
}%
}%
}
\def\smaller@style#1{%
\ifx#1\displaystyle\scriptstyle\else
\ifx#1\textstyle\scriptstyle\else
\scriptscriptstyle
\fi
\fi
}
\makeatother

\begin{document}

$\overrightsmallarrow{D}_v$ $\overleftsmallarrow{D}_v$ $\overleftrightsmallarrow{D}_v$

$D_v$ $D_v$ $D_v$
\end{document}


Note that \oveleftarrow is centered while subscripts and superscripts are not.

Note that this is math style dependent. That can be fixed with a lot more effort. See here.

\documentclass{article}
\usepackage{mathtools}

\newcommand{\combine}[3]% #1=centered preop, #2=operand, #3=postop
{\bgroup
\sbox0{$#1{\phantom{#2}}$}%
\sbox1{$#2$}%
\usebox0\hspace{-0.5\wd0}\hspace{-0.5\wd1}\usebox1\hspace{-\wd1}\phantom{#2}#3
\egroup}

\begin{document}
$\overleftarrow{D} D_v \combine{\overleftarrow}{D}{_v}$

\end{document}