【问题标题】:LaTeX to png conversionLaTeX 到 png 的转换
【发布时间】:2013-01-27 11:34:18
【问题描述】:

this previous question 之后,我想我可以首先将stargazer(汇总统计表的乳胶代码)的输出转换为 png,在 R 块中使用 R 命令(如 dvi、dvips...),或者,在最坏的情况下,调用系统命令(参见post),然后使用类似

的命令将生成的 png 导入到我的 Rmd 文件中
![alt text](summary_lm.png)

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

【问题讨论】:

  • 我真的不确定将表格转换为 png 是一个好主意。你会得到不可重排的布局,不可搜索的内容,大文件大小,没有像素化就不可能放大......
  • 我同意。你能建议一个更好的方法来在我的 (knit2html) 页面中包含这样的表格 r-statistics.com/wp-content/uploads/2013/01/… 吗?
  • 好吧,正如@Yihui 在您之前的问题中指出的那样,理想情况下,您会将表格生成为 HTML。但我不知道是否有一个包可以像stargazer 那样很好地格式化这些,不幸的是......
  • 如果您创建了一个 pdf 文件并使用了 \includepdf (texblog.org/tag/includepdf),那么至少该表格是可搜索的。
  • @Largh 问题是我创建的不是 LaTeX 文档而是 Rmd。

标签: windows r latex png converter


【解决方案1】:

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

一个棘手的问题是生成的 PDF 文件的大小 LaTeX 必须提前设置;但是,要显示表格,您可以 如果 PDF 文件的大小与表格的大小完全相同,则更喜欢 避免大的空白边距和/或滚动条。 您可以使用 GhostScript 裁剪 PDF 文件。

这是一个例子。

# 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 )

【讨论】:

  • 谢谢。你能增加一个额外的步骤吗?将裁剪后的 pdf 转换为 jpg,以确保结果将显示在任何浏览器中
  • JPEG 虽然对照片很有用,但对于文本或图表来说却是一个非常糟糕的主意:即使您不尝试放大它也会显得模糊。 PNG是优选的。我已经相应地更新了我的答案。
  • ghostscript.com/download 安装了ghostscript,但我仍然收到'gs' not found 错误
  • 它在 Windows 上的调用方式可能不同(gswin64c.exe?)。您可能还必须手动将其添加到您的 PATH 中,或使用可执行文件的完整路径。
  • 可能有一个完全不相关的convert 命令,出现在您的PATH 中的ImageMagick 之前:尝试指定可执行文件的完整路径。
猜你喜欢
  • 2011-09-29
  • 1970-01-01
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
  • 2015-01-16
  • 2016-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多