75
$\begingroup$

Is it possible to draw a (simple) commutative diagram using MathJax?

Amscd doesn't seem to work here on math.SE.

M(N)WE:

$$ \begin{CD} K(X) @>{ch}>> H(X;\mathbb Q);\\ @VVV @VVV \\ K(Y) @>{ch}>> H(Y;\mathbb Q); \end{CD} $$

Edit: [A.K. May 7, 2013]

As pointed elsewhere by Davide, this can be remedied now that MathJax 2.2 [beta] is deployed, by adding \require{AMScd}. The code

$$\require{AMScd}
\begin{CD}
K(X) @>{ch}>> H(X;\mathbb Q);\\
@VVV @VVV \\
K(Y) @>{ch}>> H(Y;\mathbb Q);
\end{CD}$$

produces the following diagram:

$$\require{AMScd} \begin{CD} K(X) @>{ch}>> H(X;\mathbb Q);\\ @VVV @VVV \\ K(Y) @>{ch}>> H(Y;\mathbb Q); \end{CD}$$

$\endgroup$
12
  • 3
    $\begingroup$ As a temporary workaround, I think that you should be able to adapt Arturo's solution here to your needs. (maybe replacing \rightarrow by \longrightarrow looks a bit better, but I haven't tried). $\endgroup$
    – t.b.
    Jun 9, 2011 at 14:15
  • 1
    $\begingroup$ All this is very puzzling, to me. If people want to draw commutative diagrams, why don't they use software that's intended for drawing?? Writing code to define a picture seems strange. Even more strange than writing code to define a textual document. $\endgroup$
    – bubba
    May 7, 2013 at 3:45
  • 4
    $\begingroup$ @bubba commutative diagrams contain still a lot of text and you want the typography of your diagrams to match the typography of your main text. $\endgroup$ May 7, 2013 at 5:16
  • 4
    $\begingroup$ @bubba: because mathematicians don't work with MS Word most of the time, and they using external software is bothersome and annoying. The output in LaTeX is much cleaner, better, contains less mistakes, and it notifies you of any possible mistake. Since diagrams are not graphs and pie charts, but rather arrows and labels, there is no sense in adding an external software when LaTeX does an excellent work on its own (with the occasional help of prepared packages, of course). So mathematicians are used to working with just LaTeX, and they want to have that with MathJax as well. $\endgroup$
    – Asaf Karagila Mod
    May 7, 2013 at 7:53
  • 1
    $\begingroup$ @Asaf -- who said anything about MS Word?? $\endgroup$
    – bubba
    Jun 18, 2013 at 16:11
  • 3
    $\begingroup$ I think it would be the same for all diagrams, not just commutative ones. $\endgroup$
    – PyRulez
    Jan 22, 2015 at 23:59
  • 2
    $\begingroup$ @bubba As Michael Greinecker says: it can be really hard to match fonts in drawing programs sometimes. I once tried to use SolidWorks to prepare - in my mind - magnificent diagrams for mathematics expositions: SolidWorks is a CAD suite and produces the most stunning renderings of 3D geometry. But the fonts it uses are crap and overall the result looked simply dreadful - so a great deal of work wasted. $\endgroup$ Apr 17, 2015 at 11:58
  • 3
    $\begingroup$ @WetSavannaAnimalakaRodVance --SolidWorks uses whatever fonts you tell it to use. It can use any Windows font, so something like Latin Modern Math would be suitable if you're trying to match fonts in a traditional LaTeX document. $\endgroup$
    – bubba
    Aug 1, 2015 at 11:23
  • 1
    $\begingroup$ @AsafKaragila -- the whole point is that LaTeX does not do an excellent job on its own. Look at all the posts on TeX.Stackexchange from people struggling to draw things using Tikz. $\endgroup$
    – bubba
    Aug 1, 2015 at 11:28
  • 1
    $\begingroup$ @bubba Interesting. I don't pretend to have mastered SolidWorks, but it just didn't seem to be a very smooth fit at the time. I was struck by its amazing output, but it's not really meant for this kind of thing - indeed you need to think about geometry very differently in SolidWorks from what you would normally do in mathematics and this is wholly appropriate: mechanical design needs geometry to be defined by physical contact and one can't simply behest an object to have arbitrary co-ordinates of orientation as one does in thought experiments or in mathematical reasoning. $\endgroup$ Aug 1, 2015 at 11:39
  • 1
    $\begingroup$ @WetSavannaAnimalakaRodVance -- well, actually, most CAD systems do allow you to place objects with arbitrary locations and orientations, though typically they don't encourage this. I'm certainly not claiming that CAD systems are always the most suitable drawing tool, though. I think different types of drawings/diagrams require different apps. Sure, you can do everything in TeX or Tikz if you try hard enough, but personally I think it's goofy to struggle to do everything using one tool. That's why we have screw-drivers in addition to hammers. $\endgroup$
    – bubba
    Aug 1, 2015 at 13:41
  • 3
    $\begingroup$ this answer doesn't specify where exactly to put the \require{AMScd}. I think It should be after the $ but before \begin{CD} $\endgroup$
    – usr0192
    Nov 26, 2016 at 16:52

7 Answers 7

22
$\begingroup$

MathJax 2.2 beta was released recently, and it includes support for AMScd. We have Mathjax 2.2 beta deployed, although without AMScd support for now. Hopefully that too would be added soon enough. As Davide Cervone points out, one can manually load AMScd by adding \require{AMScd} after $$ or $ and using the \begin{CD}...\end{CD} environment. One may want to consult the AMScd manual for instruction on how to draw diagrams with this tool.

While AMScd doesn't support diagonal arrows, it will make rectangular diagrams easier to draw.

$\endgroup$
0
41
$\begingroup$

It is possible to do (somewhat primitive) commutative diagrams using \array:

$$\begin{array}{ccccccccc} 0 & \xrightarrow{i} & A & \xrightarrow{f} & B & \xrightarrow{q} & C & \xrightarrow{d} & 0\\ \downarrow & \searrow & \downarrow & \nearrow & \downarrow & \searrow & \downarrow & \nearrow & \downarrow\\ 0 & \xrightarrow{j} & D & \xrightarrow{g} & E & \xrightarrow{r} & F & \xrightarrow{e} & 0\end{array}$$

\begin{array}{ccccccccc}   
0 & \xrightarrow{i} & A & \xrightarrow{f} & B & \xrightarrow{q} & C & \xrightarrow{d} & 0\\\

\downarrow & \searrow & \downarrow & \nearrow & \downarrow & \searrow & \downarrow & \nearrow & \downarrow\\\  

0 & \xrightarrow{j} & D & \xrightarrow{g} & E & \xrightarrow{r} & F & \xrightarrow{e} & 0  
end{array}  

I'm not sure it's possible to label diagonal arrows using this approach though.

