3

Inspired by David Carlisle's answer and comment from my previous question, I wanted to see if I could try and redefine the math mode accent macros (\bar, \hat, \dot, etc.) to kern specifically for \mathbb and \mathcal inputs because I want to use AMS blackboard bold and Computer Modern calligraphic respectively instead of Latin Modern's in LuaTeX with unicode-math.

Here's my MWE:

MWE

% !TEX program = lualatex

% DOCUMENT CLASS
\documentclass[11pt,preview,varwidth,%
border={3pt 5pt 3pt 5pt}% left bottom right top
]{standalone}

% PACKAGES
\usepackage{amsmath}
\usepackage{unicode-math}
    \setmathfont{Latin Modern Math}
    \let\lmmathbb\mathbb % Latin Modern blackboard bold
    \let\lmmathcal\mathcal % Latin Modern calligraphic
    \DeclareMathAlphabet{\amsmathbb}{U}{msb}{m}{n} % AMS blackboard bold
    \DeclareMathAlphabet{\cmmathcal}{OMS}{cmsy}{m}{n} % Computer Modern calligraphic

% START OF DOCUMENT
\begin{document}
\begin{tabular}{lcr}
    Latin Modern's \verb|\mathbb| & : & \(\bar{\lmmathbb{A}} \hat{\lmmathbb{A}} \vec{\lmmathbb{A}} \dot{\lmmathbb{A}} \ddot{\lmmathbb{A}}\) \\
    \(\cmmathcal{AMS}\)'s \verb|\mathbb| (with \textit{unkerned} accents) & : & \(\bar{\amsmathbb{A}} \hat{\amsmathbb{A}} \vec{\amsmathbb{A}} \dot{\amsmathbb{A}} \ddot{\amsmathbb{A}}\) \\
    Latin  Modern's \verb|\mathcal| & : & \(\bar{\lmmathcal{A}} \hat{\lmmathcal{A}} \vec{\lmmathcal{A}} \dot{\lmmathcal{A}} \ddot{\lmmathcal{A}}\) \\
    Computer Modern's \verb|\mathcal| (with \textit{unkerned} accents) & : & \(\bar{\cmmathcal{A}} \hat{\cmmathcal{A}} \vec{\cmmathcal{A}} \dot{\cmmathcal{A}} \ddot{\cmmathcal{A}}\)
\end{tabular}
\end{document}
% END OF DOCUMENT

I want to achieve the following:

Desired Result

.
.
.
\begin{tabular}{lcr}
    Latin Modern's \verb|\mathbb| & : & \(\bar{\lmmathbb{A}} \hat{\lmmathbb{A}} \vec{\lmmathbb{A}} \dot{\lmmathbb{A}} \ddot{\lmmathbb{A}}\) \\
    \(\cmmathcal{AMS}\)'s \verb|\mathbb| (with \textit{kerned} accents) & : & \(\bar{\amsmathbb{A}} \hat{\amsmathbb{A}} \vec{\amsmathbb{A}} \dot{\amsmathbb{A}} \ddot{\amsmathbb{A}}\) \\
    Latin  Modern's \verb|\mathcal| & : & \(\bar{\lmmathcal{A}} \hat{\lmmathcal{A}} \vec{\lmmathcal{A}} \dot{\lmmathcal{A}} \ddot{\lmmathcal{A}}\) \\
    Computer Modern's \verb|\mathcal| (with \textit{kerned} accents) & : & \(\bar{\cmmathcal{A}} \hat{\cmmathcal{A}} \vec{\cmmathcal{A}} \dot{\cmmathcal{A}} \ddot{\cmmathcal{A}}\)
\end{tabular}
.
.
.

How can I redefine \bar, \hat, \vec, \dot, and \ddot efficiently to achieve the desired result above for \amsmathbb and \cmmathcal inputs? (With the requirement that they behave normally otherwise)

