Skip to content

Commit

Permalink
Add tex helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jemus42 committed May 8, 2019
1 parent f99bda3 commit 5fb1aa5
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
export("%>%")
export(cite_loaded_pkgs)
export(make_pkg_bib)
export(mattex)
export(pkg_export)
export(render_tex_files)
export(rs_daily)
export(rs_preview)
export(rs_version)
Expand Down
65 changes: 65 additions & 0 deletions R/tex-helpers.R
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, "}\\]"))
}
9 changes: 8 additions & 1 deletion inst/rmarkdown/templates/uebungsblatt/resources/template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@
\usepackage{amssymb,amsmath}
% Note: mathspec loads fontspec anyway and things complain if fontspec is loaded before mathspec
%\usepackage{mathspec}
\usepackage{unicode-math}

% unicode-math seems to be the preferred superset of mathspec (and fontspec?)
% ISO means bold math is set italics, =TeX would make bold math upright
\usepackage[bold-style=ISO]{unicode-math}
% To make regular \boldsymbol be like \symbf (bold greek letters etc.)
% this makes e.g. bold beta work in both PDF and mathjax (in HTML output)
\renewcommand{\boldsymbol}[1]{\symbf{#1}}
\defaultfontfeatures{Ligatures=TeX}%,Scale=MatchLowercase}

% Useing the (sets) makes math non-italics for some reason?
Expand Down Expand Up @@ -240,6 +246,7 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% The actual document %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\allowdisplaybreaks
\begin{document}

$for(include-before)$
Expand Down
26 changes: 26 additions & 0 deletions man/mattex.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions man/render_tex_files.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5fb1aa5

Please sign in to comment.