英文:
How to save a LateX rendered image (via Latexify) to disk in png in Julia
问题
我正在使用Julia。
我使用Latexitfy从模型方程式生成图像,使用以下代码:
Latexify.latexify(model_equations) |> render
现在我想将生成的图像导出以保存到我的硬盘上。
我该如何做呢?
英文:
I am using Julia.
I used Latexitfy to generate an image from a model equations using the following code:
Latexify.latexify(model_equations) |> render
Now I'd like to export that image generated to save it on my disk.
How can I do that ?
答案1
得分: 2
以下是翻译好的部分:
你可以使用render
函数的第二个参数来渲染到目标MIME类型。文档提供了一个有用的示例:
latexify(:(x/y)) |> s -> render(s, MIME("image/png"))
解析这个一行代码:
l = latexify(:(x/y))
# 将分数 x/y 转换为 LaTeX 文本m = MIME("image/png")
# 设置目标为 PNG 的 MIME 类型(也可以是 "image/svg")render(l, m)
# 将渲染后的 LaTeX 保存为临时的 PNG 并尝试显示它
render
函数接受几个关键字参数,但文档记录得不太清楚。如果你想将渲染后的 LaTeX 写入具有特定名称的文件,请传递name
参数。请注意,扩展名 .png
将自动添加:
render(l, m, name="./my_file_name")
关于文件名有两点需要注意:
- 文件名不包含扩展名 ".png"。该包将自动添加适当的扩展名。它还可能在同一目录下创建其他文件,这些文件是编译 LaTeX 文档的副产品,如
my_file_name.tex
、my_file_name.aux
、my_file_name.log
和my_file_name.pdf
。 - 文件名包括一个目录。由于该包在此帖子后不久修复的错误,如果在文件名中没有包含目录名,该方法将抛出
ERROR: IOError: cd(""):no such file or directory (ENOENT)
。截止到 Latexify v0.15.19,这个问题已经修复了。
带有name
参数的方法调用仍会尝试显示图像。如果你不想显示图像,请将open
参数设置为false
:
render(l, m, name="./my_file_name", open=false)
英文:
You can render to a target MIME type using the second argument to render
. The documentation provides a useful example:
latexify(:(x/y)) |> s -> render(s, MIME("image/png"))
Breaking down this one-liner:
l = latexify(:(x/y)) # convert the fraction x/y to LaTeX text
m = MIME("image/png") # set up a MIME type target of PNG (can also be "image/svg")
render(l, m) # save the rendered LaTeX to a temporary PNG and try to display it
The render
function takes several keyword arguments that are not well documented. If you want to write the rendered LaTeX to a file with a specific name, pass the name
argument. Note that the extension .png
will be added automatically:
render(l, m, name="./my_file_name")
Note two things about the file name:
- The name does not have a ".png" extension. The package will add the appropriate extension itself. It is also likely to create other files in the same directory that are byproducts of compiling a latex document, like
my_file_name.tex
,my_file_name.aux
,my_file_name.log
, andmy_file_name.pdf
. - The name includes a directory. Because of a bug in the package that was fixed shortly after this post, if you did not include a directory name in the file name, the method would throw
ERROR: IOError: cd(""): no such file or directory (ENOENT)
. This has been fixed as of Latexify v0.15.19.
The method call with the name
argument will still attempt to display the image. If you do not want the image to be displayed, pass false
to the open
argument:
render(l, m, name="./my_file_name", open=false)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论