Skip to content

Instantly share code, notes, and snippets.

@druedin
Created February 3, 2013 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save druedin/4701474 to your computer and use it in GitHub Desktop.
Save druedin/4701474 to your computer and use it in GitHub Desktop.
Modified to run on Linux; original from Will Lowe http://conjugateprior.org/2013/01/no-more-ascii-art/
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