Modified to run on Linux; original from Will Lowe http://conjugateprior.org/2013/01/no-more-ascii-art/
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
as_pdf <- function(x){ | |
require(tools) | |
fname <- tempfile(pattern = "texview-", tmpdir = tempdir(), | |
fileext = ".tex") | |
header <- "\\documentclass{article} | |
\\usepackage[margin=10pt,font=small,labelformat=empty, | |
labelsep=none]{caption} | |
\\usepackage{dcolumn} | |
\\thispagestyle{empty} | |
\\begin{document}" | |
footer <- "\\end{document}" | |
cat(header, file=fname, sep='\n') | |
cat(x, file=fname, append=TRUE) | |
cat(footer, file=fname, append=TRUE, sep='\n') | |
newfile <- paste0(file_path_sans_ext(fname), ".pdf") | |
cropfile <- paste0(file_path_sans_ext(fname), "-crop.pdf") | |
origdir <- getwd() | |
tryCatch({ | |
setwd(tempdir()) ## next to the tex file | |
texi2pdf(fname, clean=TRUE) | |
system2("pdfcrop", args=c("--margins", "10", newfile, cropfile), | |
stdout=FALSE) | |
}, finally=setwd(origdir)) | |
system2("evince", args=c(cropfile)) | |
## shell.exec(file_path_as_absolute(cropfile)) ## for Windows, probably | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment