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.

I would like to define a phrase once that contains both math and text, and be able to use usable in both math and text mode. For example:

\newcommand{\Constraint}{\ensuremath{x \neq 0} \text{ and } \ensuremath{x \neq -1}}

This works fine in math mode, but leaves too much space in text mode. Of course:

\newcommand{\Constraint}{\ensuremath{x \neq 0} \text{and} \ensuremath{x \neq -1}}

works fine in text mode, but not in math mode.

Well that led me to:

\newcommand{\Space}{\ifmmode{ }\else{}\fi}%
\newcommand{\Constraint}{\ensuremath{x \neq 0} \text{\Space and\Space} \ensuremath{x \neq -1}}

which of course is a brilliant solution, except for the fact that it only yields correct results in text mode!! I attempted to replace the { } with various math spacing (\;, and \hspace{1ex}) and they had no effect in math mode.

The one case that does yield correct spacing in both is using an \mbox as in:

\newcommand{\Constraint}{\mbox{\ensuremath{x \neq 0}  and \ensuremath{x \neq -1}}}

but then the text can not be split across line boundaries so is not good enough.

Below are my various failed attempts commented out. The code as below yields:

enter image description here

\documentclass{article}
\usepackage{amsmath}
\usepackage{xspace}
\usepackage{showframe}

%%%% Math Mode: Works fine
%%%% Text Mode: Too much space before and after "and"
%\newcommand{\Constraint}{\ensuremath{x \neq 0} \text{ and } \ensuremath{x \neq -1}}

%%%% Math Mode: No space before and after "and"
%%%% Text Mode: Works fine 
%\newcommand{\Constraint}{\ensuremath{x \neq 0} \text{and} \ensuremath{x \neq -1}}


%%%% Math Mode: Works fine
%%%% Text Mode: Too much space before and after "and"
%\newcommand{\Constraint}{\ensuremath{x \neq 0} \text{~and~} \ensuremath{x \neq -1}}


%%%% Math Mode: missing space after "and"
%%%% Text Mode: Too much space before and after "and"
%\newcommand{\Constraint}{\ensuremath{x \neq 0} \text{~and\xspace} \ensuremath{x \neq -1}}

%%%% Math Mode: missing spaces before and after
%%%% Text Mode: Works fine.
\newcommand{\Space}{\ifmmode{ }\else{}\fi}%
\newcommand{\Constraint}{\ensuremath{x \neq 0} \text{\Space and\Space} \ensuremath{x \neq -1}}

\begin{document}
\begin{align*}
    \frac{2x}{x+1} &= \frac {2x-1}{x}\\
    \implies 2x  (x)   &= (x+1) (2x-1) \qquad\Constraint
\end{align*}
%
In a short sentence note the constraint: \Constraint.

In a long sentence note the above the constraint must be satisfied: $\Constraint$.
\end{document}
share|improve this question
    
Could you work around the problem by using the working text-mode-definition, and then use \text{\Constraint}? Or perhaps this isn't robust enough... –  cmhughes Mar 27 '12 at 2:30
    
@cmhughes: Using the two examples that work fine in math mode and adding \text{} around it still yields too much space in \text mode. –  Peter Grill Mar 27 '12 at 3:52

3 Answers 3

up vote 6 down vote accepted

Edit: Next try: take the spaces outside \text

\newcommand\Space{\penalty\z@\hspace{1ex}}
\newcommand{\Constraint}{\ensuremath{x \neq 0\Space\text{and}\Space x \neq -1}}

Sample output

I'd have expected there to be a breakable space for math mode already, so maybe I'm still getting something wrong...

share|improve this answer
    
See updated MWE. This fails when the text crosses line boundaries. –  Peter Grill Mar 27 '12 at 5:24
    
In what way does it fail? What behaviour do you expect? –  Stephan Lehmke Mar 27 '12 at 5:37
    
@PeterGrill It works by @StephanLehmke's way. I added a picture. Your method (and mine) won't break since there is a \mbox. –  Harish Kumar Mar 27 '12 at 5:47
    
The only break points are after the two \neq. –  egreg Mar 27 '12 at 6:44
    
@egreg Oops I wasn't aware that \text creates a box. New try with breakable space... –  Stephan Lehmke Mar 27 '12 at 8:04

The only thing to do is to say

\newcommand{\Constraint}{%
  \relax\ifmmode
    x\neq 0 \text{ and } x\neq 1
  \else
    $x\neq 0$ and $x\neq 1$
  \fi}

If one has loaded babel, then \textormath could be used instead of the conditional, but it's the same concept.

Why not \ensuremath{x\neq 0 \text{ and } x\neq1}?

First of all because it uses \ensuremath. :) Seriously, because the only break points would be after the relation symbols \neq, which is where the break is less preferable. The problem is, in fact, that \text creates a box.

Add ties and \nobreak where it seems suitable. And be careful in defining such abbreviations (that will be mysterious when, a couple of months from now, you'll be again reading the document).

How to avoid repeating the text? My solution would be to spell the text without resorting to mysterious abbreviations. Or, if you insist,

\newcommand{\ensuretext}[1]{\ifmmode#1\else\text{#1}\fi}
\newcommand{\Constraint}{%
  \ensuremath{x\neq 0}\ensuretext{ and }\ensuremath{x \neq 1}}

which is worse that specifying the text twice.

share|improve this answer
    
May I ask (just curiosity), why do you start with \relax? Other than that, this is the cleanest solution so far IMHO, because it does exactly what you would do when typesetting it manually. –  tohecz Mar 27 '12 at 8:45
1  
@tohecz It's only necessary if \Constraint happens to be in a \halign cell (for example in a tabular environment), because at the start of a cell TeX is in text mode and expands tokens looking for \omit. In this case it wouldn't be really necessary, but it's a good habit to prefix \ifmmode with \relax (unless very special things are needed). –  egreg Mar 27 '12 at 9:09
    
Is it a good habit with other if's as well? –  tohecz Mar 27 '12 at 14:03
1  
@tohecz With \ifvmode, for instance, and the reason is the same: at the start of a \halign cell TeX is in restricted horizontal mode. –  egreg Mar 27 '12 at 14:05
    
@egreg: Thanks, but I was trying to avoid specifying the constraint twice. This is all within a small self contained document, so should not be a problem when I come back to it. –  Peter Grill Apr 5 '12 at 18:40

Replace ensuremath by the \( \).

\documentclass{article}
\usepackage{amsmath}
\usepackage{xspace}
\newcommand{\Constraint}{\mbox{\(x \neq 0\)  and \(x \neq -1\)}} %<---- replace \ensure math{} by \( \).
%------------------------------------
\begin{document}
\begin{align*}
    \frac{2x}{x+1} &= \frac {2x-1}{x}\\
    \implies 2x  (x)   &= (x+1) (2x-1) \qquad\Constraint
\end{align*}
%
Note in the above the constraint: \Constraint.
\end{document}

enter image description here

share|improve this answer
1  
See updated MWE. This fails when the text crosses line boundaries. –  Peter Grill Mar 27 '12 at 5:23

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.