I've tried to create a figure in LaTeX for my MSc thesis using two separate tikz figures. The tikz figures were exported from MatLAB using the following command:

matlab2tikz('tikz/figure.tikz', 'height', '\figureheight', 'width', '\figurewidth');

The result is a tikz file that points to a corresponding .png file, as this is not a plot, but an image. That seems to be what is causing issues.

To have it said, the goal with using the tikz approach, is to maintain the fonts used in the LaTeX document for the axis labels etc.

Now, the figures are in fact easily added as two separate figures as such:




However, whatever I try, I cannot seem to display them side by side? I've tried quite a lot of suggestions found online, but in most cases I get the "TeX capacity exceeded, sorry [grouping levels=255]. \end{axis}", which points to the first added tikz file.

I've for example tried this:

        \subcaption{loremipsum subfigure 1}
        \subcaption{loremipsum subfigure 2}
\caption{loremipsum for total figure}

And I've tried wrapping each tikzpicture tag in minipages as such: \begin{minipage}[b]{.5\linewidth} ... \end{minipage}).

The minipage approach works brilliantly if the tikz-file is a plot, I've successfully managed to put both two plots side-by-side, and four plots in a 2x2 formation into the document. But if as in this case, the tikz file points to a .png, it seems impossible to make this work?

Can anyone help me understand what I'm doing wrong, or let me know if I've completely lost my way here?

My preamble contains (I've added all usepackage statements in case I've "done stupid" and/or got some incompatible stuff here):

\documentclass[12pt, a4paper]{book}
\usepackage[utf8]{inputenc} % File encoding, utf8 includes æ ø å
\usepackage[T1]{fontenc} % Use EC fonts
\usepackage{ae} % Fonts for pdf-files
\usepackage{textcomp} % Text-companion-symbols (e.g. \texteuro)
\usepackage{pgfplots} % Matlab plots with LaTeX (http://www.howtotex.com/packages/beautiful-matlab-figures-in-latex/)
\pgfplotsset{compat=1.3} % Adjusts distance between y-label and y-tick in tikz-figures
\usepackage[titletoc, toc]{appendix}
\usepackage[numbered, framed, final]{mcode}
    nonumberlist,   % Do not show page numbers for where listing is found
    acronym,        % Generate acronym listing
    sort=def,   % Sorting. Options are standard, def and use
    toc]            % Show listing as entries in table of contents

The tikz files look like so (equal apart from the \addplot and some point meta min/max, titles etc):

% This file was created by matlab2tikz
%The latest updates can be retrieved from
%where you can also make suggestions and rate matlab2tikz.
scale only axis,
point meta min=-44.9058788364739,
point meta max=42.6233629409421,
axis on top,
separate axis lines,
every outer x axis line/.append style={black},
every x tick label/.append style={font=\color{black}},
every outer y axis line/.append style={black},
every y tick label/.append style={font=\color{black}},
y dir=reverse,
axis background/.style={fill=white},
title={$some latex math formula title$},
colorbar style={separate axis lines,every outer x axis line/.append style={black},every x tick label/.append style={font=\color{black}},every outer y axis line/.append style={black},every y tick label/.append style={font=\color{black}}}
\addplot [forget plot] graphics [xmin=0.5,xmax=3601.5,ymin=0.5,ymax=4201.5] {path/to/tikz/figure/figure-1.png};

Thanks in advance.

  • If you don't wrap them in tikzpicture environments one-by-one, why do you wrap them in it when putting them side-by-side? I don't understand the idea of using TikZ. If they are PNG files, whatever fonts you use in the document will have no effect on the images at all. They may happen to match, if you're careful, but you'd just adding a bitmap graphic and TikZ isn't relevant. But I don't know why you think they are PNGs when they seem to be code. In any case, just don't nest tikzpicture environments as it causes trouble very often (though not absolutely always).
    – cfr
    Commented Mar 18, 2016 at 2:04
  • OMG, you are perfectly right! I cannot believe I didn't see that. Of course, the tikzfile contains a begin/end tikzpicture as well, which will clog this up. It is by the end of the day a png though, as the tikzfile points to a png, bug anyway; this works:
    – Vandalf
    Commented Mar 19, 2016 at 12:52

1 Answer 1


As @cfr very correctly points out, I've just wrapped the images in double tikzpicture environments, once in the .tex file and one in the .tikz file... Of course that won't work...

The following code will work though (notice there's no changes done to the .tikz, I've just changed the call to the .tikz file in the .tex file):

        \subcaption{loremipsum subfigure 1}
        \subcaption{loremipsum subfigure 2}
    \caption{loremipsum for total figure}

Guess sometimes one just gets blind staring at things...

Thanks a million to @cfr, you saved my weekend! :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .