How can I replicate this LaTeX command in Typst?

How can I replicate the following LaTeX command in Typst?

\newcommand\p[3]{{}^{#1}{p}^{#2}_{#3}}

Here is what \p{A}{B}{C} looks like in LaTeX:

image

In Typst, I tried to write the following:

#let p(a, b, c) = $#super(a)p#sub(c)#super(b)$

but the output of #p("A", "B", "C") is not quite the same as the output of LaTeX:

The letters “A”, “B”, and “C” are not italic and the letter “B”'s position way off.

Questions

  1. How can I replicate the exact output of LaTeX?
  2. In LaTeX, I can write the p command as \p{A}{B}{C} while in Typst I have to include quotation marks around the letters “A”, “B”, and “C”, as shown in the command #p("A", "B", "C"). Including quotation marks can be cumbersome when writing. How can I avoid having to include quotation marks (i.e. write something like p(A,B,C))? If it helps, I plan on using the p command only in math mode.

The correct way to add indices and exponents in equations is using the Attach Functions – Typst Documentation. Your function will then be

#let p3(a, b, c) = $attach(p, tl: #a, tr: #b, br: #c)$
$p3(A, B, C)$

If you call this in an equation, you do not need quotation marks and the letters will be italic by default. I named the function p3() since the function p() would just be interpreted as the character p in an equation. You could still call the function with #p(), but the arguments will not “inherit” the math mode in that case. (I’m not sure if there is some trick/workaround to use single-character function names in equations.)

4 Likes