$\endgroup$
4
  • 4
    $\begingroup$ As far as I know amscd doesn't support diagonal arrows either, so this should be good enough for the moment. $\endgroup$
    – t.b.
    Jun 9, 2011 at 14:19
  • 1
    $\begingroup$ rotate in htmlcss package at last check. $\endgroup$
    – user645636
    Dec 20, 2019 at 1:36
  • $\begingroup$ rotate attempt: $\newcommand{\len}{\rule{3 em}{0 em}} \newcommand{\diaglen}{\rule{5em}{0em}} \newcommand{\da}[1]{\style{display: inline-block; transform: rotate(90deg)}{{\xrightarrow[\len]{\style{display: inline-block; transform: rotate(-90deg)}{#1}}}}} \newcommand{\dra}[1]{ \style{display: inline-block; transform: rotate(45deg)} { \xrightarrow[ \style{display: inline-block; transform: rotate(-45deg)}{#1} ]{\diaglen} } }$ $$ \begin{array}{ccc} A \hspace{-1em}&\xrightarrow[\len]{f }\hspace{-1.3em} &B \\ & \dra{g\circ f}\hspace{-1.3em}& \da{g} \\ && C \end{array} $$ $\endgroup$ Jul 30 at 10:37
  • $\begingroup$ Version with amscd: $$\require{AMScd} \begin{CD} A @>f>> B\\ @. {_{\rlap{\ g\circ f}}\style{display: inline-block; transform: rotate(30deg)}{{\xrightarrow[\rule{4em}{0em}]{}}}} @VVgV\\ @. D \end{CD}$$ $\endgroup$ Jul 30 at 10:41
33
$\begingroup$

Here is the commutative diagram from Arturo's fix, touched up to use extensible arrows, and with some spacing tightened up a bit.

$$ \newcommand{\ra}[1]{\!\!\!\!\!\!\!\!\!\!\!\!\xrightarrow{\quad#1\quad}\!\!\!\!\!\!\!\!} \newcommand{\da}[1]{\left\downarrow{\scriptstyle#1}\vphantom{\displaystyle\int_0^1}\right.} % \begin{array}{llllllllllll} 0 & \ra{f_1} & A & \ra{f_2} & B & \ra{f_3} & C & \ra{f_4} & D & \ra{f_5} & 0 \\ \da{g_1} & & \da{g_2} & & \da{g_3} & & \da{g_4} & & \da{g_5} & & \da{g_6} \\ 0 & \ra{h_1} & 0 & \ra{h_2} & E & \ra{h_3} & F & \ra{h_4} & 0 & \ra{h_5} & 0 \\ \end{array} $$

The code is valid in both mathjax and latex. In latex, one should include the amsmath package to get extensible arrows, and I would also recommend less negative spacing (or use a better commutative diagram environment).

$$
\newcommand{\ra}[1]{\!\!\!\!\!\!\!\!\!\!\!\!\xrightarrow{\quad#1\quad}\!\!\!\!\!\!\!\!}
\newcommand{\da}[1]{\left\downarrow{\scriptstyle#1}\vphantom{\displaystyle\int_0^1}\right.}
%
\begin{array}{llllllllllll}
0 & \ra{f_1} & A & \ra{f_2} & B & \ra{f_3} & C & \ra{f_4} & D & \ra{f_5} & 0 \\
\da{g_1} & & \da{g_2} & & \da{g_3} & & \da{g_4} & & \da{g_5} & & \da{g_6} \\
0 & \ra{h_1} & 0 & \ra{h_2} & E & \ra{h_3} & F & \ra{h_4} & 0 & \ra{h_5} & 0 \\
\end{array}
$$
$\endgroup$
2
  • 1
    $\begingroup$ This diagram code doesn't show up in Internet Explorer. Is there a fix for that? $\endgroup$
    – tomcuchta
    Jul 8, 2011 at 22:29
  • 2
    $\begingroup$ negative values work in hspace. $\endgroup$
    – user645636
    Dec 20, 2019 at 1:38
24
$\begingroup$

I would modify Jack's answer slightly:

\newcommand{\ra}[1]{\kern-1.5ex\xrightarrow{\ \ #1\ \ }\phantom{}\kern-1.5ex}
\newcommand{\ras}[1]{\kern-1.5ex\xrightarrow{\ \ \smash{#1}\ \ }\phantom{}\kern-1.5ex}
\newcommand{\da}[1]{\bigg\downarrow\raise.5ex\rlap{\scriptstyle#1}}
\begin{array}{c}
0 & \ra{f_1} & A & \ra{f_2} & B & \ra{f_3} & C & \ra{f_4} & D & \ra{f_5} & 0 \\
\da{g_1} & & \da{g_2} & & \da{g_3} & & \da{g_4} & & \da{g_5} & & \da{g_6} \\
0 & \ras{h_1} & 0 & \ras{h_2} & E & \ras{h_3} & F & \ras{h_4} & 0 & \ras{h_5} & 0 \\
\end{array}

$$ \newcommand{\ra}[1]{\kern-1.5ex\xrightarrow{\ \ #1\ \ }\phantom{}\kern-1.5ex} \newcommand{\ras}[1]{\kern-1.5ex\xrightarrow{\ \ \smash{#1}\ \ }\phantom{}\kern-1.5ex} \newcommand{\da}[1]{\bigg\downarrow\raise.5ex\rlap{\scriptstyle#1}} \begin{array}{c} 0 & \ra{f_1} & A & \ra{f_2} & B & \ra{f_3} & C & \ra{f_4} & D & \ra{f_5} & 0 \\ \da{g_1} & & \da{g_2} & & \da{g_3} & & \da{g_4} & & \da{g_5} & & \da{g_6} \\ 0 & \ras{h_1} & 0 & \ras{h_2} & E & \ras{h_3} & F & \ras{h_4} & 0 & \ras{h_5} & 0 \\ \end{array} $$

This would also work in true $\rm\LaTeX$ except for one thing: the \rlap{\scriptstyle#1} would need to be \rlap{$\scriptstyle#1$}.

$\endgroup$
0
16
$\begingroup$

I have used presheaf in the past, it's really easy (if you know xypic) and comfortable to use.

$\endgroup$
2
  • 2
    $\begingroup$ Are there any web pages that do similar things for tikzcd?? $\endgroup$
    – user537667
    Jul 11, 2018 at 2:25
  • 4
    $\begingroup$ @user537667 Yes, for example this one $\endgroup$ Feb 10, 2020 at 8:11
7
$\begingroup$

A potential solution to posting a commutative diagram is to include it as an image. This can allow you have diagonal arrows. :) Here's my workflow for creating commutative diagrams in LaTeX using TikZ. A minimal working LaTeX document looks like this:

\documentclass{standalone}
\usepackage{tikz-cd}

\begin{document}
\begin{tikzcd}
    C \arrow[d,hookrightarrow] \arrow[dr,hookrightarrow,bend left=50,dashed] & &
    \\
    Y \arrow[r,hookrightarrow] \arrow[d,twoheadrightarrow] 
        & X \arrow[d,twoheadrightarrow,dashed] 
            \arrow[dr,twoheadrightarrow,bend left=50] &
    \\
    B \arrow[r,hookrightarrow,dashed] & Y' \arrow[r,twoheadrightarrow,dashed] & A
\end{tikzcd}
\end{document}

Then you can compile this document however you do, zoom into your commutative diagram, take a screenshot, and crop the image.

A commutative diagram

Or you can avoid taking a screenshot and cropping if you have the other appropriate tools. On my Linux OS I can do this at the command line. I've used this technique on my website, where it's better to have the diagrams be PNG images with transparent backgrounds. For the PDF of the diagram named main.pdf, use

pdftoppm -png -r 600 main.pdf > white.png
convert white.png -fuzz 10% -transparent white transparent.png

That 600 corresponds to the resolution of the image white.png.

See also these questions on other Stack Exchange sites.

$\endgroup$
1
  • $\begingroup$ A shame tikzcd doesn't seem to be directly supported. Such a good package. I live-tex'd commutative diagrams with it in grad school. $\endgroup$ Feb 26, 2021 at 8:40
-2
$\begingroup$

The simplest Solution is this website. After making the diagram, you can press windows + shift + S, to turn on snipping tool to cut out the diagram and post it elsewhere.

I've made your diagram here. Pic:

enter image description here


If you want to remove the dotted lines:

Click the button as shown:

enter image description here

You will get a window, copy code from begin tikzcd to end tikzcd:

enter image description here

Go to overleaf and make a new project, import package tikz-cd, and then, copy previously copied code between begin and end doc like so:

enter image description here

Press the recompile button on middle right to get the diagram, you can scroll below it to see the zoom in button to get better resolution after that:

enter image description here

Final result:

enter image description here

$\endgroup$
5
  • $\begingroup$ There is also quiver mentioned in a math se ad. Maybe there is a way to export the diagram? $\endgroup$ Jun 17 at 13:16
  • 1
    $\begingroup$ From the fact that you imbedded some images, should I surmise that this does not allow one "to draw a (simple) commutative diagram using MathJax?" (quoting the Question here) $\endgroup$
    – hardmath
    Jun 25 at 14:46
  • $\begingroup$ I don't think so it is possible to imitate the functionality required in mathjax. Bruno Stonek and Mike pierce have also posted ad hoc answers. @hardmath $\endgroup$ Jun 25 at 14:50
  • $\begingroup$ I'm familiar with the history of this Question. While I'm not your downvoter, I don't think the various ways to create images (including the online site you mention, see also here) advance the immediate topic. $\endgroup$
    – hardmath
    Jun 25 at 15:06
  • $\begingroup$ Oh I didn't catch that post. I searched how to draw commutative diagrams and this came first. It was unhelp and hence I decided to write an answere myself :P $\endgroup$ Jun 25 at 15:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .