Commutative diagrams using MetaPost or Asymptote

Xy-pic, TikZ and PSTricks seem to be the graphics packages commonly used to draw commutative diagrams. Having heard about the power of MetaPost and Asymptote, I would like to experiment with them.

How good are MetaPost and Asymptote for drawing commutative diagrams? There does not seem to be any official packages for this purpose at the moment.

-
Asymptote is designed for 3D drawings. Those would have to be some super-complicated commutative diagrams to make it worthwhile... – Seamus Dec 16 '10 at 23:06
Actually, Asymptote is not really designed for 3D drawings. It can do 3D, it did from the beginning extend the metapost path syntax to 3D, but it did not have true 3D drawing capabilities (like hidden surface removal) till quite recently. The asymptote gallery at asymptote.sourceforge.net has a number of good 2D examples. I don't see any commutative diagrams, but they would certainly be possible. – Jan Hlavacek Dec 16 '10 at 23:30

For Metapost, look at this page. I am sure you can do the same with Asymptote, but I am not aware of any examples.

-

Zoonekynd's example #269 from his Metapost examples page (excuse the grainy Gif):

The code conceals a whole MP library defining a begindiag...enddiag group defining node and rarrow primitives for the parts of the CD, with which the above diagram can be typeset using:

  begindiag;
node "A";
rarrowto(1,0, "above" => "a",
"shape" => "middle",
"curved" => 3mm,
"dashed" => withsmalldots);
rarrowto(0,1, "below" => "b",
"color" => blue,
"shape" => "mapsto",
"dashed" => evenly);
node "A";
rarrowto(1,0, "above" => "c", "width" => 1bp, "shape" => "inj");
rarrowto(0,1, "below" => "d", "shape" => "mono");
node "A";
nextline;
node "A";
rarrowto(1,0, "below" => "e", "shape" => "epi");
node "A";
rarrowto(1,-1, "below" => "f", "curved" => -3mm, "shape" => "half_dotted");
enddiag;


Jan links to Alan Kennington's Metapost examples (thanks: I didn't know of them), but from what I can see, while the examples there are impressive, there is no real CD library from what I saw.

Like Jan, I know of no Asymptote code for CDs, but it should not be difficult to trabnslate the Alan Kennington CD examples to it, because they don't make use of equation solving.

-

For simple CDs, you could use the rather appealing (IMHO) approach of Eplain (link is a PDF-file), from which the following example is taken:

\input eplain
$$\commdiag{ Y & \mapright^f & E \cr \mapdown & \arrow(3,2)\lft{f_t} & \mapdown \cr Y \times I & \mapright^{\bar f_t} & X }$$
\bye


Which produces:

-

Here's a non-serious example "using Asymptote" (which is to say, using TikZ inside Asymptote):

settings.outformat = "pdf";    // Tell Asymptote to output a pdf ("eps" is also an acceptable choice).
unitsize(1cm);                 // One unit of distance should be translated as 1cm rather than the default 1pt. Actually, for this particular setup, I think this is unnecessary.
usepackage("tikz-cd");         // Whenever you execute LaTeX code, add the line \usepackage{tikz-cd} to the preamble
string str = "\begin{tikzcd}[ampersand replacement=\&]
A \rar{\phi} \dar{\theta} \& B \dar{\pi}
\\ C \rar{\beta} \& D
\end{tikzcd}";                 // Create a string containing some LaTeX code.
label(str, (0,0));             // Place a label (think TikZ node) at position (0,0) containing the result of running the string str through LaTeX.


For an explanation of "ampersand replacement" in the TikZ code, see this answer.

Here's the output:

-
 @texenthusiast: I'm hardly an expert on how to execute the code. In my own case, I found that with an up-to-date version of MacTeX, Asymptote is automatically installed, so it suffices to save the code above into a file named filename.asy and then execute on the command line asy filename.asy. – Charles Staats Apr 28 at 13:24 @texenthusiast: I've added some comments to explain the code. – Charles Staats Apr 28 at 13:37