6

The braket package defines Dirac's braket notation. To use it in Lyx, I add the line:

\usepackage{braket}

to the LaTeX preamble. Then Lyx will be able to compile commands like \bra and \ket. Problem is that the Lyx editor itself won't recognize this commands.

If instead of using the braket package I had defined math-macros for the commands \bra, \ket, etc., I could tell Lyx how these things should look in the editor. That is, I can tell it to put an \rangle before the argument (that Lyx knows how to draw) and a | after.

Is there a way to use the \bra, \ket, etc., commands from the braket package, and still be able to tell Lyx how these should look in the editor?

EDIT: Right after posting, I discovered this question, which mentions the Instant Preview feature of Lyx. Although this kind of fixes my issue, it's not exactly what I want. I want to be able to specify what the command should look like in the editor, in terms of other commands that Lyx already recognizes (in the editor), like I would do with a math-macro.

6
  • If I recall correctly, there are two fields when you define a new macro from within LyX, one for LyX's preview and one for TeX's use. Does this have the effect that I remember? Commented Jun 7, 2013 at 0:37
  • @SeanAllred That's right. Two fields in a math-macro in Lyx. The problem is that if I want to use the braket package, that means that the LaTeX command \ket is already defined, so I can't create a macro \ket in this case. I could define a command with another name, like \ket2, but that seems too ugly... isn't there a better way?
    – a06e
    Commented Jun 7, 2013 at 2:54
  • 1
    You may be able to \let\realket\ket and then renew ket to be real ket. Commented Jun 7, 2013 at 21:56
  • @SeanAllred That works! I put the \let\realket\ket command in the LaTeX preamble. I had not seen \let before. It defines a synonym?
    – a06e
    Commented Jun 8, 2013 at 17:31
  • @SeanAllred Maybe you could post an answer.
    – a06e
    Commented Jun 8, 2013 at 17:31

2 Answers 2

3

Place \let\realket\ket in your preamble, and then redefine \ket using LyX's new command feature, providing LyX with the TeX code to use and the LyX code to display.


\let, like \def, takes two arguments. When you say \let\realket\ket, you instruct TeX to copy the definition of \ket into \realket. This differs from \def in as much as \ket isn't itself mentioned in the definition of \realket (IIRC, it's a one-level-deep copy), whereas \def\realket\ket would simply have \realket 'pointing to' \ket, where \kets definition could change at any time. Since the definition of \ket from braket is saved into \realket, you are free to redefine \ket to whatever you please :)

3
  • @becko You're most welcome :) Commented Jun 9, 2013 at 2:23
  • Could you elaborate on how to use LyX's new command feature in this context? Do I still have to define a macro in LyX or can I just \renewcommand{\ket}[1]{\realket{\#1}}? The latter did not work for me... Commented Jul 11, 2022 at 14:47
  • @Suppenkasper At this point, you should post a new question js provide a link back to this one for context. Commented Jul 11, 2022 at 16:13
1

This inconvenience is finally solved in From LyX-2.4.0-beta3 (format 616). You can just fill the LyX box to define how the macro displays in LyX and leave the TeX box empty, and things will work as expected.

lyx macro screenshot

As written in the LyX source code:

* Format incremented to 616: Do not output LaTeX for a macro if the LaTeX part is empty
- This allows the user to define the LyX display for LaTeX commands
  that are already defined (e.g., in user preamble or a package).

You must log in to answer this question.

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