[Haskell-beginners] How to construct complex string

Brent Yorgey byorgey at seas.upenn.edu
Wed Aug 7 21:15:12 CEST 2013

On Mon, Aug 05, 2013 at 09:37:18PM +0200, martin wrote:
> Hello all,
> recently I was working on a haskell program, which does some
> calculations and the produces a "plot", i.e. a set of Tikz (a LaTeX
> thing) commands.
> While this was not a problem in itself, it lacked composability. While
> all my plots show a grid, a caption and some data points, some of them
> additionally show a line with a legend, some of them show two.
> Now I clearly do not want to duplicate the entire function just to add
> another line. I'd rather construct the Tikz commands (which at the end
> of the day is just a String) in a stepwise fashion.
> When constructing the set of Tikz commands, I cannot rely that
> additional commands are placed at the end. It is typically more an
> inside-out thing. The last thing I need to do is wrap the whole thing in
> \begin and \end directives.

Hi Martin,

I think the key thing you are missing is that a String is just a
particular concrete representation of a TikZ program.  That is not
what a TikZ program really *is*.  Instead, TikZ programs are trees.
String is nice for storing a program in a file, but it is a terrible
representation to use when working with programs, whether generating
them, type-checking them, or whatever.  As you have discovered, when
working with Strings you have to insert stuff at various arbitrary
places (at the beginning, the end, the middle), which is quite
annoying and difficult (especially the middle!), whereas those same
operations are simple on a tree representation of the program.

To do what you are trying to do in a nice way would require (1) making
an algebraic data type to represent the structure of TikZ programs (2)
changing your program so that it generates values of this type,
instead of Strings, and (3) making a single function to convert a
value of this type into a String.  This can be quite a bit of work up
front, but well worth it if you are going to do more than just a bit
of TikZ generation.

With that said, however, did you know there is a TikZ backend for
diagrams? [1] It should be possible for you to describe your plots
using diagrams and then output them to TikZ (or any other format for
which diagrams has a backend---SVG, PDF, PS, or PNG).  I have not
looked at it recently but would be happy to support it if you run into
any problems (I will probably want it myself sooner or later).

[1] http://hackage.haskell.org/package/diagrams%2Dtikz


> Additionally there may be "global" values (like the scale) which need to
> be known at more than one step. I had a brief look at the "Diagrams"
> package, which MUST have similar issues, and they "do everything with
> monoids", but I fail to see the light.
> Could anyone point me in the right direction?
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

More information about the Beginners mailing list