Find $\displaystyle \int \frac{\sec x(2+\sec x)}{(1+2\sec x)^2}\,dx$
I couldn't find my answer so I looked up the solution which is as follows $$\displaystyle \begin{align}&\int \frac{\sec x(2+\sec x)}{(1+2\sec x)^2}\,dx\\&= \int \frac{2\cos x+1}{(2+\cos x)^2}\,dx\\&= \int \frac{2\cos x+\cos^2 x+\sin^2 x}{(2+\cos x)^2}\,dx\\&=\int \frac{\cos x}{2+\cos x}\,dx+\int \frac{\sin^2 x}{(2+\cos x)^2}\,dx\\&=\frac{1}{2+\cos x}\cdot(\sin x)-\int \sin x\cdot\frac{\sin x}{(2+\cos x)^2}\,dx+\int \frac{\sin^2 x}{(2+\cos x)^2}\,dx\\&=\frac{\sin x}{2+\cos x}+C\end{align}$$ I was stuck with $\displaystyle\int \frac{2\cos x+1}{(2+\cos x)^2}\,dx$ and didn't think of replacing $1$ with $\cos^2 x+\sin^2 x$. I want to know the motivation behind this because the reason of this substitution wasn't very obvious to me at first and it only became clear to me in the second last step. Other ways to solve this are also welcome.
Analogous of how integration by parts comes from the product rule, from the quotient rule of differentiation:
$$\begin{align*} \frac uv &= \int \frac {du}{v} - \int\frac{u\ dv}{v^2}\\ \int\frac{u\ dv}{v^2} &= -\frac uv + \int \frac {du}{v}\\ \end{align*}$$
Or directly deriving from integration by parts:
$$ \int \frac{u\ dv}{v^2} = \int u\ d\left(-\frac1v\right) = -\frac{u}{v} + \int\frac{du}{v} $$
This might have motivated breaking down $1$ into $\cos^2 x + \sin^2 x$, to apply the quotient rule backwards,
$$\begin{align*} I &= \int \frac{\cos x}{2+\cos x} dx + \int \frac{\sin^2 x}{(2+\cos x)^2}dx\\ &= \int\frac{d(\sin x)}{2+\cos x} - \int \frac{\sin x\ d(2+\cos x)}{(2+\cos x)^2} \end{align*}$$
But the exact break down of the numerator was not obvious to me. Though by this special case of integration by parts via the quotient rule, let
$$\begin{align*} v &= 2 + \cos x & dv &= -\sin x\ dx\\ u &= -(2\cot x +\csc x)& du &= (2\csc^2x + \csc x \cot x) dx \end{align*}$$
$$\begin{align*} \int \frac{2\cos x + 1}{(2+\cos x)^2} dx &= \int\frac{-(2\cot x + \csc x)(-\sin x\ dx)}{(2+\cos x)^2}\\ &= \frac{2\cot x + \csc x}{2+\cos x} + \int \frac{2\csc^2 x + \csc x \cot x}{2+\cos x} dx\\ &= \frac{2\cot x + \csc x}{2+\cos x} + \int \csc^2 x\cdot \frac{2+\cos x}{2+\cos x} dx\\ &= \frac{2\cot x + \csc x}{2+\cos x} + \int \csc^2 x\ dx\\ &= \frac{2\cot x + \csc x}{2+\cos x} - \cot x + C\\ &= \frac{(2\cot x + \csc x) - \cot x(2+\cos x)}{2+\cos x} + C\\ &= \frac{\csc x - \cot x\cos x}{2+\cos x} + C\\ &= \frac{\sin x (\csc^2 x - \cot^2 x)}{2+\cos x} + C\\ &= \frac{\sin x}{2+\cos x} + C\\ \end{align*}$$
Alternatively, without multiplying the starting numerator and denominator by $\cos^2 x$, let
$$\begin{align*} v &= 1 + 2\sec x & dv &= 2\sec x \tan x\ dx\\ u &= \frac12(2\cot x + \csc x)& du &= -\frac12(2\csc^2x + \csc x \cot x) dx \end{align*}$$
$$\begin{align*} \int \frac{\sec x(2+\sec x)}{(1+ 2\sec x)^2} dx &= \int\frac{\frac12(2\cot x + \csc x)(2\sec x \tan x\ dx)}{(1+2\sec x)^2}\\ &= -\frac{\frac12(2\cot x+\csc x)}{1+2\sec x} + \int\frac{-\frac12 (2\csc^2 x + \csc x \cot x)}{1+2\sec x} dx\\ &= -\frac{\frac12(2\cot x+\csc x)}{1+2\sec x} +\frac12 \int(-\csc x \cot x)\cdot\frac{2\sec x + 1}{1+2\sec x} dx\\ &= -\frac{\frac12(2\cot x+\csc x)}{1+2\sec x} +\frac12 \int(-\csc x \cot x)\ dx\\ &= -\frac{\frac12(2\cot x+\csc x)}{1+2\sec x} +\frac12 \csc x + C\\ &= \frac{-\cot x-\frac12\csc x + \frac12\csc x(1+2\sec x)}{1+2\sec x} + C\\ &= \frac{-\cot x+ \csc x\sec x}{1+2\sec x} + C\\ &= \frac{\tan x(-\cot^2 x+ \csc^2 x)}{1+2\sec x} + C\\ &= \frac{\tan x}{1+2\sec x} + C\\ \end{align*}$$