提问人:je2018 提问时间:4/27/2023 更新时间:4/27/2023 访问量:61
有没有办法将一段 html 或 latex 代码导出为 pdf 图像?
Is there a way to export a piece of html or latex code as a pdf image?
问:
我正在使用库 ztable 生成一个表,需要将其导出为 pdf 图像。此代码将存放在计算集群中;因此,使用 rmarkdown 或在 R Studio 的查看器中渲染绘图不是可行的选项。绘图必须存放在pdf文件中。
library(ztable)
library(magrittr)
options(ztable.type="html") #or latex for latex output.
z=ztable(head(iris),caption="Table 1. Basic Table")
z
根据 ztable 文档,这部分应该在 Rviewer 中渲染绘图,并允许将其封装在 pdf('file.pdf')...dev.off() 语句。但是,它只在控制台中打印 html 或 latex 代码。我尝试将latexpdf库与以下命令一起使用,但这只是返回一个错误。tex2pdf(z)
Error in file.exists(x) : invalid 'file' argument
有没有办法重定向 html 或 latex 的输出并获取 pdf 中呈现的表格?
答:
0赞
DrEspresso
4/27/2023
#1
您可以使用将打印的乳胶输出作为对象,然后将其写入 LaTeX 文件,最后您可以从该文件创建 pdf:capture.output()
cat()
library(ztable)
library(latexpdf)
options(ztable.type="latex") #or latex for latex output.
z <- ztable(head(iris),caption="Table 1. Basic Table")
cat(capture.output(z), sep = "\n", file = "tablez.tex")
tex2pdf("tablez.tex")
但请注意,pdf 输出默认为整页,您可能需要以某种方式更改默认值或进一步对 pdf 进行后处理:
评论
0赞
je2018
4/27/2023
此解决方案不适用于 MAC ->参数“show.output.on.console”、“minimized”和“invisible”仅适用于 Windows sh:pdflatex:找不到命令
0赞
DrEspresso
4/27/2023
@je2018我在 Linux 上测试了我的解决方案,如果它在 Mac 上不起作用,我会感到惊讶,因为它们的操作系统是基于 UNIX 的。你的机器上有LaTeX安装吗?
评论