2

I am preparing the final version of a paper to be processed (and edited) by a publisher. This means converting the source to the publisher style, and removing dependency on certain packages.

One of those packages is tikz, and the source contains quite a few tikz figures. It's not so hard to compile all the figures into pdfs using the tikz/externalize library, but this does not remove the dependency on the package since tikz takes care of the inclusion.

Basically, I would like to convert a document:

\documentclass{acmart}

\usepackage{tikz} %\usetikzlibrary{external} %\tikzexternalize[prefix=figures/]

\begin{document}

\title{A paper with tikz figures} \author{Jakub Opršal} \email{jakub@example.com}

\maketitle

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

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

\caption{A circle} \end{figure}

\end{document}

Into:

\documentclass{acmart}

\begin{document}

\title{A paper with tikz figures} \author{Jakub Opršal} \email{jakub@example.com}

\maketitle

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

\begin{figure} \includegraphics{figures/stack-exchange-figure0.pdf}

\caption{A circle} \end{figure}

\end{document}

While compiling all tikz figures in the process (which not such a big issue, since it is enough to compile the original document once).

Do you know of some efficient way to replace the tikz figures in the code by the corresponding graphics include?

  • @JohnKormylo Naturally. But that is not a problem for me, since the files that I will submit are just the resulting pdf figures and the main source file. – Jakub Opršal Mar 18 '21 at 13:04
  • The endfloat package can copy/move any environment (such as tikzpicture) to a separate file. However it will not change the source code. See https://tex.stackexchange.com/questions/412829/longtable-with-endfloat/412944?r=SearchResults&s=5|37.1133#412944 and https://tex.stackexchange.com/questions/423109/export-each-figure-as-a-separate-pdf-file/423253?r=SearchResults&s=11|26.4866#423253 – John Kormylo Mar 18 '21 at 13:13
  • @JohnKormylo That's interesting, but I am afraid not so useful for me. Changing the source code (or producing a new source code) is literally what I would like to do. The tikz/standalone package produces as a byproduct the pdfs of the figures, so in the worst case, I can use those. – Jakub Opršal Mar 18 '21 at 13:27
  • 2
    Just run the tikz in their own document (or use externalize) and then edit the source file to use \includegraphics as you suggest. That is just an editing question, so depends on your editor how easy that is, but I assume in a journal submission you don't have thousands of these to edit. – David Carlisle Mar 18 '21 at 14:14
  • @DavidCarlisle Well, I would prefer to automate that. I am sure I will do the same thing next time, I get a paper published. It's surprising that nobody has done that before. We have scripts for simplifying many source files into one (latexpand) but not for this? – Jakub Opršal Mar 18 '21 at 14:20
  • well in simple cases it would just be a regexp-replace in your editor, so not much scripting needed, but if you need to catch cases where the tikzpicture is hidden in macros etc, it gets harder. Given the non zero chance of a global regex replace script messing up your document completely, if you have less than say 50 of them, doing them by hand is probably safer and quicker than getting (and trusting) an answer here. – David Carlisle Mar 18 '21 at 14:26
  • May using the ifthen package is an option? Using the tikzcode in the one case and the \includegraphics in the other? You would only have to change the value of the switch before submitting. – Markus G. Mar 18 '21 at 17:08
  • Just to note that I am in the same situation again, and it's 9 figures this time. – Jakub Opršal Jun 20 '22 at 09:17
  • Well, in that case what was your strategy to avoid the same issue you already encountered before? Maybe someone can build an answer based on your new approach? – Markus G. Jun 22 '22 at 16:06
  • @MarkusG. No new approaches, just the same problem again. I think I will eventually write the script. – Jakub Opršal Jun 23 '22 at 17:22
  • 1
    TikZ and the external library can be switched out for the tikzexternal package that only imports the pdfs where \tikz and tikzpictures are (without having to change the actual document (just the preamble). Is this what you're after? The tikzexternal.sty can be simply placed besides the main tex file. – Qrrbrbirlbel Oct 18 '22 at 13:24
  • @Qrrbrbirlbel That is closer to what I need, thanks! Though the trouble is usually that the publisher has a list of allowed packages which will not include tikzexternal. I need a tool that changes the TeX source. – Jakub Opršal Oct 19 '22 at 14:05

1 Answers1

0

You could use the ifthen package.

Here is an example:

\documentclass{scrartcl}
\usepackage{graphicx}
\usepackage{ifthen}
\newif\ifsubmit
%\submitfalse % work with this
\submittrue % submit this
\newcommand{\submissionfigure}[2]{\ifsubmit #1 \else #2 \fi}

\submissionfigure{}{ \usepackage{tikz} \usetikzlibrary{external} \tikzexternalize[prefix=figures/] }

\begin{document} \begin{figure} \submissionfigure{ \includegraphics[scale=1]{figures/submission-figure-figure0.pdf} }{
\begin{tikzpicture} \draw (0,0) circle (1); \end{tikzpicture} } \caption{A circle} \end{figure} \end{document}

In principle this should allow you to keep the tikz code in your document, but it should not be processed by the journals systems. Although whether this approach works or not will depend on the journal and how the files are processed, which to me appears a bit intransparent tbh.

Markus G.
  • 2,735
  • Unfortunately, this does not solve my problem since it amounts to editing the source by hand. I could just replace each \begin{tikzpicture}...\end{tikzpicture} with \includegraphics{...} as @DavidCarlisle suggested. The point is that the paper is already written, I just need to produce an intermediate output which is a latex source (to be edited by the journal typesetters) as opposed to pdf or dvi. – Jakub Opršal Mar 19 '21 at 13:06
  • 3
    In that case, you should play with an external tool like Python for example such as to produce automatically a new source from yours. – projetmbc Apr 18 '21 at 07:56