3

I am working on a Quarto book rendered both as HTML and as pdf. The HTML consists of multiple pages with separate qmd files. A set of LaTeX macros, defined via \newcommands, should be used across the whole book. LaTeX is rendered with MathJax.

Tried three different approaches:

A – Insert the macros towards the beginning of each qmd file. This way the macros are correctly displayed when used, but there are two drawbacks: repetition of the macro definitions (and they are many) in each qmd file, and XeTeX complaining of already-defined commands.

B – Define the macros in a separate macros.qmd file, and call that common file at the beginning of the other qmd ones by inserting

{{< include macros.qmd >}}

in each of them, as explained in the Quarto guide. This succeeds in producing the macros correctly in the HTML, but XeTeX still complains about already defined commands. – I suppose replacing \newcommand with \providecommand could solve this.

C – Call the macros.qmd file using include-in-body: or similar options in the _quarto.yml file, as suggested in the answers to this, this, and this questions. For example:

format:
  html:
    include-in-body: macros.qmd

or

format:
  html:
    include-in-body:
      - text: {{< include macros.qmd >}}

But the renderer does not seem to load those macros definitions (and XeTeX doesn't see them either).

I wonder if there is a simple solution that works for both HTML and XeTeX and avoids text redundancies (on the author's side) such as repeated definitions or calls to external files. My impression is that many authors using Quarto for maths/physics would face this problem.

pglpm
  • 516
  • 4
  • 14

1 Answers1

2

I would use your option C, but with include-in-header, e.g.

project:
  type: book

book:
  title: "testbook"
  author: "Jane Doe"
  date: "23.11.2022"
  chapters:
    - index.qmd
    - intro.qmd

format: 
    html:
      include-in-header:
         - file: macros.html
    pdf:
      include-in-header:
         - text: |
               \newcommand{\foo}{E=mc^{2}}

with macros.qmd:

---
format: html
---

::: {.hidden}
$$
\newcommand{\foo}{E=mc^{2}}
$$
:::
Julian
  • 6,586
  • 2
  • 9
  • 33
  • Thank you for the suggestion! I'm following this to the letter but with `macros.qmd` in the header (I suppose that's a typo). What happens is that I just see `-- format: html --- ::: {.hidden} ` and so on at the beginning of the rendered html page. I don't quite know what's causing this. Same behaviour if I use a file `macros.html` instead, and also if I remove the chosen theme `cosmo`. – pglpm Jun 04 '23 at 16:37
  • 1
    I had the same thing, when I used `macros.qmd`, but rendering it once to html and then using it as `macros.html` worked for me. – Julian Jun 05 '23 at 08:11
  • 1
    Thank you, I'll try that and report back. – pglpm Jun 05 '23 at 10:04
  • Did you have any success? – Julian Jun 12 '23 at 15:07
  • For some reason your setup doesn't work in my case. There seem to be several independent problems. One is that the `\newcommand`s only work if they are *not* within `$$`. I'll file a report on the Quarto website about this. But mainly there's the inconvenience of having to specify each command twice, once for the html and once for the tex. So for the moment I'm using strategy B in my question. – pglpm Jun 13 '23 at 05:36