I answered elsewhere according to the gemara but you asked for a response according to Tanya as well. The definition of a Tzadik in Tanya is different from the way we use the word today (e.g., "this rav is a real tzadik").
R Nadav Cohen explains this in chapter 1 of this book on Tanya (see also here), which I summarize
According to the Torah basic level of interpretation, a person is
judged only according to his deeds, i.e., that which we can see on the
surface. As such, all he has to do is perform more mitzvot than sins
to be considered a Tzadik.
But the accurate usage of the title Tzadik means that the person is
a Tzadik in his entire essence and being. According to the Torah's
deeper and more inner level of interpretation, a person is measured by
his true nature.
Tanya explains a person has two souls, an "animal soul" (focused on
physical needs and desires) and a "Godly soul" (who desires only to
connect to his source: God). These two souls wage an intense, constant
battle deep within us, yet they strike a different balance in each
This provides us with new parameters for understanding who is a
Tzadik. In a Tzadik, the battle between the two souls is over and the winner is the Godly soul. It subdued the animal soul, took it
prisoner and gave it a completely new identity. Not only has it
stopped opposing the Godly soul, it actually changed sides. As a
result the Tzadik never sins, does only good deeds and mitzvot and
has absolutely no desire or interest in anything outside the realm of
holiness, mitzvot, and goodness - not in his actions, words, or even
a single thought.