Question inside $\int \tan^2 x dx$ $$\int \tan^2 x dx$$ the question isn't behind on how to do it but rather on its solution, let me go through my solution quickly:
If we do a substitution $u=\tan(x)$ then $du=\sec^2 x dx=(u^2+1)dx$ hence we get $$\int \frac{u^2}{1+u^2}du=\int\left(1-\frac{1}{1+u^2}\right)du=\tan x-\arctan(\tan x)+C$$ so no problem until here. The solution from the book or WolframAlpha is: $\tan x-x+C$ so it raised a question, since $\tan $ isn't injective and only has an inverse for $x\in(-\frac{\pi}{2},\frac{\pi}{2})$, for which then $\arctan(\tan(x))=x$ is only true for $x\in(-\frac{\pi}{2},\frac{\pi}{2})$. But then why if we do $\arctan(\tan x)=x$ in my solution we then get $$\tan x-x+C$$ which is the same as the correct solution the book or WolframAlpha shows, while we have only just considered the $x\in(-\frac{\pi}{2},\frac{\pi}{2})$ in my method? Isn't the indefinite integral considering $\forall x\in \mathbb{R}$? Why is this happening?
Note: Not looking at all for an alternative solution or how to get there, rather why I get the correct solution by considering $x\in(-\frac{\pi}{2},\frac{\pi}{2})$ while an integral should be $\forall x\in \mathbb{R}$
 A: This is a phenomenon that happens with all indefinite integrals. The original integral
$$\int \tan^2 x\:dx$$
has singularities at $\frac{\pi}{2} + \pi k$ for $k\in\Bbb{Z}$, therefore, no, the original integral was not for all $x\in\Bbb{R}$. Whenever you have discontinuities like that, they are tucked away in the $+C$. For example, take the textbook case of
$$\int \frac{dx}{x} = \log|x| + C$$
but this answer is a little misleading misleading. Consider the function
$$f(x)=\begin{cases}\log( -x) +7 & x<0 \\ \log (x )-2 & x>0\end{cases}$$
Taking the derivative, we see that it is still $\frac{1}{x}$. That is because $f$ can still be written as
$$f(x) = \log|x| + C$$
but in this case, the constant $C$ changes values across the discontinuity
$$C = \begin{cases}7 & x<0 \\ -2 & x>0\end{cases}$$
and that is allowed for $+C$. In your answer, we have that
$$x = \arctan(\tan x) + C$$
where the $+C$ hides the arbitrary constant (s)
$$C = \pi\left\lfloor\frac{x+\frac{\pi}{2}}{\pi}\right\rfloor$$
which makes the two answers equivalent, since they are only off by an arbitrary constant.
A: Elementary Explanation
Most elementary textbook authors are fairly careful to define things in such a way that the situation you are describing is not an issue.  Because I have been teaching out of it recently, here is what Thomas' Calculus (instructor's 13th ed) has to say:

Definition: (p. 232) a function $F$ is an antiderivative of $f$ on an interval $I$ if $F'(x) = f(x)$ for all $x$ in $I$.

and

Definition: (p. 237) The collection of all antiderivatives of $f$ is called the indefinite integral of $f$ with respect to $x$, and is denoted by
$$ \int f(x)\,\mathrm{d}x. $$

Notice that the indefinite integral of $f$ is defined in terms of antiderivatives of $f$, and the antiderivative of $f$ is only defined if $f$ is defined on an interval.
This implies that before you even start to ask what the indefinite integral of a function is, you first have to be clear about the definition of that function.  What does the collection of symbols $\tan(x)^2$ represent?[1]
Barring any other information, I would assume that $\tan$ represents the tangent function, which is defined on the domain $\mathbb{R}\setminus \{\pi/2 + k\pi : k\in\mathbb{Z}\}$ (that is, the tangent function is defined everywhere that the cosine is not zero).  BUT there is a problem here, since the question is about the antiderivative of the square of the tangent function, and the tangent function is naturally defined on a set which is not an interval.
This implies that
$$\int \tan(x)^2\,\mathrm{d}x$$
does not, in fact, refer to the square of the tangent function on its natural domain, but only on some connected component of that domain, say $(-\pi/2,\pi/2)$, or $(\pi/2, 3\pi/2)$.  Before you can evaluate the indefinite integral, you must first specify the domain of the function which you are integrating.
But...
In their answer, Ninad Munshi argues that one can discuss antiderivatives of functions which are defined on disconnected domains, and that the "disconnection" is swallowed by the constant of integration.  I will not argue that they are wrong, but I do think that this is a wrongheaded approach, which could, potentially, lead to pain later on—being able to "split out" the behaviour of a function across singularities is often helpful.
Looking at the example of $x \mapsto 1/x$, we have two facts:

*

*if we assume that $x > 0$, then
$$ \int \frac{1}{x}\, \mathrm{d}x = \log(x) + C, $$
where $C$ is an arbitrary constant of integration, and

*if we assume that $x < 0$, then
$$ \int \frac{1}{x}\, \mathrm{d}x = \log(-x) + C, $$
where $C$ is an arbitrary constant of integration.

If we want to talk about an "antiderivative"[2] or "indefinite integral"[3] of $x \mapsto 1/x$ on the set $\mathbb{R}\setminus \{0\}$, then I think that it is wise to keep track of the various domains of definition.  Hence I would argue that the "correct" antiderivative is something like
$$ \int \frac{1}{x} \,\mathrm{d}x = \begin{cases} \log(x) + C & \text{if $x > 0$, and} \\ \log(-x) + D & \text{if $x < 0$,} \end{cases}
$$
where both $C$ and $D$ are arbitrary constants of integration.
In the case of $\tan(x)^2$, it would not be unreasonable to write
$$\int \tan(x)^2 \,\mathrm{d}x = \begin{cases}
\tan(x) + x + C_k & \text{if $x \in \left(\dfrac{\pi}{2} + (k-1)\pi, \dfrac{\pi}{2} + k\pi\right)$,} \end{cases}
$$
where $C_k$ is an arbitrary constant of integration for each $k\in \mathbb{Z}$.

[1] Minor pet peeve:  I work in an area of mathematics where $f^2$ means $f \circ f$, i.e. $f$ composed with itself.  Because of this, I find the notation $\tan^2(x)$ somewhat ambiguous.  Yes, it is common notation, and no, you aren't going to be misunderstood if you use it.  But my choice is to use $\tan(x)^2$, as I think that it is less ambiguous.  This explains my choice above.  Please make your own choices when writing.
[2] "Antiderivative" is in quotes because, per the definition at the top, an antiderivative is only defined on an interval, and we are about to talk about a function which is not defined on an interval, so the definition we are working with doesn't really apply.
[3] Ditto.
A: When you apply a substitution u=f(x) for an integral, the function f(x) must be bijective so that the inverse $f^{-1}$ exists.  In your case, you should let
$u=\tan x$ for $x\in \left(-\frac{\pi}{2}, \frac{\pi}{2}\right)$ so that $\arctan(\tan x)=x.$
For otherwise, if we fix an integer $n$ and let
$u=\tan x$ for $x \in\left( n \pi-\frac{\pi}{2}, n \pi+\frac{\pi}{2}\right)$,
then $$\arctan(\tan x)=n \pi+x$$
and
$$I=\tan x-\arctan(\tan x)+C= \tan x-(n \pi+x)+C=\tan-x+C’,$$
where $C’=C-n\pi$ absorbs $-n\pi$ despite of the choice of principal values for the function $\arctan x$.
Wish it helps :-)
