I want to make comments onto a matlab2tikz figure. One way is to nest the matlab generated code using a \node inside a tikzpicture.

However, when I do so, the axis labels and axis tick labels get "messed up". To show you, my MWE consists of two figures: One normal one and one nested figure with messed up ticks+labels.


\pgfplotsset{plot coordinates/math parser=false}


    \caption{figure without comments}

        %%The matlab2tikz plot is drawn as a node
        \node[anchor=south west,inner sep=0] (myplot) at (0,0) {
        \begin{scope}[x={(myplot.south east)}, y={(myplot.north west)}]
            \draw[<-,thick,red] (0.6,.53)--(.7,.4) 
            node[right,draw=red,rounded corners,
            text width=1cm,align=center,text=black] 
    \caption{figure with comments}


The test_fig.tex file, which was created using matlab2tikz:

% 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.

scale only axis,
xlabel={$x$ [-]},
ylabel={$f(x)$ [-]},
axis background/.style={fill=white},
axis x line*=bottom,
axis y line*=left,
legend style={legend cell align=left,align=left,draw=white!15!black},
legend style={font=\tiny}, ticklabel style={font=\footnotesize}, xlabel style={font=\small}, ylabel style={font=\small}
\addplot [color=mycolor1,solid,line width=1.2pt]
   table[row sep=crcr]{%
1   1\\
2   2\\
3   3\\
4   4\\
5   5\\


For me the result looks like: Normal and nested figure

You can see that the tick labels overlap with the axes. Therefore my question: How to retain its format when nesting the matlab2tikz figure inside a \node?

Thank you in advance! Any help is appreciated!

  • 1
    Your question is the wrong question. Never nest tikzpictures. In simple cases, it may work, but it is expected to break and should (almost) always be avoided. tikzmark provides \subnode for these kinds of cases. – cfr Dec 2 '16 at 20:26
  • Well, unfortunately I am not familiar with tikz/pgfplots very much. I only use the matlab2tikz tool tbh. That might be why I try to solve the "wrong problem". Anyways, thanks for the hint...I've checked tixmark. It seems I have to alter the matlab2tikz code, but I would like to avoid this in order to easily update the matlab figure. – Erisch889 Dec 2 '16 at 20:42
  • 1
    You could include it as an image. You should also try not externalising it. – cfr Dec 2 '16 at 21:57
  • 1
    I'm not sure how well tikzmark would work with pgfplots anyway. Including it as an external image is probably the way to go here. – cfr Dec 2 '16 at 21:58
  • Jap, actually that is what I did in the end: included it as an external image. It is a little workaround since I have to compile the new image first when the figure is updated....but it is reasonable effort. Thanks and I'll close this one. – Erisch889 Dec 3 '16 at 10:03

Your Answer

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

Browse other questions tagged or ask your own question.