0

Here's my MWE:

% !TEX program = lualatex
\documentclass[preview,border={5mm 2.5mm 5mm 0mm},varwidth]{standalone}
\usepackage{mathtools}
\usepackage[warnings-off={mathtools-colon,mathtools-overbracket}]{unicode-math}
    \setmathfont{Latin Modern Math}
\usepackage{setspace}\setdisplayskipstretch{}

\newcommand{\testprime}[3][-2]{A' #2 x #3 \; B' #2 x #3 \; C' #2 x #3 \; P' #2 x #3 \; Q' #2 x #3 \; R' #2 x #3 \; f' #2 x #3 \; g' #2 x #3 \; h' #2 x #3}
\newcommand{\testkernedprimeleftdelimiter}[3][-2]{A' \mkern#1mu#2 x #3 \; B' \mkern#1mu#2 x #3 \; C' \mkern#1mu#2 x #3 \; P'  \mkern#1mu#2 x #3 \; Q' \mkern#1mu#2 x #3 \; R' \mkern#1mu#2 x #3 \; f' \mkern#1mu#2 x #3 \; g' \mkern#1mu#2 x #3 \; h' \mkern#1mu#2 x #3}

\begin{document}
\onehalfspacing%
\[\testprime{(}{)}\]
\[\testkernedprimeleftdelimiter{(}{)}\]

\[\testprime{[}{]}\]
\[\testkernedprimeleftdelimiter[-0.5]{[}{]}\]

\[\testprime{\{}{\}}\]
\[\testkernedprimeleftdelimiter{\{}{\}}\]

\end{document}

Minimum Working Example

I want to globally redefine ' or ^{\prime} to be kerned using \mkern only when it encounters a left delimiter (specifically -2mu for ( and \{, and -0.5mu for [) in math mode (maybe using \@ifnextchar?).

How do I achieve this?

*Related: This TeX.SX question and answer and the definition of ' in latex.ltx.

\def\active@math@prime{^\bgroup\prim@s}
{\catcode`\'=\active \global\let'\active@math@prime}
\def\prim@s{%
  \prime\futurelet\@let@token\pr@m@s}
\def\pr@m@s{%
  \ifx'\@let@token
    \expandafter\pr@@@s
  \else
    \ifx^\@let@token
      \expandafter\expandafter\expandafter\pr@@@t
    \else
      \egroup
    \fi
  \fi}
\def\pr@@@s#1{\prim@s}
\def\pr@@@t#1#2{#2\egroup}
1

1 Answer 1

1

You can do it with \@ifnextchar.

\documentclass{article}

\makeatletter

\def\pr@m@s{%
  \ifx'\@let@token
    \expandafter\pr@@@s
  \else
    \ifx^\@let@token
      \expandafter\expandafter\expandafter\pr@@@t
    \else
      \expandafter\expandafter\expandafter\pr@@@end
    \fi
  \fi}
\def\pr@@@end{\egroup
    \@ifnextchar({\mkern-2mu }{\@ifnextchar\{{\mkern-2mu }{\@ifnextchar[{\mkern-.5mu }{}}}}

\makeatother

\begin{document}

$A'\ B''\ C''^{xy}\ f''(x)\ g'[y]\ X'''\{z\}$

\end{document}

output

Note that in cases like f'^x (... the space will not be added. If you do want it, you'll have to change the \egroup in\pr@@@t to \pr@@@end as well.

3
  • Thank you very much! Jul 2, 2022 at 3:40
  • I'm sorry but it doesn't seem to be working if I add it to the preamble of my MWE. Could you help me? Jul 2, 2022 at 3:49
  • I was testing this with plain LaTeX. In your MWE if unicode-math and \setmathfont is removed it works. Perhaps someone more familiar with those packages can fix, since the patches here are made for latex.ltx.
    – plante
    Jul 2, 2022 at 4:00

You must log in to answer this question.

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