Latex macro in bookdown theorem

Viewed 177

I am trying to use some latex macro in a bookdown project (which is an amazing technology!).

I would like to write in the text (and in the theorem environment) symbols like |x>, for which in normal latex document I would get using the package braket. In practice, in a latex file I would write

\usepackage{braket}

I don't understand why, but adding in the preamble \usepackage{braket} does not work, as $\ket{x}$ does not get rendered correctly in the text.

To circumvent this issue I got the idea of adding some macros, at the beginning of the markdown file.

\DeclarePairedDelimiter\bra{\langle}{\rvert}
\DeclarePairedDelimiter\ket{\lvert}{\rangle} 
\DeclarePairedDelimiterX\braket[2]{\langle}{\rangle}{#1 \delimsize\vert #2} 

Unfortunately, it does not solve the issue. In both cases, I get the following error:

[WARNING] Could not convert TeX math '\ket{\psi}', rendering as TeX:
  \ket{\psi}
      ^
  unexpected "{"
  expecting "%", "\\label", "\\nonumber" or whitespace

Any idea on how to solve this problem? Thanks.

1 Answers

You have to put the LaTeX code into a separate .tex file, which you then provide to the YAML option includes.

Example:

add_packages.tex

\usepackage{braket}

index.Rmd

--- 
output:
  bookdown::pdf_book:
    includes:
      in_header: add_packages.tex
documentclass: book
site: bookdown::bookdown_site
---

# Let's test `braket`

$$\bra{\Psi}\ket{\Psi}$$

enter image description here

See Yihui's documentation of the bookdown package for more.

Related