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}
$\expn{.}{..}$
. How are you using it? – Werner Feb 2 '19 at 1:38$\expn{X - Y}$
. Which does display correctly and seems to compile fine. Just that the macro above is placed in a separatemath_macros.tex
file which I include in my main article. Themacros_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:43macros_math.tex
for the offending line described above. – user4687531 Feb 2 '19 at 2:11macros_math.tex
andmain.tex
placed in overleaf above. Hope this helps – user4687531 Feb 2 '19 at 2:22