3
  • It seems that there is a bug in LaTeX's Unicode math. If you remove \usepackage{unicode-math} and use simply $\hat{\cal A}$ then the result is correct. Moreover, I tried OpTeX with Unicode math and with LatinModern-Math main font and \cal from Computer Modern (see OpTeX trick petr.olsak.net/optex/optex-tricks.html#orical ) and the result is correct too.
    – wipet
    Commented Nov 30, 2022 at 5:31
  • There’s a preliminary problem: are you sure your readers will be able to distinguish between CM and LM calligraphic letters in the same document? The situation is even worse for blackboard bold.
    – egreg
    Commented Nov 30, 2022 at 9:58
  • @egreg I don't plan to actually use Latin Modern for either blackboard bold or calligraphic letters in my documents, I just simply wanted to be able to still access them for accessing sake (the MWE is used to show the differences when using accents). My issue is only with the kerning of accents when using AMS's blackboard bold and CM's calligraphic fonts. Commented Nov 30, 2022 at 13:33

1 Answer 1

0

This is my approach but I do not know whether it is "efficient" or not and any feedback would be much appreciated.

My Approach

% !TEX program = lualatex

% DOCUMENT CLASS
\documentclass[11pt,preview,varwidth,%
border={3pt 5pt 3pt 5pt}% left bottom right top
]{standalone}

% PACKAGES
\usepackage{amsmath}
\usepackage{unicode-math}
    \setmathfont{Latin Modern Math}
    \let\lmmathbb\mathbb % Latin Modern blackboard bold
    \let\lmmathcal\mathcal % Latin Modern calligraphic
    \DeclareMathAlphabet{\amsmathbb}{U}{msb}{m}{n} % AMS blackboard bold
    \DeclareMathAlphabet{\cmmathcal}{OMS}{cmsy}{m}{n} % Computer Modern calligraphic
