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
\newcommandmacros itself. This interaction only happens if\newcommandis placed outside the typicalTeXfences$$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.qmdfile.mathjaxcan also deal with\newcommands but the\newcomandhave 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\newcommandin the text field and later remove it, it is "sticky" since mathjax already executed it.KaTeXassumes every equation delimited by$$as independent for speed and is therefore unable to deal with\newcommands defined in a different equation.LaTeXdoes not like\newcommandused twice for the same macro and throws a compilererror in this case. A redefinition is usually achieved with\renewcommand. Another alternative is\providecommandwhich only defines the macro if it does not exist yet.TL;DR: Ideally, pandoc processes the macros such that
mathjax/KaTeXdo not have to deal with them. If pandoc does not process the macros, then onlymathjaxcan be used. Forhtmlthe macros need to be included in every file, whearas forLaTeXthey 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.ymlconfiguration file and multiple.qmdchapter files.include-in-header,include-before-bodyin the_quarto.ymlconfiguration file for thehtmlformat (similar for the pdf format). It is important to note that every such include has the note "Include contents of file, verbatim" so\newcommandmacros.texor.htmland are therefore not converted by pandoc. Pandoc will therefore not convert\newcommands in them. Furthermore it is only possible to replace existing templates or template partials and not possible to introduce new ones.qmdfile 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.ymlconfiguration 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
htmltarget.Best method (to my knowledge) at the moment
_macros.qmdfile. Only use\newcommandin it (no markdown), it should also be valid as a.texfile!before-title.textemplate partial (this is a predefined partial that is empty by default) with the following content_macros.qmdis also valid as a.texfile..qmdchapter file include at the beginning:::: {.content-hidden} {{< include _macros.qmd >}} :::htmlpage, while the.content-hiddenproperty ensures it is removed from theTeXoutput. This ensures that\newcommandis not used multiple times for the same macro.Pros & Cons
The strategy above
\newcommandmacros because the macros are in a.qmdfileTeXandhtmlHowever, it has the drawbacks:
.qmdchapter file_macros.qmdfile to explain macros because.texcomments would break.qmdsyntax and therefore thehtmltarget whereashtmlcomments would break.texsyntax and therefore the\includeused to import the macros into thetextarget.Alternative
The user baptiste had another approach (#7518 (comment)) that may give you the ability to use comments:
Instead of a
_macros.qmdfile, use a_macros.texfile and change the\includein thebefore-title.texCreate the following
_macros.qmdfilelatexpandis 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\newcommandsthe file
tex_comment_strip.pyshould 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 calllatexpandfrom 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-headerthat is not treated like raw text. Perhaps global macros should also be a first class citizen and get its ownyamlargument.References
Beta Was this translation helpful? Give feedback.
All reactions