How to write a LaTeX matrix: pmatrix and brace usage
Explain clearly how to write matrices in LaTeX, compare the applicable scenarios of pmatrix, bmatrix, vmatrix and cases and other environments, and explain why curly braces should be written as \{ \}, how curly braces automatically adjust their size according to the content, and finally give a checklist after writing.
Key points of this article
- The matrix must be put into the environment provided by the amsmath macro package. Use pmatrix for parentheses, bmatrix for square brackets, vmatrix for determinants, and cases for piecewise functions.
- Curly braces are grouping symbols in LaTeX. Direct input of { and } will result in an error. They should be written as \{ and \}.
- For braces that need to automatically become taller with the content, use \left\{ and \right\} to pair them, and use \right. to complete the side that does not need to be displayed.
- Each \begin must have a \end with the same name, the number of &'s in each line must be consistent, and no extra \\ should be added at the end of the line. This is the focus of troubleshooting matrix errors.
When many people write a matrix in a paper or assignment for the first time, they will look for a matrix command in LaTeX, but the result will be without brackets; or they will want to add a large curly bracket, but various errors will be reported. The problem is usually not a certain symbol, but the structure: the matrix must be wrapped in an environment. The brackets are divided into small brackets, square brackets, and curly brackets. The curly brackets themselves are the grouping symbols of LaTeX and cannot be typed directly.
This article is written for students, researchers, and technical writers who need formatting formulas. After reading, you can judge which environment to choose when writing matrices in LaTeX, know how to type curly braces, and check whether there are any grammatical problems after writing.
The following examples are all codes in math mode and can be directly put into formulas to try. For the steps involving the UniDoc web editor, we only talk about the common methods of formula input and checking.
How to write a matrix in LaTeX? One sentence conclusion
In most cases, it is sufficient to use the pmatrix environment to lay out parenthesized matrices. Other bracket forms, determinants and segmented structures can be replaced by the corresponding environment. The following table lists common situations.
| your situation | Recommended practices | reason |
|---|---|---|
| Ordinary matrix, need parentheses outside | pmatrix environment | The writing method is common and is commonly found in textbooks and papers. |
| Square brackets are required outside | bmatrix environment | The structure is the same as pmatrix, except that the brackets are replaced by square brackets |
| Determinant, with vertical bars on both sides | vmatrix environment | Specifically used for determinants, Vmatrix is a double vertical bar |
| Curly braces are required outside the matrix | Bmatrix environment | Same structure as pmatrix, brackets are curly braces |
| Piecewise function or conditional branch | cases environment | There is automatically a large curly brace on the left, suitable for segmented definitions |
| Just the elements, without any parentheses | matrix environment | Without parentheses, you need to make up the appearance yourself. |
These environments come from the amsmath package. When handwriting a .tex file, you need to add \usepackage{amsmath} in the introductory area; if you paste the formula into another editor, you must also check whether the macro package has been loaded in the introductory area.
Before you start: Confirm these points first
Check the following four things before taking action to avoid most errors.
- Whether the macro package is loaded. Both the matrix and cases environments rely on amsmath. When it is not loaded, the error message will usually prompt an undefined environment name. In this case, just add the macro package.
- Whether in math mode. The matrix must be placed in the formula. Inline formulas are wrapped with
$...$, and independent line formulas are wrapped with\[...\]or the equation environment. If written directly in the text, Missing $ inserted will be reported. - Whether the curly braces should be escaped. In LaTeX, { and } are used to group parameters for commands, and they are not displayed as curly braces when entered directly. To show curly braces, you must write
\{and\}. - The row and column structure of the matrix. Elements in the same line are separated by &, lines are separated by
\\, and\\is not added after the last line. If the positions of these three symbols are wrong, the matrix will be disordered.
Steps to enter a matrix with braces in UniDoc
The following steps are suitable for entering matrix formulas in the UniDoc web editor (app.unidoc.top), and then exporting them for use after writing.
- Open the UniDoc web editor (app.unidoc.top), create a new document, or open an existing document.
- Insert a formula at the required position and write the content in LaTeX code in the formula input area.
- Write the main body of the matrix first, starting with
\begin{pmatrix}and ending with\end{pmatrix}, using & in the middle to separate columns and\\to separate rows. For example,\begin{pmatrix} a & b \\ c & d \end{pmatrix}will arrange a 2×2 matrix. - Write curly braces. When displaying curly braces alone, write
\{ x \}. If you want the curly braces to automatically adjust their height with the content, pair\left\{with\right., such as\left\{ \begin{matrix} x+y=2 \\ x-y=0 \end{matrix} \right.. The\right.on the right here means not to display the right brace. - When piecewise functions are needed, use the cases environment instead. For example,
f(x)=\begin{cases} x, & x\ge 0 \\ -x, & x<0 \end{cases}, the conditions of each paragraph are written after &. - Check the rendering results and confirm that each \begin has a \end with the same name, the brackets are in pairs, and the number of rows and columns is as expected.
- If you need to use it elsewhere, export the document to LaTeX or Word format and open it again in the target environment to confirm that the formula is still correct.
- When using it in a paper or official document, compare the final formula with the original manuscript item by item. Important parts of machine-generated formulas and layouts still require manual verification.
How to type curly braces: three common writing methods
There are three main ways of writing curly braces in LaTeX, you can choose according to the purpose.
- Ordinary curly braces: written as
\{and\}, only the symbol itself is displayed, the size is fixed, and it is suitable for collections, grouping and other occasions, such as\{1,2,3\}. - Auto-sized braces: written as
\left\{and\right\}, the height of the brackets will change with the content in the middle, suitable for wrapping tall matrices or multi-line formulas. If only one side of the curly brace is needed, use\right.or\left.to indicate a blank space on the other side. - Bmatrix and cases: When you don’t want to write curly braces manually, use the Bmatrix environment to sort out the matrix with curly braces, and use the cases environment to sort out the segmented structure. Both automatically handle bracket placement and size.
It should be noted that \left and \right must appear in pairs. If you only write \left\{ but omit \right, an error will be reported directly.
Check each item after completion
Once you have the formula, check it over with the checklist below.
| Check item | What do you think? | What if there's a problem? |
|---|---|---|
| Is the environment name consistent? | The names after\ begin and\ end are exactly the same. | Correct spelling when errors such as "\ begin ended by" occur |
| Whether the parentheses are in pairs | Count whether\ left matches\ right,\ {and\} | Make up for the missing\ right, and use\ right on the side that does not need to be displayed. |
| Is the number of rows and rows correct? | The number of each line is the same, and there is no extra\ at the end of the line. | Redundant & delete, remove\\ from the last line |
| Does the special character display normally? | Are curly braces, percent signs, pound signs, underscores, etc., regarded as commands | Precede special characters with a backslash, such as\%,\ _ |
| Is it consistent with the manuscript? | Compare the manuscript or the original formula item by item | Please check the complex formula manually, don't just look at the neatness of typesetting. |
# frequently asked questions (FAQ)
1. Why do you directly hit {error report, and\ {can display normally?
LaTeX uses {and} as grouping symbols to define the parameter range of the command. \{ is an escaped word that tells the typesetting system to display a curly bracket character instead of starting a grouping.
2. What's the difference between pmatrix and matrix?
The column structure of the two is exactly the same, except that there are parentheses outside the pmatrix and there are no parentheses in matrix. Use pmatrix when you need parentheses, and use matrix when you need to control the appearance.
3. How to write the ellipsis in the matrix?
Horizontal ellipsis uses \cdots, vertical ellipsis uses \vdots, oblique ellipsis uses \ddots. \begin{pmatrix} a_{11} & \cdots & a_{1n} \\ \vdots & & \vdots \\ a_{m1} & \cdots & a_{mn} \end{pmatrix}, for example, can represent an m × n matrix.
4. What if I report an error Missing $inserted?
This error usually indicates that the matrix or formula command appears outside the mathematical model. Check whether the matrix is wrapped by $...$ or \[...\]. If you write the matrix directly in the text, put it in the formula symbol.
5. Can the column alignment of the matrix be changed?
Standard pmatrix, bmatrix and other environments are in the middle list and cannot be changed directly. If you need left or right alignment, you can use the array environment and specify the alignment of each column in the parameters, for example, \begin{array}{lr} represents the left alignment of the first column and the right alignment of the second column.
# Summary
The key to how LaTeX writes a matrix is to choose the right environment, then write curly braces as \{ and \}, and use & and \\ to control the column structure. After writing, check item by item according to the environment name, parenthesis matching and the number of rows and rows, and most of the errors can be found.
Open OmniDoc to edit, convert or translate your LaTeX document


