# automatically pleasant spacing in the dot product

Is there a package in the meantime that automatically takes care of spacing before and after \top as well as the vertical position of \top in the dot product (i.e., canonical scalar product) constructs such as

v^{\top} \tilde w


depending on the actual stuff to be multiplied?

My current solution is

\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage{xparse}
\usepackage{amsmath}

\ExplSyntaxOn
%% \transpose{stuff} typesets ⊤ as a right superscript of stuff.
%% \transpose{stuff}[-5mu] typesets ⊤ as a right superscript of stuff, shifting ⊤ by 5mu to the left.
%% Both versions try to do the right kerning between stuff and ⊤.
\NewDocumentCommand{\transpose}{m O{0mu}}{
#1^{\mathchoice
{\transposeAux{#1}{#2}{\displaystyle}}
{\transposeAux{#1}{#2}{\textstyle}}
{\transposeAux{#1}{#2}{\scriptstyle}}
{\transposeAux{#1}{#2}{\scriptscriptstyle}}
}
}
\NewDocumentCommand{\transposeAux}{mmm}{%
\str_case:nnT {#1}
{{A}{\mkern-2mu}    {B}{\mkern-1mu}    {C}{\mkern-1mu}    {D}{\mkern-1mu}
{G}{\mkern-1mu}    {L}{\mkern-2mu}    {M}{\mkern-2mu}    {O}{\mkern-1mu}
{P}{\mkern-1mu}    {Q}{\mkern-1mu}    {R}{\mkern-1mu}    {S}{\mkern-1mu}
{V}{\mkern-1mu}    {W}{\mkern-1mu}
{a}{\mkern-2mu}    {b}{\mkern-2mu}    {c}{\mkern-2mu}    {d}{\mkern-1mu}
{e}{\mkern-2mu}    {f}{\mkern-1mu}    {g}{\mkern-2mu}    {h}{\mkern-2mu}
{i}{\mkern-2mu}    {j}{\mkern-2mu}    {k}{\mkern-2mu}    {l}{\mkern-1mu}
{m}{\mkern-2mu}    {n}{\mkern-2mu}    {o}{\mkern-2mu}    {p}{\mkern-2mu}
{q}{\mkern-2mu}    {r}{\mkern-2mu}    {s}{\mkern-2mu}    {t}{\mkern-1mu}
{u}{\mkern-3mu}    {v}{\mkern-2mu}    {w}{\mkern-2mu}    {x}{\mkern-2mu}
{y}{\mkern-2mu}    {z}{\mkern-2mu}    {0}{\mkern-3mu}
{\bar c}{\mkern-2mu}    {\bar u}{\mkern-2mu}
{\check u}{\mkern-2mu}
{\hat u}{\mkern-3mu}
{\tilde c}{\mkern-2mu}
{c'}{\mkern-2mu}
}
{\mkern-1mu}
\mkern#2
\str_case:nnTF {#1}
{{a}{\raisebox{-.3ex}{$$#3\top$$}}    {b}{\raisebox{-.1ex}{$$#3\top$$}}
{c}{\raisebox{-.3ex}{$$#3\top$$}}    {e}{\raisebox{-.3ex}{$$#3\top$$}}
{g}{\raisebox{-.3ex}{$$#3\top$$}}    {h}{\raisebox{-.3ex}{$$#3\top$$}}
{i}{\raisebox{-.3ex}{$$#3\top$$}}    {j}{\raisebox{-.3ex}{$$#3\top$$}}
{k}{\raisebox{-.3ex}{$$#3\top$$}}    {m}{\raisebox{-.3ex}{$$#3\top$$}}
{n}{\raisebox{-.3ex}{$$#3\top$$}}    {o}{\raisebox{-.3ex}{$$#3\top$$}}
{p}{\raisebox{-.3ex}{$$#3\top$$}}    {q}{\raisebox{-.3ex}{$$#3\top$$}}
{r}{\raisebox{-.3ex}{$$#3\top$$}}    {s}{\raisebox{-.3ex}{$$#3\top$$}}
{u}{\raisebox{-.3ex}{$$#3\top$$}}    {v}{\raisebox{-.3ex}{$$#3\top$$}}
{w}{\raisebox{-.3ex}{$$#3\top$$}}    {x}{\raisebox{-.3ex}{$$#3\top$$}}
{y}{\raisebox{-.3ex}{$$#3\top$$}}    {z}{\raisebox{-.3ex}{$$#3\top$$}}
{0}{\raisebox{-.1ex}{$$#3\top$$}}
{\bar c}{\raisebox{-.1ex}{$$#3\top$$}}
{\bar u}{\raisebox{-.1ex}{$$#3\top$$}}
}
{} {#3\top}
}
\NewDocumentCommand{\dotProduct}{ m m }{
\transpose{#1}
\str_case:nnTF {#2}
{{a}{\mkern-2mu}    {b}{\mkern-1mu}    {c}{\mkern-2mu}    {d}{\mkern-2mu}
{e}{\mkern-2mu}    {f}{\mkern-2mu}    {g}{\mkern-2mu}    {h}{\mkern-1mu}
{i}{\mkern-2mu}    {j}{\mkern-2mu}    {k}{\mkern-1mu}    {l}{\mkern-1mu}
{m}{\mkern-2mu}    {n}{\mkern-2mu}    {o}{\mkern-2mu}    {p}{\mkern-2mu}
{q}{\mkern-2mu}    {r}{\mkern-2mu}    {s}{\mkern-2mu}    {t}{\mkern-2mu}
{u}{\mkern-2mu}    {v}{\mkern-2mu}    {w}{\mkern-2mu}    {x}{\mkern-2mu}
{y}{\mkern-2mu}    {z}{\mkern-2mu}
}
{}{}
#2
}
\ExplSyntaxOff
\begin{document}
$\dotProduct{v}{\tilde w} = \dotProduct{\tilde v}{w} = \dotProduct{V}{\tilde W} = \dotProduct{\tilde V}{W} = \dotProduct{W}{\tilde V} = \dotProduct{\tilde W}{V}$
\end{document}


The code inside \dotProduct contains, essentially, huge case splits (probably, there is no way around it). For example, to pleasantly typeset v^{\top}\tilde w you would actually like to issue something like v^{\mkern-2mu\raisebox{-.3ex}{\top}}\mkern-1mu \tilde w. But for V^{\top} W you better not introduce any negative vertical changes and only minor horizonal ones. My version is not exhaustive, very nonsystematic, ad hoc, homebred, and, subjectively speaking, absolutely, terribly disgusting. Maybe, someone has already done this tedious exercise and could share his/her code which is better than mine?

I am using the downtack symbol (the reasons for this preference are outside of this question, just accept it). The produced fonts are T1. It would be nice to have a solution for more than T1, but as of now I'd be happy with just T1 in the first place.

• it would be easier to answer if you supplied some sample code to start with, also looking at your fragments can you confirm that you are just using juxtaposition not a dot for the dot product? also any such adjustments are clearly font dependent and a complete example docuemnt would have confirmed which fonts are to be used Oct 6, 2016 at 9:07

(Not an actual answer, but a suggestion) To add simplicity to the code you could do, for instance:

\NewDocumentCommand \dotProduct { m m }
{
\transpose{#1}
\tl_if_in:nnT { #2 } { bhkl                   } { \mkern -1mu }
\tl_if_in:nnT { #2 } { acdefgijmnopqrstuvwxyz } { \mkern -2mu }
#2
}


Ideally you would nest \tl_if_in:nnTF { .. } { .. } { .. } { \tl_if_in:nnTF .. } but I wrote the answer to simplify the code, and I don't think it does much harm to have the inefficient version.

• I don't think it will be that much worse, but I haven't tested. You can test yourself. If you write for instance bhkl\tilde a yes it won't work because you are adding an a to the list. May be you can achieve something more robust with \clist_if_in:NnTF. by defining clists with b,h,k,l,\tilde a,etc. Oct 6, 2016 at 22:13