Rendering PGF plots inside Quarto
Coding
Abstract
Rendering pgf plots directly in Quarto.
Keywords
Latex, PGF, RMD, Quarto, R Studio
\(\require{cancel}\) \(\def\oot{\frac{1}{2}}\)
This is a quick tip on rendering pgf plots inside Quarto directly. You need to create a chunk like the following. The header of the chunk runs the tikz engine with the template option.
Here is the complete code of the chunk in Quarto format:
#| label: fig-pgfplot
#| echo: false
#| fig-cap: "PGF plot rendered in Quarto."
#| fig-ext: !expr if (knitr:::is_latex_output()) 'pdf' else 'svg'
#| engine: tikz
#| engine-opts:
#| dvisvgm.opts: "--no-fonts --bbox=min --libgs=/opt/homebrew/opt/ghostscript/lib/libgs.dylib" #MacOS related
#| template: tikz2pdf.tex
\begin{tikzpicture}
\begin{axis}
\addplot3[surf,domain=0:360,samples=40]
{sin(x)*sin(y)};
\end{axis}
\end{tikzpicture}
The template “tikz2pdf.tex” loads the relevant \(\LaTeX\) libraries. You need to save it under the same directory or change the path accordingly.
\documentclass{article}-func, pst-plot, pst-math, pstricks-add,tikz,pgfplots}
\usepackage{paralist,pst
\usetikzlibrary{patterns,matrix,arrows}=1.16}
\pgfplotsset{compat
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\usepackage[pdftex,active,tightpage]{preview}
\usepackage{amsmath}
\usetikzlibrary{matrix}
\begin{document}
\include{preview}
\begin{preview}%% TIKZ_CODE %%
\end{preview} \end{document}
Once evaluated, the image will look like Figure 1:
And that’s it!
No matching items