Almost Sure

LaTeX in Comments

WordPress.com supports the use of standard LaTeX in posts and comments, which is displayed using embedded PNG images with the LaTeX source code appearing in the alt text. So, including inline maths in your comments is easy! All you have to do is to surround your \LaTeX code by the tags $latex … $. For example, typing

$latex \int_{-\infty}^\infty e^{-x^2}\,dx=\sqrt\pi$

gives \int_{-\infty}^\infty e^{-x^2}\,dx=\sqrt\pi. Note that the initial space after the opening $latex is necessary. If this is omitted then the code will not be processed as LaTeX and will just appear as typed. There are some problems that can occur, but are easily avoided. As WordPress.com has some support for html in comments, any < or > signs could be misinterpreted as html tags, messing up the comment. This is avoided by either ensuring that spaces are left around these symbols or by using &lt; and &gt; in place of < and > respectively. Also, you can have newlines in your LaTeX code, which is treated as whitespace as with regular LaTeX. However, do not put a newline directly after the opening $latex, as this prevents the LaTeX code from being processed. Any LaTeX containing errors is displayed as \badlatex.

Unfortunately, WordPress.com does not support previewing or editing comments. So, if you want to test your LaTeX code before posting as a comment, feel free to first post it as a comment here. Also, I will correct any incorrectly entered LaTeX in comments as I come across them. Happy commenting!

The displaymath environment

In standard LaTeX it is possible to use the displaymath environment, which causes the formula to appear centered on its own line, with whitespace above and below, and with slightly different formatting making use of the additional space. WordPress.com does not directly support displaymath but, fortunately, it is possible to emulate it with inline LaTeX. This requires doing two things. First, use the html p (paragraph) tag to insert whitespace above and below the expression with the align attribute set to “center”. Then, the expression can be made to appear with the correct displaymath formatting by starting it with the \displaystyle LaTeX keyword. For example, typing

<p align="center">$latex \displaystyle\int_{-\infty}^\infty e^{-x^2}\,dx=\sqrt\pi$</p>

gives

\displaystyle\int_{-\infty}^\infty e^{-x^2}\,dx=\sqrt\pi

Typing this afresh every time you want to display a formula is likely to lead to mistakes, so you can simply copy the expression above directly from this page and paste it into your comment, replacing my LaTeX code with yours while retaining the \displaystyle command.

Multiline expressions and alignment

It is often desirable to break long formulas and expressions down into multiple lines. In standard LaTeX this can be done with the align environment, which uses \\ to end a line and aligns the expressions on the & character. However, the align environment is not supported in WordPress. Instead, the array environment can be used, as this works with standard LaTeX inside inline maths formulas. There are some things that should be done to get this to display properly though. To reduce the rather excessive amount of space that appears in place of the & alignment characters, the command \setlength\arraycolsep{2pt} should be used immediately before entering the array environment. Secondly, to make sure that enough space appears between lines, put \smallskip immediately before the \\ command for a newline. Also, to get correct displaymath formatting, the \displaystyle command should be reentered after every & alignment character and at the start of each line. You can copy and paste the following text into your comment, replacing [my-latex] with your LaTeX code.

<p align="center">$latex \setlength\arraycolsep{2pt}\begin{array}{rl}
\displaystyle[my-latex]&\displaystyle[my-latex]\smallskip\\
\displaystyle[my-latex]&\displaystyle[my-latex]
\end{array}$</p>

For example, the following multiline expression

\setlength\arraycolsep{2pt}\begin{array}{rl}  \displaystyle\int_{-\infty}^\infty e^{-x^2}\,dx&\displaystyle=\sqrt\pi,\smallskip\\  \displaystyle\int_{-\infty}^\infty e^{-ax^2}\,dx&\displaystyle=\int_{-\infty}^\infty e^{-y^2}\frac{dy}{\sqrt a}\smallskip\\  &\displaystyle=\sqrt{\frac\pi a}  \end{array}

was produced by typing the following text.

