Quick answer:
$$\int\tan x\,dx = -\ln|\cos x| + C = \ln|\sec x| + C$$
The integral of tan x is one of the first results you meet where the answer is a logarithm even though the integrand has no logarithm in it. Below you’ll see where that log comes from, two short derivations, seven worked examples from warm-up to exam level, the mistakes that cost points, and practice problems with answers.
Why a logarithm appears
Every time the top of a fraction is the derivative of the bottom (up to a constant), the antiderivative is a log. That’s the pattern behind the integral of 1/x:
$$\int\frac{f'(x)}{f(x)}\,dx = \ln|f(x)| + C$$
Tangent is secretly one of these fractions. Write it as sine over cosine, and notice that the derivative of \( \cos x \) is \( -\sin x \). The numerator is the derivative of the denominator, except for a minus sign. That minus sign is exactly where the minus in \( -\ln|\cos x| \) comes from.
There’s also a picture worth keeping in mind. On \( [0, \frac{\pi}{2}) \), \( \tan x \) starts at 0 and shoots up toward a vertical asymptote. The area under it from 0 to \( b \) is \( -\ln(\cos b) \). As \( b \) approaches \( \frac{\pi}{2} \), \( \cos b \) shrinks toward 0, its log heads to negative infinity, and the area grows without bound. The formula and the graph tell the same story.
Derivation with u-substitution
Write tangent as sine over cosine:
$$\int\tan x\,dx = \int\frac{\sin x}{\cos x}\,dx$$
Let \( u = \cos x \). Then \( du = -\sin x\,dx \), so \( \sin x\,dx = -du \):
$$\int\frac{-du}{u} = -\ln|u| + C = -\ln|\cos x| + C$$
(See u-substitution if this step feels unfamiliar.)
A second derivation: substitute sec x
You can also reach the other form directly. Multiply the top and bottom by \( \sec x \):
$$\int\tan x\,dx = \int\frac{\sec x\tan x}{\sec x}\,dx$$
Now let \( u = \sec x \), so \( du = \sec x\tan x\,dx \). The integral becomes \( \int\frac{du}{u} \), which gives \( \ln|\sec x| + C \) in one line. Both routes use the same idea: find a function whose derivative is sitting in the numerator.
Why the two answers are the same
Using \( -\ln a = \ln\frac1a \):
$$-\ln|\cos x| = \ln\frac{1}{|\cos x|} = \ln|\sec x|$$
Both forms are correct; textbooks use either one.
Check by differentiating
$$\frac{d}{dx}\left(-\ln|\cos x|\right) = -\frac{-\sin x}{\cos x} = \tan x \checkmark$$
Checking by differentiation takes ten seconds, and it’s the fastest way to catch a lost minus sign. Get in the habit of doing it on every log answer, since sign slips are the most common error with this integral. If you want to review the other direction, the derivative of tan x is \( \sec^2 x \), a different fact entirely.
Symmetry and periodicity shortcuts
Two properties of tangent can save you from computing anything at all.
Tangent is odd. Since \( \tan(-x) = -\tan x \), the graph on the left of the origin is an upside-down copy of the graph on the right. On any symmetric interval that avoids the asymptotes, the signed areas cancel exactly. For instance, the integral from \( -\frac{\pi}{4} \) to \( \frac{\pi}{4} \) equals 0. The formula agrees: \( -\ln|\cos x| \) is an even function, so plugging in \( a \) and \( -a \) gives the same number, and the difference is zero.
Tangent repeats every \( \pi \). Shifting the interval by \( \pi \) doesn’t change the area. So the integral from \( \pi \) to \( \frac{5\pi}{4} \) equals the integral from 0 to \( \frac{\pi}{4} \), which is \( \frac{\ln 2}{2} \). Again the formula agrees, because \( |\cos x| \) also repeats every \( \pi \).
Both shortcuts only apply when the whole interval sits between two neighboring asymptotes. If an odd multiple of \( \frac{\pi}{2} \) lies inside, the cancellation argument is not valid, because each half diverges on its own.
Worked examples
Example 1 (linear inside). Find \( \int\tan(5x)\,dx \). The inside is \( 5x \), so let \( u = 5x \) and \( dx = \frac{du}{5} \). You get one fifth of the basic answer:
$$\int\tan(5x)\,dx = -\frac15\ln|\cos 5x| + C$$
Example 2 (definite). Evaluate from 0 to \( \frac{\pi}{4} \). Plug the limits into \( -\ln|\cos x| \):
$$\begin{aligned} \int_0^{\pi/4}\tan x\,dx &= \left[-\ln|\cos x|\right]_0^{\pi/4} \\ &= -\ln\frac{\sqrt2}{2} + \ln 1 \\ &= \frac{\ln 2}{2} \approx 0.3466 \end{aligned}$$
Be careful not to integrate across \( x = \frac{\pi}{2} \), where \( \tan x \) has a vertical asymptote: that integral diverges.
Example 3 (another interval). Evaluate \( \int_{\pi/6}^{\pi/3}\tan x\,dx \). Here \( \cos\frac{\pi}{3} = \frac12 \) and \( \cos\frac{\pi}{6} = \frac{\sqrt3}{2} \):
$$\begin{aligned} \int_{\pi/6}^{\pi/3}\tan x\,dx &= -\ln\tfrac12 + \ln\tfrac{\sqrt3}{2} \\ &= \ln 2 + \ln\sqrt3 - \ln 2 \\ &= \frac{\ln 3}{2} \approx 0.5493 \end{aligned}$$
Example 4 (half angle). For \( \int\tan\frac{x}{2}\,dx \), the coefficient of \( x \) is \( \frac12 \), so you divide by \( \frac12 \), which means multiplying by 2:
$$\int\tan\frac x2\,dx = -2\ln\left|\cos\frac x2\right| + C$$
Example 5 (two correct answers). For \( \int\tan x\sec^2 x\,dx \), you could let \( u = \tan x \) and get \( \frac{\tan^2 x}{2} + C \). Or you could let \( u = \sec x \), since \( du = \sec x\tan x\,dx \), and get \( \frac{\sec^2 x}{2} + C \). Both are right: because \( \sec^2 x = \tan^2 x + 1 \), the two answers differ only by the constant \( \frac12 \), which the \( C \) absorbs.
Example 6 (odd power). For \( \int\tan^3 x\,dx \), peel off one \( \tan^2 x \) and use \( \tan^2 x = \sec^2 x - 1 \):
$$\begin{aligned} \int\tan^3 x\,dx &= \int\tan x\sec^2 x\,dx - \int\tan x\,dx \\ &= \frac{\tan^2 x}{2} + \ln|\cos x| + C \end{aligned}$$
Notice the plus sign on the log: subtracting \( -\ln|\cos x| \) flips it.
Example 7 (exam level). For \( \int e^x\tan(e^x)\,dx \), the inside function is \( e^x \), and its derivative \( e^x \) is already there. Let \( u = e^x \):
$$\int e^x\tan(e^x)\,dx = \int\tan u\,du = -\ln|\cos(e^x)| + C$$
The lesson generalizes: when tangent has a complicated inside, look for that inside’s derivative multiplying everything else. If it’s there, the substitution reduces the problem to the basic formula. If it isn’t, the integral usually needs a different technique or has no elementary answer.
You can check any of these with the integral calculator or the solver below.
Related integrals
| Integral | Result |
|---|---|
| \( \int\tan(ax)\,dx \) | \( -\frac1a\ln\lvert\cos ax\rvert + C \) |
| \( \int\cot x\,dx \) | \( \ln\lvert\sin x\rvert + C \) |
| \( \int\tan^2x\,dx \) | \( \tan x - x + C \) |
| \( \int\tan x\sec^2x\,dx \) | \( \frac{\tan^2 x}{2} + C \) |
| \( \int\sec x\,dx \) | \( \ln\lvert\sec x + \tan x\rvert + C \) |
Why \( \tan^2 x \) works: use \( \tan^2 x = \sec^2 x - 1 \), then integrate sec²x to get \( \tan x \) and subtract \( x \).
Common mistakes
- Answering \( \sec^2 x \). That’s the derivative of \( \tan x \), not its integral. Integrating tan x must produce something whose derivative is tan x.
- Dropping the minus sign. Writing \( \ln|\cos x| \) gives an answer whose derivative is \( -\tan x \). The correct form is \( -\ln|\cos x| \), or equivalently \( \ln|\sec x| \).
- Forgetting the absolute value. Cosine is negative on \( (\frac{\pi}{2}, \frac{3\pi}{2}) \), and the log of a negative number is undefined. Keep the bars.
- Forgetting to divide by the inner coefficient. The integral of \( \tan 3x \) is \( -\frac13\ln|\cos 3x| + C \), not \( -\ln|\cos 3x| + C \).
- Integrating straight through an asymptote. An integral such as \( \int_0^{\pi}\tan x\,dx \) is not zero by symmetry. The integrand blows up at \( \frac{\pi}{2} \), so it’s an improper integral that diverges.
Where it’s used
The integral of tan x shows up whenever you integrate odd powers of tangent, as in Example 6, and in the reduction formulas for \( \int\tan^n x\,dx \). It also appears in differential equations: for \( y' + (\tan x)\,y = g(x) \), the integrating factor is \( e^{\int\tan x\,dx} = e^{\ln|\sec x|} = |\sec x| \). Its twin, \( \int\cot x\,dx = \ln|\sin x| + C \), comes from the same substitution with \( u = \sin x \). More broadly, the idea of spotting a derivative in the numerator is one of the most reusable habits in integration: it handles \( \frac{2x}{x^2+1} \), \( \frac{e^x}{e^x+1} \) and \( \cot x \) with exactly the same one-line argument.
Try it yourself
Step-by-step solver·Exact symbolic engine
Interactive Calculus Problem Solver
Derivatives, antiderivatives, definite and improper integrals, and limits. Every answer comes with the rules used, and antiderivatives are verified by differentiating them back.
Input syntax
- Powers
x^2, rootssqrt(x),cbrt(x), absolute value|x| - Implicit multiplication works:
3x sin(2x) sin cos tan sec csc cot,asin acos atan,sinh cosh tanhe^xorexp(x);ln(x)andlog(x)are both the natural log- Constants
piande; bounds acceptinfand-inf
Practice problems
Try these before checking the answers.
- \( \int\tan(2x)\,dx \)
- \( \int_0^{\pi/3}\tan x\,dx \)
- \( \int x\tan(x^2)\,dx \)
- \( \int\cot(3x)\,dx \)
- \( \int\frac{\sec^2 x}{\tan x}\,dx \)
- \( \int_0^{\pi/6}\tan(2x)\,dx \)
Answers: (1) \( -\frac12\ln|\cos 2x| + C \); (2) \( \ln 2 \); (3) \( -\frac12\ln|\cos(x^2)| + C \); (4) \( \frac13\ln|\sin 3x| + C \); (5) \( \ln|\tan x| + C \); (6) \( \frac{\ln 2}{2} \).
FAQ
Is the integral of tan x equal to sec²x?
No, \( \sec^2 x \) is the derivative of \( \tan x \) (see derivative of tan x).
Why the absolute value?
\( \cos x \) is negative on some intervals, and \( \ln \) of a negative number is undefined. The absolute value makes the formula valid wherever \( \tan x \) is defined.
What is the integral of tan x from 0 to π/2?
It diverges. As \( x \) approaches \( \frac{\pi}{2} \) from the left, \( -\ln(\cos x) \) grows without bound, so the area under the curve is infinite.
Should I write −ln|cos x| or ln|sec x|?
Either one earns full credit. They’re the same function, so pick the form that makes the rest of your problem simpler.
What is the integral of tan x times sec x?
That’s \( \sec x + C \), because the derivative of \( \sec x \) is \( \sec x\tan x \). No logarithm is involved.
How do I integrate tan(ax + b)?
Substitute \( u = ax + b \), so \( dx = \frac{du}{a} \). The answer is \( -\frac1a\ln|\cos(ax + b)| + C \). For example, the integral of \( \tan(4x + 1) \) is \( -\frac14\ln|\cos(4x + 1)| + C \). The constant \( b \) shifts the graph but never changes the factor in front.
Related: integral of sec x.
Further reading
- Integrals Involving Trig Functions (Paul’s Online Math Notes) — strategies for powers of tangent and secant, with many worked examples.
- Substitution Rule for Indefinite Integrals (Paul’s Online Math Notes) — a thorough review of the substitution technique used in the derivation.
Calculators for this topic
Keep learning
Fundamental Theorem of Calculus (Parts 1 and 2) Explained
The Fundamental Theorem of Calculus links derivatives and integrals. Both parts explained with intuition, formulas and worked examples.
Improper Integrals: How to Tell If They Converge or Diverge
Improper integrals have infinite limits or infinite integrands. Learn the limit definition, the p-integral test, comparison, and 6 worked examples.

