If it's only if you invest,

1. You have $5000 worth of equities in your brokerage account. You withdraw $1000 from the account to buy some really expensive candy, putting you -1000 into your margin. The interest on that $1000 is not tax deductible.

2. You have $5000 worth of equities in your brokerage account. You sell $1000 worth of the equities. You withdraw the proceeds to buy the aforementioned candy. Then you rebuy the $1000 worth of equities, putting you into -1000 of your margin. The interest on that $1000 is tax deductible.

Is that right?