Giter VIP home page Giter VIP logo

Comments (6)

ismayc avatar ismayc commented on May 29, 2024

I think I would try using a different engine for the chunks you are referring to in different languages: http://rmarkdown.rstudio.com/authoring_knitr_engines.html

from thesisdown.

grmpfhmbl avatar grmpfhmbl commented on May 29, 2024

Thank you for your quick answer. I might have poorly expressed my problem and what I wanted to achieve. I do not want to run the code, just display it with syntax highlighting. That's why eval = FALSE. But thank you for pointing out the bash engine. I'll use that. My problem is: When I use the code above, I see the script file in the PDF output as I want to, but it does not get a caption. It basically looks like that

pdf-output

The TeX output created by it looks like that

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{for} \ExtensionTok{foo}\NormalTok{ in (ls bar)}
\KeywordTok{do}
  \BuiltInTok{echo} \VariableTok{$foo}
\KeywordTok{done}
\end{Highlighting}
\end{Shaded}

For example, when I use include_graphics - I am aware that this is an R function that creates the figure -
in an R script

```{r img, results="asis", echo=TRUE, fig.cap="A figure", out.width='\\textwidth'}
include_graphics("./figure/afigure.png")
```

the TeX output looks like that

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{include_graphics}\NormalTok{(}\StringTok{"./figure/afigure.png"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{figure}
\includegraphics[width=\textwidth]{./figure/afigure} \caption{A figure}\label{fig:img}
\end{figure}

What I basically want to achieve is to get a \begin{figure} .. \end{figure} with a label etc around the \begin{shaded} .. \end{shaded} part. I thought that this is not an uncommon problem and as I could not find anything on Google I was asking here. The solutions I see at that point are

  1. Creating an image by hand and including it with include_graphics(). I don't really like that solution as that is error prone especially when the code changes.
  2. I could probably use a knitr hook and making a new option echo.asfig. When this is set, I create the needed TeX before and after the echoed output. Probably the easiest to implement.
  3. I could write an R function like "highlight(sourcefile)" that renders the code (using a tool like prism or highlight) and then includes the figure using \include_graphics. That is probably the solution that takes the most effort to implement and creates dependencies to other (platform dependent) tools - prism for example is written in JavaScript and would need nodeJs to run.

Do you have any suggestions on what other options I may have, or have I just overlooked on how to do it in Rmd?

from thesisdown.

ismayc avatar ismayc commented on May 29, 2024

Ah, thanks for clarifying. I didn't quite understand. I think your best bet here is a knitr hook. Here is a related example, though it is using the old Rnw format: https://github.com/yihui/knitr-examples/blob/master/074-example-environment.Rnw

from thesisdown.

grmpfhmbl avatar grmpfhmbl commented on May 29, 2024

Thank you. I'll think about it and try around a bit over the next couple of weeks. If I have a solution are you interested in a pull request?

from thesisdown.

ismayc avatar ismayc commented on May 29, 2024

Yes, please!

from thesisdown.

github-actions avatar github-actions commented on May 29, 2024

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org whenever possible) and link to this issue. If a reprex is not applicable, recording a short Loom video showing what you are seeing can go a long way in helping to diagnose problems.

from thesisdown.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.