这将允许您获得没有边距的 TikZ 图片的单个 PDF。那么你可以 例如,使用 PDF 到 PNG 转换器来获取 PNG(推荐用于绘图的 Web 发布)。 SVG 格式会更好,因为它是矢量格式,但并非所有浏览器都能够显示它。
这里有一些示例代码。 TikZ 图片文件(例如 pic.tex):
\documentclass{standalone}
\usepackage{tikz}
% all other packages and stuff you need for the picture
\begin{document}
\begin{tikzpicture}
% your picture code
\end{tikzpicture}
\end{document}
主文档:
\documentclass{article} % or whatever class you are using
\usepackage{standalone}
\usepackage{tikz}
% All other packages required
\begin{document}
% Text text text
% somewhere where you want the tikz picture
\input{pic}
% Text text text
\end{document}
I would recommend the following approach. Place the TikZ picture in question in a separate file and use the standalone class to compile it standalone. It uses the preview package mentioned in the other answers. To include the picture in the main document, load the standalonepackage there first and then use the \input command on the picture file.
This will allow you to get a single PDF of the TikZ picture without margins. Then you can use, say, a PDF-to-PNG converter to get a PNG (recommended for web publishing of drawings). The SVG format would be nicer, because it’s a vector format, but not all browsers might be able to display it.
Here some example code. The TikZ picture file (e.g., pic.tex):
\documentclass{standalone}
\usepackage{tikz}
% all other packages and stuff you need for the picture
\begin{document}
\begin{tikzpicture}
% your picture code
\end{tikzpicture}
\end{document}
The main document:
\documentclass{article} % or whatever class you are using
\usepackage{standalone}
\usepackage{tikz}
% All other packages required
\begin{document}
% Text text text
% somewhere where you want the tikz picture
\input{pic}
% Text text text
\end{document}
Then compile the picture and convert it, e.g., with ImageMagick:
If you are able to use Cirkuit or a similar editor, or write a script for yourself to put your diagram into that template and run the appropriate tools, you'll have a quick way to convert TikZ code into a PNG image.
To answer your question more directly... no, I don't know of any way to convert a TikZ diagram directly to PNG without going through a PDF file (or at least DVI) at some stage.
请参阅 TikZ 手册“外部化图形”部分。这使您可以制作图形的 EPS 或 PDF 版本。我使用 EPS 文件,将它们转换为 TIFF,然后可以将它们放在我需要的任何地方。
See the TikZ manual section “Externalizing Graphics”. This lets you make EPS or PDF versions of your graphics. I use EPS files, convert them to TIFFs and can then put them wherever I need to.
ghostview (gs)可能可以用 convert 替换,并且您可以用另一个临时文件前缀替换 example 。
I use this to Makefile rule create PNG files and thumbnails from .tex files that contain one tikzpicture each. It works similar to the creation of images at TeXample.net:
发布评论
评论(6)
我会推荐以下方法。将有问题的 TikZ 图片放在单独的文件中,并使用
standalone
类对其进行独立编译。它使用其他答案中提到的preview
包。要将图片包含在主文档中,请先加载standalone
package,然后对图片文件使用\input
命令。这将允许您获得没有边距的 TikZ 图片的单个 PDF。那么你可以
例如,使用 PDF 到 PNG 转换器来获取 PNG(推荐用于绘图的 Web 发布)。
SVG 格式会更好,因为它是矢量格式,但并非所有浏览器都能够显示它。
这里有一些示例代码。
TikZ 图片文件(例如
pic.tex
):主文档:
然后编译图片并转换它,例如使用 ImageMagick:
或尝试 SVG:
I would recommend the following approach. Place the TikZ picture in question in a separate file and use the
standalone
class to compile it standalone. It uses thepreview
package mentioned in the other answers. To include the picture in the main document, load thestandalone
package there first and then use the\input
command on the picture file.This will allow you to get a single PDF of the TikZ picture without margins. Then you can
use, say, a PDF-to-PNG converter to get a PNG (recommended for web publishing of drawings).
The SVG format would be nicer, because it’s a vector format, but not all browsers might be able to display it.
Here some example code.
The TikZ picture file (e.g.,
pic.tex
):The main document:
Then compile the picture and convert it, e.g., with ImageMagick:
or try SVG:
跟进我的评论: Cirkuit 通过运行某些内容将 TikZ 图转换为图像像下面的命令序列:
这里
img.tex
将是一个遵循此模板的 LaTeX 文件:如果您能够使用 Cirkuit 或类似的编辑器,或者为自己编写一个脚本来放置您的图表进入该模板并运行适当的工具,您将有一个快速的方法将 TikZ 代码转换为 PNG 图像。
为了更直接地回答你的问题...不,我不知道有什么方法可以将 TikZ 图直接转换为 PNG,而无需在某个阶段通过 PDF 文件(或至少是 DVI)。
Following up on my comment: Cirkuit converts TikZ diagrams into images by running something like the following sequence of commands:
Here
img.tex
would be a LaTeX file that follows this template:If you are able to use Cirkuit or a similar editor, or write a script for yourself to put your diagram into that template and run the appropriate tools, you'll have a quick way to convert TikZ code into a PNG image.
To answer your question more directly... no, I don't know of any way to convert a TikZ diagram directly to PNG without going through a PDF file (or at least DVI) at some stage.
请参阅 TikZ 手册“外部化图形”部分。这使您可以制作图形的 EPS 或 PDF 版本。我使用 EPS 文件,将它们转换为 TIFF,然后可以将它们放在我需要的任何地方。
See the TikZ manual section “Externalizing Graphics”. This lets you make EPS or PDF versions of your graphics. I use EPS files, convert them to TIFFs and can then put them wherever I need to.
我通常使用以下内容:
生成的 pdf 文件包含独立的 TikZ 图片。要将其转换为任何其他文件格式,只需使用 Gimp 打开它即可。
I'm generally using something along these lines:
The pdf file generated contains the standalone TikZ picture. To convert it to any other file format, simply open it with Gimp.
我使用此 Makefile 规则从 .tex 文件创建 PNG 文件和缩略图,每个文件包含一张 tikzpicture。它的工作原理类似于在 TeXample.net 上创建图像:
ghostview (
gs
)可能可以用convert
替换,并且您可以用另一个临时文件前缀替换example
。I use this to Makefile rule create PNG files and thumbnails from .tex files that contain one tikzpicture each. It works similar to the creation of images at TeXample.net:
ghostview (
gs
) can probably be replaced byconvert
and you can replaceexample
with another tempfile prefix.Windows:只是为了像我这样的菜鸟完全明确地
使用 prewiew 包,如上面 Habi 在乳胶代码中建议的那样来删除边距。
安装 MIktex 和 Inkscape 并使用以下 bat 文件:
Windows: Just to be completely explicit for noobs like me
Use the prewiew package as Habi suggested in the above in your latex code to remove margins.
Install MIktex and Inkscape and use the following bat file: