Is there a way to rigorously define "taking the derivative with respect to a function"? Intuitively, whenever we use the chain rule we're taking the derivative of one function with respect to another. If $f(x)=(h\circ g)(x)$, then we often write $
\frac{\text{d}f}{\text{d}x}=\frac{\text{d}f}{\text{d}g}\frac{\text{d}g}{\text{d}x}$. Formally, $\frac{\text{d}f}{\text{d}g}$ is meaningless, and the chain rule should instead be written $f'(x)=h'(g(x))g'(x)$. In other words, we're using $\frac{\text{d}f}{\text{d}g}$ to mean $h'(g(x))$. Obviously, we could just take this to be the definition of $\frac{\text{d}f}{\text{d}g}$, but this only works when $f$ can be written as a composition of some function with $g$. Is there a way to define this more generally, so that we can take derivatives with respect to arbitrary functions, in the way that we can with integrals (e.g. the Riemann-Stieltjes integral)?
 A: If $M$ is a 1-dimensional manifold, then $T^*M$ is 1-dimensional at any point. Now for any function $f, g\in C^\infty(M)$, $df, dg$ are linearly dependently at all points $x\in M$. In particular, if $dg$ doesn't vanish anywhere, $\frac{df}{dg}$ is a well-defined function on $M$ such that $df = 
"\frac{df}{dg}" dg$ is an identity on $T^*M$ everywhere. This gives a rigorous meaning to $\frac{df}{dg}$ as long as $g'\not=0$.
We may try to define $\frac{df}{dg}(x_0) = \lim_{x\rightarrow x_0}\frac{f(x)-f(x_0)}{g(x)-g(x_0)}$, and this is the same as $\frac{df}{dg}$ in the above sense by L'Hôpital's rule, as long as $g'(x_0)\not=0$. This defintion generalizes to cases when the order of vanishing of $f$ is not less than the one of $g$ at $x_0$, but still it cannot be definied all the time. It's not unreasonable that integrals can be defined for more general classes of functions than derivatives.
A: There is some ... muddle ... in your notation.  As I noted in comments,
$$  \frac{\mathrm{d}f}{\mathrm{d}x} = \frac{\mathrm{d}h(g(x))}{\mathrm{d}x} = \frac{\mathrm{d}h}{\mathrm{d}g}\frac{\mathrm{d}g}{\mathrm{d}x}  \text{.}  $$
In the slightly more detail that I teach it (in preparation for $u$-substitution in integrals a few weeks later),
$$  \frac{\mathrm{d}}{\mathrm{d}x} h(g(x)) = \left.\frac{\mathrm{d}h(u)}{\mathrm{d}u}\right|_{u = g(x)}\frac{\mathrm{d}g}{\mathrm{d}x}  \text{.}  $$
This highlights that we differentiate $h$ with respect to its formal parameter, temporarily ignoring that $g(x)$ is in any way involved.  After we complete that differentiation, we specialize the formal parameter to $g(x)$.  (This is why "$\frac{\mathrm{d}f}{\mathrm{d}g}$" is wrong: $g(x)$ is not the argument of $f$, it is the argument of $h$.)
After that, the notation suggests a way to get what you are describing, but I predict the result is disappointing.  For instance, from $f(x) = \sin^2(x)$, we have $h(u) = u^2$ and $g(x) = \sin(x)$.  Then,
$$  \frac{\mathrm{d}}{\mathrm{d}x} \sin^2 x = \left.\frac{\mathrm{d}u^2}{\mathrm{d}u}\right|_{u = \sin(x)}\frac{\mathrm{d}\sin x}{\mathrm{d}x}  \text{.}  $$
Rearranging and specializing $u$ without evaluating the derivative,
$$  \frac { \frac{\mathrm{d}}{\mathrm{d}x} \sin^2 x } { \frac{\mathrm{d}\sin x}{\mathrm{d}x} } = \frac{\mathrm{d}\sin^2 x}{\mathrm{d}\sin x}  \text{.}  $$
Of course the left-hand side is
$$  \frac{2 \sin x \cos x}{\cos x} = 2 \sin x  \text{,}  $$
exactly what we would expect from the right-hand side, treating "$\sin x$" as an independent variable.  Notice that this is unavoidable -- the substitution for $u$ on the right-hand side of the elaborated chain rule guarantees that this fairly transparent "result" is all we can hope to obtain by this method.
