31

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?

  • 2
    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
35

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}

output2

| improve this answer | |
  • 1
    @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
  • 1
    The complete Section 2 shows automatic scaling. – Thorsten Donig Dec 8 '13 at 19:51
25

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.

| improve this answer | |
  • that's exactly what I was looking for. Cheers! – Gottfried Oct 26 '13 at 12:55
  • 5
    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
  • 1
    @SvendTveskæg - I'm aware of the mathtools package and its \DeclarePairedDelimiter macro. The OP was asking specifically for a solution using \newcommand -- presumably in part so that he/she could learn how to use this method -- so that's why I provided a solution that involves only \newcommand and no additional package. :-) – Mico Oct 26 '13 at 13:39
  • 1
    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
18

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.

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

enter image description here

| improve this answer | |
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.