1

I'm writing my PhD thesis and I have just discovered the matlab2tikz script for MATLAB so I wanted to convert my plots and put them in the document using pgfplots. My thesis is already very big and when I compile it takes a long time so I wanted to make use of the standalone package to compile the graphs externally so as to speed up the process. matlab2tikz outputs a .tex or .tikz file with the necessary code, an example is:

\begin{tikzpicture}
\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt        
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
\draw   (23,42) .. controls (23,28.19) and (34.19,17) .. (48,17) .. controls (61.81,17) and (73,28.19) .. (73,42) .. controls (73,55.81) and (61.81,67) .. (48,67) .. controls (34.19,67) and (23,55.81) .. (23,42) -- cycle ;
\end{tikzpicture}
\end{tikzpicture}

In this case, I saved the file as a .tex. Then, following the instructions of the standalone package manual, I modified the file as follows

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt        
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
\draw   (23,42) .. controls (23,28.19) and (34.19,17) .. (48,17) .. controls (61.81,17) and (73,28.19) .. (73,42) .. controls (73,55.81) and (61.81,67) .. (48,67) .. controls (34.19,67) and (23,55.81) .. (23,42) -- cycle ;
\end{tikzpicture}
\end{document}

and saved it as test.tex. When I try to compile the document, whether I have \includestandalone[width=\textwidth]{test} or \input{test} I get Can be used only in preamble error. I'm sure it has to do with the fact that test.tex is not in the main document folder, it's instead in the same folder as all the images. My question is, therefore, this: What should be included in the preamble to point to the folder that contains the files with the TikZ plots? Below is my preamble with a minimal working example:

\documentclass[11pt,english,a4paper]{report}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{mathpazo}
\usepackage{setspace}
\usepackage[acronym,nopostdot,style=index,nogroupskip,nomain]{glossaries-extra}
\usepackage{epsfig}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{ragged2e}
\usepackage{graphicx}
\usepackage{comment,array,url}
\usepackage[font={small,it},center]{caption}
\usepackage[list=true]{subcaption}
\usepackage{multirow}
\usepackage{geometry}
\usepackage{framed}
\usepackage[Conny]{fncychap}
\usepackage[toc,page]{appendix}
\usepackage{bibentry}
\nobibliography*
\usepackage{gensymb}
\usepackage{standalone}
\usepackage{pgfplots}
\usepackage[hidelinks]{hyperref}

\makeglossaries

\setabbreviationstyle[acronym]{long-short}

\loadglsentries{D:/Dropbox/NTU/Research/Resources/03_glossary.tex}

\graphicspath{{D:/Dropbox/NTU/Research/Resources/02_All_images/}{}}

\begin{document}
\begin{figure}
\includestandalone[width=\textwidth]{test}
\caption{Caption}
\end{figure}
\end{document}
epR8GaYuh
  • 2,432
enea19
  • 107

2 Answers2

0

I use a slightly different approach. I save all TikZ files only with \begin{tikzpicture} and its \end and then insert them with \input{fig_TikZ_name}. Therefore, the preamble of the main file must have the \usepackages for tikz and pgfplots.

The message Can be used only in preamble appears when a \usepackage is inside the \begin{document}. To edit those TikZ file I use TikzEdt or a minimal .tex that only inserts the TikZ file I'm editing.

The package \adjustbox is just one option to scale the picture, not best, not all-mighty, just useful in many scenarios.

It may still be possible to use the approach with standalone, I just don't know how or if it is possible. Sorry for that.

A MWE follows.

fig_TikZ_name.tex file:

\begin{tikzpicture}
    \draw[blue, ->]   (0,0) -- (10,10);
\end{tikzpicture}

Main file:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots} % Can be used only in preamble. \usepackage

\usepackage{adjustbox}

\begin{document}
\begin{figure}[h]
    \centering
    \input{figTikZname}
    \caption{text}
\end{figure}
\begin{figure}[h]
    {\adjustbox{width = 0.9\linewidth}{\input{figTikZname}}}
    \caption{adjustbox}
\end{figure}
\input{figTikZname}
\end{document}

enter image description here

FHZ
  • 3,939
  • 2
    I also thought of using the same method but from what I understand the problem is that then the TikZ pictures have to be compiled every time I compile the document, whereas using standalone they are only compiled once. Am I wrong as far as you know? – enea19 Feb 14 '20 at 02:33
  • Maybe this can help: https://tex.stackexchange.com/q/45/140133. Check the answer about the PGF Manual and some of the comments. – FHZ Feb 14 '20 at 02:51
  • This defies the purpose of OP putting the pictures/figures in separate files – hpekristiansen Feb 25 '24 at 07:36
0

Be careful when you modify generated code. In your case, you have two \begin{tikzpicture} but only one \end{tikzpicture}.

The correct test.tex file should be:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
    \tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt        
    \begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
        \draw   (23,42) .. controls (23,28.19) and (34.19,17) .. (48,17) .. controls (61.81,17) and (73,28.19) .. (73,42) .. controls (73,55.81) and (61.81,67) .. (48,67) .. controls (34.19,67) and (23,55.81) .. (23,42) -- cycle;
    \end{tikzpicture}
\end{document}

I also removed innecesary stuff from your MWE:

\documentclass[11pt,english,a4paper]{report}
\usepackage{graphicx}
\usepackage{standalone}
\usepackage{pgfplots}
\usepackage[hidelinks]{hyperref}

\begin{document} \begin{figure} \includestandalone[width=\textwidth]{test} \caption{Caption} \end{figure} \end{document}

This compiles without error.

output

Mane32
  • 1,252