How to Fix ChkTeX Line Numbers That Point to the Wrong Place
At a glance
- 1. Check the file directly
Run ChkTeX on the saved .tex file from a terminal, not only through your editor.
- 2. Compare the reported line
Use numbered source output to see whether the warning matches the file ChkTeX actually checked.
- 3. Check editor settings
If terminal results are correct, inspect the editor's command, working directory, and temporary-file behavior.
- 4. Retest after saving
Save the source and rerun ChkTeX to rule out unsaved edits or stale diagnostics.
When ChkTeX reports a warning on the wrong line, first find out which file it checked and compare the warning with that file on disk. The mismatch is often caused by an editor checking a temporary copy, unsaved changes, or a diagnostic that belongs to a different source file—not by ChkTeX miscounting lines in the file you are viewing.
Check ChkTeX outside your editor
Open a terminal in the project directory and run ChkTeX on the saved file:
chktex main.tex
Replace main.tex with the file you want to check. ChkTeX’s output includes a file name and line number. Compare both with the document open in your editor; do not rely on the line number alone.
To display line numbers in the source, use this on Ubuntu:
nl -ba main.tex | sed -n '1,80p'
The -ba option numbers every line, including blank ones. If ChkTeX reports a warning in chapter1.tex, inspect that file rather than assuming the location is in main.tex. A project may have several source files, and a warning’s reported file is the best clue to which one to open.
Interpret the comparison
There are three useful outcomes:
| Result | Likely explanation | What to do |
|---|---|---|
| Terminal and editor show the same location | The location is consistent; the warning may be about nearby syntax | Inspect the reported line and the preceding lines |
| Terminal points to the right place, editor does not | The editor integration is using a different file or interpreting output incorrectly | Check the editor’s ChkTeX command and diagnostic settings |
| Both point to an unexpected location | ChkTeX may be reading a different copy, or the source may have unusual line endings or generated content | Verify the path and inspect the actual file on disk |
A warning can refer to a token whose relevant context starts on an earlier line. For example, a command or group opened above may not be understood as intended until ChkTeX encounters a later line. Read the reported line together with the surrounding source before deciding the line count itself is wrong.
Check the file path and saved contents
Confirm that the file you pass to ChkTeX is the same one your editor has open. From the project directory, you can print the absolute path with:
realpath main.tex
Then save the document and rerun the check. If the terminal and editor disagree, look at the editor’s configured command for ChkTeX: it may pass a temporary file, a copy in a build directory, or a different project file. Also check whether the editor runs ChkTeX on the current document or on the root LaTeX document. The root file and the file currently open are not necessarily the same.
If your workflow sends source through a pipe or standard input rather than naming a file, compare that input with the saved file. A generated wrapper or a command that prepends text can shift line numbers. Test the plain file directly first; if that fixes the issue, the wrapper or editor command is where to investigate.
Inspect line endings only if needed
Different line-ending conventions—Unix LF and Windows CRLF—can cause problems in some tools and scripts. They are not, by themselves, a reliable explanation for an offset in every ChkTeX setup. Check the file type with:
file main.tex
If you confirm that the file has CRLF endings and suspect they are involved, make a backup before converting it. On Ubuntu, dos2unix can convert a file when the utility is installed:
dos2unix main.tex
Rerun ChkTeX afterward and compare the same warning. Avoid converting files blindly, especially in a project shared with other tools or contributors.
A worked check
Suppose your editor displays a warning as main.tex:24, but the highlighted line appears harmless. First run:
chktex main.tex
nl -ba main.tex | sed -n '18,30p'
If the terminal reports chapter1.tex instead of main.tex, open chapter1.tex and compare its numbered lines around the reported location. If the terminal reports main.tex:24 and the numbered output confirms that line 24, the editor may be showing stale diagnostics or checking another copy. Save the document, verify its path, and rerun the editor’s check. If the command-line warning disappears after saving, the editor had been checking content that did not match the current buffer on disk.
Common mistakes
- Counting only visible nonblank lines. Blank lines count. Use
nl -bato number them all. - Assuming the open file is the root file. A project can have a root document and several included source files; trust the file name in ChkTeX’s message.
- Changing line endings before checking paths. First establish which file was checked and compare direct command-line output. Convert only when there is evidence that line endings matter.
- Treating every warning as a precise pointer to one token. The reported location may be where ChkTeX detects a problem whose cause is earlier in the source. Inspect nearby lines and the surrounding command or environment.
If direct command-line checks still produce a reproducible mismatch for the exact saved file, record the ChkTeX version, the command used, and a small source example that preserves the problem. That makes it possible to distinguish a tool issue from an editor-integration or project-configuration issue.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.