14,239,876 members

Using LaTeX in articles on CodeProject

Rate this:
10 Jun 2014CPOL
A brief intro to using LaTeX in your articles

Introduction

The development of the MathJax javascript library has dragged us kicking and screaming out of the dark days of ASCII math1.  Gone are the days when n = n^2 is acceptable because it's just as easy to write $n = n^2$.

Using MathJax in your articles

Enclose your mathematics within a tag of class "math" and use $...$ to wrap equation blocks and  $...$ to wrap inline equations.  eg <div class="math">$...$</div> to wrap a block of equations, or <span class="math">$...$</span> for an inline equation.

View the MathJax Tex/LaTeX pages for information on the commands supported.

You also may find it handy to use this online LatTeX editor: http://www.codecogs.com/latex/eqneditor.php. There are a few useful items here:

1. It is an easy way to find the particular syntax you need
2. If you notice your formula is not rendering properly in the preview you can paste it into here for review. It will tell you, for example, "you have too many unclosed {"

Examples

Some quick examples taken directly from the MathJax pages (but adapted to our implementation) to get you started.

The Lorenz Equations

<div class="math">\begin{aligned} \dot{x} & = \sigma(y-x) \\ \dot{y} & = \rho x - y - xz \\ \dot{z} & = -\beta z + xy \end{aligned}</div>

becomes

\begin{aligned} \dot{x} & = \sigma(y-x) \\ \dot{y} & = \rho x - y - xz \\ \dot{z} & = -\beta z + xy \end{aligned}

The Cauchy-Schwarz Inequality

<div class="math">$\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)$</div>

becomes

$\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)$

A Cross Product Formula

<div class="math">$\mathbf{V}_1 \times \mathbf{V}_2 = \begin{vmatrix} \mathbf{i} & \mathbf{j} & \mathbf{k} \\ \frac{\partial X}{\partial u} & \frac{\partial Y}{\partial u} & 0 \\ \frac{\partial X}{\partial v} & \frac{\partial Y}{\partial v} & 0 \end{vmatrix}$</div>

becomes

$\mathbf{V}_1 \times \mathbf{V}_2 = \begin{vmatrix} \mathbf{i} & \mathbf{j} & \mathbf{k} \\ \frac{\partial X}{\partial u} & \frac{\partial Y}{\partial u} & 0 \\ \frac{\partial X}{\partial v} & \frac{\partial Y}{\partial v} & 0 \end{vmatrix}$
<p>The probability of getting <span class="math">(k)</span> heads when flipping <span class="math">(n)</span> coins is</p>

becomes

The probability of getting (k) heads when flipping (n) coins is

<div class="math">$P(E) = {n \choose k} p^k (1-p)^{ n-k}$</div>

becomes

$P(E) = {n \choose k} p^k (1-p)^{ n-k}$

An Identity of Ramanujan

<div class="math">$\frac{1}{\Bigl(\sqrt{\phi \sqrt{5}}-\phi\Bigr) e^{\frac25 \pi}} = 1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}} {1+\frac{e^{-8\pi}} {1+\ldots} } } }$</div>
$\frac{1}{\Bigl(\sqrt{\phi \sqrt{5}}-\phi\Bigr) e^{\frac25 \pi}} = 1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}} {1+\frac{e^{-8\pi}} {1+\ldots} } } }$

A Rogers-Ramanujan Identity

<div class="math">$1 + \frac{q^2}{(1-q)}+\frac{q^6}{(1-q)(1-q^2)}+\cdots = \prod_{j=0}^{\infty}\frac{1}{(1-q^{5j+2})(1-q^{5j+3})}, \quad\quad \text{for$|q|<1$}.$</div>

becomes

$1 + \frac{q^2}{(1-q)}+\frac{q^6}{(1-q)(1-q^2)}+\cdots = \prod_{j=0}^{\infty}\frac{1}{(1-q^{5j+2})(1-q^{5j+3})}, \quad\quad \text{for$|q|<1$}.$

Maxwell’s Equations

<div class="math">\begin{aligned} \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\ \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\ \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ \nabla \cdot \vec{\mathbf{B}} & = 0 \end{aligned}</div>
\begin{aligned} \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\ \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\ \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ \nabla \cdot \vec{\mathbf{B}} & = 0 \end{aligned}

1 Actually, MathJax supports ASCIIMath too.

Share

Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

 First Prev Next
 greate article, greate LaTex szcathay29-Jun-15 17:09 szcathay 29-Jun-15 17:09
 great news BillW3311-Jun-14 6:25 BillW33 11-Jun-14 6:25
 Needs enabled on the rest of the site Dan Neely11-Jun-14 3:08 Dan Neely 11-Jun-14 3:08
 Re: Needs enabled on the rest of the site BillW3311-Jun-14 6:22 BillW33 11-Jun-14 6:22
 My vote of 5 CPallini11-Jun-14 2:34 CPallini 11-Jun-14 2:34
 Latex Cheat Sheet DaveAuld11-Jun-14 1:41 DaveAuld 11-Jun-14 1:41
 Cool stuff! Shao Voon Wong10-Jun-14 17:20 Shao Voon Wong 10-Jun-14 17:20
 Typo Matt T Heffron10-Jun-14 11:00 Matt T Heffron 10-Jun-14 11:00
 Re: Typo Chris Maunder10-Jun-14 11:04 Chris Maunder 10-Jun-14 11:04
 YEEEEEES! Kenneth Haugland10-Jun-14 9:20 Kenneth Haugland 10-Jun-14 9:20
 my vote of 4 LuxGiammi10-Jun-14 8:32 LuxGiammi 10-Jun-14 8:32
 Last Visit: 18-Jul-19 22:17     Last Update: 18-Jul-19 22:17 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Tip/Trick
Posted 10 Jun 2014

17.5K views
24 bookmarked