2

I generate a plot in MATLAB using the code below (MWE):

x = [1:100];
y = x.^0.5;
[hLine1]=semilogx(x,y);
hleg1 = legend(sprintf('%.4f',0.4334));
set(get(hleg1,'title'),'string','test');
set(hleg1, 'EdgeColor', 'w');
set(hleg1,'Location','NorthWest');

sample matlab plot.

(The legend title seems to not be factored in when using set location Northwest but that is a question for the MATLAB S.E...)

I slide the legend to a different position in the plot and then convert it to a .tikz.

It is compiled in latex with:

\documentclass[11pt,a4paper]{report}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{figure}
\centering
    \input{fig1.tikz}
    \caption{...}
\end{figure}

\end{document}

This creates the following plot:

latex graph

the legend's title has disappeared. I can not find it anywhere in the .tikz created file.

So this may be a glitch with matlab2tikz but any workaround? Thanks.

Edit: So this is probably a matlab2tikz issue. Leaving that bit, I managed to get a legend title in tikz using some code I found. But it does not center the title over the line + text, just the text. Full MWE to show the problem:

\documentclass{article}
\usepackage{pgfplots}
% and optionally (as of Pgfplots 1.3):
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\newlength\figureheight
\newlength\figurewidth
\begin{document}

\begin{tikzpicture}

\begin{axis}[%
width=4.52083333333333in,
height=3.565625in,
scale only axis,
xmode=log,
xmin=1,
xmax=100,
xminorticks=true,
ymin=1,
ymax=10,
legend style={at={(0.03,0.97)},anchor=north west,draw=none,legend cell align=left}
]
\addlegendimage{legend image code/.code=}   %These two lines add the title to the legend
\addlegendentry{example}                    %But I would like it centered over the line and the text
\addplot [color=blue,solid]
  table[row sep=crcr]{1 1\\
2   1.4142135623731\\
3   1.73205080756888\\
4   2\\
5   2.23606797749979\\
6   2.44948974278318\\
7   2.64575131106459\\
8   2.82842712474619\\
9   3\\
10  3.16227766016838\\
};
\addlegendentry{0.4334};
\end{axis}
\end{tikzpicture}%
\end{document}

legend title example

How could I center the legend title? Thanks

  • Is this the version of matlab2tikz, http://github.com/nschloe/matlab2tikz, which you are using? – Malipivo Apr 05 '14 at 15:06
  • It is supposed to convert to a .tex file according to the documentation. You have a .tikz file. Is that a typo? – dustin Apr 05 '14 at 15:36
  • @Malipivo Hi, yes I am using the latest version: v0.4.7. – Steve Hatcher Apr 05 '14 at 15:58
  • @dustin on the mathworks website the example produced a .tikz. Either way they come out with the same contents and I have been using it fine with .tikz up to now. Thanks – Steve Hatcher Apr 05 '14 at 16:01
  • 1
    I believe that sounds like a feature request (perhaps for matlab2tikz?). Note that pgfplots offers a title for the entire figure (but none for the legend). – Christian Feuersänger Apr 05 '14 at 18:16
  • 1
    @dustin .tikz, .tex, doesn't matter. .tikz is used a lot to indicate that it just contains TikZ/pgfplots code, but it just a matter of taste. @Steve: If matlab2tikz doesn't pick up the legend title, as it appears, then you should add a feature request on GitHub as Christian says. If you would like to add a title manually, see e.g. http://tex.stackexchange.com/questions/2311/ – Torbjørn T. Apr 05 '14 at 21:49
  • @Christian Feuersänger. Thanks, maybe because pgfplots does not have a standard title feature for the legend this was not so easy for matlab2tikz to impliment. – Steve Hatcher Apr 06 '14 at 05:53
  • @Torbjørn T Thanks, I may ask them, for the mean time I am doing it manually using some code I found: \addlegendimage{legend image code/.code=} \addlegendentry{title}. The only problem with this method is it does not center the title over the line + text annotations under it, it justifies it with the text. Trying not to ask a second question but I think this falls into kind of the same category of what I originally wanted. I will edit my original question for this. – Steve Hatcher Apr 06 '14 at 05:55
  • 1
    (Did you see the post I linked to in my previous comment?) A non-automatic way of doing it is adding some negative horizontal space just before the title, i.e. \hspace{-0.5cm}example. – Torbjørn T. Apr 06 '14 at 09:16
  • @Torbjørn T. Thank you. I originally tried the one that you linked me but it would override the line description. Combining that with the blank legend image worked perfectly. Thanks a lot. – Steve Hatcher Apr 06 '14 at 09:51

0 Answers0