12

When should I use \DeclarePairedDelimiter and when should I use \newcommand?

For example I have defined

%Ceiling function
\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
%Floor function
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}

Is this the best way to do it?

1 Answer 1

16

\DeclarePairedDelimiter is a very specific command in the mathtools package for opening and closing delimiters (e.g. parentheses, brackets, braces) where you want the option to automatically resize them.

For example, it allows you to replace \lfloor...\rfloor with \floor{...} and \left\lfloor...\right\rfloor with \floor*{...}.

Other common examples are:

\DeclarePairedDelimiter\paren{(}{)}           % (parentheses)
\DeclarePairedDelimiter\ang{\langle}{\rangle} % <angle brackets>
\DeclarePairedDelimiter\abs{\lvert}{\rvert}   % |absolute value|
\DeclarePairedDelimiter\norm{\lVert}{\rVert}  % ||norm||
\DeclarePairedDelimiter\bkt{[}{]}             % [brackets]
\DeclarePairedDelimiter\set{\{}{\}}           % {braces}

As in the other example, I get commands that have fixed size, or that scale with the expression. See the mathtools documentation for more information about this command.

\newcommand, however, is much more general, allowing for a much broader class of shortcuts. For example, if I'm writing about combinatorics, then I would type combinatorics a lot. I could shorten this by writing \newcommand{\cbs}{combinatorics}, and then whenever I type \cbs in the document, it's expanded out to combinatorics. There's no need to relate it to delimiters. [1]

You can also declare commands with arguments: for example, \newcommand{\dfr}[2]{d #1/d #2} defines a command for derivatives; then, I can call \dfr{f}{x} to get df/dx and \dfr{f}{y} to get df/dy. See the Wikibooks page on macros for more information.

You can use \newcommand to make delimiters, e.g. \newcommand{\floor}[1]{\lfloor #1 \rfloor}, though then you don't get both a version that scales and one that doesn't. The point is that \newcommand is so much more general than \DeclarePairedDelimiter, but doesn't handle scaling automatically.

Finally, for, "is this the best way to do it?" I would say that since you want to use the functionality that \DeclarePairedDelimiter offers, you may as well use it, so yes, you're doing it right.


[1]: A more useful example would be to define a \newcommand for something that you might go back and change later. For example, if you have a piece of notation that you use a lot but haven't finalized, defining a \newcommand for it means when you change it, you only have to change it once. For example, in definitions, I italicize the word being defined, so I can use \newcommand{\term}[1]{\textit{#1}} and then call \term{word being defined}. Then, if later on I decide I want to underline definitions, all I have to do is update the \newcommand in a single place in the document.

3
  • 3
    I hope you mean \newcommand{\term}{\textit}, as you use it like \term{word being defined}. Dec 15, 2015 at 9:49
  • 1
    The macro \itshape is a switch and doesn't take an argument. Thus, you should define the \termmacro as follows: \newcommand{\term}[1]{{\itshape #1}}. (Note the extra pair of curly braces that limits the scope of \itshape.) A shortcoming (flaw?) of this definition is that one doesn't get an automatic italics correction at the end of the argument of \term. Thus, it's better to define the macro via \newcommand{\term}[1]{\textit{#1}}.
    – Mico
    Dec 15, 2015 at 10:08
  • @HenriMenke and Mico: whoops, I ought to have known that. Thank you for your corrections; I have edited my post to fix it. Dec 15, 2015 at 18:18

You must log in to answer this question.

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