-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#' Render a bunch of small .tex files | ||
#' | ||
#' Use case: A bunch of stuff done in LaTeXiT, resulting in some helper calculations | ||
#' or derivations contained in tex files. These are then rendered to PDF using `tex_fun`, per | ||
#' default [tinytex::xelatex], and then optionally converted to `png` using the system `convert` | ||
#' tool via imagemagick. | ||
#' @param path Folder where `.tex` files are. | ||
#' @param cleanup `TRUE`: Whether to cleanup auxilliary files (e.g. `.aux`, `.log`, ...) | ||
#' @param tex_fun Which function to use, default ist [tinytex::xelatex]. | ||
#' @param to_png `TRUE`: Whether to use `convert` to convert PDF to png. | ||
#' | ||
#' @return Nothing | ||
#' @export | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' render_tex_files("formulas") | ||
#' } | ||
render_tex_files <- function(path, cleanup = TRUE, tex_fun = tinytex::xelatex, to_png = TRUE) { | ||
tex_files <- list.files(path, pattern = ".tex$", full.names = TRUE, recursive = TRUE) | ||
lapply(tex_files, tex_fun) | ||
|
||
# Cleanup | ||
if (cleanup) { | ||
garbage <- list.files(path, pattern = ".aux$|.fls$|.synctex.gz$|.xdv$|.log$", full.names = TRUE) | ||
file.remove(garbage) | ||
} | ||
|
||
# Convert to png? | ||
if (to_png) { | ||
f_pdfs <- shQuote(list.files(path, pattern = ".pdf$", full.names = TRUE, recursive = TRUE)) | ||
|
||
for (pdf in f_pdfs) { | ||
f_png <- sub("\\.pdf", ".png", pdf) | ||
command <- paste("convert -density 300 -flatten -colorspace RGB", pdf, f_png) | ||
cat(command, "\n") | ||
system(command) | ||
} | ||
} | ||
} | ||
|
||
#' Print a matrix for LaTeX. | ||
#' | ||
#' @param mat A [matrix]. | ||
#' @param type `[character(1): "pmatrix"]`: Matrix environment to use, e.g. `bmatrix` for `[]`-enclosed | ||
#' (brackets) or `pmatrix` for `()` (parentheses). | ||
#' @param round `[integer(1): 3]` | ||
#' | ||
#' @return Nothing, output is `cat`ed. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' mat <- matrix(1:9, ncol = 3) | ||
#' mattex(mat) | ||
mattex <- function(mat, type = "pmatrix", round = 3) { | ||
mat <- round(mat, round) | ||
rows <- seq_len(nrow(mat)) | ||
spaced_rows <- sapply(rows, function(row) { | ||
row <- mat[row, ] | ||
paste(row, collapse = " & ") | ||
}) | ||
matrix_rows <- paste(spaced_rows, collapse = " \\\\ \n ") | ||
|
||
cat(paste0("\\[\\begin{", type, "}\n", matrix_rows, "\n\\end{", type, "}\\]")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.