# Integral of tan x: Formula, Derivation and Examples

> The integral of tan x is ln|sec x| + C, or -ln|cos x| + C. See the u-substitution with u = cos x, why both forms agree, and worked definite integrals.

Source: https://practicecalculus.com/integrals/tan-x
Updated: 2026-11-06

$$ \int \tan x\,dx = \ln\lvert\sec x\rvert + C $$

## At a glance

- **Antiderivative:** $\ln\lvert\sec x\rvert + C$
- **Equivalent form:** $-\ln\lvert\cos x\rvert + C$
- **Method:** Rewrite as $\dfrac{\sin x}{\cos x}$, then $u = \cos x$
- **Linear form:** $\int\tan(ax)\,dx = \dfrac{1}{a}\ln\lvert\sec(ax)\rvert + C$
- **Check:** $\dfrac{d}{dx}\ln\lvert\sec x\rvert = \tan x$

## Key takeaways

- The integral of $\tan x$ is $\ln\lvert\sec x\rvert + C$, which is the same as $-\ln\lvert\cos x\rvert + C$.
- Write $\tan x = \frac{\sin x}{\cos x}$ and substitute $u = \cos x$.
- It is not $\sec^2 x$. That is the derivative of $\tan x$.
- The absolute value keeps the formula valid where $\cos x < 0$.

The **integral of $\tan x$ is $\ln\lvert\sec x\rvert + C$**. There is no basic derivative that gives $\tan x$ directly, so the answer comes from a quick substitution.

$$\int \tan x\,dx = \ln\lvert\sec x\rvert + C = -\ln\lvert\cos x\rvert + C$$

## Graph of tan x and its antiderivative

```graph
title: 'Area under y = tan x from 0 to π/4, with the antiderivative y = ln|sec x|'
caption: 'The shaded area is $\ln\sqrt{2} \approx 0.347$. The dashed antiderivative has a minimum at $x = 0$, where $\tan x = 0$, and shoots up near the asymptotes at $x = \pm\frac{\pi}{2}$.'
x: [-1.6, 1.6]
y: [-3, 3]
functions:
  - expr: tan(x)
    label: y = tan x
    domain: [-1.25, 1.25]
  - expr: ln(abs(sec(x)))
    label: y = ln|sec x| (antiderivative)
    dashed: true
    domain: [-1.52, 1.52]
shade: {expr: tan(x), from: 0, to: 0.7854}
points:
  - [0.7854, 1, '(π/4, 1)']
```

## Derivation, step by step

1. Rewrite with sine and cosine: $\displaystyle\int\tan x\,dx = \int\frac{\sin x}{\cos x}\,dx$.
2. Let $u = \cos x$. Then $du = -\sin x\,dx$, so $\sin x\,dx = -du$.
3. Substitute: $\displaystyle\int\frac{-du}{u} = -\ln\lvert u\rvert + C$.
4. Put $u = \cos x$ back: $-\ln\lvert\cos x\rvert + C$.
5. Use a log rule to tidy up: $-\ln\lvert\cos x\rvert = \ln\frac{1}{\lvert\cos x\rvert} = \ln\lvert\sec x\rvert$.

Step 3 uses the [integral of 1/x](/integrals/1-over-x). This is the pattern $\int\frac{f'}{f}\,dx = \ln\lvert f\rvert$, with a minus sign because the top is $-f'$.

## Check by differentiating

$$\frac{d}{dx}\ln\lvert\sec x\rvert = \frac{1}{\sec x}\cdot\sec x\tan x = \tan x \checkmark$$

This uses the [derivative of sec x](/derivatives/sec-x), $\sec x\tan x$. Compare with the [derivative of tan x](/derivatives/tan-x), which is $\sec^2 x$: a completely different result.

## Definite integrals

$$\int_0^{\pi/4}\tan x\,dx = \Big[\ln\lvert\sec x\rvert\Big]_0^{\pi/4} = \ln\sqrt{2} - \ln 1 = \frac{1}{2}\ln 2$$

$$\int_0^{\pi/3}\tan x\,dx = \ln\sec\frac{\pi}{3} - 0 = \ln 2$$

Since $\tan x$ is odd, $\int_{-a}^{a}\tan x\,dx = 0$ for $a < \frac{\pi}{2}$. Integrating up to $\frac{\pi}{2}$ is improper and diverges, because $\ln\sec x \to \infty$.

## Substitution variants

$$\int\tan(ax + b)\,dx = \frac{1}{a}\ln\lvert\sec(ax + b)\rvert + C$$

For example, $\int\tan(3x)\,dx = \frac{1}{3}\ln\lvert\sec(3x)\rvert + C$.

With a matching inside derivative: $\int x\tan(x^2)\,dx = \frac{1}{2}\ln\lvert\sec(x^2)\rvert + C$.

## Worked examples

**Example 1: tan times sec².** $\int\tan x\sec^2 x\,dx$ with $u = \tan x$, $du = \sec^2 x\,dx$, gives $\frac{\tan^2 x}{2} + C$.

**Example 2: tan cubed.** Use $\tan^2 x = \sec^2 x - 1$:

$$\int\tan^3 x\,dx = \int\tan x\sec^2 x\,dx - \int\tan x\,dx = \frac{\tan^2 x}{2} - \ln\lvert\sec x\rvert + C$$

**Example 3: the cot partner.** The same steps with $u = \sin x$ give $\int\cot x\,dx = \ln\lvert\sin x\rvert + C$. See the [integral of cot x](/integrals/cot-x).

## Common mistakes

1. **Answering $\sec^2 x$.** That is the derivative of $\tan x$, not the integral.
2. **Answering $\ln\lvert\cos x\rvert$ without the minus sign.** Its derivative is $-\tan x$.
3. **Dropping the absolute value.** $\cos x$ is negative on $\left(\frac{\pi}{2}, \frac{3\pi}{2}\right)$, and $\ln$ of a negative number is undefined.
4. **Mixing it up with $\int\tan^2 x\,dx$**, which is $\tan x - x + C$. See the [integral of tan² x](/integrals/tan-squared-x).

Practise with [u-substitution problems](/practice-problems/u-substitution).

## Frequently asked questions

### What is the integral of tan x?

The integral of $\tan x$ is $\ln\lvert\sec x\rvert + C$. You will also see it written as $-\ln\lvert\cos x\rvert + C$.

### Are ln|sec x| and -ln|cos x| the same?

Yes. Since $\sec x = \frac{1}{\cos x}$, $\ln\lvert\sec x\rvert = \ln\frac{1}{\lvert\cos x\rvert} = -\ln\lvert\cos x\rvert$.

### What is the integral of tan x from 0 to π/4?

It is $\ln\sec\frac{\pi}{4} - \ln\sec 0 = \ln\sqrt{2} = \frac{1}{2}\ln 2 \approx 0.347$.

### What is the integral of tan(2x)?

It is $\frac{1}{2}\ln\lvert\sec(2x)\rvert + C$.
