7

I'm interested in writing a command which would result in output that varies when called within $...$ tags and when called within, say, displaymath environment.

For instance, assume that I want to define \xnorm command which works as

\newcommand{\xnorm}[1]{\|#1\|}

when called in the inline mode and which works as

\newcommand{\xnorm}[1]{\Big\|#1\Big\|}

when called in the "usual" math mode. How to achieve this?

I know that there exists a control sequence like ifmmode, but it tests whether I am in any type of math mode. Are there any more specific control sequences? Or, is there any other solution?


Question answered. The goal can be achieved with \mathchoice command. An example code snippet can be found below, in the answers to this post. Thanks all very much.


Nevertheless, it is worth a warning to others who have ideas similar to mine that math layout generated by \mathchoice can differ with the original one.

For example, try the following two functions:

\newcommand{\xnorma}[1]{\Big\|#1\Big\|}

and

\newcommand{\xnormb}[1]{\mathchoice{\Big\|#1\Big\|}{}{}{}}

(the first argument of \mathchoice provides a definition for "usual" math mode, other arguments do not play role in this example).

Now, simply try

\begin{document}
\begin{displaymath}
\xnorma{x^2}^2 \xnormb{x^2}^2
\end{displaymath}
\end{document}

A difference will be visible.

4
  • Welcome to TeX.SX. tex.stackexchange.com/questions/1223/… is probably of interest. Commented Dec 13, 2013 at 17:23
  • Do you want to create a set of norm "fences" that are always small when used in inline-math mode, but are allowed to grow in size as needed in display-math mode?
    – Mico
    Commented Dec 13, 2013 at 17:39
  • 1
    Not exactly. My editorial problem was the following. In inline mode, I usually use very short pieces of math (as everyone), e.g. a norm of vector v. In display mode, more complex expressions with norms appear, e.g. a product of norms of, say, five subexpressions with different size. If the size of each norm "fence" was adapted to height of the contained subexpression, then I would obtain a line of norm "fences" of significantly varying heights, what doesn't look good. Therefore, my idea was to fix the height of norm "fences" in display mode to some compromise height.
    – Gregory
    Commented Dec 13, 2013 at 20:44
  • Thanks for providing this additional information. I'd say @egreg's solution should be more or less what you need.
    – Mico
    Commented Dec 13, 2013 at 20:47

1 Answer 1

6

I don't think you really wants this. However, here it is:

\documentclass{article}
\usepackage{amsmath}

\newcommand{\xnorm}[1]{%
  \mathchoice{\Bigl\|#1\Bigr\|}{\lVert#1\rVert}{\lVert#1\rVert}{\lVert#1\rVert}%
}

\begin{document}
A norm in text mode is $\xnorm{v}$, while in display math it is
\[
\xnorm{v}=\sum_{i=1}^n\lvert v_i\rvert
\]
\end{document}

The result is really ugly (and wrong):

enter image description here

If you change \Bigl and \Bigr with \bigl and \bigr you get a more acceptable result, but I can't see the point of increasing the fences' size.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .