To derive the formula for the elasticity of substition I consider a function with two arguments $f(x_1,x_2)$. I then consider a level curve $f(x_1,x_2) = c$ assumed to define $x_2$ as a function of $x_1$. From this it follows that
$$\frac{\partial }{\partial x_1} f(x_1,x_2(x_1)) = c \Leftrightarrow \\[8pt]
f_1(x_1,x_2) + f_2(x_1,x_2) \frac{\partial x_2}{\partial x_1} = 0 \Leftrightarrow \\[8pt] \frac{\partial x_2}{\partial x_1} = - \frac{f_1(x_1,x_2) }{f_2(x_1,x_2) }$$
giving the slope of the level curve in terms of $f_i := \partial f/\partial x_i$ for $i=1,2$. Given the definition of MRS I conclude that
$$(1) \ \ MRS(x_1,x_2) := \frac{f_1(x_1,x_2) }{f_2(x_1,x_2) } = -\frac{\partial x_2}{\partial x_1}.$$
Then define the ratio function
$$r(x_1) = x_2(x_1)/x_1$$
again using that $x_2$ is a function of $x_1$. This function can be inverted to get $x_1 = r^{-1}(r)$ and using again that $x_2$ is a function of $x_1$ the MRS can be written as
$$MRS(r) := \frac{f_1(r^{-1}(r),x_2(r^{-1}(r)))}{f_2(r^{-1}(r),x_2(r^{-1}(r)))} $$
and differentiated with respect to $r$ to get
$$(2)\ \ \frac{\partial MRS(r)}{\partial r} = \left[\frac{\left(f_{11} + f_{12}\frac{\partial x_2}{\partial x_1}\right) f_2 - \left(f_{21} + f_{22}\frac{\partial x_2}{\partial x_1}\right)f_1}{f_2^2} \right]\frac{\partial r^{-1}(r)}{\partial r},$$
where the expression in square brackets is $\partial MRS(x_1,x_2(x_1))/\partial x_1$.
I then use that $r^{-1}(r) = x_1$ implying that $\partial r^{-1}(r)/\partial r = 1/\partial r(x_1)/\partial x_1$ and from the definition of $r(x_1)$ it follows that
$$(3) \ \ \frac{\partial r(x_1)}{\partial x_1} = \frac{\frac{\partial x_2}{\partial x_1} x_1 - x_2}{x_1^2} = \frac{}{}-\frac{f_1}{f_2}\frac{1}{x_1} - \frac{x_2}{x^2_1} = -f_1 r\left[ \frac{1}{f_2x_2} + \frac{1}{f_1x_1}\right],$$
using that $r =x_1/x_1$ and that $\frac{\partial x_2}{\partial x_1} = - f_1/f_2$. Inserting the reciprocal value of (3) into (2) it follows that
$$(4)\ \ \frac{\partial MRS(r)}{\partial r} = -\left[\frac{\left(f_{11} + f_{12}\frac{\partial x_2}{\partial x_1}\right) f_2 - \left(f_{21} + f_{22}\frac{\partial x_2}{\partial x_1}\right)f_1}{f_2^2} \right] \left[ \frac{1}{f_1 r\left[ \frac{1}{f_2x_2} + \frac{1}{f_1x_1}\right]}\right].$$
What remains to be done is inserting this expression into the definition of the elasticity of substitution and cleaning it up. The Elasticity of substition is defined as the elasticity of $r = x_2/x_1$ as a function of $MRS$:
$$(5) \ \ \sigma := \frac{MRS}{r} \frac{\partial r}{\partial MRS} = \frac{1}{\frac{r}{MRS} \frac{\partial MRS(r)}{\partial r}}$$
being the reciprocal of the elasticity of MRS with respect to $r=x_2/x_1$. Inserting (4) into (5) it therefore follows that
$$1/\sigma =- \left[\frac{r}{f_1/f_2}\right]\left[\frac{\left(f_{11} + f_{12}\frac{\partial x_2}{\partial x_1}\right) f_2 - \left(f_{21} + f_{22}\frac{\partial x_2}{\partial x_1}\right)f_1}{f_2^2} \right] \left[ \frac{1}{f_1 r\left[ \frac{1}{f_2x_2} + \frac{1}{f_1x_1}\right]}\right],$$
which is reduced by removing the factor $r$ and using again that $\partial x_2/\partial x_2 = -f_1/f_2$ to get
$$1/\sigma =- \left[\frac{1}{f_1/f_2}\right]\left[\frac{\left(f_{11} - f_{12}\frac{f_1}{f_2}\right) f_2 - \left(f_{21} - f_{22}\frac{f_1}{f_2}\right)f_1}{f_2^2} \right] \left[ \frac{1}{f_1 \left[ \frac{1}{f_2x_2} + \frac{1}{f_1x_1}\right]}\right] \\[8pt]
= -\left[\frac{\left(\frac{f_{11}f_2}{f_1} - f_{12}\right) f_2 - \left(f_{21} \frac{f_2}{f_1}- f_{22}\right)f_1}{f_2^2} \right] \left[ \frac{1}{f_1 \left[ \frac{1}{f_2x_2} + \frac{1}{f_1x_1}\right]}\right]\\[8pt]
= -\left[\frac{\frac{f_{11}f^2_2}{f_1} - f_{12} f_2 - f_{21} f_2+ f_{22}f_1}{f_2^2} \right] \left[ \frac{1}{f_1 \left[ \frac{1}{f_2x_2} + \frac{1}{f_1x_1}\right]}\right]\\[8pt]
= -\left[\frac{f_{11}}{f^2_1} - \frac{f_{12}}{f_1f_2} - \frac{f_{21}}{f_1f_2} + \frac{f_{22}}{f_2^2} \right] \left[ \frac{1}{\left[ \frac{1}{f_2x_2} + \frac{1}{f_1x_1}\right]}\right].$$
Taking the reciprocal and using that $f_{12} = f_{21}$ it follows that
$$ \sigma = - \frac{\frac{1}{f_2x_2} + \frac{1}{f_1x_1}}{\frac{f_{11}}{f^2_1} - \frac{2f_{12}}{f_1f_2} + \frac{f_{22}}{f_2^2}} ,$$
which is the formula found in for example: Daniel McFadden Constant Elasticity of Substitution Production Functions
The Review of Economic Studies , Jun., 1963, Vol. 30, No. 2 (Jun., 1963), pp. 73-
83 [stated as equation (1)] or in
Knut Sydsæter, Arne Strøm, Peter Berck Economists’ Mathematical
Manual Fourth Edition [stated as formula 5.21].
I leave it for you to show that this formula is equivalent to the one stated in the question (I however prefer this one because the way the indeces are written makes it easier to remember).