Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
share|improve this question
add comment

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer
    
Thanks, xspace solves it. About two commands - I think, I can handle it. The \s commands were prepared for use inside equation environment, where I can't use commands that use $ character to create math enviro. After I've learned about \ensuremath, there was no place for two commands ;). However, my "space" problem remained and it was easier to search for solution with this two command model. –  Soul Reaver Feb 19 '12 at 13:48
5  
I'm happy to know that it helped you. But I insist that the approach of always typing math as math is better and more rewarding in the long run. –  egreg Feb 19 '12 at 13:50
1  
On second thought using \xspace may not be the best idea. Most commands don't add this space, and now there's confusion. Speaking about normal commands, I would rather have consistent behaviour: This \somecmd\ in \TeX\ works. and formulas used explicitly: This $\sabc$ is $$\sabc.$$ –  Frg Feb 19 '12 at 15:12
    
I agree with egreg. Most of my mathish macros blithely ignore that they might be mistakenly used outside math mode. If someone complains about weird errors, I'll add an \enforcemath to croak about the macro being used outside math mode. Regarding xspace: It is so easy to forget the backslash before the space (and all those backslashes look downright ugly). \xspace is a huge savior. –  David Hammen Feb 19 '12 at 19:34
add comment

It seems to me that the biggest problem that you mentioned was that you got:

no space between my symbol and xyz text

when you typed \mabc xyz.

The problem is that when LaTeX parses your text and finds the macro \mabc, it will ignore the trailing space by convention. To prevent this, you must provide open and closing braces like so, so that the trailing space is not ignored:

\mabc{} xyz
share|improve this answer
1  
Alternatively you can do \mabc\ (note the space after the \ . See comments on egreg's answer for more on this. –  qubyte Feb 20 '12 at 15:07
    
thanks for another answers, but for now, I'll stay with \xspace solution, because I'm closing my (first) document soon. however, those answers may help me with my future work on LaTeX documents, with creating "my style" of writing tex files, so thanks again ;) –  Soul Reaver Feb 20 '12 at 19:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.