# math mode parsing in NewDocumentCommand

I came across the following NewDocumentCommand (source).

\NewDocumentCommand{\expn}{gg}{%
\IfNoValueTF{#1}
{%
\Exp
}
{%
\IfNoValueTF{#2}
{%
\Exp\!\left(#1\right)
}
{%
\Exp{#2}\!\left(#1\right)
}
}%
}


When putting in overleaf document, I noticed that it throws an error starting at:

\left(#1\right)
}
{%
\Exp{#2}\!\left(#1\right)
}
}%
}


The error message is:

left can only be used in math mode

Could anyone please assist in fixing this error? I am new to NewDocumentCommand.

EDIT: I was asked to post a fully replicable example. Please create a macros_math.tex file and a main.tex file. In macros_math.tex place the following:

% Utility packages
\usepackage{amsmath,amssymb,amsthm,bm,bbm,amsfonts,mathtools} %math
\usepackage{xparse}
\usepackage{enumitem}

\usepackage{xspace} %used at the end of macros to automatically determine whether spaces should be eaten or

\newcommand{\Exp}{\mathbb{E}}

% Expectation
\NewDocumentCommand{\expn}{gg}{%
\IfNoValueTF{#1}
{%
\Exp
}
{%
\IfNoValueTF{#2}
{%
\Exp\!\left(#1\right) % offending line in overleaf!
}
{%
\Exp{#2}\!\left(#1\right)
}
}%
}


In main.tex place the following:

\documentclass{article}
\usepackage[utf8]{inputenc}

\input{macros_math.tex} % Import the math macros we created
\title{test}
\date{February 2019}

\begin{document}

\maketitle

\section{Introduction}
$\expn{X-Y}$ % Test - this works but overleaf does not like the way the macro is constructed
\end{document}

• The error suggests you should use $\expn{.}{..}$. How are you using it? – Werner Feb 2 '19 at 1:38
• Thanks - I use it in the article as $\expn{X - Y}$. Which does display correctly and seems to compile fine. Just that the macro above is placed in a separate math_macros.tex file which I include in my main article. The macros_math.tex file is throwing the overleaf error and I just want to clean up the macro to stop throwing it. Does that clarify? – user4687531 Feb 2 '19 at 1:43
• It's difficult to replicate the behaviour with what you're describing, as you're mentioning that the macro works, but it doesn't. Can you post something that we can replicate? – Werner Feb 2 '19 at 1:52
• Sure, it may be an overleaf problem. I created a temporary project here replicating the issue here: overleaf.com/9983455123mgvbdccmdtzn. This is the best I can do in replicating. You can see the highlighted issue in overleaf in macros_math.tex for the offending line described above. – user4687531 Feb 2 '19 at 2:11
• @Werner If external links are not allowed then I've pasted the code for macros_math.tex and main.tex placed in overleaf above. Hope this helps – user4687531 Feb 2 '19 at 2:22

This is an error that you can complete ignore if you never intend to compile macros_math.tex.
Overleaf interprets all .tex files as possible source files that will be compiled into an output PDF. As such, it parses the file to see whether the syntax is in order. Since it doesn't recognise the \NewDocumentCommand syntax as forming part of a macro definition that could be used elsewhere (inside a math environment, say), it assumes the use of \left and \right is incorrectly placed outside of math mode and shows as an error:
A way to resolve this error is to provide a different extension to macros_math.tex, perhaps something like macros_math.sty:
Remember to also change the code in your main.tex to \input{macros_math.sty}, or better, \usepackage{macros_math}!
• thank you so much, a really informative answer. Once clarification, you mentioned to convert macros_math.tex to a macros_math.sty file and import with usepackage{macros_math} if I don't need to compile macros_math.tex. Could you please explain when I would need to compile such a macros file? I am happy to go with the sty and usepackage approach but wanted to understand any limitations going forward. – user4687531 Feb 2 '19 at 17:46
• @user4687531: There's no need to compile macros_math.tex. It allows you to use \input{macros_math} (without the .tex extension) and is convenient, because you can insert that anywhere in your document. \usepackage requires a .sty and can only be used in the preamble (before \begin{document}). – Werner Feb 2 '19 at 19:42
• thanks for the clarification. I will go with the sty approach for now. Seems to avoid the error and be useful for my purposes. – user4687531 Feb 2 '19 at 20:14