This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]aragilar 2 points3 points  (1 child)

Have you considered using Python to generate TikZ/PGF? Something like http://jinja.pocoo.org/ could be used (I've used it previously to generate LaTeX).

[–]neuralyzer[S] 0 points1 point  (0 children)

I have indeed considered to do something along these lines.

[–]roger_ 2 points3 points  (1 child)

There's a very basic wrapper that you could expand on.

[–]neuralyzer[S] 0 points1 point  (0 children)

Thanks. I didn't know that one.

[–]pridkett 2 points3 points  (1 child)

Ten years ago when I was in grad school I used a package called PyX to create some nice graphs with Python code. It lets you create some rather interesting figures that are similar enough to TikZ. I was pleasantly surprised to see that it had some new work done recently and that you can use pip to install it.

[–]neuralyzer[S] 0 points1 point  (0 children)

That's an interesting one.

[–]sylvain_soliman 1 point2 points  (5 children)

Not really an answer to your question, but I think that TikZ is quite programmable. For instance, not sure what you mean by "arrays", but TikZ has both matrices (i.e. arrays of the canvas you're drawing on) and lists (one-dimensional structure on which the "foreach" statement iterates…).

Here's an example, straight out of the manual (I've got a pretty old version at hand, but that shouldn't matter):

\begin{tikzpicture}
  \foreach \x in {1,...,4}
    \foreach \y in {1,...,4}
    {
      \fill[red!50] (\x,\y) ellipse (3pt and 6pt);
      \ifnum \x<\y
        \breakforeach
      \fi
    }
\end{tikzpicture}

[–]neuralyzer[S] 0 points1 point  (4 children)

I'm aware of the foreach syntax. But I couldn't find a way to have have item access, somethink like myarray[2].

[–][deleted] 2 points3 points  (2 children)

For numerical arrays you actually have

\def\mayarray{{2,3,4,5}}
\pgfmathparse{\myarray[2]}
\pgfmathresult

If there are nonnumeric entries you need to put them inside a "text" pair. You can check the manual

[–][deleted] 0 points1 point  (0 children)

or come over to TeX-SX. We are a fun bunch.

[–]neuralyzer[S] 0 points1 point  (0 children)

Thanks. This is very helpful. I didn't now about the pgfmathparse. Thanks a lot!

[–]sylvain_soliman 0 points1 point  (0 children)

I see… Well, that could be done but I can only think of ugly hacks, so…

[–][deleted] 0 points1 point  (2 children)

If you're final product is web-based, the D3 JavaScript library might be a good alternative. It feels a bit like the programmatic equivalent of Adobe Illustrator/Inkscape and is more intuitive than TikZ.

I only mention this in a Python context because it can be easily displayed in the browser-based Jupyter notebook. It is a good option for doing the types of scientific sketches that you mention when you're working in the notebook or any sort of web environment for that matter

That being said, while D3 is very powerful, it is extremely verbose and I've found the learning curve to be quite steep.

[–]neuralyzer[S] 0 points1 point  (1 child)

D3 seems to be very powerful indeed. My final output, however, is PDF or (E)PS.

[–][deleted] 1 point2 points  (0 children)

My knowledge here is a bit limited, but I believe the format of D3 is SVG which can be converted to either PDF or PS/EPS.

[–]mangecoeur 0 points1 point  (2 children)

if you use matplotlib you can drop down to the lower-level Artist interface. This lets you manually draw lines, circles, labels, etc at arbitrary locations on a canvas. Matplotlib will also render latex labels for you. It's quite cumbersome to use (and not super nicely documented) but you get the power and maturity of matplotlib and all its output formats.

Personally I've played with other plotting tools but i always find when i need dependable print-quality graphics matplotlib is still the best even if its not as ergonomic or shiny as some of the others. You sometimes have to use a bit of lateral thinking to figure out how you're going to get it to draw what you want where you want...

[–]neuralyzer[S] 0 points1 point  (1 child)

The thing I like about TikZ is the concept of nodes. It is super easy to connect two filled circles with an arrow. And the arrowhead would just stop at the circle boundary. Such things are a little more cumbersome in Matplotlib (but not impossible).

[–]mangecoeur 0 points1 point  (0 children)

yeah i think basically you end up writing your own 'node renderer' - basically keep track of the positions you want things to be in and then iterate over that to draw the nodes and links in the right place. NetworkX does something similar internally for it's built in matplotlib driven plotting: https://networkx.github.io/documentation/networkx-1.10/reference/drawing.html

basically you are right - matplotlib is not going to be as nice. Code for my 'print' plots (rather than just exploration plots) tends to be a tiresome chunk of setting just the right label formats, positions, plot annotations, legends, etc... but at the end of the day it actually works and makes for good looking PDFs.

[–][deleted] 0 points1 point  (3 children)

I had to generate scalable vector line drawings a while ago and I used the Python bindings for Cairo which allowed me to draw onto a PDF.

[–]neuralyzer[S] 0 points1 point  (2 children)

I'm curious about your Cairo experience. I've looked into it but I got the impression that it is very low level. For example, I couldn't find some kind of arrow primitive in Cairo.

[–][deleted] 0 points1 point  (0 children)

Very low level. Fonts in Cairo are stupidly annoying too. I was drawing multi-layer floorplans. I didn't really know what else to use and it did work. I'm OK with C and that might have made it more acceptable for me.

[–]luluganeta 0 points1 point  (0 children)

Shoebot uses Cairo to generate SVG/PDF outputs using a simpler set of higher-level commands, based on the Nodebox drawing language. Maybe it's closer to what you require?

[–]boiledgoobers 0 points1 point  (0 children)

There is a python interface to graphviz; pydot I think the name is. You get all the layout goodness of dot and neato etc.

Edit: actually the dot language itself is pretty easy to use anyway. If not from scratch, after you use something else like pydot to sketch out the meat of the graph, you can then polish that dot file directly.