Quoting from this question, DeclareMathOperator
is designed to create commands that should typeset operator names such as sin
and lim
. The default font for these quantities is upright. If you want part of the operator name to be slanted (in the way variables are usually typeset), you have to specify this explicitly:
\DeclareMathOperator{\z}{{\mathbf{z}}(\mathnormal{k})}
On the other hand, I would guess that what you actually want is that only the 'z' itself is the operator name:
\DeclareMathOperator{\z}{\mathbf{z}}
The part in parenthesis should be the argument to this operator. If you frequently use the same argument, you could define a short hand for this, using \newcommand
:
\newcommand{\zk}{\z(k)}
Concerning your second question, if you encapsulate your definition with \ensuremath
, you can use your command both within or outside of math environments:
\newcommand{\zk}{\ensuremath{\z(k)}}
With the last definition, both $\zk$
and \zk{}
should give the same result.
If you are sure that \grad
will be followed by a fixed (but easily extendable) set of binary operators, this should work:
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\DeclareMathOperator{\gradop}{grad}
\ExplSyntaxOn
% the list of admissible binary operators
\tl_const:Nn \c_denis_grad_ops_tl { \cdot \wedge }
\NewDocumentCommand{\grad}{}
{
\gradop
\peek_after:Nw \denis_grad_check:
}
\cs_new_protected:Nn \denis_grad_check:
{
\tl_map_inline:Nn \c_denis_grad_ops_tl
{% if the token matches one in the list, issue {\!}
\token_if_eq_meaning:NNT \l_peek_token ##1 { \tl_map_break:n { {\!} } }
}
}
\ExplSyntaxOff
\begin{document}
\begin{gather*}
\grad F \\
\mathop{\mathrm{grad}} F\\
\grad \cdot F \\
\mathrm{grad}\cdot F \\
\grad \wedge F \\
\mathrm{grad}\wedge F
\end{gather*}
\end{document}
If a binary operator in the list follows, we issue {\!}
that fixes the spacing: the thin space between the operator and the empty atom is nullified by \!
and the empty atom will provide the required bit for the spacing around binary operators.
A slightly different version with an interface for adding binary operators; the initial declaration
\OperatorBinary{\cdot,\wedge} % initialize
should go in the class, together with instructions such as
You can define operator names that behave well when followed by binary operation symbols with
\DeclareMathOperatorX{<cs>}{<name>}
for example
\DeclareMathOperatorX{\grad}{grad}
to be used like \grad f
or else like \grad\cdot F
. The predefined list of admissible binary operators includes \cdot
and \wedge
, but it can be augmented by saying in the preamble, for instance,
\OperatorBinary{\times}
(the argument can be a list like in the initial declaration which is \OperatorBinary{\cdot,\wedge}
. No operator of this type is predefined, use \DeclareMathOperatorX
for defining the one you'll be using.
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\DeclareMathOperatorX}{mm}
{
\NewDocumentCommand{#1}{}
{
\operatorname{#2}
\peek_after:Nw \denis_opx_check:
}
}
\NewDocumentCommand{\OperatorBinary}{m}
{
\clist_gput_right:Nn \g_denis_opx_binary_clist { #1 }
}
\clist_new:N \g_denis_opx_binary_clist
\cs_new_protected:Nn \denis_opx_check:
{
\clist_map_inline:Nn \g_denis_opx_binary_clist
{
\token_if_eq_meaning:NNT \l_peek_token ##1 { \clist_map_break:n { {\!} } }
}
}
\ExplSyntaxOff
\OperatorBinary{\cdot,\wedge} % initialize
\DeclareMathOperatorX{\grad}{grad}
\begin{document}
\begin{gather*}
\grad F \\
\mathop{\mathrm{grad}} F\\
\grad \cdot F \\
\mathrm{grad}\cdot F \\
\grad \wedge F \\
\mathrm{grad}\wedge F
\end{gather*}
\end{document}
Best Answer
If
\id
denotes the identity map, then it shouldn't be an operator, but an ordinary symbol:Notice also that
\DeclareMathOperator*
is meant for defining operators that take limits above and below them (such as\min
and\max
).The behavior is easily explained: when we write
\log -2
we want the-
not to be treated as a binary operation symbol. For TeX your\id\circ g
is just the same: if a Bin atom follows an Op atom, it is converted into an Ord atom. It's a mistake to define something with\DeclareMathOperator
just to have upright letters.