# Command to replace a variable inside angle bracket

In my current document, I have a lot of angle brackets, which are computed via:

\langle a, b \rangle


Is there anyway I could create a new command, something in the look of:

\newcommand{\dotproduct{ab}}{\langle a, b \rangle}


so that I could replace the a and b by other variables? To be more specific, I would love for that command template to work also for:

\dotproduct{cd}, \dotproduct{Q,R}

• \dotproduct{a,b} seems much easier to parse. – egreg Aug 18 '20 at 20:48
• why not simply \newcommand\dotproduct[2]{\langle#1,#2\rangle} then \dotproduct{Q}{R} ? – David Carlisle Aug 18 '20 at 20:58
• @DavidCarlisle I like your suggestion. If you could turn that into an answer I would accept it happily :) – Quang Thinh Ha Aug 18 '20 at 21:20

The standard LaTeX syntax would be

\newcommand\dotproduct[2]{\langle#1,#2\rangle}


then

 \dotproduct{Q}{R}


I'd prefer the syntax suggested by @DavidCarlisle.

If you still want your syntax (which is imho unclean) this macro will test whether your argument contains a comma, if so it just uses it as is, if not it will put a comma after the first item of your input.

\documentclass[]{article}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand \dotproduct { m }
{
\langle
\tl_if_in:nnTF { #1 } { , }
{ #1 }
{ \tl_head:n { #1 } , \tl_tail:n { #1 } }
\rangle
}
\ExplSyntaxOff

\begin{document}
$\dotproduct{a,b} \dotproduct{cd} \dotproduct{abc}$
\end{document}