TransWikia.com

Command to replace a variable inside angle bracket

TeX - LaTeX Asked by Quang Thinh Ha on June 1, 2021

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}

2 Answers

The standard LaTeX syntax would be

newcommanddotproduct[2]{langle#1,#2rangle}

then

 dotproduct{Q}{R}

Correct answer by David Carlisle on June 1, 2021

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}

Answered by Skillmon on June 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP