LaTeX forum ⇒ Graphics, Figures & TablesTikz for matlab plots - randomly fails

Information and discussion about graphics, figures & tables in LaTeX documents.
jacql
Posts: 5
Joined: Wed Aug 28, 2013 11:08 am

Tikz for matlab plots - randomly fails

Postby jacql » Wed Aug 28, 2013 11:49 am

I apologise for the long essay, but it's an obscure error with no useful messages.

I'm writing up my thesis and want to include a lot of MATLAB plots. I'm using matlab2tikz to export the plots into tikz files and, for those which work, they look fabulous in the LaTeX result, so I'd really like to get them to work, if possible. I've used the same code to export 8 similar graphs and I've checked that the files are absolutely identical (other than the data, of course), but three of them refuse to work. I don't think it has anything to do with the tikz code in the files themselves, because I got one of them to work when I changed the order in my .tex file to run it first. But now there are three left which won't work, even if they are the only tikz input calls in the .tex file.

I've trawled through the logfiles produced for each image and can see no errors, other than a few of these which occur just as the image is being built:
Overfull \hbox (8.30591pt too wide) in paragraph at lines 23--23

In the Build output window, all that happens is this:
----------
"filename"This is pdfTeX, version 3.1415926-2.4-1.40.13 (MikTeX 2.9 64-bit)
entering extended mode
===== 'mode=convert with system call': Invoking 'pdflatex -shell-escape -halt-on-error -interaction=batchmode -jobname "
tikzfigures/xrdFitCa415" "\def\tikzexternalrealjob{Thesis}\input{Thesis}"' ========
)
<some Overfull hbox messages, because the image isn't large enough, I think>
[4 <xrdFitCa415.pdf>]
------------

The external call to pdflatex works and an image is produced, but it's only a fragment of the full plot and all squashed up (see
xrdFitCa415.pdf
failed pdf
(45.02 KiB) Downloaded 265 times
). It should look like this:
xrdFitCa355.pdf
this
(176.8 KiB) Downloaded 213 times
and there is no code difference between the tikz files.

Can it have something to do with the plot size (there are 6 datasets per figure)? I switched to externalization because I had memory errors before, but perhaps there is still some sort of limit to how much data can fit into the LaTeX result?

Alternately, can I convert each image to pdf manually from the tikz files and if so, how? Perhaps then I would see more errors.

Any help will be greatly appreciated. Also let me know if you need more information.

Tags:

User avatar
localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Wed Aug 28, 2013 11:55 am

Without a concrete code example it is nearly impossible to spot the probable cause for the problem. Perhaps it's easier to export the data sets to CSV files and plot them with pgfplots. If you are unsure how to do that, attach a sample data set (e.g. that of the presented plot) to your next post by upload to the forum server and we will see what can be done. If there are functions to be plotted, please add them completely with all parameters.


Best regards and welcome to the board
Thorsten
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

jacql
Posts: 5
Joined: Wed Aug 28, 2013 11:08 am

Postby jacql » Wed Aug 28, 2013 1:18 pm

Fair enough - I just wasn't sure which code bits to display and was hoping someone would recognise it as a similar problem.

In my main project .tex file, I have:
  1. %% Packages for Graphics & Figures %%%%%%%%%%%%%%%%%%%%%%%%%%
  2. \usepackage{graphicx} %%For loading graphic files
  3. \usepackage{caption}
  4. \usepackage[position=top]{subfig}
  5. \usepackage{float}
  6. \usepackage{tikz,pgfplots}
  7. \pgfplotsset{compat=newest}
  8. \pgfplotsset{
  9. every tick label/.style = {font=\scriptsize},
  10. every axis label/.style = {font=\scriptsize},
  11. }
  12. % needed for memory management with tikz images
  13. \usetikzlibrary{external}
  14. \tikzexternalize[optimize=false,prefix=tikzfigures/]
  15. % define these for drawing figures
  16. \newlength\figurewidth
  17. \newlength\figureheight
  18. \newlength\fullwidth
  19. \newlength\fullheight
  20. \setlength\fullwidth{14cm}
  21. \setlength\fullheight{9cm}
  22.  
  23. \DeclareGraphicsExtensions{.pdf,.jpg,.png}


This file includes a .tex file, which further includes a .tex file - mentioning this, just in case it's a case of overnesting?. In this last .tex file is the code to call the figures:

  1. \begin{figure}[H]
  2. \begin{center}
  3. \setlength\figurewidth{5.5cm}
  4. \setlength\figureheight{6cm}
  5. \subfloat[300 K]{
  6. \tikzsetnextfilename{xrdFitNaRT}
  7. \input{figures/xrdFitNaRT.tikz}
  8. \label{fig:xrdNaFitsRT}
  9. }
  10. \subfloat[355 K]{
  11. \tikzsetnextfilename{xrdFitNa355}
  12. \input{figures/xrdFitNa355.tikz}
  13. \label{fig:xrdNaFits355}
  14. }\qquad
  15. \subfloat[415 K]{
  16. \tikzsetnextfilename{xrdFitNa415}
  17. \input{figures/xrdFitNa415.tikz}
  18. \label{fig:xrdNaFits415}
  19. }
  20. \subfloat[475 K]{
  21. \tikzsetnextfilename{xrdFitNa475}
  22. \input{figures/xrdFitNa475.tikz}
  23. \label{fig:xrdNaFits475}
  24. }\qquad
  25. \caption{blah blah}
  26. \label{fig:xrdNaFits}
  27. \end{center}
  28. \end{figure}
  29.  
  30. \begin{figure}[H]
  31. \begin{center}
  32. \setlength\figurewidth{5.5cm}
  33. \setlength\figureheight{6cm}
  34. \subfloat[355 K]{
  35. \tikzsetnextfilename{xrdFitCa355}
  36. \input{figures/xrdFitCa355.tikz}
  37. \label{fig:xrdCaFits355}
  38. }
  39. \subfloat[415 K]{
  40. \tikzsetnextfilename{xrdFitCa415}
  41. \input{figures/xrdFitCa415.tikz}
  42. \label{fig:xrdCaFits415}
  43. }\qquad
  44. \subfloat[475 K]{
  45. \tikzsetnextfilename{xrdFitCa475}
  46. \input{figures/xrdFitCa475.tikz}
  47. \label{fig:xrdCaFits475}
  48. }
  49. \subfloat[300 K]{
  50. \tikzsetnextfilename{xrdFitCaRT}
  51. \input{figures/xrdFitCaRT.tikz}
  52. \label{fig:xrdCaFitsRT}
  53. }\qquad
  54. \caption{blah blah}
  55. \label{fig:xrdCaFits}
  56. \end{center}
  57. \end{figure}
Last edited by localghost on Wed Aug 28, 2013 1:26 pm, edited 1 time in total.
Reason: Source code has to be tagged as such to keep a post clear and legible (see Board Rules).

jacql
Posts: 5
Joined: Wed Aug 28, 2013 11:08 am

Postby jacql » Wed Aug 28, 2013 1:20 pm

Update: I have discovered that the small, squashed up image is due to not resetting the figure dimensions just before calling, however this still does not resolve the problem of some images just not forming at all.

When I isolate each tikz input into its own .tex file and include only this file (leaving out the full set of figures) then each independent image works. This means that I can get around the problem by creating each one separately and thereafter tikz sees that a .pdf exists and moves on. However, it would be nice to know why they don't work together. (this is why I suspected some sort of memory error, despite externalizing)

User avatar
localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Wed Aug 28, 2013 1:29 pm

Please provide a self-contained and minimal example as described on the page when following the link in my last reply. At the moment your code is quite useless because not compilable as is. And we would appreciate it very much if you use the [Code] button to tag code as such.
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

jacql
Posts: 5
Joined: Wed Aug 28, 2013 11:08 am

Postby jacql » Wed Aug 28, 2013 5:02 pm

Unfortunately, it seems this problem is one where making a self-contained minimal example is not possible and also takes much more than just half an hour (I've just spent over 3 hours on it and now the .tex file is too big to attach). It is very elusive and I have encountered several red herrings along the way to working out what makes this problem "minimal".

The error only happens when I put all the figures in. Since it must be self-contained, this makes one very big file. I have 10 figures, all of which have 6 datasets within them. When I only add 8 of the figures, it works. When I go over this, it fails with this error:
! TeX capacity exceeded, sorry [buffer size=200000].

However, before assuming that it's just that my datasets are too big (which is probably also true), it's not quite as simple as that.
a) Even if the pdf files for the previous 8 files (which all worked) have been created in a previous build (i.e. the tikz skips them on subsequent builds), the ninth one fails with the same error, which does not make sense.
b) If I move the last figure up to the top, it works fine.
c) This buffer exceeded error is somewhat random: I've just added a tenth figure at the end and it works fine suddenly.

So my questions boil down to:
1. Has my externalization not worked properly?
2. Despite externalization, is there still some sort of limit to how big the plots can be? (which seems a bit odd: PhD theses are notoriously big on datasets and I've only put in 9 so far!)
3. If there is a limit, why is it so capricious? :)

If you still need the code before you'll consider my question, please can I submit the .tex and the figure .tikz files separately?

User avatar
localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Wed Aug 28, 2013 5:30 pm

jacql wrote:Unfortunately, it seems this problem is one where making a self-contained minimal example is not possible and also takes much more than just half an hour (I've just spent over 3 hours on it and now the .tex file is too big to attach). It is very elusive and I have encountered several red herrings along the way to working out what makes this problem "minimal". […]

But how are we supposed to help if the problem is not reproducible for others? Do you have an idea? I don't. You can choose between struggling endlessly with that problem or invest the time to cut it down and so allow us to help. And until now you did say nothing regarding the suggestions in my first reply. At the moment I see no possibility to help you.

jcql wrote:[…] If you still need the code before you'll consider my question, please can I submit the .tex and the figure .tikz files separately?

You can do so by upload to the forum server or ask by PM for other contact information. But exported code is mostly quite bad and hard to debug. If you have the data sets for the plot you presented in your question, please attach them also for working out alternatives. And perhaps a corresponding Know How article can bring you closer to a solution.
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

jacql
Posts: 5
Joined: Wed Aug 28, 2013 11:08 am

Postby jacql » Wed Aug 28, 2013 10:51 pm

If you mean the suggestion about converting it to CSV files for pgfplots, I admit that I wasn't sure how to respond to that. Unless I misunderstood what you're saying (which is always possible :) that sounds like it would simply bypass the problem, which is specific to tikz. But please feel free to correct me: I am just a newbie user of tikz and pgplots.

But how are we supposed to help if the problem is not reproducible for others? Do you have an idea? I don't.

If no-one has ever seen this problem before, then of course that's true and our only recourse is to reproduce it elsewhere. I was hoping that it was something that had been encountered before. However, I do think that it should still be possible to answer my general question about memory limitations without actually reproducing the problem locally. Is it known that Tex struggles to deal with too many datasets, even though it's externalized? Or should what I want to do be possible, regardless of my dataset size & number?

I have already cut the file down and made it self-contained: I am not avoiding doing that by any means. However, the file upload size limit is stopping me from sharing it with you. But your comment:
If you have the data sets for the plot you presented in your question, please attach them also for working out alternatives.

leads me to think that we may be talking about the same thing. My tikz files include all the datasets (they were generated by matlab2tikz, as I said) and this is what makes them so big. If you're saying that I can strip the datasets out into separate files and submit those separately along with the self-contained .tex file, then I'll be happy to do that.

User avatar
Johannes_B
Site Moderator
Posts: 4100
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Thu Aug 29, 2013 9:31 am

I guess you could just put them all in a zip-file and attach the whole bunch to your next post.
However, help with projects as big as yours seems to be is not guaranteed.

A few words to explain your problem: Matlab plots your datapoints. Consider the following: You have two datapairs (1,1) and (2,2). Matlab plots them with a line inbetween. The tikzexport then outputs \draw-commands not only for the datapairs, but also for the line inbetween, the x-axis with all the ticks and the y-axis with all the ticks. Your simple plot can have more than a thousand new datapairs to draw everything.
On the other hand, as Thorsten suggested, pgfplots uses TikZ internally, either computing everything every time or converting the plot once to pdf and including the pdf afterwards. This is called externalizing.

Just see for yourself:

  1. %\documentclass{standalone}
  2. \documentclass{article}
  3. \usepackage{pgfplots}
  4. \begin{document}
  5. \begin{tikzpicture}
  6. \begin{axis}[xlabel={Number of Chickens},
  7. ylabel={Output $\chi$}
  8. ]
  9. \addplot coordinates {
  10. (1,1)
  11. (2,2)
  12. };
  13. \end{axis}
  14. \end{tikzpicture}
  15. \end{document}


Exporting your data as csv (meaning a simple text-file) is quite easy, it is something like save('filename.dat', '<variableA>', '<variableB>', '-ASCII'). Pgfplots can also plot data by reading a file, no need to copy-paste any data.

Best regards
Johannes
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

feuersaenger
Posts: 34
Joined: Sun Oct 16, 2011 5:56 pm

Postby feuersaenger » Sun Sep 01, 2013 10:44 am

Hi Jacql,

I am author of pgfplots and have read about your problems.

Let me stress that there is no upper limit on the number of plots which can be included into a document. Take a look at the pgfplots manual: ~400 pages containing ~400 plots (at least). And it does not even make use of externalization (only for expensive pictures).

I still did not see the actual error message and I am a bid confused what failed at all. The only thing that I can see is that you observed memory issues. Have you tried to compile it by means of lualatex? It is better at memory management.

Since your setup is quite complex and it seems to be difficult to create a minimum working example, I would like to make the following suggestion: you could cram the smallest available document which exhibits the problems into one zip archive, send it to me by email, and I will see if I can reproduce and perhaps explain your issue. Make sure that it compiles and remove as many packages as possible from its preamble.

Regarding the suggestions of others who recommended to export your data to CSV and import that into pgfplots: you already did that. The script matlab2tikz is actually a sophisticated CSV export tool from matlab to pgfplots.

If you agree that you can send your code to me, you can find my email address in the pgfplots manual. I will treat your files as confidential and will only post the answer of my analysis here.

Note that I have time today (September 1) and then again next Thursday.

Kind regards

Christian


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 3 guests