我正在使用 TikZ 在 LaTeX 中绘制图表,然后我想将其隔离为图像文件以放在网上。我正在使用 TeXnicCenter 来编辑源代码。
有没有办法直接提取这些图表,而不必从完成的 PDF 文件中提取它们?
最佳答案
我会推荐以下方法。将有问题的 TikZ 图片放在单独的文件中,并使用 standalone
类对其进行独立编译。它使用其他答案中提到的 preview
包。要将图片包含在主文档中,请先加载独立
包,然后对图片文件使用\input
命令。
这将允许您获得没有边距的 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}
然后编译图片并进行转换,例如使用 ImageMagick:
pdflatex pic
convert -density 600x600 pic.pdf -quality 90 -resize 800x600 pic.png
或尝试 SVG:
convert pic.pdf pic.svg
关于latex - 是否可以将 TikZ 图提取为图像文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2701902/