UpMaths
中文
Math & Markdown12 min read·Updated 2026-10-27

How to Write LaTeX Math: From Inline Symbols to Aligned Equations

Six separate problems hide inside “my formula won’t render”: where the notation goes, how scripts bind, how brackets grow, how matrices are written, how lines align, and how to read an error. Each one has a fixed rule.

1. Inline math versus display math

The syntax differs by a single dollar sign, but the typesetting is completely different. Inline math flows with the sentence; display math takes its own line, centres itself, and adds vertical space. Most “the formula looks wrong” problems start right here.

SyntaxNameWhen to use
$x^2$InlineThe formula sits inside a sentence: “for $x>0$ the function increases”
$$…$$DisplayThe formula owns the line and is centred, best when structure matters
\(…\)Inline (TeX native)Rarely useful on the web, some Markdown parsers ignore it
\[…\]Display (TeX native)Same caveat — KaTeX supports it, many editors do not

One detail that bites: put display delimiters on their own lines. Block-level parsing expects a line break around $$, so writing “text $$ formula $$ text” can silently degrade to inline mode, and your limits jump to the right side.

The other frequent accident: formulas inside CJK punctuation

“当$x>0$时” renders fine in a browser, but paste it into email, WeChat, or some CMS and the dollar signs get treated as currency markers, exposing the raw source. Leave one space on each side of the formula: 当 $x>0$ 时. Renderers ignore the extra space; parsers need the boundary.

2. Twelve constructions you actually use

You will not need more than twenty commands day to day. Look them up by intent rather than by alphabet — that is how you write faster.

What you wantSyntaxWhere people slip
Fraction a/b\frac{a}{b}Each argument needs its own braces; otherwise only the first character is captured
Square root, nth root\sqrt{x}, \sqrt[n]{x}The index goes in brackets, not braces
Subscript x₁₀x_{10}x_10 subscripts only the 1 and leaves the 0 at full size
Superscript xⁿ⁺¹x^{n+1}Multi-character superscripts need braces
Greek α β θ\alpha \beta \thetaIt is \theta, not \teta; \phi and \varphi are different glyphs
Multiplication\times, \cdotNever use the letter x — spacing and italics are both wrong
≤ ≥ ≠\le \ge \neWriting <= produces two separate symbols
Element of\in, \notin\notin is a single command
Infinity\infty\infinity is undefined and will error
Summation\sum_{i=1}^{n}Limit placement depends on inline versus display mode
Integral\int_{0}^{1}Bounds use _ and ^, not \limits
Limit\lim_{x \to 0}\to is the arrow; \rightarrow is the long form

3. Scripts: braces are the only way to bind

The rule is brutally simple: _ and ^ apply to the next single token. A letter is one token; a braced group is one token. That is why multi-character scripts are not a styling preference but a syntax requirement.

latex
x_10        → x₁0      wrong: only the 1 becomes a subscript
x_{10}      → x₁₀      correct

x_i^2       → xᵢ²      correct: one base, script above and below
{x_i}^2     → (xᵢ)²    correct: take x_i first, then square the whole thing

x_{i+1}^{2} → xᵢ₊₁²    correct: order of _ and ^ does not matter

The second pair deserves a note. x_i^2 and {x_i}^2 look identical in simple cases but mean different things: the first is “the i-th component, squared”; the second is “square the object x_i”. When a subscript is followed by an operator — a derivative, an absolute value, another exponent — the braced form is the safe choice.

4. Fractions, roots, and brackets that grow

Plain parentheses have a fixed height, so a fraction inside them ends up looking like it escaped. \left and \right size the delimiters to the content, and they must come in pairs.

Stretchy brackets around a fraction; the exponent sits outside the closing delimiter

Two practical rules. First, do not reach for \left\right when nothing needs to stretch — it changes the spacing and simple expressions end up looking loose. Second, for one-sided delimiters (evaluation bars, intervals) use \left. or \right. as a placeholder; the dot draws nothing.

latex
\left. \frac{dy}{dx} \right|_{x=0}
→ a vertical bar on the right only, nothing on the left

5. Sums and integrals: why the limits moved to the right

The most-asked question in the whole subject. Limit placement depends on math style: inline mode uses compact style and pushes limits beside the operator so the line height stays even; display mode uses the full style and stacks them above and below. Nothing is broken — that is the rule.

In display mode, summation and integral bounds stack properly

If you truly need stacked limits inline, use \limits; if you want them beside the operator in display mode, use \nolimits. For teaching material the better move is usually to promote the expression to display math rather than fight the typesetter.

latex
$\sum\limits_{i=1}^{n} a_i$     inline, limits stacked
$$\sum\nolimits_{i=1}^{n} a_i$$   display, limits beside

6. Matrices and piecewise functions

The three matrix environments differ only in their fences: pmatrix for parentheses, bmatrix for square brackets, vmatrix for determinant bars. Columns are separated by &, rows by \\ — that holds for every matrix-like environment.

Same entries: pmatrix gives a parenthesised matrix, vmatrix a determinant

Piecewise definitions use cases. Here the & separates the expression from its condition — not two columns — so write exactly one & per line, and add the comma yourself after the expression.

latex
f(x) = \begin{cases}
  x^2, & x \ge 0 \\
  -x,  & x < 0
\end{cases}

The two classic cases bugs

Miss the & and the conditions jam against the expressions; miss the \\ and every row collapses onto one line. If it compiles but looks wrong, count those two symbols first.

7. Multi-line alignment: aligned versus align

To align a derivation on the equals sign, use aligned or align. The difference: aligned is an environment block you can drop inside $…$ as a unit, while align is a display environment with numbering. The alignment point is &, line breaks are \\.

One & per line, placed before the equals sign

One & per line is the rule most people break. Extra ampersands create extra alignment columns, and everything after the second one gets pushed to the right, which reads as a layout collapse.

Compatibility advice: default to aligned. Some online editors and lightweight parsers never enabled align and will simply error out. aligned is supported almost everywhere; the only thing you give up is automatic equation numbering.

8. CJK text, units, and upright roman

Math mode treats letters as variables — italic, with math spacing — so dropping Chinese characters straight in produces wrong kerning and the wrong glyph source. Wrap prose in \text{}, and keep units and function names upright with \mathrm{} or \text{}.

latex
\text{for } x > 0, \quad f(x) = \log_2 x
5\,\mathrm{cm}   ✓ thin space between number and unit
5cm              ✗ c and m become variables: italic, wrong spacing

Function names are already commands: \sin, \cos, \log, \ln, \max set themselves upright with correct spacing. Never type “sin” by hand. Inverse trig uses \arcsin.

What actually goes wrong with CJK inside formulas on the web

KaTeX fonts contain no CJK glyphs, so Chinese inside \text{} falls back to the page font and rarely matches the math baseline or size. In body copy, keep the prose outside the math: write “for $x>0$” rather than one \text{} blob. It avoids font fallback and keeps the formula copyable on its own.

9. Six error messages, decoded

The red text looks intimidating but collapses into six categories. Scan the table before reading the English word by word.

Message or symptomWhat it meansFix
Undefined control sequenceA command is misspelled, or the backslash was eatenCheck \theta and \frac; in Markdown a single backslash may be escaped away
Missing $ insertedText command inside math mode, or unbalanced $Count the $ pairs; wrap prose in \text{}
Extra } / Missing } insertedBraces do not balanceWork from the innermost group outward and balance each pair
Whole formula shown as source, or turns redThe parser does not know this environmentReplace align with aligned; drop the star from align*
Scripts have no effectMissing braces, or full-width charactersSwitch to an ASCII keyboard layout and retype _ ^ { }
Matrix will not break lines\\ became a single \, or Markdown consumed itWrite it inside a code block, or use four backslashes

One habit saves real time: never write a deeply nested formula in one pass. Get the outer shell compiling, then fill inward layer by layer. Error localisation becomes an order of magnitude cheaper.

10. Moving formulas between the web and Word

What you see on a web page is rendered output. Select and copy it and you get typeset characters with the structure gone: x² becomes an x plus a superscript glyph, a fraction becomes a slash, a matrix dissolves into loose lines of text. To keep the structure, copy the source, not the rendering.

  1. Write the formula, then copy the LaTeX source between the $ delimiters — not the rendered result.
  2. Paste into Word: recent versions offer an Equation → LaTeX input mode and convert it into an editable Word equation object.
  3. For batch delivery, write everything in Markdown first and convert the whole document at once, instead of pasting equation by equation.
  4. Spot-check three things before delivering: matrices still form a block, the piecewise brace survived, inline math sits on the same baseline as the text.

If you only need live preview while writing, use the in-site editor; the document-delivery details are covered in the Markdown-to-Word guide.

Frequently asked

What is the actual difference between $ and $$?

One dollar sign is inline math: it flows with the sentence and places operator limits beside the symbol. Two dollar signs are display math: its own centred line, limits stacked above and below. Getting this wrong changes both position and limit style.

Why does my formula show as raw source or turn red?

Parsing failed. Three usual causes: a misspelled command (\teta instead of \theta), unbalanced braces or dollar signs, or an environment the parser did not enable — align is the common one. Try aligned first.

Can I copy a formula from a web page into Word?

Not by copying the rendering — the structure is lost. Copy the LaTeX source and paste it into Word’s equation field with LaTeX input mode; recent versions convert it into an editable equation object.

Do \left and \right have to be paired?

Yes. For a one-sided delimiter, use \left. or \right. as a placeholder; the dot renders nothing.

Is KaTeX syntax the same as MathJax?

For everyday constructs, identical. They diverge on environment coverage and extension packages. Conservative choices — aligned, cases, pmatrix — work in both; align, custom macros, and parts of mhchem may not work in KaTeX.

Why do Chinese characters inside formulas misalign?

Math fonts carry no CJK glyphs, so text inside \text{} is rendered by a fallback page font with different baselines and metrics. Keeping prose outside the math is the more robust fix.

Tools mentioned above

  • Markdown & LaTeX editorWrite on the left, preview live on the right. Inline $ and block $$ math, rendered with KaTeX
  • Markdown to Word and PDFHow to move formulas, tables, and CJK layout without breaking them
  • Word to PDFFreeze a finished document into a fixed layout that opens the same everywhere

Read next

LaTeX Math Cheat Sheet — Subscripts, Fractions, Matrices, Alignment | UpMaths