3

I'm trying to write to define \ds(a,b) so that it results in $||a - b||_1$. This should also work inside a math environment e.g. as part of an inequality statement, but I should be able to use it in the body of the text as \ds(a,b) without enclosing it in $$.

So far, I've managed this, browsing other similar questions in Tex.SE:

\def\ds#1{\innerds(#1)}
\def\innerds(#1,#2) {\ensuremath{||#1 - #2||_1}\xspace}

This works to a certain extent - I can write \ds(a,b) as part of text, but for some reason, I get the following error when I use it inside math mode:

Runaway argument?
c||_1\xspace \] \end {document} 
! File ended while scanning use of \innerds.
<inserted text> 
                \par 
<*> mwe_macro.tex

! Emergency stop.
<*> mwe_macro.tex

!  ==> Fatal error occurred, no output PDF file produced!

MWE (that I've tried) is given below:

\usepackage[fleqn]{amsmath}
\usepackage{amssymb}
\usepackage{xspace}

\def\ds#1{\innerds(#1)}
\def\innerds(#1,#2) {\ensuremath{||#1 - #2||_1}\xspace}

\begin{document}
Outside math mode: \ds(a,b)
Inside math mode: $\ds(a,b)$
Inside displaymath:
\[ \ds(a,b) + \ds(b,c) \geq \ds(a,c) \]
\end{document}

EDIT: Combining the two answers and the comments below, this is what I've adopted which works pretty fine for my intents:

  \def\ds(#1,#2){\innerds{#1}{#2}}
  \def\innerds#1#2{\ensuremath{\lVert #1 - #2\rVert}}

(Note that I've dropped the subscript 1 - I need it only once when I define the notion of the distance being used, hence I've moved it out of the macro).

  • 1
    What's bad in $\ds(a,b)$? It's math, after all, so it is never in text. In any case, you don't need \xspace. – egreg Dec 2 '14 at 23:19
  • @egreg - I don't want to have to type $$ every time i use the notation - I've used ensuremath before to do that, so I know its doable (or should be doable at least!) – TCSGrad Dec 2 '14 at 23:24
  • No! \lvert\lvert is wrong! It's simply \lVert. – egreg Dec 2 '14 at 23:51
  • ensuremath is generally not a good idea. TeX has a clear distinction between math mode and non math mode, compare \^ (text accent) \hat (math accent) making commands work in either is possible but complicates the language and the code and doesn't make the document any clearer. – David Carlisle Dec 2 '14 at 23:54
  • Please do not do that. Sincerely, your Copy Editor. – yo' Dec 2 '14 at 23:56
1

As it stands, when you use \ds(a,b) the \ds macro just picks up ( as its argument. You need something like this.

\def\ds(#1,#2){\innerds{#1}{#2}}
\def\innerds#1#2{\ensuremath{||#1 - #2||_1}\xspace}
  • 1
    What's \xspace supposed to do? Macros with arguments never need \xspace. And why two macros when one is sufficient? Why || which is the wrong way to obtain two bars? – egreg Dec 2 '14 at 23:36
  • @egreg --- no idea; I just fixed the problem that caused to OP's construction to fail. – Ian Thompson Dec 3 '14 at 8:29
3

The definition of \ds is wrong. If you want a syntax like

\ds(a,b)

then you should simply do

\def\ds(#1,#2){\ensuremath{\|#1-#2\|_1}}

(note \| and not ||). There's no need for \xspace, because spaces are not ignored after ).

However, I don't see why using \ensuremath either. I find it much clearer if math is always treated as math. There's not much gain in typing

The distance \ds(a,b) is less than $1$

instead of

The distance $\ds(a,b)$ is less than $1$

So I'd much prefer

\def\ds(#1,#2){\|#1-#2\|_1}

With amsmath you can improve it as

\def\ds(#1,#2){\lVert #1-#2\rVert_1}
  • 1
    @TCSGrad You may be fond of \xspace, but really it does nothing where you place it (I'm referring to the accepted answer). To be more precise: it can only cause harm. – egreg Dec 2 '14 at 23:42
  • OK, I've removed it - it got there as an artifact of a previous newcommand that I was modifying to get this macro to work, which did need it. But other than that, I would really prefer to remove the requirement of $$ each time I type it, which is the primary reason I accepted the other answer! – TCSGrad Dec 2 '14 at 23:45
  • 2
    @TCSGrad no macro needs \xspace (it really wasn't that good an idea to make that package:-) – David Carlisle Dec 2 '14 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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