LaTeX error "Text line contains an invalid character": cause and fix
The line contains a character that TeX refuses to read. In LaTeX these are the NUL character (byte 0) and the DEL character (byte 127), which are usually invisible on screen. In the log the line is broken right behind the character, which is shown as ^^@ or ^^?.
Why it happens
- An invisible control character got into the file, for example through copy and paste from another program. The example writes it as
^^?, the notation TeX reads as the DEL character. - The file is saved as UTF-16. Every second byte is then a NUL character, and the error appears many times per line. LaTeX expects UTF-8.
- A binary file is read as text, for example an image passed to
\inputinstead of\includegraphics.
How to fix it
- Delete the whole line the log names and type it again. Do not copy it, or the character comes along.
- Save the file as UTF-8 if the error shows up on every line.
- Load images with
\includegraphicsand PDF pages withpdfpages.\inputis for TeX source only.
A document that produces the message
\documentclass{article}
\begin{document}
The result is^^? final.
\end{document}The corrected document
\documentclass{article}
\begin{document}
The result is final.
\end{document}Both documents were compiled with pdfLaTeX from TeX Live 2023. The first produces the message, the second compiles without an error.
Finding the line in the editor
The editor lists every error and warning of a compile run with file and line. Select an entry and the cursor jumps to that line. See Error list with jump to the line.