[Tex/LaTex] Correct usage of math inside text

math-modemathtools

It's easy to find how to correctly embedd text in math mode, but not the other way. That makes me think, that my problem is somehow odd, or nobody takes it for a problem.

I'm using many symbols in equations, but also in text. That's why I've created separate file with new commands, e.g.:

\newcommand{\sabc}{A_{\mathrm{b}}^c}
\newcommand{\mabc}{$\sabc$}

Commands that begin with \s are used in equations, \m's are for in text use, to get just a math symbol. My problem starts with the second command. When used in text, e.g. \mabc xyz I get no space between my symbol and xyz text. It can be solved that way:

\newcommand{\mabc}{$\sabc$~}

but then, in case of using comma or dot (e.g. \mabc, xyz) i get unnecessary space between my symbol and comma. Yes, I can use ~ character each time I need it, but is there a way for normal space after \mabs command to be treated like I want?

P.S.
I've also tried using \ensuremath to simplify my commands (to have one command instead of two), but I end up with same results.


My preambule:

\documentclass[twoside,titlepage]{article}
\usepackage[polish]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper,inner=3.5cm,outer=2.0cm,top=2.0cm,bottom=2.0cm]{geometry}

\usepackage{fancyhdr}
\usepackage{multicol}
\usepackage[titletoc,title]{appendix}
\usepackage{enumitem}
\usepackage[table]{xcolor}
\usepackage{tabulary}
\usepackage{multirow}
\usepackage[fleqn]{mathtools}
\usepackage{relsize}
\usepackage[%
    % font=small,
    labelfont=bf,
    % tableposition=top
    ]{caption}
\usepackage{subcaption}
\input{commands}        % that's my file with additional commands, as described above

Best Answer

You gain nothing, in my opinion, in having two different commands: just say

This is some text with \(\sabc\) and a formula \(\sabc+x\).

If you want to save some keystrokes, use

This is some text with $\sabc$ and a formula $\sabc+x$.

Math should be always math. With two commands you'll almost surely end up with using the wrong one and editing will be painful.

A different approach might be to say

\usepackage{xspace}
\newcommand{\sabc}{\ensuremath{A_{\mathrm{b}}^c}\xspace}

using \sabc both in text and math. But with the former approach you'll get the benefit of correct syntax highlighting, if your editor does it.

Related Question