How to use variables in texts (between quotation) in math mode?

For example, I want a function to force its argument to be text, even though it is used in math mode. These don’t work:

#let fun(arg) = $"#arg"_n$
#let fun(ft) = $#text[#arg]_n$

Is there a way so that fun, which is called in math content, shows arg as text?

The upright text you’re describing is the one in non-maths fonts.

Text in math mode should use text font is a pending issue.

#show math.equation: set text(font: "Fira Math")

// Using quotes -> upright Fira Math
$"Upright text" x + y = z$

// Using function `math.op` -> upright Fira Math with slightly less spacing
$op("Upright text") x + y = z$

// Using function `math.upright` -> upright Fira Math
$upright("Upright text") x + y = z$

// Using function `text` -> upright Fira Math
$text("Upright text") x + y = z$

// Using function `text` with own font -> own upright font
$text(font: "Libertinus Serif", "Upright text") x + y = z$

So you would want to use the last option, where Libertinus Serif is known to be the default outside font:

#let fun(arg) = $text(font: "Libertinus Serif", arg)_n$

// In code, use as any of the following

$fun("xyz") 1 + 1 = 2$

$#fun[xyz] 1 + 1 = 2$

$#fun([xyz]) 1 + 1 = 2$

image

Further automation might be possible throught context. That way, the outside font would be automatically applied.

Quick searches bring up these relevant posts:

1 Like

I am not sure this is what you are looking for, but this may be it:

#let fun(arg) = $#arg _n$

$x_n$
#fun($x$)
$fun(x)$
$fun("x")$

If not, could you please also describe what you are expecting or provide a more comprehensive example?

1 Like

In principle it’s impossible, because the function itself can’t change the type of argument produced on the side calling the function. In general, when a function is called in math mode, the argument will be some kind of content, as demonstrated:

#let fun(arg) = str(type(arg))
$fun(x) + fun(1) + fun(1 / 2) + fun(integral d x)$