Open aksigkvgithub opened 1 year ago
@aksigkvgithub could you share a full example of how you tried to use knitr for a typst document ?
For now we have no specific support and I am curious of how you tried to use it. Especially an example related to
Fortunately, it has Markdown type source code block type. So, I tried to render R code block embedded into such an R code block in a Typst file, through knitr::knit() function , and then compiled this rendered file through Typst compiler.
This would help be sure we understand correctly what you are trying to do.
Also, just so you are aware, there is a tool called Quarto which uses knitr under the hook for R users, and it supports producing a typst output from a markdown syntax document (called a .qmd
file). See https://quarto.org/docs/prerelease/1.4/typst.html
this is currently available in pre-release only if you want to try it: https://quarto.org/docs/download/prerelease.html
Within this .qmd
you can write Markdown syntax, and use R code chunk that will be evaluated using knitr. The whole rendering will produce a typst document using Pandoc. Quarto brings some feature support in addition to knitr and Pandoc.
Just sharing in case you don't know about it. We are still interested into your use case with (I suppose) .typ
file.
Ok. I shall submit below the files created and obtained in the process, sequentially to make example workflow clrear:
I use Windows system. So, for Windows I downloaded Typst software from Github. From the downlaod, I put the typst.exe on the systems path in the Environment variables.
Please create a folder, say, named as "Analysis of Covariance".
Within this folder, please keep the following file, after donloading:
After downloading this .txt file, please rename it to .typ format, to make it a typst file. This file contains a small typst document with some R codes in a Rmarkdown block format. And this code block uses following data file. Please downlaod that file too: Leprosy.txt
After downloading this data file, please rename this .txt file to .csv format, as it comtains comma separated data, which the R codes from within its block, will read. And please keep this data file too in the same "Analysis of Covariance" folder, after renaming to .csv format.
Then open R console, in Windows of course as I use it, that will require installation of R for sometime, if not already downlaoded, which is open source and can be downloaded from CRAN repository. And on the R console, please make it sure to install "knitr" package, which may require installation of some other dependable packages, which generally R installs automatically mostly. But sometimes, when it cannot, then it indicates in its messages to install them first, then "knitr" will be installed.
After successful intallation of "knitr" package, please run the following command, which will verify whether "knitr" has been successfully installed or not:
library(knitr)
If package "knitr" is successfully installed, it will simply load that library and show the R prompt. Otherwise it will throw some error message.
knitr::knit("2. ANCOVA in CRD.r.typ","2. ANCOVA in CRD.typ")
Please download this file and rename it from .txt format to .typ format to make it typst file too. In this file all the R output are created in Markdown format.
Finally, please compile this file named file "2. ANCOVA in CRD.typ" into typst, by simply typing following command on the Windows command prompt console, invoked in the same "Analysis of Covariance" folder, as typst.exe is already on the path:
typst compile "2. ANCOVA in CRD.typ"
Everything is already supported by typst, except that the plot from the /figure subdirectory is not fetched, despite that plot is being referenced by R in the Markdown type of image referencing syntax.
I feel that the image referencing in typst is different for image / figure with and without caption, as is illustrated at the web page https://typst.app/docs/tutorial/writing-in-typst/
So, my request is, whether in the knitr package can image referencing be made like that of typst for .typ files, so that R users can also use Typst documentation for articles and books, along with rendering of R codes code blocks, as Typst has some very good stuff for math users.
Thanks and regards,
Dr. A.K. Singh
Hi Dr. A.K. Singh @aksigkvgithub hope it's not too late, here I got a solution for this: you can try to hook the plot function of knitr, and construct the typst style image with chunk options. This can be done by adding below code chunk at the top of your typ file.
# this chunk should be at the beginning of your paper. chunk option should be {r, echo=FALSE}
local({
hook_old <- knitr::knit_hooks$get("plot")
knitr::knit_hooks$set(plot = function(x, options) {
#print(options)
image_path <- paste(options$fig.path, options$label, '-', options$fig.cur, '.', options$fig.ext, sep="")
image_caption <- options$fig.cap
result_string <- sprintf("#figure(\n image(\"%s\", width:%s),\n caption: [%s],\n)",image_path, options$out.width, image_caption)
})
})
I only passed caption and width, but possibilities are there, you can try your own things.
The workflow I used is:
knit("feed-me-to-knitr.typ", "feed-me-to-typst.typ")
typst compile "feed-me-to-typst.typ" final.pdf
to get the final pdf.all files mentioned are packaged into this zip: typst-r.zip
Dear Sir,
You might be already aware of a new Typst markup language. It has very good math support and a good pdf rendering.
Fortunately, it has Markdown type source code block type. So, I tried to render R code block embedded into such an R code block in a Typst file, through knitr::knit() function , and then compiled this rendered file through Typst compiler.
Everything was rendered well into pdf of Typst except the image rendered by knitr in the/figure subdirectory, probably because knitr rendered Markdown type referencing and Typst has a different image referencing syntax.
So, I wish to know if the image referencing as per Typst syntax can be added into knitr for Typst file types.
Thanks and regards,
Dr. A. K. Singh
By filing an issue to this repo, I promise that
xfun::session_info('knitr')
. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version:remotes::install_github('yihui/knitr')
.I understand that my issue may be closed if I don't fulfill my promises.