外部化されたTikZ画像にPNGまたはPDFを選択的に使用する

外部化されたTikZ画像にPNGまたはPDFを選択的に使用する

ImageMagics のおかげで、TikZ 画像を外部化するコードに、生成された PDF を PNG 画像に変換するよう要求することが可能です。

ただし、これにより、一部の軽量フィギュアのフィギュア重量が高くなります。

私が望むのは、2 つの図形のうち最も軽いものを選択するための重量チェック コードです。この方法では、軽い図形はベクター形式のままになり (理想的です)、重い図形は PNG になります (これにより、より適切な重量が可能になり、スペースと表示時間が節約されます)。

私は、PDF 外部化関数で 20ko から 9MO までの範囲の非常に多くの数字を扱っています。それらはループから自動的に生成されるため、手動で選択することが問題になります。

これは MWE です。理想的には、最初の図はベクトルで、2 番目の図は自動的に PNG になります (ここでは手動で強制しました)。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[mode=list and make]

\tikzset{
    png export/.style={
        % First we call ImageMagick; change settings to requirements
        external/system call/.add={}{; convert -density 100 -transparent white "\image.pdf" "\image.png"},
        % Now we force the PNG figure to be used instead of the PDF
        /pgf/images/external info,
        /pgf/images/include external/.code={
            \includegraphics[width=\pgfexternalwidth,height=\pgfexternalheight]{##1.png}
        },
    }
}

\begin{document}

\begin{tikzpicture}
    \draw (0,0) circle (1) ;
\end{tikzpicture}

% Activate the plot as png
\tikzset{png export}

\begin{tikzpicture}
\foreach \xnum in {1,2,3,...,99,100}
{
    \draw (0,0) circle (1) ;
}
\end{tikzpicture}

\end{document}

基本的に、 にコードを追加して/pgf/images/include external/.codeサイズを確認し、何らかのロジックを使用して PNG または PDF を使用するかどうかを決定できるのではないかと思います。

答え1

どうやら、次のように拡張できるようですsystem call:

    external/system call/.add={}{;
        convert -density 100 -transparent white "\image.pdf" "\image.png";
        wc -c "\image.pdf" | awk '{print "PDF is " $1}';
        wc -c "\image.png" | awk '{print "PNG is " $1}';
        echo "what is next? sir?"
    }

これにより、

これで、シェルでロジックをプログラムできるようになりました。

答え2

@Symbol 1 との議論に基づいて、私は次の結論に達しました:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[mode=list and make]

\tikzset{
    png export/.style={
        % First we call ImageMagick; change settings to requirements
        external/system call/.add={}{; convert -density 100 -transparent white "\image.pdf" "\image.png";
            mkdir -p fig2plot;
            if [[ `wc -c "\image.pdf" | cut -d' ' -f1;` -lt `wc -c "\image.png" | cut -d' ' -f1;` ]];
            then cp "\image.png" fig2plot;
            else cp "\image.pdf" fig2plot;
            fi},
        % Now we force the PNG figure to be used instead of the PDF
        /pgf/images/external info,
        /pgf/images/include external/.code={
            \includegraphics[width=\pgfexternalwidth,height=\pgfexternalheight]{fig2plot/##1}
        },
    }
}

\begin{document}

% Activate the plot as png
\tikzset{png export}

\begin{tikzpicture}
    \draw (0,0) circle (1);
\end{tikzpicture}

\begin{tikzpicture}
\foreach \xnum in {1,2,3,...,99,100}
{
    \draw (0,0) circle (1) ;
}
\end{tikzpicture}

\end{document}

最小の画像をコピーするサブフォルダーを作成し、拡張子に関係なくそのサブフォルダーからプロットします。

ただし、makefile 内のコマンドはシェルで動作しますが、if欠陥があるようです。

/bin/sh: 1: [[: not found

また、フォルダー内の図は両方とも pdf です (したがって、はif常に false になりますが、bash でコマンドを実行すると正しい答えが返されます)。

関連情報