# How to make a \newcommand that acts like \dim?

The \dim command is good at deciding how to space things following it: for example, both $\dim M$ and $\dim_A M$ look right (the subscript immediately follows the dim, whereas the non-subscripted version has a space before the M).

I'd like to make a similarly-acting command \depth. I've been using:

\newcommand{\depth}{\textnormal{depth }}


but this forces a space, so $\depth_A M$ doesn't look good. I tried using \xspace as per this question:

\newcommand{\depth}{\textnormal{depth}\xspace}


but it doesn't seem to do anything (there is no difference between using it and not, in the above command).

How can I get the right behavior?

-
–  Stefan Kottwitz Sep 20 '11 at 21:42

\usepackage{amsmath}
\DeclareMathOperator{\depth}{depth}


However, although \depth does not appear to be defined, it's used in some box contexts, so maybe it would be better to change the name:

\DeclareMathOperator{\Depth}{depth}


The space you see between "dim" and "M" is inserted because \dim qualifies as a math operator; and the space is inserted automatically also in \dim_M X, for instance, but not in \dim_M(A+B). The same will be with \Depth that produces an operator named "depth".

The \DeclareMathOperator* variant builds an operator that takes limits above and below in displays (like \min).

-