[Tex/LaTex] How to LaTeX insert math formula in text paragraph

math-mode

If I want to insert a math formula, usually just a math symbol, in text paragraph, LaTeX put the formula to the next line and put it in the middle of the line?

How can I just insert between text?

Best Answer

I think what Harish was intending to suggest in his comment is that you can use single dollar-sign delimited strings to shift into math mode and insert a formula.

So if you wanted to typeset "The expected rate of events is λ." you would write

The expected rate of events is $\lambda$. 

Were you to instead use \[ … \] or $$ … $$ to delimit your text, it would put the formula on the next line because this defines a displayed equation, which is placed on its own line, and (usually) centered.

See below (On \[ … \] vs. $$ … $$) for a further explanation of why you would choose between \[ … \] and $$ … $$.

The quick heuristic would be:

  • if you're writing LaTeX for CLI and local compilation, \[ … \]
  • if you are writing to display maths in a browser, $$ … $$.

So were you to typeset the previous example as:

The expected rate of events is \[\lambda\]. 

that would be rendered as:

The expected rate of events is

λ

.

Note that the period is also placed after a new line because of the way that equation environments work.

--Edit--

As pointed out by Ethan in the comments, often if you are going to use this notation, it is more common to type:

The expected rate of events is \[
\lambda
\].

This makes explicit in the plaintext representation of your maths that you intend it to be on its own line.

See the attached image for a rendered example of what I mean.

enter image description here


On \[ … \] vs. $$ … $$

Though using double dollar signs ($$) to delimit displayed math mode in LaTeX is discouraged — see Why is \[ … \] preferable to $$? — this is not always feasible if you want your LaTeX to be rendered appropriately by MathJax based LaTeX interpreters that are common on web based text editors that provide maths typesetting support.

For example, the Jupyter notebook is designed to display both code and markup stored in JSON format, where mathematical content is LaTeX formatted plaintext that is interpreted via MathJax. However, if you use \[ … \] instead of $$ … $$ your LaTeX will refuse to render correctly because of the way that JSON requires escaping slashes (\). You can instead use \\[ … \\] but then this can cause other issues with interpreting LaTeX inside the delimiters forcing you to use nonvalid LaTeX to be able to write (for example) subscripts by escaping underscores. In this case and with other JSON based storage solutions for encoding plaintext, $$ … $$ is preferable in order to avoid these issues around \ and escaping.

Note: the Cross Validated StackExchange website explicitly encourages the use of $$ … $$. Or – more accurately – it encourages using:

$$
…
$$
Related Question