How do I generate a new Code-Block from a function inside an R-Package? #7887
-
DescriptionHello there! I'm currently seeking a one-line method to include TIKZ pictures from an external file within my .qmd file. `// Headers etc. '''{r} '''{r, child=tikz_path} '''` Here's what I'm doing :
However, I aim to automate the insertion and execution of this 'child' code chunk to avoid manual intervention. Is there a way to programmatically generate and execute this 'child' code chunk in the .qmd file using R or any other method? Any insights, suggestions, or alternative approaches would be greatly appreciated! Thanks in advance for your help! (Note: As it's my package, I'm open to modifying the code and return type of the function if necessary.) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
In case you don't know, there is a Qmd example---
title: Fix asis_output
format: pdf
keep-md: true
engine: knitr
---
# TikZ graphics
## Description
The engine inserts the code into a latex-string-template, which is then processed by LaTeX (and the **magick** package if `fig.ext` is not `pdf`).
## Options
You can pass some options to the engine by defining `engine.opts`, e.g. use your own template instead of the default one to include the tikz code: `engine.opts = list(template = "mytemplate.tex")`. The default template can be found under `system.file('misc', 'tikz2pdf.tex', package = 'knitr')`.
## Example
An example of the tikz-engine from <https://raw.github.com/sdiehl/cats/master/misc/example.md>
```{tikz, tikz-ex, fig.cap = "Funky tikz", fig.ext = 'png', cache=TRUE}
\usetikzlibrary{arrows}
\begin{tikzpicture}[node distance=2cm, auto,>=latex', thick, scale = 0.5]
\node (P) {$P$};
\node (B) [right of=P] {$B$};
\node (A) [below of=P] {$A$};
\node (C) [below of=B] {$C$};
\node (P1) [node distance=1.4cm, left of=P, above of=P] {$\hat{P}$};
\draw[->] (P) to node {$f$} (B);
\draw[->] (P) to node [swap] {$g$} (A);
\draw[->] (A) to node [swap] {$f$} (C);
\draw[->] (B) to node {$g$} (C);
\draw[->, bend right] (P1) to node [swap] {$\hat{g}$} (A);
\draw[->, bend left] (P1) to node {$\hat{f}$} (B);
\draw[->, dashed] (P1) to node {$k$} (P);
\end{tikzpicture}
```
## Tips
To develop the tikz-code, you could use `qtikz` or `ktikz`. This would be one way to insert a tikz plot.
Why do you need to convert tex into .qmd ? If you already have a
Using
What would you like to type in your document ? You could probably leverage R to create a single function that would create the right markdown markup to insert asis in the document. The cookbook for R Markdown shows how: https://bookdown.org/yihui/rmarkdown-cookbook/results-asis.html You could create a function that create the right character vector to include in the document, and do Some content
`r mypackage::add_tikz_graph()`
Some other content And the inline code part would be a character vector of Raw markdown You can also do it as a chunk ```{r}
#| echo: false
mypackage::add_tikz_graph()
``` Using Hope it helps |
Beta Was this translation helpful? Give feedback.
-
Thank you!!!! The provided example did not exactly what I wanted, since it included the asis_output in the slides, but i resolved the issue and could use this code to implement it in my package. Using the glue::glue function is also awesome, since then i can use an options-parameter to generate multiple options to the tikz-code! Amazing support! |
Beta Was this translation helpful? Give feedback.
Thank you!!!!
The provided example did not exactly what I wanted, since it included the asis_output in the slides, but i resolved the issue and could use this code to implement it in my package. Using the glue::glue function is also awesome, since then i can use an options-parameter to generate multiple options to the tikz-code!
Amazing support!