<p align="center">$latex \setlength\arraycolsep{2pt}\begin{array}{rl}
\displaystyle\int_{-\infty}^\infty e^{-x^2}\,dx&\displaystyle=\sqrt\pi,\smallskip\\
\displaystyle\int_{-\infty}^\infty e^{-ax^2}\,dx&\displaystyle=\int_{-\infty}^\infty e^{-y^2}\frac{dy}{\sqrt a}\smallskip\\
&\displaystyle=\sqrt{\frac\pi a}
\end{array}$</p>

Equation numbering

LaTeX documents support the use of equation numbering. This displays a right-aligned number inside parentheses after displayed equations, as follows.

\displaystyle\int_{-\infty}^\infty e^{-x^2}\,dx=\sqrt\pi.

(1)

In LaTeX this is done automatically whenever the equation environment is used to display formulas. Unfortunately, as WordPress does not support the equation environment, it does not directly support equation numbering either. One simple way to force an equation to be numbered is to insert the number directly into the LaTeX expression by putting something like \qquad\qquad{\rm(2)} before the terminating $, giving

\displaystyle\int_{-\infty}^\infty e^{-x^2}\,dx=\sqrt\pi.\qquad\qquad{\rm(2)}

This works, but it is not an ideal solution. As the equation number has been inserted into the LaTeX expression with a space separating it from the main formula, it means that it is displayed as part of the included PNG image. The gap between the formula and the number is fixed, so that the number either floats somewhere between the right of the expression and the right of the screen or, if too much space is used, it will protrude beyond the right-hand margin and the equation will not be properly centered. A more satisfactory result can be achieved by using an html table to display and right-align the equation number, and separately display and center the LaTeX formula. This approach also works for giving a single number to a multiline formula such as

\setlength\arraycolsep{2pt}\begin{array}{rl}  \displaystyle\int_{-\infty}^\infty e^{-ax^2}\,dx&\displaystyle=\int_{-\infty}^\infty e^{-y^2}\frac{dy}{\sqrt a}\smallskip\\  &\displaystyle=\sqrt{\frac\pi a}.  \end{array}

(3)

Typing in the code to do this from scratch would almost certainly lead to errors, so you can copy and paste the following text into your comment. Just replace [my-latex] by your LaTeX expression and *eqno* by the equation number. For a numbered single line formula, use

<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody><tr><td align="center" width="93%"><p>
$latex \displaystyle[my-latex]$
</p></td><td align="left" width="7%"><p>(*eqno*)</p>
</td></tr></tbody></table>

For a numbered and aligned multiline expression use

<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody><tr><td align="center" width="93%"><p>
$latex \setlength\arraycolsep{2pt}\begin{array}{rl}
\displaystyle[my-latex]&\displaystyle[my-latex]\smallskip\\
\displaystyle[my-latex]&\displaystyle[my-latex]
\end{array}$
</p></td><td align="left" width="7%"><p>(*eqno*)</p>
</td></tr></tbody></table>

5 Comments »

  1. I wonder if right-aligned equation numbering works in comments?

    \setlength\arraycolsep{2pt}\begin{array}{rl}\displaystyle\sum_{n=1}^Nn^s&\displaystyle=\frac{1}{s+1}\sum_{j=0}^s\binom{s+1}{j}B_jN^{s+1-j}\smallskip\\ &\displaystyle=\frac{1}{s+1}N^{s+1}+\frac12N^s+\cdots+B_sN.\end{array}

    (1)

    It does! I’ll update this page shortly to describe how this can be done.

    Comment by George Lowther — 16 April 11 @ 2:42 AM | Reply

  2. very handy, thanks!

    Comment by Jefferson Huang — 27 December 11 @ 7:01 AM | Reply

  3. The ‘&’ should work without the use of \displaystyle

    Comment by hddadmin — 17 January 14 @ 11:25 PM | Reply

  4. test \int_{-\infty}^\infty e^{-x^2}\,dx=\sqrt\pi .

    Comment by Anonymous — 30 March 15 @ 3:22 PM | Reply

  5. Test: \sum_{n=1}^\infty \frac{1}{n^2}=\frac{\pi^2}{6}.

    Comment by heartofwaterblog — 6 October 16 @ 4:04 AM | Reply


RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.