newcommand with option for bracket size

I want to write a new command for the trace of a matrix that I've defined like:

\newcommand{\trace}[1]{\mathsf{trace}\!\left[#1\right]}


but when I call \trace{A^\top} the automatic scaling makes the [ and ] characters \bigl[ and \bigr]. How can I pass an optional additional argument to my new command so that I can control the scaling? Ideally it would be like \trace[\big]{A^\top} to get the scaled version and \trace{A^\top} to get the standard version.

I suggest you employ the mathtools package and its \DeclarePairedDelimiterXPP macro to create a user macro called \trace that satisfies your syntax requirements. With this setup,

• \trace may be used to generate basic-size square brackets,

• \trace* may be used to auto-size the square brackets, and

• \trace[\big] through \trace[\Bigg] are available to choose specific sizes of the square brackets directly.

\documentclass{article} % or some other suitable document class
\usepackage{mathtools}  % for '\DeclarePairedDelimiterXPP' macro
% See pp. 27f. of user guide of 'mathtools' package:
\DeclarePairedDelimiterXPP\trace[1]{\operatorname{\mathsf{trace}}}[]{}{#1}

\begin{document}
$\trace{A}$, $\trace[\big]{A^\top}$, $\trace*{A^\top}$, $\trace[\Bigg]{A^\top}$
\end{document}

• \operatorname{\mathsf{trace}} Feb 26 at 8:32
• @egreg - Many thanks for suggesting this improvement!
– Mico
Feb 26 at 8:41

You can define two new commands like in this code:

\documentclass[11pt,a4paper]{article}
\usepackage{amsmath}

\newcommand{\trace}[1]{\mathsf{trace}\!\left[#1\right]}
\newcommand{\traceb}[1]{\mathsf{trace}\!\Bigl[#1\Bigr]} %<-- Big not big
\begin{document}
$\traceb{A^\top}$ \qquad $\trace{A^\top}$

$\traceb{\dfrac{x+y}{x-y}}$ \qquad $\trace{\dfrac{x+y}{x-y}}$

$\traceb{\begin{matrix}a&b\\c&d\end{matrix}}$
\qquad  $\trace{\begin{matrix}a&b\\c&d\end{matrix}}$

\end{document}


The output is: