The antiderivative of \(\tan x\) is \(-\ln|\cos x| + C\), which can also be written as \(\ln|\sec x| + C\). Both forms are the same, because \(-\ln|\cos x| = \ln\left|\frac{1}{\cos x}\right|\). To get it, write \(\tan x = \frac{\sin x}{\cos x}\) and substitute \(u = \cos x\).
Most students expect the antiderivative of \(\tan x\) to be another trig function. It is not. It is a logarithm, and that surprise is exactly why this integral shows up so often on quizzes. This guide builds the answer from scratch in four steps, shows a second way to get it, explains the absolute value, and then covers the related integrals people search for: \(\tan^2 x\), \(\tan^3 x\), \(\tan(ax)\) and the inverse tangent, \(\arctan x\).
Derive It in 4 Steps
- Rewrite tangent as a fraction. \(\displaystyle\int \tan x\,dx = \int \frac{\sin x}{\cos x}\,dx\)
- Pick \(u\) as the bottom. Let \(u = \cos x\). Then \(du = -\sin x\,dx\), so \(\sin x\,dx = -du\).
- Substitute. \(\displaystyle\int \frac{\sin x}{\cos x}\,dx = \int \frac{-du}{u} = -\ln|u| + C\)
- Put \(x\) back. \(\displaystyle\int \tan x\,dx = -\ln|\cos x| + C\)
The key move is step 2: the top, \(\sin x\), is (minus) the derivative of the bottom, \(\cos x\). Whenever the top of a fraction is the derivative of the bottom, the answer is the natural log of the bottom. That pattern comes from the rule \(\int \frac{1}{u}\,du = \ln|u| + C\), explained in the antiderivative of 1/x guide. The substitution method itself is covered in u-substitution.
A Second Way: Substitute \(u = \sec x\)
Multiply the top and bottom by \(\sec x\):
\[\int \tan x\,dx = \int \frac{\sec x\tan x}{\sec x}\,dx.\]
Now the top, \(\sec x\tan x\), is exactly the derivative of the bottom, \(\sec x\). Let \(u = \sec x\), \(du = \sec x\tan x\,dx\):
\[\int \frac{du}{u} = \ln|\sec x| + C.\]
Two different methods, two different-looking answers. Are they really equal?
Why \(-\ln|\cos x|\) and \(\ln|\sec x|\) Are the Same
Use the log rule \(-\ln a = \ln\frac{1}{a}\):
\[-\ln|\cos x| = \ln\left|\frac{1}{\cos x}\right| = \ln|\sec x|.\]
So the two answers are identical, not just “equal up to a constant”. Use whichever form your teacher or textbook prefers. You can always check either one by differentiating:
\[\frac{d}{dx}\big(-\ln|\cos x|\big) = -\frac{1}{\cos x}\cdot(-\sin x) = \frac{\sin x}{\cos x} = \tan x. \;✓\]
Why the Absolute Value Matters
The natural log only accepts positive numbers. But \(\cos x\) is negative for many values of \(x\), for example between \(\frac{\pi}{2}\) and \(\frac{3\pi}{2}\), and \(\tan x\) is still defined there. Without the absolute value, \(-\ln(\cos x)\) would not exist on those intervals, and your antiderivative would have holes.
| \(x\) | \(\cos x\) | \(-\ln(\cos x)\) | \(-\ln|\cos x|\) |
|---|---|---|---|
| \(\frac{\pi}{4}\) | \(0.707\) | \(0.347\) | \(0.347\) |
| \(\frac{3\pi}{4}\) | \(-0.707\) | undefined | \(0.347\) |
| \(\pi\) | \(-1\) | undefined | \(0\) |
With the absolute value, the formula works on every interval where \(\tan x\) exists. The vertical asymptotes of \(\tan x\) at \(x = \frac{\pi}{2} + k\pi\) are also where \(\cos x = 0\) and the antiderivative blows up.
Don’t Mix Up the Derivative and the Antiderivative
| Operation on \(\tan x\) | Result |
|---|---|
| Derivative | \(\sec^2 x\) |
| Antiderivative | \(-\ln|\cos x| + C = \ln|\sec x| + C\) |
Because the derivative of \(\tan x\) is \(\sec^2 x\), the reverse is also true: \(\int \sec^2 x\,dx = \tan x + C\). That is a different integral from \(\int \tan x\,dx\), and confusing the two is a classic test mistake.
The Tangent Family
\(\int \tan(ax)\,dx\)
Divide by the inside slope, just like with sine and cosine:
\[\int \tan 3x\,dx = -\frac13\ln|\cos 3x| + C \qquad \int \tan\frac{x}{2}\,dx = -2\ln\left|\cos\frac{x}{2}\right| + C\]
\(\int \tan^2 x\,dx\)
Use the identity \(\tan^2 x = \sec^2 x – 1\), then integrate each part:
\[\int \tan^2 x\,dx = \int (\sec^2 x – 1)\,dx = \tan x – x + C.\]
\(\int \tan^3 x\,dx\)
Split off one \(\tan^2 x\) and use the same identity:
\[\int \tan^3 x\,dx = \int \tan x\,(\sec^2 x – 1)\,dx = \int \tan x\sec^2 x\,dx – \int \tan x\,dx.\]
The first part is a substitution with \(u = \tan x\), giving \(\frac{\tan^2 x}{2}\). The second part is our main result. Together:
\[\int \tan^3 x\,dx = \frac{\tan^2 x}{2} + \ln|\cos x| + C.\]
\(\int \sec x\tan x\,dx\) and \(\int \tan x\sec^2 x\,dx\)
- \(\int \sec x\tan x\,dx = \sec x + C\), because \(\frac{d}{dx}\sec x = \sec x\tan x\).
- \(\int \tan x\sec^2 x\,dx = \frac{\tan^2 x}{2} + C\), with \(u = \tan x\).
Antiderivative of \(\tan^{-1} x\) (arctan x)
\(\tan^{-1} x\) usually means the inverse tangent, \(\arctan x\), not \(\frac{1}{\tan x}\). Its antiderivative uses integration by parts with \(u = \arctan x\) and \(dv = dx\), so \(du = \frac{1}{1 + x^2}\,dx\) and \(v = x\):
\[\int \arctan x\,dx = x\arctan x – \int \frac{x}{1 + x^2}\,dx = x\arctan x – \frac12\ln(1 + x^2) + C.\]
And going the other way, \(\arctan x\) is itself an antiderivative: \(\int \frac{1}{1 + x^2}\,dx = \arctan x + C\).
If your problem means \(\frac{1}{\tan x}\), that is \(\cot x\), and \(\int \cot x\,dx = \ln|\sin x| + C\). Read the notation carefully.
Quick Reference Table
| Integral | Answer |
|---|---|
| \(\int \tan x\,dx\) | \(-\ln|\cos x| + C\) or \(\ln|\sec x| + C\) |
| \(\int \tan(ax)\,dx\) | \(-\frac1a\ln|\cos ax| + C\) |
| \(\int \tan^2 x\,dx\) | \(\tan x – x + C\) |
| \(\int \tan^3 x\,dx\) | \(\frac{\tan^2 x}{2} + \ln|\cos x| + C\) |
| \(\int \sec^2 x\,dx\) | \(\tan x + C\) |
| \(\int \sec x\tan x\,dx\) | \(\sec x + C\) |
| \(\int \cot x\,dx\) | \(\ln|\sin x| + C\) |
| \(\int \arctan x\,dx\) | \(x\arctan x – \frac12\ln(1 + x^2) + C\) |
A Definite Integral Example
\[\int_0^{\pi/3} \tan x\,dx = \Big[-\ln|\cos x|\Big]_0^{\pi/3} = -\ln\frac12 + \ln 1 = \ln 2 \approx 0.693.\]
Be careful with limits: if the interval crosses an asymptote such as \(x = \frac{\pi}{2}\), the integral does not have a finite value, and you cannot just plug in the endpoints. That case is an improper integral. You can see the area in the definite integral calculator.
Spot the Mistake
Each “answer” below is wrong. Can you see why before opening it?
\(\int \tan x\,dx = \sec^2 x + C\)
That is the derivative of \(\tan x\), not the antiderivative. Correct: \(-\ln|\cos x| + C\).
\(\int \tan x\,dx = \frac{\tan^2 x}{2} + C\)
The power rule only works on \(x^n\). Differentiating \(\frac{\tan^2 x}{2}\) gives \(\tan x\sec^2 x\), not \(\tan x\).
\(\int \tan x\,dx = \ln|\cos x| + C\)
The minus sign is missing. It comes from \(du = -\sin x\,dx\) in the substitution.
Practice: Try These Yourself
1. \(\int 5\tan x\,dx\)
\(-5\ln|\cos x| + C\), or \(5\ln|\sec x| + C\).
2. \(\int \tan 4x\,dx\)
\(-\frac14\ln|\cos 4x| + C\).
3. \(\int (\tan^2 x + 1)\,dx\)
\(\tan^2 x + 1 = \sec^2 x\), so the answer is \(\tan x + C\).
4. \(\int x\tan(x^2)\,dx\)
Let \(u = x^2\), \(du = 2x\,dx\). Answer: \(-\frac12\ln|\cos(x^2)| + C\).
5. \(\int_0^{\pi/4} \tan x\,dx\)
\(-\ln\left(\cos\frac{\pi}{4}\right) = -\ln\frac{\sqrt2}{2} = \frac12\ln 2 \approx 0.347\).
Check your work with the antiderivative calculator or practice the method with the u-substitution calculator.
Calculators for This Topic
Practice what you just read. Each tool shows full steps and checks its own answer.
- ∫Antiderivative Calculator – Find any antiderivative with full steps and a check.
- uU-Substitution Calculator – See how to pick u and du for any substitution.
- ∫ₐᵇDefinite Integral Calculator – Area under a curve between two limits, with a graph.
Frequently Asked Questions
What is the antiderivative of tan x?
The antiderivative of \(\tan x\) is \(-\ln|\cos x| + C\), which is the same as \(\ln|\sec x| + C\).
Why is the integral of tan x a logarithm?
Because \(\tan x = \frac{\sin x}{\cos x}\), and the top is minus the derivative of the bottom. Integrals of the form \(\frac{u’}{u}\) always give \(\ln|u|\).
What is the antiderivative of tan squared x?
Use \(\tan^2 x = \sec^2 x – 1\). The answer is \(\tan x – x + C\).
What is the antiderivative of tan inverse x?
For \(\arctan x\), the antiderivative is \(x\arctan x – \frac12\ln(1 + x^2) + C\), found with integration by parts.
Is ln|sec x| the same as −ln|cos x|?
Yes. Since \(\sec x = \frac{1}{\cos x}\), the log rule \(\ln\frac1a = -\ln a\) shows the two forms are exactly equal.