\usepackage{xstring} % For \string and \IfSubStr

    % \shiftmath (https://tex.stackexchange.com/a/666905/)
\newcommand{\shiftmath}[3]{#1{{\mkern-#2mu}#3{\mkern#2mu}}}

    % \amsmathbbkerning and \cmmathcalkerning
\newcommand{\amsmathbbkerning}{-2}
\newcommand{\cmmathcalkerning}{1.5}

% \ArgZapSpaces (https://tex.stackexchange.com/a/652826/)
\ExplSyntaxOn
\cs_new_protected:Npn \__user_arg_zap_spaces:n #1
  {
    \tl_set:Nn \ProcessedArgument {#1}
    \tl_remove_all:Nn \ProcessedArgument { ~ }
  }
\cs_set_eq:NN \ArgZapSpaces \__user_arg_zap_spaces:n
\ExplSyntaxOff

% \bar redefinition
\let\oldbar\bar
\NewDocumentCommand{\newbar}{>{\ArgZapSpaces}m}{%
    \IfSubStr{\string#1}{\string\amsmathbb}{%
        \shiftmath{\oldbar}{\amsmathbbkerning}{#1}  % if \amsmathbb
    }{%
        \IfSubStr{\string#1}{\string\cmmathcal}{%
            \shiftmath{\oldbar}{\cmmathcalkerning}{#1} % if \cmmathcal
        }{%
            \oldbar{#1} % if neither \amsmathbb nor \cmmathcal
        }
    }
}
\AtBeginDocument{\def\bar{\newbar}}

    % \hat redefinition
\let\oldhat\hat
\NewDocumentCommand{\newhat}{>{\ArgZapSpaces}m}{%
    \IfSubStr{\string#1}{\string\amsmathbb}{%
        \shiftmath{\oldhat}{\amsmathbbkerning}{#1}  % if \amsmathbb
    }{%
        \IfSubStr{\string#1}{\string\cmmathcal}{%
            \shiftmath{\oldhat}{\cmmathcalkerning}{#1} % if \cmmathcal
        }{%
            \oldhat{#1} % if neither \amsmathbb nor \cmmathcal
        }
    }
}
\AtBeginDocument{\def\hat{\newhat}}

    % \vec redefinition
\let\oldvec\vec
\NewDocumentCommand{\newvec}{>{\ArgZapSpaces}m}{%
    \IfSubStr{\string#1}{\string\amsmathbb}{%
        \shiftmath{\oldvec}{\amsmathbbkerning}{#1}  % if \amsmathbb
    }{%
        \IfSubStr{\string#1}{\string\cmmathcal}{%
            \shiftmath{\oldvec}{\cmmathcalkerning}{#1} % if \cmmathcal
        }{%
            \oldvec{#1} % if neither \amsmathbb nor \cmmathcal
        }
    }
}
\AtBeginDocument{\def\vec{\newvec}}

    % \dot redefinition
\let\olddot\dot
\NewDocumentCommand{\newdot}{>{\ArgZapSpaces}m}{%
    \IfSubStr{\string#1}{\string\amsmathbb}{%
        \shiftmath{\olddot}{\amsmathbbkerning}{#1}  % if \amsmathbb
    }{%
        \IfSubStr{\string#1}{\string\cmmathcal}{%
            \shiftmath{\olddot}{\cmmathcalkerning}{#1} % if \cmmathcal
        }{%
            \olddot{#1} % if neither \amsmathbb nor \cmmathcal
        }
    }
}
\AtBeginDocument{\def\dot{\newdot}}

    % \ddot redefinition
\let\oldddot\ddot
\NewDocumentCommand{\newddot}{>{\ArgZapSpaces}m}{%
    \IfSubStr{\string#1}{\string\amsmathbb}{%
        \shiftmath{\oldddot}{\amsmathbbkerning}{#1}  % if \amsmathbb
    }{%
        \IfSubStr{\string#1}{\string\cmmathcal}{%
            \shiftmath{\oldddot}{\cmmathcalkerning}{#1} % if \cmmathcal
        }{%
            \oldddot{#1} % if neither \amsmathbb nor \cmmathcal
        }
    }
}
\AtBeginDocument{\def\ddot{\newddot}}

% START OF DOCUMENT
\begin{document}
\begin{tabular}{lcr}
    Latin Modern's \verb|\mathbb| & : & \(\bar{\lmmathbb{A}} \hat{\lmmathbb{A}} \vec{\lmmathbb{A}} \dot{\lmmathbb{A}} \ddot{\lmmathbb{A}}\) \\
    \(\cmmathcal{AMS}\)'s \verb|\mathbb| (with \textit{unkerned} accents) & : & \(\oldbar{\amsmathbb{A}} \oldhat{\amsmathbb{A}} \oldvec{\amsmathbb{A}} \olddot{\amsmathbb{A}} \oldddot{\amsmathbb{A}}\) \\
    \(\cmmathcal{AMS}\)'s \verb|\mathbb| (with \textit{kerned} accents) & : & \(\bar{\amsmathbb{A}} \hat{\amsmathbb{A}} \vec{\amsmathbb{A}} \dot{\amsmathbb{A}} \ddot{\amsmathbb{A}}\) \\
    Latin  Modern's \verb|\mathcal| & : & \(\bar{\lmmathcal{A}} \hat{\lmmathcal{A}} \vec{\lmmathcal{A}} \dot{\lmmathcal{A}} \ddot{\lmmathcal{A}}\) \\
    Computer Modern's \verb|\mathcal| (with \textit{unkerned} accents) & : & \(\oldbar{\cmmathcal{A}} \oldhat{\cmmathcal{A}} \oldvec{\cmmathcal{A}} \olddot{\cmmathcal{A}} \oldddot{\cmmathcal{A}}\) \\
    Computer Modern's \verb|\mathcal| (with \textit{kerned} accents) & : & \(\bar{\cmmathcal{A}} \hat{\cmmathcal{A}} \vec{\cmmathcal{A}} \dot{\cmmathcal{A}} \ddot{\cmmathcal{A}}\)
\end{tabular}
\end{document}
% END OF DOCUMENT
3
  • 1
    Welcome to tex.sx. Commented Nov 29, 2022 at 20:40
  • it's bit odd to use xstring in l3 code, I doubt the package adds any functionality here also as commented on your last question, don't use \let on latex commands Commented Nov 29, 2022 at 21:20
  • 1
    @DavidCarlisle I'm not personally proficient in l3 code as I haven't read the documentations yet and most of my knowledge comes from reading TeX.SX answers, if you don't mind could you please show me how to improve the solution? Thank you. Commented Nov 30, 2022 at 5:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .