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?
-
2Happy 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
3 Answers
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,\mathopen{}#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}

- 31,033
-
1@Gottfried: All this stuff is provided by the »physics« package. – Thorsten Donig Dec 08 '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 08 '13 at 15:25
-
1
-
@ThorstenDonig It seems that using the
physicspackage will result in very poor spacing. – Svend Tveskæg Jul 18 '23 at 22:43 -
I like how you defined practically every common delimiter command except the one that was requested. – Gaussler Jul 19 '23 at 07:40
A straightforward way to define such a macro would be
\newcommand{\ang}[1]{\langle #1 \rangle}
If you need the sizes of the angle brackets to grow with the size of the argument, you might want to define \ang as
\newcommand{\ang}[1]{\mleft\langle #1 \mright\rangle}
where \mleft and \mright are macros provided by the mleftright package. Using \mleft and \mright instead of \left and \right has certain advantages, specifically in terms of the whitespace padding that would be inserted by TeX.
Either way, \ang takes one argument, which will be typeset surrounded by left-hand and right-hand angle brackets.
- 506,678
-
-
6This is good/correct/..., but I perfer
mathtool's solution; see http://tex.stackexchange.com/a/140770/15874. – Svend Tveskæg Oct 26 '13 at 13:21 -
1@SvendTveskæg - I'm aware of the
mathtoolspackage and its\DeclarePairedDelimitermacro. 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\newcommandand no additional package. :-) – Mico Oct 26 '13 at 13:39 -
1I 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\newcommandmust be the way. Both answers solved my inquiry, needless to say! – Gottfried Oct 30 '13 at 23:27 -
You might want to replace
\left...\rightby either\mleft...\mrightfrom themleftrightpackage or an equivalent construction. (Or discourage the use of automatic scaling altogether. ) – Gaussler Jul 19 '23 at 07:43 -
1
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}

- 31,033
- 539
-
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