TikZ in R Markdown

10,714

In this post it is described how to use LaTeX packages in R Markdown. Not sure if the tikz-package works with R Markdown though.

Share:
10,714
Admin
Author by

Admin

Updated on June 23, 2022

Comments

  • Admin
    Admin about 2 years

    R Markdown is a good tool for document authoring. It can use LaTeX statement in R Markdown directory. But when I try to insert a TikZ picture, I am lost about how to add preamble \usepackage{tikz} in it, so compile will fail.

    Example code:

    ---
    title: "R Markdown with tikz picture"
    author: "Me"
    date: "January 10, 2015"
    output: beamer_presentation
    ---
    
    ## TikZ picture
    - Here is a TikZ picutre
    
    \begin{tikzpicture}
    \draw (0,0) circle (2cm);
    \end{tikzpicture}
    

    Since actually it will generate a LaTeX document, so it's fine to just add it in LaTeX and then compile it to PDF but I wish I can just do it in R Markdown, then click the "Knit to PDF" button to generate PDF document directly.

  • Admin
    Admin over 9 years
    Great,it's what I want.Need to add section in YAML: header-includes: - \usepackage{tikz}
  • histelheim
    histelheim almost 9 years
    @Beatlej: Did it work to use the tikz package with RMarkdown?
  • Admin
    Admin almost 9 years
    @histelheim Sure, check a simple example from my answer below.
  • krlmlr
    krlmlr over 8 years
    I am now using tikzDevice (via knitr::chunk_options$set(dev = "tikz")) in an rmarkdown document that builds as a PDF. It just works, the above line was enough, everything else seems to happen magically behind the scenes.