6

I would like to know how to pass out the output of one \newcommand to the input of another \newcommand. For example:

\newcommand{\bra}[1]{$\langle{#1}|$}
\newcommand{\comm}[2]{$ \left[{#1},{#2}\right]={#1}{#2}-{#2}{#1}$}

\comm{\bra{X},\bra{Y}} should return :

$\langle X|\langle Y| - \langle Y|\langle X|$

Seems simple enough - except I can not figure out the correct syntax or to find a similar question to work off of.

Thanks for your input.

Brian

1
  • 2
    If you do \newcommand{\comm}[2]{...} then you can't call this like you seem to do, but like \comm{\bra{X}}{\bra{Y}}. Leave out all the $, to begin with.
    – egreg
    Commented Dec 21, 2014 at 16:57

2 Answers 2

5

If you want to use a comma-separated list of arguments for your \comm you could do this:

\documentclass{article}
\newcommand{\bra}[1]{\langle{#1}|}
\newcommand{\comm}[1]{\tempcomm(#1)}
\def\tempcomm(#1,#2){\left[{#1},{#2}\right]={#1}{#2}-{#2}{#1}}

\begin{document}
\[
\comm{\bra{X},\bra{Y}}
\]
\end{document} 

enter image description here

2

A possible definition would be

\newcommand{\bra}[1]{\langle#1\mathclose{|}}
\newcommand{\comm}[2]{[#1,#2]=#1#2-#2#1}

Example

\documentclass{article}

\newcommand{\bra}[1]{\langle#1\mathclose{|}}
\newcommand{\comm}[2]{[#1,#2]=#1#2-#2#1}

\begin{document}

$\bra{X}$

$\comm{X}{Y}$

$\comm{\bra{X}}{\bra{Y}}$

\end{document}

enter image description here

Note that when you do

\newcommand{\foo}[2]{...#1...#2...}

you have to call it as

\foo{first}{second}

and not as \foo{first,second}. You shouldn't be using $ in the definitions of those commands, which are supposed to be used in a formula.

There are better ways for this, however.

\documentclass{article}

\usepackage{mathtools}

\DeclarePairedDelimiter{\bra}{\langle}{|}
\DeclarePairedDelimiterX{\comm}[2]{[}{]}{#1,#2}
\newcommand{\commx}[2]{#1#2-#2#1}

\begin{document}

$\bra{X}$

$\comm{X}{Y}=\commx{X}{Y}$

$\comm[\big]{\bra{X}}{\bra{Y}}=\commx{\bra{X}}{\bra{Y}}$

\end{document}

Note that \comm and \bra accept an optional argument for changing the delimiters' size; also you can use \comm* and \bra* for automatic sizing (use with care).

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .