Since my implementation of the base classes of category theory has received an “almost good to go” status, I am now fully transitioning into the second stage of my project: drawing diagrams. I expect this stage to be considerably more thrilling since during it I am going to write some actual functionality, i.e., code that does something useful™.
Well, I guess I don’t really have to expect anything any more, since I am already thrilled: a couple days ago I found an awesome LaTeX package, Xy-pic, which allows typesetting precisely what I need: diagrams with grid-like layout. This Wikipedia page shows how easy it is do produce a commutative diagram with this package. My initial plan was to go with TikZ, which is also awesome, but it is much better for more general drawing tasks. With TikZ, it would have been necessary to manually arrange objects in the diagram in a grid, place text over arrows, and decide as to how the arrows should curve. This is not the most difficult part of the diagram drawing business, but it is always nice to use some stuff which someone else has already done (better).
In this post I will try to outline how I see the implementation of the diagram drawing functionality now, after I have went through the review process of my first large chunk of code in SymPy.
According to the proposal, the process of drawing a diagram includes two stages. After the first stage, the matrix which will serve as the outline of the visual representation of the diagram should be constructed. I plan to have the whole first stage done in the DiagramGrid class, which will be constructed from a Diagram and will act a lot like a matrix in that it will be possible to see its dimensions and what lies at a position given by rectangular coordinates. It will also be possible to find out which morphisms connect a certain node in the matrix to other nodes, and whether it is likely that it overlaps with other morphisms or not. Here the job of DiagramGrid ends. A minor remark is nevertheless due before proceeding to the actual drawing. In the proposal I stated that certain objects which don’t fit easily in the existing matrix will be arranged in a circle around the existing structure. I have abolished this, because it will break the idea of typesetting the diagram in a matrix, which makes the whole thing considerably harder to handle. Instead, these “unfitting” objects will be placed along one of the four edges of the frame of the diagram, while the morphisms going into these objects will be marked with the flag which shows that they may overlap with other morphisms.
The second stage of the diagram drawing process is actually typesetting the diagram matrix using Xy-pic. This part should be pretty straightforward, since it is basically about putting the matrix that has resulted from the first stage into an Xy-pic matrix. I plan to have the second stage of drawing implemented in the class DiagramGridDrawer. This class will contain the method draw_outline, which will return an Xy-pic representation of a DiagramGrid. The drawer class will have a dictionary of morphism name formatters, which will associate a property to a function of one string argument. For example, the property “exists” may be associated to a function which prepends its argument with the mathematical symbol “exists”. The drawer class will also have a dictionary of morphism styles, which will map string properties to arrow styles. I have also considered taking advantage of the fact that morphism properties are not enforced to be strings, and putting some additional formatting information into those properties. I am not yet sure that it is necessary, though, so I don’t include this into the plan.
Now, there are some changes to the timeline in the proposal, which are caused in part by the fact that I have spent so much time on bringing my first pull request up to scratch, and in part by the fact that I now have a slightly different vision on how to implement the drawing. Thus I plan to spend the coming week, June 18 – June 24, on implementing DiagramGrid. Then, in the next week, June 25 – July 1, I plan to implement DiagramGridDrawer and extensively test both classes. The week July 2 – July 8 I reserve for getting my changes merged. Not that I expect to spend a whole week on this; but I’d rather allocate slightly more time, with some reserve, than make promises which I may fail to fulfill.
I would like to explicitly remark the fact that the changes to the timeline do not delay the moment when I plan to have the drawing functionality ready. Therefore I’m still not too far astray from my planned timing