# Arguments to macros in mathmode lose their mathmode typesetting

I'm writing a little semantics package(I know that there already exists some good packages for this) in latex to help me and my friends out with our assignments.

but im having a broblem that arguments passed to macros lose mathmode formating even though the macro getts called from a mathmode enviroment and that the body of the macro has a \ensuremath.

Here is an example.

I have a macro defined as this:

\newcommand{\wAssign}[3]{
\ensuremath{
\operatorname{
{#3}[{#1} \mapsto \mathcal{A}\llbracket {#2} \rrbracket {#3}]
}
}
}


Calling this macro like so:

\subsection{Subtitle}
$$\wAssign{\mathtt{x}}{a+b}{s'}$$


Wields the output:

But what I'm after is this:

Wich is what i get when using:

$$s'[\mathtt{x} \mapsto \mathcal{A}\llbracket a+b \rrbracket s']$$


It might not be super apparent but ' is rendered completely differently and a + b are not in the correct font.

How does one fix this?

• Welcome to TeX.SX! What's the purpose of \operatorname? Just remove it. – egreg Oct 2 '15 at 10:37
• Henrik: I'm somewhat curious as to why you chose to include \operatorname in the first place. What did you think it did? (honest question) – David Z Oct 2 '15 at 17:19

\operatorname is used for typesetting names of functions like sine, cosine and logarithm. In your macro it is exactly the part that does wrong, so remove it:

\newcommand{\wAssign}[3]{%
{#3}[{#1} \mapsto \mathcal{A}\llbracket {#2} \rrbracket {#3}]%
}


I recommend not using \ensuremath and, in case, typing $\wAssign{A}{B}{C}$ that's clearer, in my opinion, albeit requiring two keystrokes more.

If you really want to use \ensuremath, pay attention to spurious spaces:

\newcommand{\wAssign}[3]{%
\ensuremath{%
{#3}[{#1} \mapsto \mathcal{A}\llbracket {#2} \rrbracket {#3}]%
}%
}


Actually just the first and fourth % are necessary, but the other two do no harm either.

• Why are the % necessary? – Henrik Sommerland Oct 2 '15 at 11:07
• that really solved it :) I had troubles before to make arguments work at all in mathmode and some other post on Tex.Sx said that the \operatorname was necessary. – Henrik Sommerland Oct 2 '15 at 11:12
• @HenrikSommerland Spaces are ignored in math mode, but not in text mode; an end-of-line in a definition counts as a space in the replacement text, unless masked off with %. – egreg Oct 2 '15 at 11:35