TikZ - 调整大小,外部化和 tikzsetnextfilename 图形

TikZ - 调整大小,外部化和 tikzsetnextfilename 图形

我有一个包含多个 TikZ 图像的文档(双栏文章),我想要:

  1. 规模将数字缩小到列宽的一半,而不改变文本的字体大小
  2. 外化文件夹里面的数字./tikz_output
  3. 设置外部文件名externalize通过使用,避免自动生成名称tikzsetnextfilename

我发现下面的代码来自这个老问题,它满足第 1 点和第 2 点,但在设置外部化文件名时不起作用:

% Automagically scale a Tikz picture, so it has the desired (given) width.
% Does NOT scale line width/text width! Needs the package "environ"!
%
% Usage:
%
% \begin{myscaletikzwidth}{\textwidth}
%   \begin{tikzpicture}[scale=\tikzscale]
%     ..
%   \end{tikzpicture}
% \end{myscaletikzwidth}
%
\makeatletter
\newsavebox{\measure@tikzpicture}
\NewEnviron{myscaletikzwidth}[1]{%
  \tikzifexternalizingnext{%
      \def\tikz@width{#1}%
      \def\tikzscale{1}\begin{lrbox}{\measure@tikzpicture}%
      \tikzset{external/export next=false,external/optimize=false}% force translation of this BODY (and do not optimize it away as it would usually do):
      \BODY
      \end{lrbox}%
      \pgfmathparse{#1/\wd\measure@tikzpicture}%
      \edef\tikzscale{\pgfmathresult}%
      \BODY
  }{% this will re-use an existing external graphics:
    \BODY
  }%
}
\makeatother


\usepackage{todonotes}
\makeatletter
\renewcommand{\todo}[2][]{%
   \tikzexternaldisable\@todo[#1]{#2}\tikzexternalenable%
}
\makeatother

当不设置任何外部化文件名时,一切正常;当设置外部化文件名时,它会崩溃,显示以下内容:

Package tikz Error: Sorry, the system call 'pdflatex -shell-escape -halt-on-error -interaction=batchmode -jobname "tikz_output/mynd_1" \def\tikzexternalrealjob{main}\input{main}"' did NOT result in a usable output file 'tikz_output/mynd_1' (expected one of .pdf:.jpg:.jpeg:.png:). Please verify that you have enabled system calls. For pdflatex, this is 'pdflatex -shell-escape'. Sometimes it is also named 'write 18' or something like that. Or maybe the command simply failed? Error messages can be found in 'tikz_output/mynd_1.log'. If you continue now, I'll try to typeset the picture.

See the tikz package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.72 \end{myscaletikzwidth}

如何修复此问题?

我的代码如下所示(我目前正在测试这个背页):

\documentclass[twocolumn]{article}

\usepackage{epstopdf}

\usepackage[hypcap=true]{subcaption}

\usepackage{lipsum}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows,arrows.meta,positioning,calc,fit}
\usepackage{tikzscale}
\usepackage{circuitikz}

\usetikzlibrary{external}
\tikzexternalize[prefix=tikz_output/]

\usepackage{environ}

% Automagically scale a Tikz picture, so it has the desired (given) width.
% Does NOT scale line width/text width! Needs the package "environ"!
%
% Usage:
%
% \begin{myscaletikzwidth}{\textwidth}
%   \begin{tikzpicture}[scale=\tikzscale]
%     ..
%   \end{tikzpicture}
% \end{myscaletikzwidth}
%
\makeatletter
\newsavebox{\measure@tikzpicture}
\NewEnviron{myscaletikzwidth}[1]{%
  \tikzifexternalizingnext{%
      \def\tikz@width{#1}%
      \def\tikzscale{1}\begin{lrbox}{\measure@tikzpicture}%
      \tikzset{external/export next=false,external/optimize=false}% force translation of this BODY (and do not optimize it away as it would usually do):
      \BODY
      \end{lrbox}%
      \pgfmathparse{#1/\wd\measure@tikzpicture}%
      \edef\tikzscale{\pgfmathresult}%
      \BODY
  }{% this will re-use an existing external graphics:
    \BODY
  }%
}
\makeatother


\usepackage{todonotes}
\makeatletter
\renewcommand{\todo}[2][]{%
   \tikzexternaldisable\@todo[#1]{#2}\tikzexternalenable%
}
\makeatother




\begin{document}

\begin{figure}[!h]
  \centering
  \begin{subfigure}[b]{0.49\columnwidth}
    \centering
\tikzsetnextfilename{mynd_1}
 \begin{myscaletikzwidth}{\textwidth}
 \begin{tikzpicture}[scale=\tikzscale]  
    \draw[->] (0,0) -- (4.5,0) node[right] {$t$};
    \draw[->] (0,0) -- (0,3) node[above] {$\ddot{s}\left(t\right)$};
    \draw plot[domain=0:4.5,smooth] (\x,{1 + cos(\x r)});
\end{tikzpicture}
\end{myscaletikzwidth}
\caption{}
  \end{subfigure}%
  \begin{subfigure}[b]{0.49\columnwidth}
    \centering
\tikzsetnextfilename{mynd_2}
\begin{myscaletikzwidth}{\textwidth}
\begin{tikzpicture}[scale=\tikzscale]   
  \draw[->] (0,0) -- (4.5,0) node[right] {$t$};
  \draw[->] (0,0) -- (0,3) node[above] {$s\left(t\right)$};
  \draw plot[domain=0:4.5,smooth] (\x,{1 + sin(\x r)});
\end{tikzpicture}
\end{myscaletikzwidth}
    \caption{}
  \end{subfigure}

\end{figure}

\lipsum[1]


\end{document}

相关内容