You do the following:
 Borrow $A at an interest rate of I (I = .078 means 7.8%).
 Invest it at a Return of R (R = .123 means 12.3%).
 After N years you cash in your investment, pay the taxes and the
interest on the loan and pocket the balance
... if there is any balance!
 Your marginal tax rate is T (T = .45 means 45%).
 You pay only a fraction f of the taxes (f = .75 means 75%, for Capital Gains).
After N years your $A investment has grown to A(1+R)^{N}
so your gross gain is A(1+R)^{N}  A.
You pay taxes on this, namely fT[A(1+R)^{N}  A]
leaving you with [A(1+R)^{N}  A](1fT).
You also have the interest to pay, namely IA per year for N years, amounting
to IAN.
You're now left with [A(1+R)^{N}  A](1fT)  IAN to spend.
Dividing by the original investment of A we get the gain per dollar
of investment, namely:
[(1+R)^{N}  1](1fT)  IN

Expressed as a percentage, that means a gain of
{1 + [(1+R)^{N}  1](1fT)  IN}^{(1/N)}  1 per year.

Example:
A = $50,000 borrowed for N = 5 years at I = .07 (7%) and invested at
R = .1 (10%) after which you cash in, pay taxes at your marginal tax rate
of 45% (T = .45) ... but it's capital gains so you only pay 75% of the
taxes (f = .75).
You get (after taxes and paying the interest):
[(1+.1)^{5}  1]{1(.75)(.45)}  .07(5) = $0.2120
for each dollar invested.
That's an annual (shall we call it a) "Return"
of
(1+.2120)^{(1/5)}  1 = .039 (or 3.9%) ... after taxes.
Here's a spreadsheet y'all
kin try.
