Using tikz graphdrawing library within RMarkdown … Need to use lualatex engine, but can't get it to work

前端 未结 2 1743
醉梦人生
醉梦人生 2020-12-22 01:50

I have the following code in an rmd file which leverages tikz for diagrams:

---
title: \"TestNonTufteLua\"
author: \"Me\"
output:
          


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-22 02:19

    Update: Meanwhile knitr no longer uses tools::texi2dvi but tinytex::latexmk. One therefore has to use options(tinytex.engine = 'lualatex') in a set-up chunk.


    This is rather tricky, since you are not using tikzDevice but the tikz engine, which uses tools::texi2dvi to convert to PDF. You can change this using options(texi2dvi = "lualatex"). However, the default template does not work with LuaLaTeX. I have therefore created a modified template:

    \RequirePackage{luatex85}
    \documentclass{article}
    \usepackage[luatex,active,tightpage]{preview}
    \usepackage{amsmath}
    \usepackage{tikz}
    \usetikzlibrary{matrix}
    \begin{document}
    \begin{preview}
    %% TIKZ_CODE %%
    \end{preview}
    \end{document}
    

    And specify that file with engine.opts = list(template = "tikz2pdf.tex"). Putting it all together here my working file:

    ---
    title: "TestNonTufteLua"
    author: "Me"
    output:
      pdf_document :
        latex_engine: lualatex
    ---
    
    ```{r}
    options(texi2dvi = "lualatex")
    ```
    
    ```{r tikTest2, eval = TRUE, engine = "tikz", engine.opts = list(template = "tikz2pdf.tex")}
    \usetikzlibrary{graphs, graphdrawing}
    \usegdlibrary{layered}
    \tikz [gr/.style={gray!50}, font=\bfseries]
    \graph [layered layout] {
        % A and F are horizontally aligned if you also set weight=0.5 for A -- C.
        A -- [minimum layers=2] C -- F,
        { [nodes=gr, edges=gr] A -- B -- { E, D -- F } }
    };
    ```
    

    Result:

    References:

    • how to set engine options
    • preview and LuaLaTeX
    • knitr using texi2pdf

提交回复
热议问题