How to right-align part of the LaTeX formula?
If you want some content to be right-aligned when displaying formulas in LaTeX, using \hfill directly is often ineffective. This article explains how to use array, aligned, and \tag methods for formatting, and provides a compiled checklist.
Key points of this article
- The displayed formulas are centered overall by default. Directly using blank commands such as \hfill usually does not take effect.
- If you want the right-hand specification to be right at the end of the formula, use the array environment and write the column specifier as lr.
- If you want the label to be placed on the right side of the center of the plate, use the equation environment with the \tag command.
- After compilation, check whether the right end exceeds the centroid and check the Overfull warning in the log.
If you write a display formula in LaTeX and want the comments, numbers, or a certain item at the end to be aligned to the right, you will most likely try commands such as \hfill and \raggedleft first. The result is that it either has no effect, or an error is reported, and the position jumps randomly. The reason lies in the display formula itself: the content between $$ is centered overall by default, and the single-line formula does not allow you to control the left and right positions.
This article is for readers who are writing papers, handouts, or technical documents and need to right-align formula lines. After reading, you will be able to determine which environment to use, write a method that can be directly compiled, and know how to check the results.
How to right-align part of a formula? One sentence conclusion
Using \hfill directly in $$ usually does not work. The formula must be put into a structure such as array, aligned or equation. The position is determined by the column alignment or labeling command. Use the table below to choose the method according to your situation.
| your situation | Recommended practices | reason |
|---|---|---|
| I just want to add a number to the right end of the formula | The equation environment matches \tag{}; without numbering, use equation; if you need custom text without brackets, use \tag{} | Labels are automatically arranged on the right side of the formula line, and the body of the formula remains centered. |
| The left side of a line is the formula, and the right side needs explanation on the right. | array environment, the column specifier is written as lr, and the columns are separated by & | The left column is left-aligned and the right column is right-aligned. The instructions follow the formula. |
| Multi-line derivation, equal signs must be aligned | aligned environment, write & before the equal sign to specify the alignment position | Each column of aligned is aligned left and right alternately, which is suitable for the derivation process. |
| Multi-line formulas, each line must be marked on the right | align environment, write \tag{} at the end of each line | Each line is marked independently, and the lines do not affect each other. |
Before you start: Confirm these points first
Before taking action, you can avoid a lot of detours by judging the following things.
- Confirm macro package. Commands such as aligned, array separation, \tag, \text, etc. all rely on the amsmath macro package, and \usepackage{amsmath} must be written in the introductory area. When it is missing, the compiler will report an error such as Undefined control sequence. The error line is usually where \tag or \text is located.
- Confirm how Chinese is handled. To write Chinese in \text{}, you need to load a macro package that supports Chinese such as ctex or xeCJK, and compile it with XeLaTeX or LuaLaTeX. When using only pdfLaTeX, Chinese will report an error or cannot be displayed properly. Pure symbols in formulas are not affected.
- Confirm right aligned reference. The width of the array is determined by the content. The entire array is still centered in the formula row, so the right column is attached to the right edge of the array itself, not necessarily to the center of the page. If you want the label to be affixed to the right of the center of the typeface, you should select \tag, which is automatically placed at the right end of the formula line by LaTeX.
- Confirm if numbering is required. Use equation when you only need to write a numbered formula; use equation when you don't want to number it; use \tag{} if you want to put a paragraph of text without brackets on the right side. Deciding it in advance can avoid repeated revisions.
Write the right-aligned formula step by step
The following steps take the need for explanatory text on the right side of the formula as an example. Each step can be checked individually.
- Open the .tex file and add \usepackage{amsmath} in the introductory area.
- Decide what content you want to right-align: a formula on the left, a description, a number, or another formula on the right.
- If there is only a number on the right side, use the equation environment and write \tag{1} at the end of the formula, and the number will be automatically attached to the right end of the formula line.
- If there is a formula on the left and a description on the right in a row, use the array environment, write the column specifier as lr, and separate the two columns with &, for example, write \begin{array}{lr} a+b & \text{(Formula 1)} \end{array}, wrapped with $$.
- If it is a multi-line derivation, use the aligned environment and write & before the equal signs that need to be aligned to vertically align the equal signs in each line.
- When the description text is in Chinese, wrap it with \text{}, and confirm that the Chinese macro package and compilation engine have been set up according to the previous requirements.
- After saving, compile twice to let the numbering and cross-references stabilize, and then open the PDF to view the results.
Check each item after completion
Once you get the PDF, don’t just take a glance at it and call it a day. The following checklist can help you identify common problems.
| Check items | How to see | What to do if there is a problem |
|---|---|---|
| Does the formula report an error? | Check whether there are any prompts such as Undefined control sequence, Missing $, etc. in the compilation log. | Check whether amsmath is loaded; check whether the number of & and newline characters \\ is consistent with the number of columns |
| Whether the content on the right side is aligned | Check whether the right end of the right column of text is aligned with the right end of the formula row in PDF | If offset, use\ tag to number instead, or shorten the description text and reduce the column width |
| Is it beyond the center of the edition? | See if the right end extends beyond the edge of the page | If there is an Overfull\ hbox warning in the log, shorten the formula, or split the longer part to the next line |
| Is the number correct? | See if the numbers are continuous and whether the quotation in the text points to the correct formula. | Compile twice; check that the names of\ label and\ ref are the same |
| Whether the Chinese language is displayed properly | See if the text in\ text {} is complete. | Confirm that you are using XeLaTeX or LuaLaTeX and that ctex or xeCJK is loaded |
If all the above checks pass, but the typesetting still does not meet the requirements, it is usually the problem of the typesetting specification itself, such as the periodical template has a special format for formula numbering. At this time, the template description shall prevail, and then return to the method of this article to make fine-tuning.
# Summary
If you want to keep the content on the right side of the formula to the right, the key is to choose the right environment: just use equation with\ tag; left formula, right description with array and write the column as lr; multi-line derivation with aligned. After compilation, a stable result can be obtained by reading the error, ultra-wide and numbered checklist.
# frequently asked questions (FAQ)
1. Why doesn't\ hfill work in $$?
\ hfill is a command designed for text lines to support horizontal whitespace and does not play a role in mathematical models. The location where the formula is displayed is determined by the environment or label command, so use array, aligned, or equation instead.
2. What's the difference between $$and equation, and when to use\ tag?
$is the display formula of TeX, which is not numbered by default. Equation is the environment provided by amsmath, automatically numbering, and supports\ tag, so when you need a number or want the number to the right, it is more suitable to use equation.
3. If you want to write a large paragraph on the right side of the formula, what should I do?
\ text {} is suitable for short comments, not for long text. Longer instructions are more suitable to be placed in the text, or to be typeset separately by means such as minipage, and then arranged side by side or up and down with the formula.
4. After compilation, the content on the right side exceeds the margin of the page, what should I do?
First look at the Overfull\ hbox warning in the log to find the corresponding line. Then shorten the formula or description, split the array into two lines, or use aligned to change the content on the right to the next line.
5. Can I change the format of the number after using\ tag?
Sure. \ tag {A} is displayed as (A), while\ tag* {A} is displayed as A without parentheses. When the numbering style needs to be unified, you can agree on the same writing method throughout the document.
Open OmniDoc to edit, convert or translate your LaTeX document


