# How to write chemical formula with branching and with labels for each branch

I would like to express a chemical reaction, which can lead to multiple products (has multiple branches). This is usually expressed using a forked arrow as in the example equation (1) below. I wrote a hacky solution to achieve this using tikz:

\documentclass{report}
\usepackage{amsmath}
\usepackage{txfonts}
\usepackage{hyperref}
\usepackage{chemfig}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{subequations}
\label{eq1}
\noindent
\begin{tikzpicture}[node distance=0cm and 1.2cm]
\node (A)
{$\text{NH}^+ + \text{H}_2$};
\node[above right=of A] (B)
{$\text{NH}_2^+ + \text{H} \qquad \Delta{H}^0 = -1.9~ \text{eV}$\qquad\hypertarget{eq1a}{(\text{1a})}
};
\node[below right=of A] (C)
{$\text{H}_3^+ + \text{N} ~~\qquad \Delta{H}^0 = -0.8~ \text{eV}$\qquad\hypertarget{eq1b}{(\text{1b})}};
\draw[-stealth] (A) -- ( $(A.0)!0.3!(B.west|-A.0)$ ) |- (B.west) node[auto,pos=0.7] {${\scriptstyle k^\text{a}_{\text{NH}^+}}$};
\draw[-stealth] (A) -- ( $(A.0)!0.3!(C.west|-A.0)$ ) |- (C.west) node[auto,pos=0.7] {${\scriptstyle k^\text{b}_{\text{NH}^+}}$};
\end{tikzpicture}
\end{subequations}

\begin{subequations}
\label{eq2}
\begin{align}
\label{eq2a}
\text{NH}^+ + \text{H}_2& \xrightarrow{k^\text{a}_{\text{NH}^+}} \text{NH}_2^+ + \text{H} &\Delta{H}^0 &= -1.9~ \text{eV} \\
\label{eq2b}
& \xrightarrow{k^\text{b}_{\text{NH}^+}} \text{H}_3^+ + \text{N}  &\Delta{H}^0 &= -0.8~ \text{eV}
\end{align}
\end{subequations}
where equation \eqref{eq2} has branches \eqref{eq2a} and \eqref{eq2b}.
\end{document}


But there are a few issues with it:

I dont know how to label the branches so that I can refer to them with eqref (as in equation (2) in the example). I am numbering the labels manually, which is not feasible in practice.

I don't know how to make the branch labels right-aligned as in the equation (2).

Does anyone know a better solution? I guess this must be a frequent problem.

Here is a proposal using empheq. (I am also loading siunitx but keep your distances.)

\documentclass{report}
\usepackage{amsmath}
\usepackage{empheq}
\usepackage{txfonts}
\usepackage{hyperref}
\usepackage{chemfig}
\usepackage{siunitx}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{subequations}\label{eq1}
\begin{empheq}[left={\begin{tikzpicture}[baseline=(A)]
\node (A)
{$\text{NH}^+ + \text{H}_2$};
\draw[-stealth] (A.east) -- ++(0.3,0) |- ++(2,0.3) node[auto,pos=0.7] {${\scriptstyle k^\text{a}_{\text{NH}^+}}$};
\draw[-stealth] (A.east) -- ++(0.3,0) |- ++(2,-0.3) node[auto,pos=0.7] {${\scriptstyle k^\text{b}_{\text{NH}^+}}$};
\end{tikzpicture}}]{align}
~\Delta{H}^0 &= -1.9~\si{\eV}\label{eq1a}\\
~\Delta{H}^0 &= -0.8~\si{\eV}\label{eq1b}
\end{empheq}
\end{subequations}
where equation \eqref{eq1} has branches \eqref{eq1a} and \eqref{eq1b}.

\begin{subequations}
\label{eq2}
\begin{align}
\label{eq2a}
\text{NH}^+ + \text{H}_2& \xrightarrow{k^\text{a}_{\text{NH}^+}} \text{NH}_2^+ + \text{H} &\Delta{H}^0 &= -1.9~\si{\eV} \\
\label{eq2b}
& \xrightarrow{k^\text{b}_{\text{NH}^+}} \text{H}_3^+ + \text{N}  &\Delta{H}^0 &= -0.8~\si{\eV}
\end{align}
\end{subequations}
where equation \eqref{eq2} has branches \eqref{eq2a} and \eqref{eq2b}.
\end{document}


And another proposal without empheq but a simple overlay tikzpicture that allows you to keep much of your original TikZ code.

\documentclass{report}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{txfonts}
\usepackage{hyperref}
\usepackage{chemfig}
\usepackage{siunitx}
\usetikzlibrary{positioning,calc}
\newcommand{\tikznode}[2]{\relax
\ifmmode%
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$#2$};
\else
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}
\begin{document}
\begin{subequations}\label{eq1}
\begin{align}
\hspace*{5cm}\tikznode{DH1}{\Delta{H}^0} &= -1.9~\si{\eV}\label{eq1a}\\
\tikznode{DH2}{\Delta{H}^0} &= -0.8~\si{\eV}\label{eq1b}
\end{align}
\end{subequations}
where equation \eqref{eq1} has branches \eqref{eq1a} and \eqref{eq1b}.

