• Tetra Quark

Other Formats

  • PDF

Rendering PGF plots inside Quarto

Coding
Published

August 3, 2023

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}
\usepackage{paralist,pst-func, pst-plot, pst-math, pstricks-add,tikz,pgfplots}
\usetikzlibrary{patterns,matrix,arrows}
\pgfplotsset{compat=1.16}
\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:

Figure 1: PGF plot rendered in Quarto.

And that’s it!

No matching items