# newcommand for math mode only

My preamble contains the following definition

\newcommand*  {\pdiffat}[3] {\left(\frac{\partial{#1}}{\partial{#2}}\right)_{#3}}


which is meant to be used in math mode only. However, overleaf complains that \left must only be used in math mode and hence colors this line and all subsequent lines in pink/red.

Is there a (ideally simple) way to avoid this? I.e. such that the macro with the same functionality still works, but overleaf is happy?

• I do not use overlead, but is wrapping the definition in \ensuremath{...} acceptable? – Steven B. Segletes Feb 11 at 14:21
• Remove the spaces between the * and the command name, then overleaf will behave again. – Ulrike Fischer Feb 11 at 14:28
• The only way is to ask Overleaf to avoid that silly behavior for macro definitions in the document preamble. – egreg Feb 11 at 14:41
• Hi, Tom from Overleaf support speaking. The solution by @UlrikeFischer should work. Still, I'll raise this with our engineers to see whether we can teach this to the parser. – yo' Feb 11 at 15:26
• @StevenB.Segletes If you make your suggestion into an answer, I'll accept that. – Walter Feb 11 at 15:58

Remove the spaces between the \newcommand* and the command name, they seem to confuse the overleaf parser. This here works fine for me:

 \newcommand*{\pdiffat}[3]{\left(\frac{\partial{#1}}{\partial{#2}}\right)_{#3}}


Despite the many reasons not to do it this way, and Alan's comment provides a link about not using \ensuremath, I nonetheless suggested placing an \ensuremath around the code, as in

\newcommand*{\pdiffat}[3]{\ensuremath{\left(\frac{\partial{#1}}{\partial{#2}}\right)_{#3}}}


In this manner, the command will function when invoked in either text or math modes.

I did not suggest this as a general approach to macro making, but only as a possible remedy for the particular overleaf problem the OP was experiencing. (I have no knowledge of overleaf, myself)

\documentclass{article}
\begin{document}
\newcommand*{\pdiffat}[3]{\ensuremath{\left(\frac{\partial{#1}}{\partial{#2}}\right)_{#3}}}

\pdiffat{a}{b}{c} vs. $\pdiffat{a}{b}{c}$
\end{document}

• I find the statements about \ensuremath a bit over the top. For pretty much anything you can construct scenarios where this something does not give the optimal result. – Schrödinger's cat Feb 11 at 16:56