# Draw a commutative diagram - compiling error

I have seen this post for writing commutative diagrams, needed for a chemistry purpose. When trying that latex returns an error. I couldn't get what's going on.

Below there is a MWE

\documentclass[a4paper,12pt]{article}
\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}
\usepackage{chemformula}

\usepackage{siunitx}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,row sep=3em,column sep=4em,minimum width=2em] {
$\ch{H2O_s}$\SI{-20}{\celsius} & $\ch{H2O_l}$\SI{50}{\celsius} \\
$\ch{H2O_s}$\SI{-0}{\celsius} & $\ch{H2O_l}$\SI{-0}{\celsius} \\};
\path[-stealth]
(m-1-1) edge node [left] {$Q_1$} (m-2-1)
edge node [below] {$Q_T$} (m-1-2)
(m-2-1.east|-m-2-2) edge node [below] {$Q_2$} node [above] {$\exists$} (m-2-2)
(m-1-2) edge node [right] {$Q_3$} (m-2-2)
edge [dashed,-] (m-2-1);
\end{tikzpicture}

\end{document}


You have a matrix of math nodes, so the content of the matrix is already set in math mode, and adding the $ confuses things. Remove all the $ from the nodes in the matrix, and things work fine.

\documentclass[a4paper,12pt]{article}
\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}
\usepackage{chemformula}

\usepackage{siunitx}
\usepackage{url}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,row sep=3em,column sep=4em,minimum width=2em] {
\ch{H2O_s}\SI{-20}{\celsius} & \ch{H2O_l}\SI{50}{\celsius} \\
\ch{H2O_s}\SI{-0}{\celsius} & \ch{H2O_l}\SI{-0}{\celsius} \\};
\path[-stealth]
(m-1-1) edge node [left] {$Q_1$} (m-2-1)
edge node [below] {$Q_T$} (m-1-2)
(m-2-1.east|-m-2-2) edge node [below] {$Q_2$} node [above] {$\exists$} (m-2-2)
(m-1-2) edge node [right] {$Q_3$} (m-2-2)
edge [dashed,-] (m-2-1);
\end{tikzpicture}

\end{document}


I would use tikz-cd which has a simpler syntax.

\documentclass[a4paper,12pt]{article}
\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}

\usepackage{chemformula,siunitx}
\usepackage{tikz-cd}
\usetikzlibrary{babel}

\begin{document}

\begin{tikzcd}
\ch{H2O_s}\SI{-20}{\celsius}
\arrow[r,"Q_T"]
\arrow[d,swap,"Q_1"]
&
\ch{H2O_l}\SI{50}{\celsius} \arrow[d,"Q_3"]
\\
\ch{H2O_s}\SI{-0}{\celsius}
\arrow[r,"\exists","Q_2" swap]
\arrow[ur,dashed,-]
&
\ch{H2O_l}\SI{-0}{\celsius}
\end{tikzcd}

\end{document}


• Excuse me lot. I was working with tikz-cd, but you preceded me. I can remove my answer if you prefer. Sorry. I put +1. – Sebastiano May 21 '18 at 20:31
• Nice, thanks. I will take it into account. What does it mean swap? – user134500 May 21 '18 at 20:34
• @santimirandarp Two symbols above and below the arrow. – Sebastiano May 21 '18 at 20:37
• @Sebastiano swap means “place the object on the opposite side than default, which is on the left of the arrow (analogously to the terminology used for river banks). – egreg May 21 '18 at 20:50
• @egreg I was imprecise as usual. I'm sorry. – Sebastiano May 21 '18 at 21:00

\documentclass[a4paper,12pt]{article}
\usepackage{mathtools}

\usepackage{tikz-cd}

\begin{document}
\begin{tikzcd}
H_2O_s-20^{\circ}\, C \arrow[d, "Q_1"'] \arrow[rr, "Q_T"'] &  & H_2O_150^{\circ}\, C \arrow[d] \\
H_2O_s 0^{\circ}\, C \arrow[rru, no head, dashed] \arrow[rr,"\exists","Q_2" swap] &  & H_2O_1 0^{\circ}\, C
\end{tikzcd}
\end{document}