# TeX memory size error when using TikZ foreach children in trees

Nearly Identical: How to expand TeX's “main memory size”? (pgfplots memory overload)

I'm getting the same error as the other question, but the answer there is not handling my issue. I believe the error from the other question was due to using many images in the same file; my error is due (I think) to having a lot of memory usage in one image alone. Thus, the externalizing solution doesn't seem to be working.

I'm trying to generate a tree diagram for a paper I'm writing. It involves the exponential growth of nodes by 3's up to 3^8 = 6,561 for a total of ~9,000 total nodes.

Here is the code:

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{tikz}
\usetikzlibrary{shapes,trees}
\usetikzlibrary{external}
\tikzexternalize

\begin{document}

\begin{tikzpicture}
[grow cyclic,
level 1/.style={level distance=30mm,sibling angle=120},
level 2/.style={level distance=30mm,sibling angle=60},
level 3/.style={level distance=10mm,sibling angle=50},
level 4/.style={level distance=6mm,sibling angle=30},
level 5/.style={level distance=4mm,sibling angle=10},
level 6/.style={level distance=2mm,sibling angle=7},
level 7/.style={level distance=2mm,sibling angle=7},
every node/.style={circle,draw,inner sep=1.5pt},
dot/.style={circle,fill,inner sep=0.5pt}]
\node {\tiny{you!}}
child foreach \x in {1,2,3} {node[dot] {}
{child foreach \x in {1,2,3} {node[dot] {}
{child foreach \x in {1,2,3} {node[dot] {}
{child foreach \x in {1,2,3} {node[dot] {}
{child foreach \x in {1,2,3} {node[dot] {}
{child foreach \x in {1,2,3} {node[dot] {}
{child foreach \x in {1,2,3} {node[dot] {}}}}}}}}}}}}}};

\end{tikzpicture}

\end{document}


I've tried calling this via pdflatex -shell-escape file, but it seems that the last iteration is what kills it. It generates fine if I just remove one of the iterative steps. In other words, 3^6 nodes works, but not 3^7. I'm actually trying to go to 3^8.

Any suggestions?

Here is the exact error while running pdflatex -shell-escape file that halts:

! Package tikz Error: Sorry, the system call 'pdflatex -shell-escape -halt-on-e
rror -interaction=batchmode -jobname "test-figure0" "\def\tikzexternalrealjob{t
est}\input{test}"' did NOT result in a usable output file 'test-figure0' (expec
ted one of .pdf:.jpg:.jpeg:.png:). Please verify that you have enabled system c
alls. For pdflatex, this is 'pdflatex -shell-escape'. Sometimes it is also name
d 'write 18' or something like that. Or maybe the command simply failed? Error
messages can be found in 'test-figure0.log'. If you continue now, I'll try to t
ypeset the picture.


Again, running with just one less level compiles fine (without -shell-escape or externalization), so I don't think this message is indicating that anything is wrong with my code, but I could be wrong.

-
Beside using lualatex you can also enlarge the main memory of pdflatex. On miktex I could compile your example with --extra-mem-top=2000000 –  Ulrike Fischer Jul 26 '11 at 8:44
@Ulrike Fischer: I got pdflatex: unrecognized option '--extra-mem-top=10000000'. Any suggestions? –  Hendy Aug 2 '11 at 2:10
I said "in miktex". I don't think that TeXLive knows this option. But you can make a local texmf.cnf. Kpathsea reads all texmf.cnf files in the search path, and values from in earlier files override those in later files, so you could try to put a small texmf.cnf in the folder of your document. –  Ulrike Fischer Aug 2 '11 at 7:53

If you compile the code without externalization, pdflatex indeed complains about exceeded memory size. A simple solution is to compile with lualatex instead of pdflatex, which doesn't seem to have a memory size limitation (or at least one that is sufficiently high). 8 levels works flawlessly with LuaLaTeX (though it did take some time to compile).

For externalization, you need to tell TikZ to use LuaLaTex:

\tikzset{external/system call={lualatex \tikzexternalcheckshellescape -halt-on-error
-interaction=batchmode -jobname "\image" "\texsource"}}

-

Thanks to @Caramdir for the suggestion, but even with externalization I'm blowing the memory. I think from what I've read, externalization works if there are many smaller figures to generate; mine is literally a huge iteration of close to 10,000 nodes/children.

I ended up following the instructions on this blog post (surely this info exists elsewhere as well) to 10 million (from the default 3 million).

$kpsewhich texmf.cnf Then open the file as root with the command$ sudo emacs /usr/share/texmf/web2c/texmf.cnf which will most likely prompt you for an administrator password. Enter the password and search for the line that begins with something such as

main_memory = 3000000 % words of inimemory available; also applies to inimf&mp Change the value “3000000″ to “10000000″, i.e. change from 3 million to 10 million. Save your edit and quit your editor. Then issue the command

\$ sudo fmtutil-sys --all and compile the book again.

I have no idea if this is robust. For example, I use Arch Linux which will probably overwrite this setting on the next TexLive update.

For now, however, the solution is allowing me 8 levels deep in a tree with growth by 3's (summation from n=0 to n=8 of 3^n total nodes).

-
Actually, my suggestion wasn't to use externalization, but to use lualatex instead of pdflatex to compile the file. Did you try Ulrike's solution? That should be more flexible than editing the system-wide configuration file. –  Caramdir Jul 28 '11 at 4:33
@Caramdir: whoops. I actually didn't see that suggestion to increase it via command-line. In terms of not using lualatex, I just have no experience with it and typically export via org-mode, so using the default pdflatex exporter is great as I don't have to make an exception for just one file. I'll try the other suggestion next time I boot into linux (in OS X at the time and don't use latex on it). Thanks for the comment. –  Hendy Jul 29 '11 at 2:13
You can compile the main document with pdflatex and only the external document with lualatex (just add the \tikzset line to your document and compile the way you normally would (with -shell-escape enabled of course)). –  Caramdir Jul 29 '11 at 2:34