LaTeX到png转换 [英] LaTeX to png conversion

查看:804
本文介绍了LaTeX到png转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个先前的问题之后,我认为我可以先转换 post ),然后使用

Following this previous question, I thought I could first convert the output of stargazer (latex code for summary statistics table) to png, using R commands (like dvi, dvips, ...) inside an R chunk or, in the worst case, invoking system commands (see this post), and then import the produced png into my Rmd file, using a command like

![alt text](summary_lm.png)

您认为这可能吗?你能告诉我怎么做,因为我没有运气吗?

Do you think this is possible? Could you show me how to do it, because I had no luck?

推荐答案

您可以使用object标记嵌入PDF文件 在HTML文件中 (但并非所有网络浏览器都支持.)

You can use the object tag to embed a PDF file in an HTML file (but it is not supported by all web browsers).

棘手的一点是,由 LaTeX必须预先设置;但是,要显示表格,您可以 如果PDF文件的大小恰好等于表格的大小,则更喜欢 避免使用较大的空白边距和/或滚动条. 您可以使用GhostScript裁剪PDF文件.

One tricky point is that the size of the PDF files generated by LaTeX has to be set in advance; but, to display a table, you may prefer if the PDF file had exactly the size of the table, to avoid large empty margins and/or scrolling bars. You can use GhostScript to crop the PDF file.

这里是一个例子.

# Sample data
library(stargazer)
example(stargazer)
code <- .Last.value$value

# Generate the LaTeX file
file <- tempfile("latex_", fileext = "")
tex_file  <- paste( file, "tex",  sep="." )
pdf_file  <- paste( file, "pdf",  sep="." )
pdf_file2 <- paste( file, "_cropped.pdf",  sep="" )
png_file  <- paste( file, "png",  sep="." )
html_file <- paste( file, "html", sep="." )
cat( 
  "\\documentclass{report}",
  # Unreasonably tall page size: we want everything on the same page
  "\\usepackage[paperwidth=10cm,paperheight=100cm,noheadfoot,margin=0in]{geometry}",
  "\\begin{document}",
  "\\pagestyle{empty}", 
  paste(code, collapse="\n"),
  "\\end{document}\n", 
  sep="\n",
  file = tex_file
)

# Generate the PDF file
old_wd <- getwd()
setwd( tempdir() )
system(paste( "pdflatex --interaction=nonstopmode", shQuote(tex_file) ))

# We need to crop the file.
# I will use Ghostscript, but you could also use
#   http://pdfcrop.sourceforge.net/
# or
#   http://www.ctan.org/tex-archive/support/pdfcrop
# First, find the dimensions 
bbox <- system(paste( "gs -sDEVICE=bbox -dNOPAUSE -dBATCH -f", pdf_file, "2>&1" ), intern=TRUE)
bbox <- bbox[ grep("%%BoundingBox", bbox) ]
bbox <- as.numeric( strsplit(bbox, " ")[[1]][-1] )
# Then, crop the file
cmd <- paste( 
  "gs -sDEVICE=pdfwrite",
  "-o", pdf_file2, 
  "-c \"[/CropBox [", paste(bbox, collapse=" "), "] /PAGES pdfmark\"",
  "-f", pdf_file 
)
system(cmd)

# Convert it to PNG, in case the browser cannot display inline PDF files.
# This assumes that ImageMagick is installed.
# You may want to play with the options to have a better quality and/or larger file.
system(paste( "convert", "-trim", "-density 400", pdf_file2, "-resize 50%", png_file ))

# You can now include it in an HTML file 
# (or a Markdown file, since you can include raw HTML).
cat(
  "<html><body><p>Here is an embedded PDF file.</p>\n",
  "<object width='100%' height='100%' type='application/pdf' data='", pdf_file2, "'>",
  "<img src='", png_file, "'/>",
  "</object>\n",
  "</body></html>",
  sep="",
  file=html_file
)

# Check that the result can be displayed in your browser
# (Chrome should be fine, but I have not had any success with the others.)
browseURL( html_file )

这篇关于LaTeX到png转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