Integrate $\int \frac{dx}{a\sin x+b\cos x}$ As far as I know, we could use the stereographic change of variables where $\tan(\frac{x}{2})=t$, $\sin x=\frac{2t}{1+t^2}$  and $\cos x= \frac{1-t^2}{1+t^2}$, then replace $dx$ also $\sin x$ and $\cos x$, and finally I get something like this:
$$
\int \frac{-2\,dt}{bt^2-2at-b}
$$
Now, I may think the next step it's some algebra. How would you proceed?
 A: There's a simpler way. Take $\theta$ such that $\cos\theta=\frac a{\sqrt{a^2+b^2}}$ and that $\sin\theta=\frac b{\sqrt{a^2+b^2}}$. Let $r=\sqrt{a^2+b^2}$. Then\begin{align}\int\frac{\mathrm dx}{a\sin x+b\cos x}&=\frac1r\cdot\int\frac{\mathrm dx}{\sin(x+\theta)}\\&=-\frac1r\log\bigl(\cot(x+\theta)+\csc(x+\theta)\bigr).\end{align}Can you take it from here?
A: $$bt^2-2at-b=b\left[t^2-2\frac abt-1\right]=b\left[\left(t-\frac ab\right)^2-\left(1+\frac{a^2}{b^2}\right)\right]$$ now use substitution to get it in the form:
$$\int\frac{dx}{x^2-1}=\frac 12\ln\left(\frac{x-1}{x+1}\right)+C$$
A: If I were doing this myself, I'd prefer José Carlos Santos' method---which among other unadvertised benefits (1) doesn't introduce a problem at odd integer multiples of $\pi$ (see below) and (2) doesn't break the symmetry of the roles of $a$ and $b$.
Here's a conventional way to finish using your approach: Completing the square in the denominator (temporarily assuming $b \neq 0$) gives
$$-b t^2 + 2 a t + b = - b \left[\left(t - \frac{a}{b}\right)^2 - \left(\frac{a^2}{b^2} + 1\right)\right] .$$
So, applying the translation substitution $u = t - \frac{a}{b}$ leaves the standard integral
$$-\frac{2}{b} \int \frac{du}{u^2 - \lambda^2}, \qquad \lambda := \frac{\sqrt{a^2 + b^2}}{b} .$$

The integrand can be decomposed using partial fractions as $$\frac{1}{u^2 - \lambda^2} = \frac{1}{2 \lambda}\left(\frac{1}{u - \lambda} - \frac{1}{u + \lambda}\right)$$ and then immediately integrated.

With some algebraic manipulation the resulting antiderivative $F(\theta)$ can be written using an expression valid even for $b = 0$; to justify that this antiderivative is still valid in that case, we need only verify that $F'(x)$ coincides with the original integrand.
NB that the given substitution $t = \tan \frac{x}{2}$ presupposes that $x$ is not an odd integer multiple of $\pi$, and so the antiderivative we produce by reversing this substitution after integrating is not a priori valid on intervals that contain such a value.
