我想使用 tikz 图像,但通过带有 includegraphics 的 PNG 将它们包含在我的文档中。这对于 PDF 和 HTML 都很好用,但原始 tikz 图像也会显示出来。我需要这个的原因是我想将它与 tex4ht 结合使用,并让生成的 HTML 文件显示 PNG 文件而不是 SVG 文件。
目前,以下代码仅显示三张图片。我想隐藏第一张图片,仅用于导出为 PNG。
\begin{tikzpicture}
\node[fill=yellow!80,ellipse] (origin) {Origin};
\node[fill=blue!30,ellipse] (destination) at (15em,0) {Destination};
\path (origin) edge[->] node[above,font=\footnotesize] {the journey} (destination);
\end{tikzpicture}
\includegraphics{figures/myfigure.png}
\includegraphics{figures/myfigure-600.png}
为了转换和复制 PNG 文件,我使用以下代码:
\tikzset{external/force remake}
\tikzset{png export/.style={
external/system call={
pdflatex \tikzexternalcheckshellescape
-halt-on-error -interaction=batchmode -jobname "\image" "\texsource";
convert -units pixelsperinch -density 150 "\image.pdf" "\image.png";
convert -units pixelsperinch -density 600 "\image.pdf" "\image-600.png";
}}}
\tikzset{png export}
\tikzsetexternalprefix{figures/}
\tikzsetnextfilename{myfigure}
或者,我可以使用 tikzpicture 命令直接使用它为输出生成的 PNG 文件(而不是依赖 SVG 文件),所以我不需要 includegraphics 命令(甚至可能是更好的解决方案:))。
以下是一个最简单的例子:https://www.overleaf.com/17313462rcpxbvktsgvd
先感谢您!
主要.tex:
\documentclass{scrbook}
\title{TEST TIKZ}
\usepackage{tikz,pgfplots}
\usetikzlibrary{matrix}
\usepgfplotslibrary{external}
\usepackage{siunitx}
\DeclareSIUnit{\sr}{sr}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
%\tikzexternalize % comment out to debug if latex errors: generates the external pdf
\tikzset{external/force remake}
\tikzset{png export/.style={
external/system call={
pdflatex \tikzexternalcheckshellescape
-halt-on-error -interaction=batchmode -jobname "\image" "\texsource";
convert -units pixelsperinch -density 150 "\image.pdf" "\image.png";
convert -units pixelsperinch -density 600 "\image.pdf" "\image-600.png";
}}}
\tikzset{png export}
\tikzsetexternalprefix{figures/}
\tikzsetnextfilename{myfigure}
\usetikzlibrary{calc}
\usetikzlibrary{positioning,shapes.arrows,shapes.symbols,decorations.pathreplacing,}
\usetikzlibrary{patterns,shapes,backgrounds,lindenmayersystems,shadings,intersections}
\begin{document}
\begin{tikzpicture}
\node[fill=yellow!80,ellipse] (origin) {Origin};
\node[fill=blue!30,ellipse] (destination) at (15em,0) {Destination};
\path (origin) edge[->] node[above,font=\footnotesize] {the journey} (destination);
\end{tikzpicture}
\includegraphics{figures/myfigure.png}
\includegraphics{figures/myfigure-600.png}
\end{document}
latexmkrc:
# html generation
$pdflatex = "htlatex %S \"htlatex/htlatex.cfg,MyFonts,NoFonts\" \"\" \"\" -shell-escape > output.txt; pdflatex -shell-escape -synctex=1 %O %S";
$clean_ext .= ' 4ct 4tc idv lg tmp xref';
$clean_full_ext .= ' css html';
htlatex/htlatex.cfg:
\Preamble{xhtml}
\usepackage{graphicx}
\DeclareGraphicsExtensions{.svg,.png,.jpg,.jpeg,.gif,.pdf,.eps}
\Configure{graphics*}
{pdf}
{\Needs{"convert -density 150 \csname Gin@base\endcsname.pdf
\csname Gin@base\endcsname.png"}%
\Picture[pict]{\csname Gin@base\endcsname.png}%
\special{t4ht+@File: \csname Gin@base\endcsname.png}
}
\Configure{graphics*}
{eps}
{\Needs{"convert -density 150 \csname Gin@base\endcsname.eps
\csname Gin@base\endcsname.png"}%
\Picture[pict]{\csname Gin@base\endcsname.png}%
\special{t4ht+@File: \csname Gin@base\endcsname.png}
}
\Configure{VERSION}{}
\Configure{DOCTYPE}{\HCode{<!DOCTYPE html>\Hnewline}}
\Configure{HTML}{\HCode{<html>\Hnewline}}{\HCode{\Hnewline</html>}}
\begin{document}
\EndPreamble
答案1
好的,我现在得到了一个完整的示例。缺少的部分是
/pgf/images/external info, /pgf/images/include external/.code=\includegraphics{##1.png}
指示编译器包含 PNG,而不是 SVG(或其他)。
MWE 的完整 main.tex 如下所示:
\documentclass{scrbook}
\title{TEST TIKZ}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzset{png export/.style={external/system call={pdflatex
\tikzexternalcheckshellescape -interaction=batchmode -jobname "\image" "\texsource";
convert -density 300 -transparent white "\image.pdf" "\image.png"},
/pgf/images/external info,
/pgf/images/include external/.code=\includegraphics{##1.png}}}
\tikzset{png export}
\usetikzlibrary{shapes}
\ifx\HCode\undefined
\tikzexternalize
\else
\tikzexternalize[mode=only graphics]
\fi
\begin{document}
\begin{tikzpicture}
\node[fill=yellow!80,ellipse] (origin) {Origin};
\node[fill=blue!30,ellipse] (destination) at (15em,0) {Destination};
\path (origin) edge[->] node[above,font=\footnotesize] {the journey} (destination);
\end{tikzpicture}
\begin{tikzpicture}
\node[fill=yellow!80,ellipse] (origin) {Origin};
\node[fill=blue!30,ellipse] (destination) at (15em,0) {Destination};
\path (origin) edge[->] node[above,font=\footnotesize] {the journey} (destination);
\end{tikzpicture}
\begin{tikzpicture}
\node[fill=yellow!80,ellipse] (origin) {Origin};
\node[fill=blue!30,ellipse] (destination) at (15em,0) {Destination};
\path (origin) edge[->] node[above,font=\footnotesize] {the journey} (destination);
\end{tikzpicture}
\end{document}
它正确生成如下 HTML 输出:
<img src="main-figure0.png" alt="PIC">
<img src="main-figure1.png" alt="PIC">
<img src="main-figure2.png" alt="PIC">
我还必须补充一点:
\ifx\HCode\undefined
\tikzexternalize
\else
\tikzexternalize[mode=only graphics]
\fi
否则编译器会出现问题,因为 .tex 文件被读取两次(一次由 pdflatex 读取以生成图像,然后由 htlatex 读取):
$pdflatex = "pdflatex -shell-escape -synctex=1 %O %S; htlatex %S \"htlatex/htlatex.cfg,MyFonts,NoFonts\" \"\" \"\" -shell-escape > output.txt";
呼。:)
感谢 cfr,includegraphics 提示让我走上了正确的道路 :)