Integration of secant $$\begin{align}
\int \sec x \, dx 
&= \int \cos x \left(  \frac{1}{\cos^2x} \right)  \, dx \\
&= \int \cos x \left(  \frac{1}{1-\sin^2x} \right) \, dx \\
& = \int\cos x\cdot\frac{1}{1-\frac{1-\cos2x}{2}} \, dx \\
&= \int \cos x \cdot\frac{2}{1+\cos2x} \, dx 
\end{align}$$
I am stuck in here. Any help to integrate secant?
 A: \begin{align*}\int\sec x\,\mathrm dx&=\int\frac1{\cos x}\,\mathrm dx\\&=\int\frac{\cos x}{\cos^2x}\,\mathrm dx\\&=\int\frac{\cos x}{1-\sin^2x}\,\mathrm dx.\end{align*}
Now, doing $\sin x=t$ and $\cos x\,\mathrm dx=\mathrm dt$, you get $\displaystyle\int\frac{\mathrm dt}{1-t^2}$. But\begin{align*}\int\frac{\mathrm dt}{1-t^2}&=\frac12\int\frac1{1-t}+\frac1{1+t}\,\mathrm dt\\&=\frac12\left(-\log|1-t|+\log|1+t|\right)\\&=\frac12\log\left|\frac{1+t}{1-t}\right|\\&=\frac12\log\left|\frac{(1+t)^2}{1-t^2}\right|\\&=\log\left|\frac{1+t}{\sqrt{1-t^2}}\right|\\&=\log\left|\frac{1+\sin x}{\sqrt{1-\sin^2x}}\right|\\&=\log\left|\frac1{\cos x}+\frac{\sin x}{\cos x}\right|\\&=\log|\sec x+\tan x|.\end{align*}
A: An alternative method: The trick here is to multiply $\sec{x}$ by $\dfrac{\tan{x}+\sec{x}}{\tan{x}+\sec{x}}$, then substitute $u=\tan{x}+\sec{x}$ and $du=(\sec^2{x}+\tan{x}\sec{x})~dx$:
$$\int \sec{x}~dx=\int \sec{x}\cdot \frac{\tan{x}+\sec{x}}{\tan{x}+\sec{x}}~dx=\int \frac{\sec{x}\tan{x}+\sec^2{x}}{\tan{x}+\sec{x}}~dx=\int \frac{1}{u}~du=\cdots$$
Not obvious, though it is efficient.
A: After $\int \cos x \left(\frac{1}{1-\sin^2x}\right)dx$ use the transformation $z = \sin x$ and $dz  = \cos x \, dx$.
Edit:
$$\int\frac{1}{1-u^2}\,du = \frac{1}{2}\int\frac{(1+u)+(1-u)}{(1+u)(1-u)} = \frac{1}{2} \int \frac{1}{1+u} + \frac{1}{1-u}\,du$$
And use, $\int \frac{1}{u}\,du = \ln|u|$
A: 
Although the integral can be evaluated in a straightforward way using real analysis, I thought it might be instructive to present an approach based on complex analysis.  To that end, we now proceed.

We use Euler's Formula, $e^{ix}=\cos(x)+i\sin(x)$, to write $\displaystyle \sec(x)=\frac2{e^{ix}+e^{-ix}}=\frac{2e^{ix}}{1+e^{i2x}}$.  Then, we have
$$\begin{align}
\int \sec(x)\,dx&=\int \frac2{e^{ix}+e^{-ix}}\\\\
&=\int \frac{2e^{ix}}{1+e^{i2x}}\,dx \\\\
&=-i2 \int \frac{1}{1+(e^{ix})^2}\,d(e^{ix})\\\\
&=-i2 \arctan(e^{ix})+C\tag 1\\\\
&=\log\left(\frac{1-ie^{ix}}{1+ie^{ix}}\right)+C\tag2\\\\
&=\log\left(-i\left(\frac{1+\sin(x)}{i\cos(x)}\right)\right)+C\tag3\\\\
&=\log(\sec(x)+\tan(x))+C'\tag4
\end{align}$$

NOTES:
In going from $(1)$ to $(2)$, we used the identity $\arctan(z)=i2\log\left(\frac{1-iz}{1+iz}\right)  $
In going from $(2)$ to $(3)$, we multiplied the numerator and denominator of the argument of the logarithm function by $1-ie^{ix}$.  Then, we used
$$\frac{1-ie^{ix}}{1+ie^{ix}}=\frac{-i2\cos(x)}{2(1-\sin(x))}=-i\frac{1+\sin(x)}{\cos(x)}$$
Finally, in going from $(3)$ to $(4)$, we absorbed the term $\log(-i)$ into the integration constant $C$ and labeled the new integration constant $C'=C+\log(-i)$.
A: Just to spell out Lord Shark the Unknown's suggestion, $$t=\tan\frac{x}{2}\implies\sec x=\frac{1+t^2}{1-t^2},\,dx=\frac{2dt}{1+t^2}\implies\int\sec xdx=\int\frac{2 dt}{1-t^2}.$$From that point on, the same partial-fractions treatment as in multiple other answers can be used. Admittedly the expression thus obtained for the antiderivative is $\ln\left|\frac{1+\tan\frac{x}{2}}{1-\tan\frac{x}{2}}\right|+C$ instead of $\frac12\ln\left|\frac{1+\sin x}{1-\sin x}\right|+C$ or $\ln|\sec x+\tan x|+C$, but of course they're all the same thanks to suitable trigonometric identities (again, obtainable by writing things as functions of $\tan\frac{x}{2}$).
