# How can I typeset this mathematical expression?

How can I typeset this mathematical expression?

• \langle w, \tilde{w} \rangle? :) – Paulo Cereda Apr 22 '14 at 23:26
• @PauloCereda Does the :) go inside or outside the dollar delimiters? 8^b – Steven B. Segletes Apr 22 '14 at 23:51
• @Steven: ooh I think we might end up with some incorret kerning. :) – Paulo Cereda Apr 23 '14 at 0:26

If it's a “one shot”, then

$\langle w,\tilde{w}\rangle$


will be sufficient. If you have several inner products to typeset it is surely better to define a personal command for them; there are several possibilities. The simplest one is

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


to be called as

$\innp{w,\tilde{w}}$
$\innp{x,y}$


Defining a command is really recommended. If you change your mind and decide that inner products should be typeset like

(x | y)


you'd have to hunt through your document in order to find out the instances of \langle and retype the correct code: the chances of errors are very high. Even if you're really convinced about the notation, using a new command is better, because, for instance, you can search for all inner products very easily.

Now let's try our hand at a more complicated definition.

\documentclass{article}
\usepackage{mathtools}
\usepackage{xparse}

\DeclarePairedDelimiter{\norm}{\lVert}{\rVert}

% first version
\DeclarePairedDelimiterX{\innp}[1]{\langle}{\rangle}{#1}

\begin{document}

$\innp{w,\tilde{w}}$
$\innp[\Big]{w,\frac{w}{\norm{w}}}$

\end{document}


Now, suppose you change your mind and want the alternative notation. Here's how you can do it:

\documentclass{article}
\usepackage{mathtools}
\usepackage{xparse}

\DeclarePairedDelimiter{\norm}{\lVert}{\rVert}

% second version
\DeclarePairedDelimiterX{\innp}[1]{(}{)}{\innpargs{#1}}
\NewDocumentCommand{\innpargs}{ >{\SplitArgument{1}{,}}m }
{\innpargsaux#1}
\NewDocumentCommand{\innpargsaux}{ m m }
{#1\;\delimsize\vert\;#2}

\begin{document}

$\innp{w,\tilde{w}}$
$\innp[\Big]{w,\frac{w}{\norm{w}}}$

\end{document}


Just a modified definition of \innp allows you to completely change the appearance of the inner products.

### Some note on the second version

We want to keep the very convenient input form

\innp{x,y}


but still exploit the features in mathtools. With

\DeclarePairedDelimiterX{\innp}[2]{(}{)}{#1\;\delimsize\vert\;#2}


we would be forced to change syntax into \innp{x}{y}. But we can use xparse: with

\DeclarePairedDelimiterX{\innp}[1]{(}{)}{\innpargs{#1}}


we set up the delimiter interface of mathtools; now with

\NewDocumentCommand{\innpargs}{ >{\SplitArgument{1}{,}}m }
{\innpargsaux#1}


we tell xparse to take the argument to \innpargs and split into two pieces at the comma; these will become the two arguments for \innpargsaux, which finally does what we'd like to, like in the two argument form discussed before.

• Please explain the second macro in more details. This looks very elegant and I would like to understand what's going on. – Svend Tveskæg Apr 23 '14 at 14:02
• @SvendTveskæg I added some explanations. – egreg Apr 23 '14 at 16:12
• @egreg: beautiful! I'll adapt at once my macros using DeclarePairedDelimitersX. I really learnt something important! – Bernard Apr 24 '14 at 2:49

To have a correct horizontal spacing and delimiters that adapt to the size of what's inside, you can use the mathtools package (it loads and extends amsmath). You have commands to define "paired delimiters" such as a scalar (inner) product:

\DeclarePairedDelimiterX\innerp[2]{\langle}{\rangle}{#1\,,\,#2}


Then you'll have two versions for inner products: \innerp* will internally add a \left and a \right before \langle and \rangle respectively. For fine-tuning of the size of delimiters, \innerp (without a star) accepts an optional argument \big, \Big, &c.

In your case it should be used as \innerp{w}{\tilde w}. Of course you can define a shortcut command:

\newcommand\foo[1]{\innerp{#1}{\widetilde{#1}}


and simply write \foo{w}.

• I thought that this was just a simple command! – Adam Apr 22 '14 at 23:42
• It can be, if you're not very fastidious.;) – Bernard Apr 22 '14 at 23:46
• @Bernard, does innerp have 1 or 2 arguments? See your foo definition. – Sigur Apr 23 '14 at 0:19
• @Sigur: Thanks for pointing me the error. Iw as so much of thinking mathematically that it took me a couple of minutes to understand what the problem was. – Bernard Apr 23 '14 at 0:29
• @Adam I love LaTeX—I really, really do—but it's times like these that teach me you can make anything complicated if you try hard enough. :) – Sean Allred Apr 23 '14 at 1:26

You can try the physics package. It provides quite a lot of macros that are useful. The following code does what you want and in my opinion, it is quite simple :-)

\documentclass{article}

\usepackage{mathtools}
\usepackage{physics}

\begin{document}
$\ev{w,\tilde{w}}.$
\end{document}


Here \ev stands for expectation value.