1

I have a document (two-column article) with several TikZ images, and I want to:

  1. Scale the figures to half of the column width, without changing the text's font size
  2. Externalize the figures inside the folder ./tikz_output
  3. Set externalized filenames, avoiding automatically generated names by externalize, using tikzsetnextfilename

I found the code below from this old question, which fulfills points 1 and 2, but fails to work when setting externalized filenames:

% 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

When not setting any externalized filenames, everything works well; when setting externalized filenames, it crashes, displaying the following:

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}

How can this be fixed?

My code looks like this (I am currently testing this in Overleaf):

\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}
  • Cymraeg? ('mynd' yw 'go'.) – cfr Feb 4 '18 at 19:22
  • Ah, indeed. Is there a vowel in it for you? ('y' is a vowel for me.) – cfr Feb 4 '18 at 19:55
  • It is pronounced "mint" – erik Feb 4 '18 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.