<s>[INST] Trigonometry substitution issue with sign When solving an integral such as $\displaystyle\int\frac{dx}{\sqrt{x^2+4}}$, you eventually end up with 
$$ \ln\lvert\sec\theta+\tan\theta\rvert+C.$$
The next step is to rewrite this in terms of $x$. My book does the following: $x=2\tan\theta$, so by drawing a triangle, it can be seen that $x$ is the opposite side, $2$ the adjacent one, and finally, $\sqrt{2^2+x^2}$ the hypotenuse. Therefore, $$\sec\theta=\frac{\sqrt{2^2+x^2}}2.$$ The problem that I see however is that $\sqrt{2^2+x^2}$ is the magnitude of the hypotenuse, so this has lost a possible negative sign since it is not necessarily true that
$$ \sec\theta = \frac{|r|}{2}.$$
How can this be omitted?
 [/INST] When you make the substitution $x=2\tan \theta$, you have to be careful to specify the domain of $\theta$: the substitution is only valid if $\theta$ has a small enough domain for $\tan \theta$ to be continuous. The simplest possible choice of domain is probably $-\frac{\pi}{2} < \theta < \frac{\pi}{2}$. Note that the range of $2\tan \theta$ on this domain is the entire real line, so taking $\theta$ in this domain doesn't lose any generality.
But when $-\frac{\pi}{2} < \theta < \frac{\pi}{2}$, we always have $\sec \theta > 0$. So in fact, if you make this choice of domain, it is always true that $2 \sec \theta=\sqrt{x^2+4}$, without any sign issues.
It's instructive to think about what happens if you choose a different domain for $\theta$. If $\sec \theta > 0$ on that domain, nothing will change. If $\sec \theta < 0$ on that domain, then
$$\int \frac{dx}{\sqrt{x^2+4}}=\int \frac{\sec^2 \theta \,d\theta}{-\sec \theta}=-\int \sec \theta \,d\theta
$$
because $\sqrt{x^2+4}$ is still positive. So the integral in terms of $\theta$ evaluates to $-\ln|\sec \theta +\tan \theta|+C$. Then, when we rewrite in terms of $x$, we again have $\sec \theta=-\sqrt{x^2+4}$, so the integral in terms of $x$ is
$$-\ln\left|-\sqrt{x^2+4}+x\right|+C=-\ln\left(\sqrt{x^2+4}-x\right) +C\, ,$$
because $\sqrt{x^2+4} > x$ for all $x$.
But then
\begin{align*}
-\ln\left(\sqrt{x^2+4}-x\right)&=\ln\left(\frac{1}{\sqrt{x^2+4}-x}\right)\\
&=\ln\left(\frac{\sqrt{x^2+4}+x}{(\sqrt{x^2+4})^2-x^2}\right)&\text{(multiplying by the conjugate)}\\
&=\ln\frac{\sqrt{x^2+4}+x}{4}\\
&=\ln(\sqrt{x^2+4}+x)-\ln 4 \, .
\end{align*}
So we get the nearly same result whatever domain we choose, but the constant term may be different.
</s>