Import Latex to R Markdown

Asked

Viewed 864 times

4

Hello! Probably this question has already been addressed here but I did not find... I use R Markdonw to generate manuals on certain R commands, but I made a diagram in Latex and wanted to know if there is any way to import it to R Markdown. Thank you!

I would like to insert this.

% arara: pdflatex

\documentclass[10pt,a4paper]{article} 
\usepackage[utf8]{inputenc}
\usepackage[hmargin=2cm,vmargin=1cm]{geometry}
\renewcommand{\rmdefault}{bch} % change default font
\usepackage{tikz} 
\usetikzlibrary{positioning}

\begin{document}    
    \begin{figure}[h]       
        \centering
        \begin{tikzpicture}
        [% style definitions
        ,every node/.style={node distance=3, font=\footnotesize}
        ,comment/.style={font=\scriptsize\sffamily}
        ,force/.style={rectangle, draw, fill=black!10, inner sep=5pt, text width=3cm, text badly centered, minimum height=1.2cm, font=\bfseries\footnotesize\sffamily}
        ,on grid
        ] 
        % Draw forces
        \node [force] (z) {Dados Arvore};
        \node [force, above = of z] (y) {Dados Originais};
        \node [force, right = 5 of z] (x) {Secção};
        \node [force, below = of z] (v) {Dados Arvore II};
        \node [force, right = 5 of v] (b) {Cubagem};
        \node [force, below = 4 of v] (w) {Arquivo Final};
        \node [force, below left = 2 and 2.5 of v] (c) {Modelos de Relaçao Hisométrica};
        \node [force, below right = 2 and 2.5 of v] (e) {Modelos de Volume};        
        % Draw the links between forces
        \path[->,thick,every node/.append style={comment,anchor=base, fill=white}] 
        (y) edge node {Avaliação} (z) edge node {Avaliação} (x)
        (z) edge node {Inconsistências e Manipulação} (v)
        (v) edge (w) edge node {Ajuste} (c) edge node {Ajuste} (e)
        (c) edge node {Predição} (w)
        (e) edge node {Predição} (w)
        (x) edge node {Inconsistências e Manipulação} (b)
        (b) edge node {Manipulação} (e);        
        \end{tikzpicture} 
        \caption[Diagrama do Script de ajuste de relação hipsométrica]{Diagrama do Script de ajuste de relação hipsométrica, volume cubado e equações de volumetria da empresa FLORESTECA.}
        \label{fig:6forces}
    \end{figure}    
\end{document}
  • It may be easier for you to turn your Rmarkdown into Latex, this will do?

  • 2

    Rmarkdown files accept Latex commands directly, have you tried to put it that way? For example, put $$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$ in a document and do the Knit.

  • Hello, I edited my question.

1 answer

2

I assume you’re using pandoc doc.rmd -o doc.pdf or similar.

The file Rmarkdown need a block of Metadata (I suggest that placed at the beginning of the document, and in this case I will use YAML) where including the headear of LateX/tikz

---
title: "Manual de ...."
date: \today
header-includes:
    - \renewcommand{\rmdefault}{bch}
    - \usepackage{tikz}
    - \usetikzlibrary{positioning}
---

Diagrama em TiKZ
================

\begin{figure}[h]
        \centering
        \begin{tikzpicture}
        [% style definitions
        ,every node/.style={node distance=3, font=\footnotesize}
        ,comment/.style={font=\scriptsize\sffamily}
        ....
        ....
\end{figure}

Is this what you needed? (Cut what doesn’t matter)

This works well for building pdf, and some other formats. For others (e.g. HTML) cases need more work -- See also pandocfilters.

  • I discovered, after many tests, that this only works to make Knit de pdfs, because I understood the document is sent again to the Latex program that correctly interprets the Tex code. For HTML and Word, nothing appears.

  • 1

    @Molx, I am ignorant in this matter but the pandoc allows miracles: gives is work... To have tikz in html you need to convert the extracts into images; this in pandac goes through pandocfilters -- in the background a program that extracts the parts, converts, includes the results. For example, in http://blog.twshodgson.co.uk/2014/12/diagrams-with-tikz-using-pandoc-filters/ you have an example of this genus.

  • Interesting. I did the tests inside Rstudio, using the button Knit, and so I think the options are more limited. But I have no experience with Latex and little with Rmd, until yesterday I didn’t even know what Pandoc was...

  • @molx, and I confess I don’t know Rstudio... does Rstudio exist on linux? (almost certainly he’s using Letex underneath. Interestingly the pandoc is written in Haskell!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.