Introduction to LaTeX Equations

This is not a general tutorial on how to create equations in \LaTeX. If you want to display equations ever, at all, anywhere, you must know how to create \LaTeX equations (IMHO).

Trac Instllation

There are a number of Latex Plugins for web pages (see Plugin History below). This one simply requires a single file to be copied into your trac plugins sudirectory. You can browse that file or download the current development version and copy into yourplugins directory. Use:

svn co http://www.openvest.com/svn/tools/trunk/LatexEquationMacro

It presumes that you have latex and dvipng installed and accessable from your server software.

Sample Equations

You can view the source of these equations in the alt= tag of an equation. Some browswers give it to you as you hover over the image, others if you view the source code (ctrl-U in Firefox). The easiest way to see them all is to look for the Plain Text link at the bottom of the page.

Equity Risk Calculation

There are two methods of placing LatexEquations? into a wiki page. LatexEquation deployed in the new stye of WikiMacros deployed as a TracPlugin. Given that, both:

[[LatexEquation(r_n=\sum_{k=1}^K{X_{n,k}\cdot b_k+u_nn)]]

and

{{{
#!LatexEquation
r_n=\sum_{k=1}^K{X_{n,k}\cdot b_k+u_nn)
}}}

display as:

r_n=\sum_{k=1}^K{X_{n,k}\cdot b_k+u_nn

Duration Calculations

Macaulay Duration

D=\frac{\displaystyle \sum_{t=1}^{m}{\frac{tC_t}{(1+r)^t}}}
       {\displaystyle \sum_{t=1}^{m}{\frac{C_t}{(1+r)^t}}}

Macaulay Duration at any date

\mathbf{
D=\frac{\displaystyle \sum_{t=1}^{m}{\frac{(t-1+\alpha)C_t}
                            {(1+r)^{t-1+\alpha}}}}
       {\displaystyle \sum_{t=1}^{m}{\frac{C_t}
                            {(1+r)^{t-1+\alpha}}}}
}

Macaulay Duration closed form solution

D=\frac{C(\alpha+\frac{1}{r})[(1+r)^m-1]-Cm+100r(m-1+\alpha)}
       {C[(1+r)^m-1]+100r}

Black-Scholes

Call Option

c=S\,N(d_1)-Xe^{-rT}N(d_2)

Put Option

p=-Xe^{-rT}N(-d_2)-S\,N(-d_1)

where

d_1=\frac{\ln(S/X)+(r+\sigma^2)T}{\sigma\sqrt{T}}\quad,\quad d_2=d_1-\sigma\sqrt{T}

and

S Stock price
X Strike Price of option
r Risk-free rate of return
T Time to Expiration in years
\sigma Volatility
N(x) Cumulative normal distribution function

For my pythonic friends out there here's some python code for Black-Scholes. Of course it has to start with the Cumulative Normal Distribution Function (CND):

# Cumulate Normal Distribution Function
def CND(X):
    (a1,a2,a3,a4,a5) = (0.31938153, -0.356563782, 1.781477937, 
     -1.821255978, 1.330274429)
    L = abs(X)
    K = 1.0 / (1.0 + 0.2316419 * L)
    w = 1.0 - 1.0 / sqrt(2*pi)*exp(-L*L/2.) * (a1*K + a2*K*K + a3*pow(K,3) +
    a4*pow(K,4) + a5*pow(K,5))
    if X<0:
        w = 1.0-w
    return w

# Black Sholes Function
def BlackSholes(CallPutFlag,S,X,T,r,v):
    d1 = (log(S/X)+(r+v*v/2.)*T)/(v*sqrt(T))
    d2 = d1-v*sqrt(T)
    if CallPutFlag=='c':
        return S*CND(d1)-X*exp(-r*T)*CND(d2)
    else:
        return X*exp(-r*T)*CND(-d2)-S*CND(-d1)

Not a python fan? Look for Balck-Scholes in your language of choice over  here.

Other equations

\[f(n) = \left\{ 
\begin{array}{l l}
  n/2 & \quad \mbox{if $n$ is even}\\
  -(n+1)/2 & \quad \mbox{if $n$ is odd}\\ \end{array} \right. \]

and

E=Mc^2

Notes on MathML

MathML is coming along but is still of niche interest. Compare \Omega=\sum_{i=0}^{\infty}\left(x_i^2+1 \over x_i-1\right)^{p_i} to the MathML version  here in your browser.

Plugin History

This came from the original  LatexFormulaMacro. For a number of reasons I went with the .11dev release of Trac (Genshi, pygments etc). With newer Trac Versions these old style macros are on the way out. I have created a small Macro where you only need to copy 1 file into the site plugins directory and you are off and running.

I aparently was working from an older (and simpler) version of the Latex macro. Good thing too, because the  Version 0.9 rendition starts to look hard. I should maybe bring some of the functionality forward. The code changes and install requirements of the Graphvis plugin scare me.

That brings me to one last note. There is a plugin version already available in  this ticket someone needs to take chagrge of this madness. I'll take a look to see which is most functional for me. YMMV!