2

I am wondering how to define a new command A with another new command B (which has one argument x) as the argument of A.

Here is a simple example:

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}
\begin{document}
\newcommand\ket[1][usedefault, addprefix=\global, 1=1]{|#1>}
\newcommand\hc[1][usedefault, addprefix=\global, 1=2]{Hc(#1)}
\newcommand\kc[1][usedefault, addprefix=\global, 1=3]{\ket[\hc[#1]]}

$$\kc[5]$$

\end{document}

The result I wanted is |Hc(5)>, but in fact it showed |Hc(5>). Very weird.

What is wrong with the above code?

13
  • Apparently you're after some bra ket state vector notation?
    – user31729
    Commented Mar 21, 2016 at 20:23
  • Please don't use $$...$$ as well, that's outdated -- Use \[...\] instead.
    – user31729
    Commented Mar 21, 2016 at 20:25
  • Thanks for the suggestion, Christian. And yes, I was after the state vector notation. I wrote it in a simple way in the code just for convenience. Commented Mar 21, 2016 at 20:28
  • Is there some situation where you want LaTeX to typeset the text "usedefault, addprefix=\global, 1=1"? That's what happens if you do \ket with no argument. Why do you have this as an optional argument? (Why not just make it \newcommand{\kc}[1]{\ket{\hx{#1}}} and then \[\k{5}\]?) Commented Mar 21, 2016 at 20:34
  • @AndrewCashner: I suppose those options come from same package not used here
    – user31729
    Commented Mar 21, 2016 at 20:36

1 Answer 1

3

You have to group (well delimit rather) the inner command, that has an optional argument , with {...}, otherwise the whole command breaks apart and the contents of the arguments are typeset in the wrong order!

Note: There is a braket package for such Dirac/Hilbert state vectors.

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}
\begin{document}
\newcommand\ket[1][usedefault, addprefix=\global, 1=1]{|#1\rangle}
\newcommand\hc[1][usedefault, addprefix=\global, 1=2]{Hc(#1)}
\newcommand\kc[1][usedefault, addprefix=\global, 1=3]{\ket[{\hc[#1]}]}

\[\kc[5]\]

\end{document}

enter image description here

6
  • Perhaps the Hc content should be in upright letters, but I leave this to the design/math content aficinados :-P
    – user31729
    Commented Mar 21, 2016 at 20:32
  • That works!! Thanks, Christian. By the way, I am curious how the LaTex executes the original code without {} to generate the weired result |Hc(5>)? Commented Mar 21, 2016 at 20:32
  • I cannot understand why '>' and ')' exchanges the order while they are originally in two different commands. Commented Mar 21, 2016 at 20:34
  • 2
    @QuantumPhysics: The reason is that in your version the \@testopt command (hidden internally) reads too far, beyond the delimiting inner [...] command, i.e. it reads the > first, assuming that this belongs to the content of the inner [...] pair. My version groups this inner command and forces LaTeX totreat this a whole argument (it's delimited), then expands the inner \hc command` and after that fetches the next token (the > character in this case)
    – user31729
    Commented Mar 21, 2016 at 20:59
  • 1
    @QuantumPhysics: You should never compare TeX/LaTeX with a language like C -- Yes, it does not work like C . And please consider to accept my answer if the code is working then.
    – user31729
    Commented Mar 21, 2016 at 21:29

You must log in to answer this question.

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