# How to declare an operator with automatic argument bracketing

I'd like to have an easy way to declare an operator that brackets its argument with a particular delimiter. As an example I'd like to be able to write

\Pr{X}


to mean the same as

\operatorname{Pr}\left[X\right]


FYI I know how to do

\newcommand{\Pr}[1]{\operatorname{Pr}\left[#1\right]}


but I thought I once saw a package that provided some command like

\DeclareBracketedOperator{\Pr}{Pr}{[}{]}


sort of a combination of \DeclarePairedDelimiter and \DeclareMathOperator and I think it also defined a starred version of the operator that didn't take any argument and didn't produce the delimiters.

-

I don't remember of any such package. Your \Pr command maybe obtained by

\usepackage{mathtools}
\DeclarePairedDelimiter{\Prfences}{[}{]}
\renewcommand{\Pr}{\operatorname{Pr}\Prfences}


With this definition, \Pr would behave exactly as if it was defined with \DeclarePairedDelimiter; that is, the *-form would use \left and \right.

If you want to always use \left and \right, you might follow Werner's suggestion, or go the hard way:

\makeatletter
\DeclareRobustCommand{\Pr}{\operatorname{Pr}\@ifstar\@firstofone\@Pr}
\newcommand{\@Pr}[1]{\left[#1\right]}
\makeatother


(requires amsmath, of course). Note: \renewcommand{\Pr}{...} is necessary, because \Pr is already defined in LaTeX.

-

Here is a way to define your own operator with an optional bracketing:

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{xparse}% http://ctan.org/pkg/xparse
\DeclareDocumentCommand{\Pr}{s m}{% \Pr[*]{..}
\operatorname{Pr}%
\IfBooleanTF{#1}% Condition on *
{#2}% Print only the argument in starred * version
{\left[#2\right]}% Print bracketed argument [ ] in unstarred version
}%
\begin{document}

This is $\Pr{2}$, and here is
$\Pr{\frac{\frac{a}{b+c}}{d-e}}=\Pr*{\frac{8}{9}}.$
\end{document}


The above code defines the macro \Pr{..} (actually redefines \Pr, since it is provided by amsmath), but also provides a starred * version. The unstarred version, as requested, prints its argument in extensible brackets, while the starred * version removes these brackets.

The xparse package provides an easy interface to specify starred versions of commands and environments. The xifthen package provides conditional support.

-
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 2, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.9450895190238953, "perplexity": 3555.829255978351}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2014-10/segments/1393999669442/warc/CC-MAIN-20140305060749-00016-ip-10-183-142-35.ec2.internal.warc.gz"}