Global TeX Macro file #12838
Replies: 4 comments 6 replies
-
Thanks a lot for this great summary with a lot to think about.
I do believe that this is Pandoc's current behavior. So with Pandoc itself, I think the problem will be the same - the following content
will need to be included in all source files rendered by Pandoc. Quarto's Better support in Pandoc itself may be required to go further. But that is just first thought. Again, thank you. |
Beta Was this translation helpful? Give feedback.
-
For reference and information, for macros there is the |
Beta Was this translation helpful? Give feedback.
-
One may hope that the Code Insertion Extension may help reduce the boilerplate, but the inserted code appears to not be processed but instead inserted raw. So I could not get this to work to insert the |
Beta Was this translation helpful? Give feedback.
-
Another hope may be that you could use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
LaTeX writers typically learn to use macros. And this makes sense: you can replace formatting instructions with semantic macros. For example
How LaTeX macros interact with quarto
Quarto consists of a bunch of technologies that result in a complicated interaction
\newcommand
macros itself. This interaction only happens if\newcommand
is placed outside the typicalTeX
fences$$
and pandoc is applied to the file. This is only the case if the file is converted by pandoc, e.g. when it is a.qmd
file.mathjax
can also deal with\newcommand
s but the\newcomand
have to be read before the macro obviously so one has to make sure that the execution order is correct. One can see the issues when this is used in a forum like math stackexchange: If you add a\newcommand
in the text field and later remove it, it is "sticky" since mathjax already executed it.KaTeX
assumes every equation delimited by$$
as independent for speed and is therefore unable to deal with\newcommand
s defined in a different equation.LaTeX
does not like\newcommand
used twice for the same macro and throws a compilererror in this case. A redefinition is usually achieved with\renewcommand
. Another alternative is\providecommand
which only defines the macro if it does not exist yet.TL;DR: Ideally, pandoc processes the macros such that
mathjax
/KaTeX
do not have to deal with them. If pandoc does not process the macros, then onlymathjax
can be used. Forhtml
the macros need to be included in every file, whearas forLaTeX
they should only be included once.Ways to include snippets in quarto
There are also multiple ways to include snippets in quarto. Let's assume we are working with a book type of project. Then there is a global
_quarto.yml
configuration file and multiple.qmd
chapter files.include-in-header
,include-before-body
in the_quarto.yml
configuration file for thehtml
format (similar for the pdf format). It is important to note that every such include has the note "Include contents of file, verbatim" so\newcommand
macros.tex
or.html
and are therefore not converted by pandoc. Pandoc will therefore not convert\newcommand
s in them. Furthermore it is only possible to replace existing templates or template partials and not possible to introduce new ones.qmd
file one can use Includes of the form{{< include _macros.qmd >}}
Neither of these options is really suited to what we want to do: The first two do not interact nicely with the latex_macros extension and in both cases we would have to mix the macros with other stuff. In the first case, we would have to mix the macros with the rest of the
_quarto.yml
configuration file (because we cannot include another file) and in the second we would have to mix it with the rest of the template because we cannot create a new blank template just for the macros.This essentially only leaves the last option for the
html
target.Best method (to my knowledge) at the moment
_macros.qmd
file. Only use\newcommand
in it (no markdown), it should also be valid as a.tex
file!before-title.tex
template partial (this is a predefined partial that is empty by default) with the following content_macros.qmd
is also valid as a.tex
file..qmd
chapter file include at the beginning:::: {.content-hidden} {{< include _macros.qmd >}} :::
html
page, while the.content-hidden
property ensures it is removed from theTeX
output. This ensures that\newcommand
is not used multiple times for the same macro.Pros & Cons
The strategy above
\newcommand
macros because the macros are in a.qmd
fileTeX
andhtml
However, it has the drawbacks:
.qmd
chapter file_macros.qmd
file to explain macros because.tex
comments would break.qmd
syntax and therefore thehtml
target whereashtml
comments would break.tex
syntax and therefore the\include
used to import the macros into thetex
target.Alternative
The user baptiste had another approach (#7518 (comment)) that may give you the ability to use comments:
Instead of a
_macros.qmd
file, use a_macros.tex
file and change the\include
in thebefore-title.tex
Create the following
_macros.qmd
filelatexpand
is bundled with TexLive (which is the most common tex distribution) so it is likely pre-installed.The following is a python version, but I cannot find a way to suppress the output when the target is not html, i.e. it appears there is no corresponding command to
knitr::is_html_output()
in python so you have to deal with WARNINGS about repeated\newcommands
the file
tex_comment_strip.py
should be filled with the contents of https://gist.github.com/dzhuang/dc34cdd7efa43e5ecc1dc981cc906c85 or similar. The script in question also requires https://github.com/dabeaz/ply. Alternatively one can of course also calllatexpand
from python.Discussion
The boilerplate necessary to get global macros is seriously annoying. Every file requiring
is not very elegant. Ideally, there would be a way to have a
include-in-header
that is not treated like raw text. Perhaps global macros should also be a first class citizen and get its ownyaml
argument.References
Beta Was this translation helpful? Give feedback.
All reactions