Anatase TiO2 doping with boron, carbon, nitrogen, and fluorine atoms has been considered in a systematic study by performing periodic DFT calculations with the hybrid B3LYP functional and large supercells. The effect on the electronic structure of replacing lattice O atoms with B, C, N, or F dopants, or to include the same atoms in interstitial positions has been considered. Clear trends emerge as a function of the atomic number of the doping element. B, C, and N atoms in substitutional positions result in magnetic impurities whose energy levels fall in the energy gap of the material. The position of these gap states closely follows the effective nuclear charge of the dopant, with B that gives states high in the gap and N which introduces states just above the top of the valence band. Fluorine, being very electronegative, has filled states below the O 2p valence band and leads to the formation of Ti3+ ions due to charge compensation. Interstitial impurities have a quite different electronic structure, which again depends on the nuclear effective charge. B acts as a net electron donor with formation of B3+ and three Ti3+ ions; C donates only two electrons to the lattice with formation of a C2+ ion; N forms a direct bond with a lattice O, and does not donate electrons to the host lattice. We also discuss possible internal charge transfers between high-lying electronic states in the gap (Ti3+ 3d1 states) and low-lying acceptor states of the dopant. The interplay between substitutional and interstitial dopants, between dopants and oxygen vacancies or titanium interstitials, and between co-dopants (B,N; N,F) are discussed in view of their beneficial effects for photocatalytic processes.