The integral of tan(x) is −ln|cos x| + C. Equivalently, it is ln|sec x| + C. This derivation works by expressing tan(x) as sin(x)/cos(x). Then, you apply u-substitution with u = cos x. This converts the trigonometric integral into the standard logarithmic form ∫ (1/u) du.
The integral of tangent is one of the first results in calculus. Here, a natural logarithm appears in the solution. This happens even though no logarithmic function is present in the original integrand. Below, you will find the complete step-by-step derivation. Also, you will see proofs for both standard forms. We include seven fully worked examples, ranging from introductory to exam level. You will also find common pitfalls to avoid and practice problems with full solutions.
Why does a logarithm appear when integrating tan(x)?
To understand why a logarithm emerges when integrating tangent, consider the foundational rule. This rule applies to logarithmic antiderivatives. Whenever an integrand is a quotient, its antiderivative is a natural logarithm. This occurs when the numerator is the exact derivative of the denominator. It can also be up to a constant multiplier. This pattern is identical to the one behind the integral of 1/x.

$$\int \frac{f'(x)}{f(x)} \, dx = \ln|f(x)| + C$$
Tangent has a natural quotient structure. You can rewrite tangent in terms of fundamental trigonometric functions. This gives:
$$\tan x = \frac{\sin x}{\cos x}$$
You can differentiate the denominator. This yields \(\frac{d}{dx}(\cos x) = -\sin x\). The numerator is \(\sin x\). It almost perfectly matches this derivative. However, it differs by only a negative sign. You can factor out that negative sign. As a result, this creates an integrand of the exact form \(\frac{f'(x)}{f(x)}\). This directly produces the natural logarithm in the answer.
On the interval \([0, \frac{\pi}{2})\), \(\tan x\) starts at 0. Geometrically, it grows rapidly. It approaches a vertical asymptote at \(x = \frac{\pi}{2}\). The accumulated area under the curve from 0 to a boundary \(b\) is \(-\ln(\cos b)\). As \(b\) approaches \(\frac{\pi}{2}\) from the left, \(\cos b\) approaches 0 from the positive side. Therefore, the logarithm of a value near zero approaches negative infinity. So, \(-\ln(\cos b)\) grows toward positive infinity. The algebraic derivation and the geometric behavior of the function describe the exact same mathematical reality.
Derivation of the integral of tan(x) using u-substitution
The primary method to evaluate \(\int \tan x \, dx\) uses basic u-substitution. Follow these exact steps:

- Rewrite tangent using fundamental identities: Express tangent as the ratio of sine to cosine. You can write it as:
$$\int \tan x \, dx = \int \frac{\sin x}{\cos x} \, dx$$ - Choose the substitution variable: Set \(u\) equal to the denominator. So, you have:
$$u = \cos x$$ - Compute the differential: Take the derivative of \(u\) with respect to \(x\). This gives you:
$$du = -\sin x \, dx \quad \implies \quad \sin x \, dx = -du$$ - Substitute into the integral: Replace \(\cos x\) with \(u\). Also, replace \(\sin x \, dx\) with \(-du\). So, the integral becomes:
$$\int \frac{\sin x}{\cos x} \, dx = \int \frac{-du}{u} = -\int \frac{1}{u} \, du$$ - Integrate the standard logarithmic form: Recall that \(\int \frac{1}{u} \, du = \ln|u| + C\). Therefore, you can integrate this form:
$$-\int \frac{1}{u} \, du = -\ln|u| + C$$ - Substitute back the original variable: Replace \(u\) with \(\cos x\). As a result, you get:
$$\int \tan x \, dx = -\ln|\cos x| + C$$
Alternative derivation: substituting sec(x)
You can derive the second standard form, \(\ln|\sec x| + C\). You can do this directly in one step. First, multiply the numerator and denominator by \(\sec x\). Then apply substitution.
You can rewrite the integral. So, you get:
$$\int \tan x \, dx = \int \frac{\tan x \cdot \sec x}{\sec x} \, dx = \int \frac{\sec x \tan x}{\sec x} \, dx$$
Now, choose \(u = \sec x\). The derivative of secant is \(\frac{d}{dx}(\sec x) = \sec x \tan x\). So, \(du = \sec x \tan x \, dx\). You can substitute these terms into the integral. This transforms the expression directly.
$$\int \frac{\sec x \tan x}{\sec x} \, dx = \int \frac{du}{u} = \ln|u| + C = \ln|\sec x| + C$$
Both derivations use a key idea. You must find a function in the denominator. Its derivative then appears directly in the numerator.
Why −ln|cos x| and ln|sec x| are mathematically identical
Students often wonder if their answer changes when they choose either \(-\ln|\cos x| + C\) or \(\ln|\sec x| + C\). However, both expressions represent the exact same mathematical function. You can demonstrate this using the logarithmic power rule, \(k \cdot \ln(a) = \ln(a^k)\).
$$-\ln|\cos x| = (-1) \cdot \ln|\cos x| = \ln\left(|\cos x|^{-1}\right)$$
Because \(|\cos x|^{-1} = \frac{1}{|\cos x|} = |\sec x|\), substituting this reciprocal into the logarithm yields:
$$\ln\left(\frac{1}{|\cos x|}\right) = \ln|\sec x|$$
Therefore, \(-\ln|\cos x| + C\) and \(\ln|\sec x| + C\) are entirely interchangeable. You may use whichever form is most convenient for your specific problem.
Verification by differentiating the result
You can verify any integration result. Just take the derivative of your answer. Then, confirm it simplifies back to the original integrand. For example, apply the chain rule to \(-\ln|\cos x|\) to get:
$$\frac{d}{dx}\left(-\ln|\cos x|\right) = -\frac{1}{\cos x} \cdot \frac{d}{dx}(\cos x) = -\frac{1}{\cos x} \cdot (-\sin x) = \frac{\sin x}{\cos x} = \tan x$$
Differentiating \(\ln|\sec x|\) yields the same result:
$$\frac{d}{dx}\left(\ln|\sec x|\right) = \frac{1}{\sec x} \cdot \frac{d}{dx}(\sec x) = \frac{1}{\sec x} \cdot (\sec x \tan x) = \tan x$$
Differentiating your final expression takes only a few seconds and serves as a reliable check against lost negative signs. Note that while the antiderivative of tangent involves logarithms, the derivative of tan x is \(\sec^2 x\).
Domain restrictions, absolute value bars, and vertical asymptotes
The absolute value bars in \(-\ln|\cos x| + C\) are a mathematical requirement. The standard natural logarithm function \(\ln(t)\) has a restricted domain. It only accepts strictly positive real numbers ($t > 0$). However, \(\cos x\) oscillates between \(-1\) and \(1\). For example, it takes negative values in intervals like \((\frac{\pi}{2}, \frac{3}{2}\pi)\). Therefore, you must enclose \(\cos x\) inside absolute value bars. This ensures the logarithm’s input. It remains strictly positive wherever \(\cos x \neq 0\).
Tangent is undefined at odd multiples of \(\frac{\pi}{2}\). For example, \(x = \frac{\pi}{2} + k\pi\) for any integer \(k\). Vertical asymptotes occur at these points. When you evaluate definite integrals of tangent, you must check the integration interval. The interval $[a, b]$ must not contain any vertical asymptotes. If an asymptote falls within or at the boundary of your interval, the expression becomes an improper integral. You must evaluate it using limits. As a result, the integral diverges in most such cases across an asymptote.
Symmetry and periodicity shortcuts for definite integrals
Understanding the algebraic properties of tangent can save time when evaluating definite integrals.

Tangent is an odd function. Because \(\tan(-x) = -\tan(x)\), the graph of tangent is symmetric about the origin. Over any interval $[-a, a]$ without vertical asymptotes, the signed area on the negative side exactly cancels the positive side.
$$\int_{-a}^{a} \tan x \, dx = 0 \quad \text{for } |a| < \frac{\pi}{2}$$
For instance, \(\int_{-\pi/4}^{\pi/4} \tan x \, dx = 0\). The formula confirms this: \(-\ln|\cos(\pi/4)| - (-\ln|\cos(-\pi/4)|) = 0\).
Tangent is periodic with period \(\pi\). You can shift an integration interval by any integer multiple of \(\pi\). This leaves the integral’s value unchanged. However, this is true only if you do not cross any asymptotes.
$$\int_{a + k\pi}^{b + k\pi} \tan x \, dx = \int_{a}^{b} \tan x \, dx$$
For example, \(\int_{\pi}^{5\pi/4} \tan x \, dx = \int_{0}^{\pi/4} \tan x \, dx = \frac{\ln 2}{2}\).
Important caution: Symmetry arguments apply only when the entire interval sits within a single continuous branch of tangent. For example, \(\int_{0}^{\pi} \tan x \, dx\) is not zero. It diverges because the integrand shoots to infinity at \(x = \frac{\pi}{2}\).
Step-by-step worked examples
Example 1: Linear inside function. Evaluate \(\int \tan(5x) \, dx\).

Let \(u = 5x\), so \(du = 5 \, dx\) and \(dx = \frac{du}{5}\). Substituting gives:
So, $$\int \tan(5x) \, dx = \frac{1}{5} \int \tan u \, du$$. This equals $$-\frac{1}{5}\ln|\cos 5x| + C$$.
Example 2: Definite integral on \([0, \frac{\pi}{4}]\). Evaluate \(\int_{0}^{\pi/4} \tan x \, dx\).
Apply the fundamental theorem of calculus using \(-\ln|\cos x|\):
$$\begin{aligned}\int_{0}^{\pi/4} \tan x \, dx &= \left[-\ln|\cos x|\right]_{0}^{\pi/4} &= -\ln\left(\cos \frac{\pi}{4}\right) - \left(-\ln(\cos 0)\right) &= -\ln\left(\frac{\sqrt{2}}{2}\right) + \ln(1) &= -\ln\left(2^{-1/2}\right) + 0 = \frac{\ln 2}{2} \approx 0.3466\end{aligned}$$
Example 3: Definite integral on \([\frac{\pi}{6}, \frac{\pi}{3}]\). Evaluate \(\int_{\pi/6}^{\pi/3} \tan x \, dx\).
Substitute the upper and lower limits:
$$\begin{aligned}\int_{\pi/6}^{\pi/3} \tan x \, dx &= \left[-\ln|\cos x|\right]_{\pi/6}^{\pi/3} &= -\ln\left(\cos \frac{\pi}{3}\right) - \left(-\ln\left(\cos \frac{\pi}{6}\right)\right) &= -\ln\left(\frac{1}{2}\right) + \ln\left(\frac{\sqrt{3}}{2}\right) &= \ln 2 + \ln\sqrt{3} - \ln 2 = \frac{\ln 3}{2} \approx 0.5493\end{aligned}$$
Example 4: Fractional coefficient. Evaluate \(\int \tan\left(\frac{x}{2}\right) \, dx\).
Let \(u = \frac{x}{2}\), so \(du = \frac{1}{2} \, dx\) and \(dx = 2 \, du\). Multiplying by 2 gives:
$$\int \tan\left(\frac{x}{2}\right) \, dx = 2 \int \tan u \, du = -2\ln\left|\cos\left(\frac{x}{2}\right)\right| + C$$
Example 5: Product of tangent and secant squared. Evaluate \(\int \tan x \sec^2 x \, dx\).
Different u-substitution methods
This integral can be solved using two different u-substitutions:
- Method A: Let \(u = \tan x\), so \(du = \sec^2 x \, dx\). The integral becomes \(\int u \, du = \frac{\tan^2 x}{2} + C_1\).
- Method B: Let \(u = \sec x\), so \(du = \sec x \tan x \, dx\). Rewriting as \(\int \sec x (\sec x \tan x) \, dx\) gives \(\int u \, du = \frac{\sec^2 x}{2} + C_2\).
Both answers are correct. Using the trigonometric identity \(\sec^2 x = \tan^2 x + 1\), you can see that \(\frac{\sec^2 x}{2} = \frac{\tan^2 x + 1}{2} = \frac{\tan^2 x}{2} + \frac{1}{2}\). The difference between the two solutions is simply absorbed into the constant of integration (\(C_2 = C_1 + \frac{1}{2}\)).
Example 6: Odd power of tangent. Evaluate \(\int \tan^3 x \, dx\).
Separate \(\tan^3 x\) into \(\tan x \cdot \tan^2 x\) and use the identity \(\tan^2 x = \sec^2 x - 1\):
$$\begin{aligned}\int \tan^3 x \, dx &= \int \tan x (\sec^2 x - 1) \, dx &= \int \tan x \sec^2 x \, dx - \int \tan x \, dx &= \frac{\tan^2 x}{2} - \left(-\ln|\cos x|\right) + C &= \frac{\tan^2 x}{2} + \ln|\cos x| + C\end{aligned}$$
Notice that subtracting \(-\ln|\cos x|\) changes the sign in front of the logarithm to positive.
Example 7: Composite exponential argument. Evaluate \(\int e^x \tan(e^x) \, dx\).
Let \(u = e^x\), so \(du = e^x \, dx\). The integral reduces immediately to the standard tangent integral:
$$\int e^x \tan(e^x) \, dx = \int \tan u \, du = -\ln|\cos(e^x)| + C$$
You can check these worked solutions using an automated integral calculator or the interactive solver below.
Related trigonometric integrals and comparison
The table below summarizes standard antiderivatives for trigonometric functions related to tangent:
| Integral | Result |
|---|---|
| \(\int \tan(ax) \, dx\) | \(-\frac{1}{a}\ln|\cos ax| + C\) |
| \(\int \cot x \, dx\) | \(\ln|\sin x| + C\) |
| \(\int \tan^2 x \, dx\) | \(\tan x - x + C\) |
| \(\int \tan x \sec^2 x \, dx\) | \(\frac{\tan^2 x}{2} + C\) |
| \(\int \sec x \, dx\) | \(\ln|\sec x + \tan x| + C\) |
To evaluate \(\int \tan^2 x \, dx\), rewrite the integrand as \(\sec^2 x - 1\). You can then integrate sec²x directly to get \(\tan x\), resulting in \(\tan x - x + C\). For integrals involving secant alone, see the detailed guide on the integral of sec x.
Common student errors and how to avoid them
- Writing \(\sec^2 x\) as the integral. \(\sec^2 x\) is the derivative of \(\tan x\), not its antiderivative. Remember that integrating tangent must yield a logarithmic function.
- Forgetting the negative sign. Writing \(\ln|\cos x| + C\) without the negative sign gives a function whose derivative is \(-\tan x\). Always verify that your answer includes either \(-\ln|\cos x|\) or \(+\ln|\sec x|\).
- Omitting absolute value bars. Leaving out absolute value bars makes the result invalid for angles where \(\cos x < 0\). Keep the bars written as \(-\ln|\cos x| + C\).
- Forgetting the chain rule factor \(1/a\). When integrating \(\tan(ax)\), you must divide by the inner constant \(a\). For instance, \(\int \tan(3x) \, dx = -\frac{1}{3}\ln|\cos 3x| + C\), not \(-\ln|\cos 3x| + C\).
- Integrating across vertical asymptotes. You must treat integrals over intervals containing \(x = \frac{\pi}{2}\) as improper integrals. Otherwise, evaluating definite integrals like \(\int_{0}^{\pi} \tan x \, dx\) leads to false results. These integrals diverge.
Practical applications in differential equations
The integral of tangent frequently appears when solving first-order linear differential equations using an integrating factor. Consider a differential equation of the form:
$$y' + (\tan x) y = g(x)$$
You compute the integrating factor \(I(x)\) as \(e^{\int P(x) \, dx}\):
$$I(x) = e^{\int \tan x \, dx} = e^{\ln|\sec x|} = |\sec x|$$
Multiplying the entire differential equation by \(\sec x\) simplifies the left side. It becomes a single derivative via the product rule: \(\frac{d}{dx}(y \cdot \sec x) = g(x) \sec x\). Integrating both sides then yields the explicit solution for \(y\).
You can derive the companion integral \(\int \cot x \, dx = \ln|\sin x| + C\). Use the exact same strategy. Set \(u = \sin x\). Recognizing how to transform quotients like \(\frac{f'(x)}{f(x)}\) into logarithms is a practical skill. This is one of the most useful skill sets in integral calculus. It handles expressions like \(\frac{2x}{x^2+1}\), \(\frac{e^x}{e^x+1}\), and \(\cot x\) with identical steps.
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)is the natural log. In this solverlog(x)also means the natural log (the usual calculus convention); typelog10(x)for base 10- Constants
piande; bounds acceptinfand-inf

Practice problems with full solutions
Test your understanding with these practice problems before reviewing the step-by-step answers below.

- \(\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\)
Solutions:
- Solution 1: Use \(u = 2x\), so \(du = 2 \, dx\). The result is \(-\frac{1}{2}\ln|\cos 2x| + C\).
- Solution 2: Evaluate \([-\ln|\cos x|]_{0}^{\pi/3} = -\ln(\cos \frac{\pi}{3}) + \ln(\cos 0) = -\ln(\frac{1}{2}) + 0 = \ln 2\).
- Solution 3: Use \(u = x^2\), so \(du = 2x \, dx\) and \(x \, dx = \frac{du}{2}\). The integral becomes \(\frac{1}{2}\int \tan u \, du = -\frac{1}{2}\ln|\cos(x^2)| + C\).
- Solution 4: Rewrite as \(\int \frac{\cos(3x)}{\sin(3x)} \, dx\). Let \(u = \sin(3x)\), so \(du = 3\cos(3x) \, dx\). The result is \(\frac{1}{3}\ln|\sin(3x)| + C\).
- Solution 5: Let \(u = \tan x\), so \(du = \sec^2 x \, dx\). The integral becomes \(\int \frac{du}{u} = \ln|\tan x| + C\).
- Solution 6: Evaluate \([-\frac{1}{2}\ln|\cos 2x|]_{0}^{\pi/6} = -\frac{1}{2}\ln(\cos \frac{\pi}{3}) + \frac{1}{2}\ln(\cos 0) = -\frac{1}{2}\ln(\frac{1}{2}) + 0 = \frac{\ln 2}{2}\).
Frequently asked questions
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). The integral of \(\tan x\) is \(-\ln|\cos x| + C\).
Why do we need the absolute value bars in −ln|cos x|?
Cosine returns negative values on intervals such as \((\frac{\pi}{2}, \frac{3\pi}{2})\). However, the natural logarithm is undefined for negative inputs. The absolute value bars keep the argument of the logarithm strictly positive across the domain.
What is the integral of tan x from 0 to π/2?
The integral diverges. As \(x\) approaches \(\frac{\pi}{2}\) from the left, \(\tan x\) approaches infinity. Also, \(-\ln(\cos x)\) grows without bound. As a result, the area under the curve becomes infinite.
Should I write −ln|cos x| or ln|sec x| on an exam?
Both expressions are completely correct and mathematically identical. You can write either form unless your instructor or textbook specifically requests one.
What is the integral of tan x times sec x?
The integral of \(\sec x \tan x\) is \(\sec x + C\). This comes directly from the standard derivative rule \(\frac{d}{dx}(\sec x) = \sec x \tan x\). It does not require any logarithms.
How do I integrate tan(ax + b)?
Substitute \(u = ax + b\), which gives \(dx = \frac{du}{a}\). The general solution is \(-\frac{1}{a}\ln|\cos(ax + b)| + C\). For example, the integral of \(\tan(4x + 1)\) is \(-\frac{1}{4}\ln|\cos(4x + 1)| + C\).
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
Integration Formulas and Integral Rules: Complete List
Every basic integration formula and integral rule: power, exponential, log, trig and inverse trig integrals, substitution, by parts and definite integrals.
Integrals Class 12: All Formulas, Methods and Solved Examples
Class 12 Integrals: standard formulas, substitution, partial fractions, integration by parts, special integrals and definite integral properties, solved.