\begin{tikzpicture}[overlay,remember picture]
\node (A) at ($(DH1.west)!0.5!(DH2.west)+(-4,0)$) {$\text{NH}^+ + \text{H}_2$};
\draw[-stealth] (A.east) -- ++(0.3,0) |- ([xshift=-1mm]DH1.west) node[auto,pos=0.7] {${\scriptstyle k^\text{a}_{\text{NH}^+}}$};
\draw[-stealth] (A.east) -- ++(0.3,0) |- ([xshift=-1mm]DH2.west) node[auto,pos=0.7] {${\scriptstyle k^\text{b}_{\text{NH}^+}}$};
\end{tikzpicture}

\begin{subequations}
\label{eq2}
\begin{align}
\label{eq2a}
\text{NH}^+ + \text{H}_2& \xrightarrow{k^\text{a}_{\text{NH}^+}} \text{NH}_2^+ + \text{H} &\Delta{H}^0 &= -1.9~\si{\eV} \\
\label{eq2b}
& \xrightarrow{k^\text{b}_{\text{NH}^+}} \text{H}_3^+ + \text{N}  &\Delta{H}^0 &= -0.8~\si{\eV}
\end{align}
\end{subequations}
where equation \eqref{eq2} has branches \eqref{eq2a} and \eqref{eq2b}.
\end{document}


• Thank you for the answer. I am going to use the second option using TikZ overlay. In this case, the arrows, are linked to the formulas using the tikznodes and automatically adjust position if the formula changes in size. – rouckas Aug 22 '18 at 12:01

Here is a solution along the lines of what you were doing for giving equation style labels to tikz diagrams. It defines an option eqn-label for a node which puts an equation number on the right hand side of the page on level with the current node. This uses the current page node so you need to include the remember picture option in the tikzpicture options.

I made this by modifying the definition of the label key. The label key is defined on line 762 of the core tikz file tikz.code.tex but the command that actually does the work is \tikz@@parse@label@nonactive which is defined on line 830.

\documentclass{report}
\usepackage{amsmath}
\usepackage{txfonts}
\usepackage{hyperref}
\usepackage{chemfig}
\usepackage{tikzpagenodes}
\usetikzlibrary{positioning,calc}

\makeatletter
\tikzset{
eqn-label/.style={
append after command = {%
\bgroup
[current point is local=true]
\pgfextra{\let\tikz@save@last@fig@name=\tikz@last@fig@name\tikz@node@is@a@labelfalse
\pgfpointanchor{current page text area}{east}
\pgf@xa=\pgf@x
\pgfpointanchor{\tikz@last@fig@name}{center}
\pgf@ya=\pgf@y
}
node [every label,
left
] at (\pgf@xa,\pgf@ya) {\refstepcounter{equation}\label{#1}\hypertarget{#1}{(\@currentlabel)}\!\!}
\pgfextra{\global\let\tikz@last@fig@name=\tikz@save@last@fig@name}
\egroup}
}
}
\makeatother
\begin{document}
\begin{subequations}
\label{eq1}
\noindent
\begin{tikzpicture}[node distance=0cm and 1.2cm, remember picture]
\node (A)
{$\text{NH}^+ + \text{H}_2$};
\node[above right=of A, eqn-label=eq1a] (B)
{$\text{NH}_2^+ + \text{H} \qquad \Delta{H}^0 = -1.9~ \text{eV}$};
\node[below right=of A, eqn-label=eq1b] (C)
{$\text{H}_3^+ + \text{N} ~~\qquad \Delta{H}^0 = -0.8~ \text{eV}$};
\draw[-stealth] (A) -- ( $(A.0)!0.3!(B.west|-A.0)$ ) |- (B.west) node[auto,pos=0.7] {${\scriptstyle k^\text{a}_{\text{NH}^+}}$};
\draw[-stealth] (A) -- ( $(A.0)!0.3!(C.west|-A.0)$ ) |- (C.west) node[auto,pos=0.7] {${\scriptstyle k^\text{b}_{\text{NH}^+}}$};
\end{tikzpicture}
\end{subequations}
where equation \eqref{eq1} has branches \eqref{eq1a} and \eqref{eq1b}.

\begin{subequations}
\label{eq2}
\begin{align}
\label{eq2a}
\text{NH}^+ + \text{H}_2& \xrightarrow{k^\text{a}_{\text{NH}^+}} \text{NH}_2^+ + \text{H} &\Delta{H}^0 &= -1.9~ \text{eV} \\
\label{eq2b}
& \xrightarrow{k^\text{b}_{\text{NH}^+}} \text{H}_3^+ + \text{N}  &\Delta{H}^0 &= -0.8~ \text{eV}
\end{align}
\end{subequations}
where equation \eqref{eq2} has branches \eqref{eq2a} and \eqref{eq2b}.
\end{document}


Here is a picture of the output:

• Thank you for showing me how to implement equation numbering within TikZ, very interesting and useful. If I had a document with many TikZ pictures to label, I would definitely use it. However, my documents usually contain just one to two such equations and lot of simple equations, so I am sticking with the approach of @marmot, which seems simpler at this point. – rouckas Aug 22 '18 at 12:14