# Defining a new function with argument in LaTeX environment

I want to define a function in a LaTeX document. More precisely, I want to define a function, say \ang{x}, where x is any input, such that the output is \rangle x \langle. Is there a way to go about this using \newcommand, or any way at all, for that matter?

• Happy to be here! – Gottfried Oct 26 '13 at 13:09
• I sincerely apologize for it taking me so long! – Gottfried Oct 30 '13 at 23:18

I normally use \DeclarePairedDelimiter from mathtools for something like this. The point is that with the starred version, the delimiters automatically scale.

You can use a lot of different brackets, and you can make the macro take multiple arguments.

\documentclass{article}

\usepackage{mathtools}
\DeclarePairedDelimiter\bra{\langle}{\rvert}
\DeclarePairedDelimiter\ket{\lvert}{\rangle}
\DeclarePairedDelimiterX\braket[2]{\langle}{\rangle}{#1 \delimsize\vert #2}
\DeclarePairedDelimiterX\inner[2]{\langle}{\rangle}{#1,#2}
\DeclarePairedDelimiter\abs{\lvert}{\rvert}
\DeclarePairedDelimiter\norm{\lVert}{\rVert}
\DeclarePairedDelimiter\set{\lbrace}{\rbrace}

\begin{document}

\begin{align*}
\bra{a}       &= \bra*{\frac{a}{1}}\\
\ket{a}       &= \ket*{\frac{a}{1}}\\
\braket{a}{b} &= \braket*{\frac{a}{1}}{\frac{b}{1}}\\
\inner{a}{b}  &= \inner*{\frac{a}{1}}{\frac{b}{1}}\\
\abs{a}       &= \abs*{\frac{a}{1}}\\
\norm{a}      &= \norm*{\frac{a}{1}}\\
\set{a,b}     &= \set*{\frac{a}{1},\frac{b}{1}}
\end{align*}

\end{document}


• @Gottfried: All this stuff is provided by the »physics« package. – Thorsten Donig Dec 8 '13 at 12:04
• @ThorstenDonig I could be missing something, but I don't see the automatic scaling of delimiters in the manual. – Svend Tveskæg Dec 8 '13 at 15:25
• The complete Section 2 shows automatic scaling. – Thorsten Donig Dec 8 '13 at 19:51

A straightforward way to define such a macro would be

\newcommand{\ang}[1]{\langle #1 \rangle}


If you needed the angle brackets to "grow" with the size of the argument, you might want to define it as

\newcommand{\ang}[1]{\left\langle #1 \right\rangle}


Either way, the macro takes one argument, which will be typeset surrounded by left and right angle brackets.

• that's exactly what I was looking for. Cheers! – Gottfried Oct 26 '13 at 12:55
• This is good/correct/..., but I perfer mathtool's solution; see tex.stackexchange.com/a/140770/15874. – Svend Tveskæg Oct 26 '13 at 13:21
• I have a feeling that this is because the OP wasn't aware of the other possibility. To quote a part of the question "...or any way at all, for that matter". :) – Svend Tveskæg Oct 26 '13 at 13:41
• Well, I was not aware of \DeclarePairedDelimiter, and I reasoned that \newcommand must be the way. Both answers solved my inquiry, needless to say! – Gottfried Oct 30 '13 at 23:27

You might want to have a look at the bropd package.

It features the br{} command that is a little more advanced. I matches the size of the brackets and automatically uses round, square, or curly brackets.

$$\frac{1}{2}\br{f\cdot\br{x \br{y+z} + \br{\frac{a}{b}+c} }+g}$$


• Neat! I wasn't aware of this package. Does it "do" angle brackets as well? – Mico Oct 26 '13 at 13:11
• Yeah, one of my favorite packages. No more forgetting closing your brackets! The features for differentials are also a huge timesaver, especially when typesetting stuff like thermodynamics. It doesn't feature angle brackets. I have actually never seen them as a way to structure equations but rather as a way to denote the mean of a quantity. – frankundfrei Oct 26 '13 at 13:20