2

mwe.tex:

%\def\Externalize{1} %This would work perfectly

\documentclass{article}
%\documentclass[varwidth=160mm]{standalone} %This has same problem

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
\usetikzlibrary{external}
\tikzset{
    tex4ht inc/.style={
        /pgf/images/include external/.code={%
            \includegraphics[]{##1.svg}%
        }
    }
}
\tikzset{
    external/system call/.add={}{%
        ; pdf2svg "\image.pdf" "\image.svg" "1"%
    }
}
\makeatletter
\@ifpackageloaded{tex4ht}{
    \tikzexternalize[mode=only graphics]%
}{
    \ifdefined\Externalize\tikzexternalize\fi% %% This produces weird     result
    %\tikzexternalize %%This works as expected even when latex ... "\def\Externalize{1} \input{mwe.tex}" is used
}
\makeatother

\begin{document}
\newsavebox{\MyPictureA}%
\savebox{\MyPictureA}{%
    \begin{tikzpicture} [scale=0.6, baseline={(current bounding box.center)}]   
    \tikzset{grid lines/.style={very thin, color=gray}}
    \draw [grid lines, step=1cm] (-1.5,-1.5) grid (7.2,6.2);

    \draw [very thick] (0,0) -- ++(4,0) -- ++(45:2) -- ++(0,4) -- ++(-4,0) -- ++(225:2) -- cycle;
    \draw [very thick] (4,0) -- (4,4) -- (0,4);
    \draw [very thick] (4,4) -- ++(45:2);   
    \end{tikzpicture}
}%
%
Kuutiojuuri on määritelmällisesti kolmanteen potenssiin korotuksen käänteistoimitus. Muistetaan, että kuution tilavuus saadaan laskettua korottamalla särmän pituus potenssiin kolme.

\begin{minipage}[c]{\textwidth - \wd\MyPictureA - 2em}
Esimerkiksi kuution, jonka särmän pituus on $4$, tilavuus saadaan laskemalla \mbox{$4^3=64$}.
\end{minipage}
\quad
\usebox{\MyPictureA}

Jos halutaan selvitää kuution särmän pituus, kun tilavuus tunnetaan. Pitäisi miettiä, mikä luku korotettuna potenssiin kolme tuottaa tilavuuden. Tätä laskutoimitusta voidaan merkitä kuutiojuurella.
\end{document}

Compiled with a command:

$ pdflatex --shell-escape "\def\Externalize{1} \input{mwe.tex}"

Produces a result like this:

mwe.pdf

Instead of making and external pdf of the tikzpicture it makes external pdf of the whole document.

How can I get this to compile normally?

Edit: Some additional info.

I want to achieve same thing as in here: https://tex.stackexchange.com/a/158921/103166 but only when I compile to html.

Compilation to html with externalized tikz to svg has to be done (?) in two steps:

  1. Normal pdflatex run to produce svgs

  2. Compilation with htlatex or make4ht to produce html that includes svgs produced during previous phase.

I also sometimes compile to regular pdf and I'd prefer not to use externalization then. Although externalization wouldn't be a problem, I would still like to know how to achieve this.

I use a bash script for compilation:

compile2html.sh:

#!/bin/bash
pdflatex --shell-escape "\def\Externalize{1} \input{$1}"
make4ht -u -e mwe.mk4 -c mwe.cfg $1

mwe.mk4:

settings.latex_par = settings.latex_par .. " -interaction=nonstopmode"

mwe.cfg:

\Preamble{xhtml,mathml,html5}
\Configure{HTML}{\HCode{<html lang="en">\Hnewline}}
{\HCode{\Hnewline</html>}}

\Configure{@HEAD}{\HCode{\Hnewline<script type="text/javascript"
   src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?
config=MML_CHTML">
   </script>\Hnewline}}

\makeatletter
\Configure{graphics*}  
         {svg}{  
          {\Configure{Needs}{File: \Gin@base.svg}\Needs{}}
          \Picture[\csname a:GraphicsAlt\endcsname]{\csname 
Gin@base\endcsname.svg \csname a:Gin-dim\endcsname}%  
         }  
\makeatother

\begin{document}

\ifdefined\tikzexternalize\tikzexternalize[mode=only graphics]\fi
\ifdefined\tikzset\tikzset{tex4ht inc}\fi

\EndPreamble
  • What are you trying to do with the \Externalize trick, given that you use \tikzexternalize in all cases? – cfr Aug 26 '17 at 02:33
  • @cfr I'm trying to achieve same thing as in the post I referred to but when I compile my documents to pdf I don't need extrnalization. That's why I want to keep externalization optional. – Jarno_C-137 Aug 26 '17 at 14:16
  • So why not just not use \tikzexternalize in that case? Why the messing around with \Externalize? – cfr Aug 26 '17 at 14:47
  • \@ifpackageloaded{tex4ht}{ \tikzexternalize[mode=only graphics]% }{} - why isn't this what you want? – cfr Aug 26 '17 at 14:48
  • The solution in the referred post requires additional normal compilation without tex4ht to first produce the svg-images. After that the the tex4ht run can include them in the html. If I omit the \tikzexternalize, the svgs don't get produced and the html step would fail (I think?). Htlatex cannot (for some reason?) produce svg with pdf2svg(preferred way) and html simultaneously. I could use @ifpackageloaded{tex4ht}{ \tikzexternalize[mode=only graphics]}{\tikzexternalize}. That would work but then it uses tikzexternalize even when I'm not producing regular pdf. – Jarno_C-137 Aug 26 '17 at 15:29
  • OK. Thanks. Now it makes sense. I'd suggest editing your question with a bit more explanation for people who (like me) aren't familiar with the tex4ht procedure for producing the SVGs. (Personally, I use externalisation whenever I can, although I only ever produce PDFs, because it saves so much compilation time.) – cfr Aug 26 '17 at 16:12
  • The bash script isn't part of the problem, right? Using the command you give directly has the same results? – cfr Aug 27 '17 at 01:17
  • Please upload images using the image button in the editor so that they can be displayed inline. – cfr Aug 27 '17 at 01:19
  • Change your document class to article. standalone used the way you're using it will produce the whole thing as a single PDF. You should still get the picture externalised, but the situation will be a lot simpler, at least, if standalone is taken out of it. – cfr Aug 27 '17 at 01:23
  • @cfr I changed the documentclass to article and the result is same. Running the second line (pdflatex --shell-escape ...) alone produces exactly the same result. I tried every imaginable combination of \def:ing and \let:ing and \newbool from etoolbox. As long as there is any connection between \def\Externalize{} at the command line and actual \tikzexternalize, problem persists. When that connection is broken, result is as expected. Very weird problem. I also tried messing with -jobname= -argument with no improvement. – Jarno_C-137 Aug 27 '17 at 16:54

1 Answers1

2

I started by minimising your example, since TeX4ht is really irrelevant. The question is purely about TikZ externalisation. I also simplified the picture and content of the document, and substituted a different macro for \Externalize (just in case).

This gave me

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzset{
  external/system call/.add={}{%
    ; pdf2svg "\image.pdf" "\image.svg" "1"
  }
}
\makeatletter
\@ifpackageloaded{tex4ht}{
  \tikzexternalize[mode=only graphics]%
}{
  \ifdefined\MiXExternalize
  \tikzexternalize
  \fi
}
\makeatother

\begin{document}
\newsavebox{\MyPictureA}%
\savebox{\MyPictureA}{%
  \begin{tikzpicture}
    \draw (0,0) rectangle (1,1);
  \end{tikzpicture}
}%
Kuutiojuuri on määritelmällisesti kolmanteen potenssiin korotuksen käänteistoimitus. Muistetaan, että kuution tilavuus saadaan laskettua korottamalla särmän pituus potenssiin kolme.

\usebox{\MyPictureA}

Jos halutaan selvitää kuution särmän pituus, kun tilavuus tunnetaan. Pitäisi miettiä, mikä luku korotettuna potenssiin kolme tuottaa tilavuuden. Tätä laskutoimitusta voidaan merkitä kuutiojuurella.
\end{document}

as a test case. I don't think the boxing has any relevance either, but I left it in just to be on the safe-side.

Then

pdflatex --shell-escape "\def\MiXExternalize{1}\input{<filename>}`

will reproduce the problem. Externalisation occurs, but the PDF and SVG files contain the entire page and not just the picture.

If you look at the console output, it becomes more obvious what's going wrong here.

===== 'mode=convert with system call': Invoking 'pdflatex -shell-escape -halt-on-error -interaction=batchmode -jobname "<filename>-figure0" "\def\tikzexternalrealjob{<filename>}\input{<filename>}"; pdf2svg "<filename>-figure0.pdf" "<filename>-figure0.svg" "1" ' 
========

The problem with this is that \MiXExternalize is not defined when the image is compiled. So, although the externalisation happens, it \tikzexternalize doesn't get executed on the externalisation run. To address this, we need our system call to define the macro, too.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzset{
  external/system call={%
    pdflatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\\def\\MiXExternalize{1} \\input{\texsource}";
    pdf2svg "\image.pdf" "\image.svg" "1"
  }
}
\makeatletter
\@ifpackageloaded{tex4ht}{
  \tikzexternalize[mode=only graphics]%
}{
  \ifdefined\MiXExternalize
  \tikzexternalize
  \fi
}
\makeatother

\begin{document}
\newsavebox{\MyPictureA}%
\savebox{\MyPictureA}{%
  \begin{tikzpicture}
    \draw (0,0) rectangle (1,1);
  \end{tikzpicture}
}%
Kuutiojuuri on määritelmällisesti kolmanteen potenssiin korotuksen käänteistoimitus. Muistetaan, että kuution tilavuus saadaan laskettua korottamalla särmän pituus potenssiin kolme.

\usebox{\MyPictureA}

Jos halutaan selvitää kuution särmän pituus, kun tilavuus tunnetaan. Pitäisi miettiä, mikä luku korotettuna potenssiin kolme tuottaa tilavuuden. Tätä laskutoimitusta voidaan merkitä kuutiojuurella.
\end{document}

With this code in place, the PDF and SVG externalisation captures just the content of the tikzpicture, as desired.

cfr
  • 198,882
  • I tested it and it is unrelated to the savebox. Your solution also works as expected with standalone class. Thank you very much! – Jarno_C-137 Aug 28 '17 at 11:53
  • @Jarno_C-137 Oh, good. The standalone thing was a guess because that class also tries to crop stuff down etc. by default. Glad you got it sorted. – cfr Aug 28 '17 at 12:40