【问题标题】:Is it possible to extract TikZ diagrams as image files? [closed]是否可以将 TikZ 图提取为图像文件? [关闭]
【发布时间】:2023-03-21 22:02:01
【问题描述】:

我正在使用 TikZ 在 LaTeX 中绘制图表,然后我想将其隔离为图像文件以放到网上。我正在使用 TeXnicCenter 来编辑源代码。

有没有一种方法可以直接提取这些图表而无需从完成的 PDF 文件中提取它们?

【问题讨论】:

  • 我使用一个名为 CirKuit 的程序作为 TikZ 编辑器,并允许您将图表保存为各种图像格式中的任何一种。不过,我认为它在幕后所做的是使用乳胶和 dvitopng 或类似的东西。如果我能在其他人发布内容之前找出详细信息,我会发布答案。
  • 您想自动将图表与文本隔离开来,还是很乐意剪切和粘贴 TikZ 代码?
  • 另请参阅Compile a LaTeX document into a PNG image that's as short as possible 以及与之相关的其他问题。

标签: latex tikz texnic-center


【解决方案1】:

我会推荐以下方法。将有问题的 TikZ 图片放在单独的文件中,并使用 standalone 类独立编译它。它使用其他答案中提到的 preview 包。要将图片包含在主文档中,请先在其中加载standalone package,然后在图片文件上使用\input 命令。

这将允许您获得没有边距的 TikZ 图片的单个 PDF。那么你也能 例如,使用 PDF 到 PNG 转换器来获取 PNG(推荐用于图纸的网络发布)。 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

【讨论】:

  • 我发现pdf2svg 是一种比 imagemagick 更可靠的将 PDF 转换为 SVG 的工具。
  • @Glutanimate:同时我开始使用inkscape
  • @MartinScharrer 我能够将 pdf 文件嵌入到父文档中,而无需将其转换为 png 或 svg:\includegraphics{file_name.pdf}
  • @user4035:当然,这是将 PDF 包含到 (pdf)LaTeX 文档中的正常方式。但是,这个问题是关于如何将图表作为光栅图像文件获取,以便它们可以在线显示。
【解决方案2】:

跟进我的评论:Cirkuit 通过运行以下命令序列将 TikZ 图表转换为图像:

pdflatex img.tex
pdftops -eps img.pdf
convert -density 300 img.eps img.png

这里的img.tex 将是一个遵循此模板的 LaTeX 文件:

\documentclass{article}
\usepackage{tikz,amsmath,siunitx}
\usetikzlibrary{arrows,snakes,backgrounds,patterns,matrix,shapes,fit,calc,shadows,plotmarks}
\usepackage[graphics,tightpage,active]{preview}
\PreviewEnvironment{tikzpicture}
\PreviewEnvironment{equation}
\PreviewEnvironment{equation*}
\newlength{\imagewidth}
\newlength{\imagescale}
\pagestyle{empty}
\thispagestyle{empty}
\begin{document}
\begin{tikzpicture}
    % (your TikZ code goes here)
\end{tikzpicture}
\end{document}

如果您能够使用 Cirkuit 或类似的编辑器,或者为自己编写脚本以将图表放入该模板并运行适当的工具,那么您将有一种将 TikZ 代码转换为 PNG 图像的快速方法。

为了更直接地回答您的问题...不,我不知道有什么方法可以将 TikZ 图 直接 转换为 PNG 而无需通过 PDF 文件(或至少 DVI)某个阶段。

【讨论】:

  • 谢谢!我想避免的只是必须手动隔离图表,所以这应该可以正常工作。
【解决方案3】:

请参阅 TikZ 手册部分“外部化图形”。这使您可以制作图形的 EPS 或 PDF 版本。我使用 EPS 文件,将它们转换为 TIFF,然后可以将它们放在我需要的任何地方。

【讨论】:

  • 截至 2013 年,我认为这可能是最好的方法。
  • 在 2017 年,这是 pgfmanual 的第 50.4 节(第 607 页)。似乎我们无法提供该部分的网页链接(它仅在 pdf 中)。 :(
  • 理论上,这很好用。在实践中,在使用 tikzexternalize 一段时间后,我已经恢复到独立的方法,因为它似乎更容易让它工作。我在使用 tikzexternalize 时遇到了很多麻烦。
【解决方案4】:

我通常使用这些方面的东西:

\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
%\setlength\PreviewBorder{2mm} % use to add a border around the image
\usepackage{tikz}
\begin{document}
\begin{preview}
\begin{tikzpicture}
    \shade (0,0) circle(2); % background
    \draw (0,0) circle(2);  % rim
    \draw (.75,1) circle(.5);   % right eye
    \fill (.66,.9) circle(.25); % right pupil
    \draw (-.75,1) circle(.5);  % left eye
    \fill (-.66,.9) circle(.25);% left pupil
    \fill (.2,0) circle (.1);   % right nostril
    \fill (-.2,0) circle (.1);  % left nostril
    \draw (-135:1) arc (-135:-45:1) -- cycle; % mouth
  \end{tikzpicture}
\end{preview}
\end{document}

生成的 pdf 文件包含独立的 TikZ 图片。要将其转换为任何其他文件格式,只需使用 Gimp 打开即可。

【讨论】:

    【解决方案5】:

    我使用这个 Makefile 规则从每个包含一个 tikzpicture 的 .tex 文件创建 PNG 文件和缩略图。它的工作原理类似于在TeXample.net 创建图像:

    %.png: %.tex @sed 's/^\\begin{文档}/\ \\pgfrealjobname{dummy}\\begin{document}\\beginpgfgraphicnamed{example}/' $ example.tex ; \ pdflatex --jobname=example example.tex ; \ gs -dNOPAUSE -r120 -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -sDEVICE=png16m \ -sOutputFile=$@ -dBATCH example.pdf ; \ 转换 -thumbnail 200 $@ $(addsuffix .thumb.png, $(basename $@)) ; \ mv example.pdf $(addsuffix .pdf, $(basename $

    ghostview (gs) 可能会被convert 替换,您可以用另一个临时文件前缀替换example

    【讨论】:

      【解决方案6】:

      Windows:对于像我这样的菜鸟来说完全明确

      在您的乳胶代码中使用上述 Habi 建议的 prewiew 包来删除边距。

      安装 MIktex 和 Inkscape 并使用以下 bat 文件:

      cd = "C:\Path to tex"
      pdflatex input-file-name.tex
      "C:\Program Files (x86)\Inkscape\inkscape.exe" input-file-name.pdf --export-plain-svg=output-file-name.svg
      

      【讨论】:

        猜你喜欢
        • 2019-11-10
        • 1970-01-01
        • 2019-02-16
        • 2014-04-01
        • 1970-01-01
        • 2013-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多