This may be a naive question, but to me this seems like it should work.
Say I earn $1,000 this year in capital gains from selling some stock that appreciated. I think this is taxed at the capital gains tax rate, i.e. 15%. So my tax hit is $150. It seems that I could then add my after-tax $850 to an IRA and deduct this from my income for this year against my income tax rate, which is 25%, meaning a tax savings of $212.50.
By doing this, it seems like I can contribute the $850 to my IRA as normal, and also keep an extra $62.50 in non-retirement cash from tax savings. I think that IRA contributions must be from my "income", but once capital gains are deposited to my account, it just becomes "money" along with the rest of my income in that account. Is this legit? Am I missing something?