2

I've created some custom commands in my document so that if I change the variable, all occurcences can be changed easily. However, some of my command variables are contained in the math environment ($$). I use them both in plain text and inside equations. When I use them within an equation, I get Missing $ inserted. [ $\mycommand], presumably due to the math mode value being nested inside math mode a second time. Here's an example:

\documentclass[a4paper,12pt]{article}

\usepackage{xspace}

\begin{document}

\newcommand{\mycommand}{$my_{command}$\xspace}

Here is my \mycommand working fine.

Here is my \mycommand not working in math mode:

$\mycommand$

\end{document}

Is there a workaround to this?

James
  • 125
  • 2
    Math macros should never include $...$ and used in text should always be used inside $...$. That gives your document the proper semantics and eliminate the need for \xspace which should never be used. – daleif Jan 25 '23 at 10:50
  • @daleif That has been pointed out to me. However, even when not using math macros, if I create a plain text command, it doesn't respect the space I put after invoking it. Which means I'm left with having to add a space in the command. That, however, causes unwanted spaces when you follow the command with a comma or a period. – James Jan 25 '23 at 13:47
  • 1
    Then make a habit of always using \foo{}. Using \xpsace is a bad idea as it does not cover all corner cases. – daleif Jan 25 '23 at 13:52

2 Answers2

3
\documentclass[a4paper,12pt]{article}
\usepackage{xspace}
\NewDocumentCommand{\mycommand}{}{\relax\ifmmode my_{command} \else $my_{command}$\xspace \fi}
\begin{document}
Here is my \mycommand working fine.

Here is my $\mycommand$ working fine.

Here is my \mycommand not working in math mode:

Here is my $\mycommand$ not working in math mode:
\end{document}

enter image description here

Clara
  • 6,012
2

For commands, that use math mode but should work in both, math mode and text mode, you should use \ensuremath{…} instead of $…$:

\documentclass[a4paper,12pt]{article}

\usepackage{xspace}

\newcommand{\mycommand}{\ensuremath{my_{command}}\xspace}

\begin{document}

Here is my \mycommand working fine.

Here is my \mycommand working in math mode:

$\mycommand$

\end{document}

enter image description here

BTW: To separate form from content, definitions should be placed in the document preamble, i.e. before \begin{document}.

Note also: In your example command is the math product of c, o, m etc. If this is not intended, you should either use \mathit{command} for the single variable command or either \mathrm{command} or \text{command} (needs package amsmath) for the text command.

However, I would recommend to use something like:

\documentclass[a4paper,12pt]{article}

\newcommand{\mycommand}{\mathit{my}_{\mathrm{command}}}

\begin{document}

Here is my $\mycommand$ working fine.

Here is my $\mycommand$ working in math mode:

[ \mycommand ]

\end{document}

with explicitly switching to math mode for math material. Or:

\documentclass[a4paper,12pt]{article}
\usepackage{amsmath}
\usepackage{xspace}

\newcommand{\mycommand}{my\textsubscript{command}\xspace}

\begin{document}

Here is my \mycommand working fine.

Here is my \mycommand working in math mode:

[ \text{\mycommand} ]

\end{document}

if the command is not intended to be math but text.

cabohah
  • 11,455
  • Thank you, that seems to fix my problem!

    I understand, but in my document, it makes sense to me to declare the command when I first refer to the variable, because that's where I'll be looking to make changes if any. But generally I agree that it is much better form.

    Right, I'll actually try for those variables that just have a subscript or superscript, thank you for the suggestion. But some of my variables are greek letters, and those I only seem to be able to invoke in math mode.

    – James Jan 25 '23 at 10:59