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.