No more ascii-art on Linux

I have just come across Will Lowe’s excellent post on using pretty Latex tables when you’re still working on the analysis.

I had to make a tiny modification to get this run on GNU/Linux: changing the “open” command to the more specific “evince” (i.e. the PDF viewer). All credit should go to Will Lowe.

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
}

One observation: The leading comment line in the stargazer package breaks the function in its current version (the Latex code is not broken down into lines). All works fine with the apsrtable package.

One Reply to “No more ascii-art on Linux”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: