2

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.

\RequirePackage{luatex85,shellesc}
\documentclass{article}

\usepackage{pgfplots}   
\usepgfplotslibrary{external}
\tikzexternalize[prefix=figures_tex/]
\pgfplotsset{plot coordinates/math parser=false}

\begin{document}

\begin{figure}
    \centering
    \tikzsetnextfilename{test_fig}
    \input{test_fig.tex}
    \caption{figure without comments}
\end{figure}

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

\end{document}

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.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}

\begin{axis}[%
width=6.656cm,
height=5cm,
at={(0cm,0cm)},
scale only axis,
xmin=1,
xmax=5,
xlabel={$x$ [-]},
xmajorgrids,
ymin=1,
ymax=5,
ylabel={$f(x)$ [-]},
ymajorgrids,
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\\
};
\addlegendentry{data1};

\end{axis}
\end{tikzpicture}%

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.