Click here to Skip to main content
14,239,876 members

Using LaTeX in articles on CodeProject

Rate this:
4.98 (26 votes)
Please Sign up or sign in to vote.
4.98 (26 votes)
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Chris Maunder
Founder CodeProject
Canada Canada
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.

Comments and Discussions

 
Generalgreate article, greate LaTex Pin
szcathay29-Jun-15 17:09
memberszcathay29-Jun-15 17:09 
Questiongreat news Pin
BillW3311-Jun-14 6:25
professionalBillW3311-Jun-14 6:25 
QuestionNeeds enabled on the rest of the site Pin
Dan Neely11-Jun-14 3:08
memberDan Neely11-Jun-14 3:08 
AnswerRe: Needs enabled on the rest of the site Pin
BillW3311-Jun-14 6:22
professionalBillW3311-Jun-14 6:22 
GeneralMy vote of 5 Pin
CPallini11-Jun-14 2:34
mveCPallini11-Jun-14 2:34 
SuggestionLatex Cheat Sheet Pin
DaveAuld11-Jun-14 1:41
protectorDaveAuld11-Jun-14 1:41 
GeneralCool stuff! Pin
Shao Voon Wong10-Jun-14 17:20
mvaShao Voon Wong10-Jun-14 17:20 
BugTypo Pin
Matt T Heffron10-Jun-14 11:00
professionalMatt T Heffron10-Jun-14 11:00 
GeneralRe: Typo Pin
Chris Maunder10-Jun-14 11:04
adminChris Maunder10-Jun-14 11:04 
QuestionYEEEEEES! Pin
Kenneth Haugland10-Jun-14 9:20
professionalKenneth Haugland10-Jun-14 9:20 
Generalmy vote of 4 Pin
LuxGiammi10-Jun-14 8:32
professionalLuxGiammi10-Jun-14 8:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin 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

Tagged as

Stats

17.5K views
24 bookmarked