2

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:

\begin{figure}[h]
    \centering
    \setlength\figureheight{6cm}
    \setlength\figurewidth{7cm}
    \input{path/to/tikzfile/figure1.tikz}
    \caption{loremipsum1}
    \label{fig:some:figure}
\end{figure}

and

\begin{figure}[h]
    \centering
    \setlength\figureheight{6cm}
    \setlength\figurewidth{7cm}
    \input{path/to/tikzfile/figure2.tikz}
    \caption{loremipsum2}
    \label{fig:some:other:figure}
\end{figure}

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:

\begin{figure}[h]
    \begin{tikzpicture}
        \setlength\figureheight{5cm}
        \setlength\figurewidth{.4\textwidth}
        \input{path/to/tikzfile/figure1.tikz}
        \subcaption{loremipsum subfigure 1}
    \end{tikzpicture}
    \begin{tikzpicture}
        \setlength\figureheight{5cm}
        \setlength\figurewidth{.4\textwidth}
        \input{path/to/tikzfile/figure2.tikz}
        \subcaption{loremipsum subfigure 2}
    \end{tikzpicture}
\caption{loremipsum for total figure}
\end{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[danish,finnish,english]{babel}
\usepackage{lmodern}
\usepackage[a4paper]{geometry}
\usepackage{graphicx}
\usepackage[font=small]{caption}
\usepackage{subcaption}
\usepackage[table]{xcolor}
\usepackage{pdfpages}
\usepackage{fancyhdr}
\usepackage{layout}
\usepackage{listings}
\usepackage{color}
\usepackage[square,super,numbers,sort&compress]{natbib}
\usepackage{algpseudocode}
\usepackage{algorithmicx}
\usepackage{algorithm}
\usepackage{amsmath}
\usepackage{bm}
\usepackage{amssymb}
\usepackage{pifont}
\usepackage{xfrac}
\usepackage{gensymb}
\usepackage{ctable}
\usepackage{array}
\usepackage{tabularx}
\usepackage{tabulary}
\usepackage{multirow}
\usepackage{pgfplots} % Matlab plots with LaTeX (http://www.howtotex.com/packages/beautiful-matlab-figures-in-latex/)
\usepackage{tikz}
\usepackage{tkz-tab}
\usepackage{latexsym}
\newlength\figureheight
\newlength\figurewidth
\pgfplotsset{compat=1.3} % Adjusts distance between y-label and y-tick in tikz-figures
\usetikzlibrary{plotmarks}
\usepackage{rotating}
\usepackage{siunitx}
\usepackage{nth}
\usepackage{lipsum}
\usepackage[titletoc, toc]{appendix}
\usepackage[hyperindex=false,pdftitle={HybridPolarizationSAR},pdfauthor={AuthorName}]{hyperref}
\usepackage{longtable}
\usepackage[numbered, framed, final]{mcode}
\usepackage[
    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
    {glossaries}

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
% 
http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz-matlab2tikz
%where you can also make suggestions and rate matlab2tikz.
%
\begin{tikzpicture}
\begin{axis}[%
width=0.799\figurewidth,
height=\figureheight,
at={(0\figurewidth,0\figureheight)},
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}},
xmin=0.5,
xmax=3601.5,
xlabel={Range},
every outer y axis line/.append style={black},
every y tick label/.append style={font=\color{black}},
y dir=reverse,
ymin=0.5,
ymax=4201.5,
ylabel={Azimuth},
axis background/.style={fill=white},
title={$some latex math formula title$},
colormap/jet,
colorbar,
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};
\end{axis}
\end{tikzpicture}%

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 Mar 18 '16 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 Mar 19 '16 at 12:52
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):

\begin{figure}[h]
    \begin{minipage}[b]{0.5\linewidth}
        \setlength\figureheight{5cm}
        \setlength\figurewidth{0.5\textwidth}
        \input{path/to/tikzfile/figure1.tikz}
        \subcaption{loremipsum subfigure 1}
    \end{minipage}
    \begin{minipage}[b]{0.5\linewidth}
        \setlength\figureheight{5cm}
        \setlength\figurewidth{0.5\textwidth}
        \input{path/to/tikzfile/figure2.tikz}
        \subcaption{loremipsum subfigure 2}
    \end{minipage}
    \caption{loremipsum for total figure}
\end{figure}

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

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.