10

This has most likely been asked before, but I wasn't able to articulate the question in a way that yielded relevant search results.

How do you declare a math operator to simplify an expression such as \langle x, y \rangle? I want to be able to define something like \innerproduct{x, y} instead.

14

I suggest you load the mathtools package and use its \DeclarePairedDelimiter macro to define a macro called, say, \inprod. This method automatically declares a companion macro called \inprod*, which auto-sizes the angle brackets. The size of the "fences" can be controlled directly as well, by writing \inprod[\big]{...}, \inprod[\Big]{...}, etc.

enter image description here

\documentclass{article} 
\usepackage{mathtools}
\DeclarePairedDelimiter\inprod\langle\rangle
\begin{document}

$\inprod{a,b}$ 
$\displaystyle \inprod*{\frac{a}{b},\frac{c}{d}}$

\end{document}
5

A variant of the mathtools solution, which takes care of empty arguments, with the xparse and etoolbox packages. It uses two arguments, separated by a comma; if some argument is empty, it is replaced with a dot:

\documentclass{article}
\usepackage{mathtools, nccmath}
\usepackage{xparse, etoolbox}

\DeclarePairedDelimiterX{\innerp}[1]{\langle}{\rangle}{\innpargs{#1}}
\NewDocumentCommand{\innpargs}{>{\SplitArgument{1}{,}}m}
{\innpargsaux#1}
\NewDocumentCommand{\innpargsaux}{mm}
{\ifblank{#1}%
{\ifblank{#2}{\,\cdot\,{,}\,\cdot\,}{{\,\cdot \,}{,}{\mkern2mu#2}}}%
{{#1\,}{,}\ifblank{#2}{\,\cdot\,}{\mkern2mu#2}}%
}%

%}%
\begin{document}

\[ \innerp{x, y}, \quad \innerp[\big]{\tfrac{1}{2}x, y} \quad \innerp*{\frac{x}{2}, y}\quad\innerp{x , }\quad \innerp{ , y}\quad \innerp{ , }\]

\end{document} 

enter image description here

2

In fact I remember a conversation about precisely this, but it may take me a while to find it. In the meantime:

\documentclass{article}
\def\iprelay#1,#2{\langle#1,#2\rangle}
\def\ip#1{\iprelay#1}
\begin{document}
\[
\ip{a,b}
\]
\end{document}
  • \def\iprelay#1,#2\relax{\langle#1,#2\rangle} \def\ip#1{\iprelay#1\relax} would be much safer. – Manuel Mar 13 '18 at 0:03

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.