Function: TeX-error
TeX-error is a byte-compiled function defined in tex.el.
Signature
(TeX-error &optional STORE)
Documentation
Display an error.
If optional argument STORE is non-nil, store the error
information in TeX-error-list instead of displaying the error.
Source Code
;; Defined in ~/.emacs.d/elpa/auctex-14.2.0/tex.el
;; TeX and ConTeXt LMTX error messages are formatted differently (and thus
;; some tweakings are necessary for ConTeXt error messages to be parsed).
;; E.g., in TeX:
;; ./file.tex:9: Undefined control sequence.
;; l.9 blah blah \undefinedINplainTEX
;; <<< line with many initial spaces
;; and in ConTeXt:
;; tex error > tex error on line 9 in file ./file.tex: Undefined control sequence
;; <<<--- empty line here
;; <line 3.9>
;; aardvark \undefinedINcontext
;; <<<--- empty line here
;; Because of how the parsing is done below, these formatting differences
;; must be handled.
(defun TeX-error (&optional store)
"Display an error.
If optional argument STORE is non-nil, store the error
information in `TeX-error-list' instead of displaying the error."
(let* ( ;; We need the error message to show the user.
(error (progn
(re-search-forward "\\(.*\\)")
(TeX-match-buffer 1)))
;; And the context for the help window.
(context-start (point))
context-available
;; And the line number to position the cursor.
(line (cond
;; regular style
((re-search-forward "l\\.\\([0-9]+\\)" nil t)
(setq context-available t)
(string-to-number (TeX-match-buffer 1)))
;; file:line:error style
((save-excursion
(re-search-backward ":\\([0-9]+\\): "
(line-beginning-position) t))
(string-to-number (TeX-match-buffer 1)))
;; ConTeXt: ^<line n.[linenum]>\n +[error message]'
((re-search-forward "^<line [^.]+[.]\\([0-9]+\\)>" nil t)
;; Need this in the ConTeXt case for the 'string' search
;; (just below here) to work correctly (FWIW):
(forward-line)
(setq context-available t)
;; ConTeXt has a blank line where pdftex does not; adjust:
(setq context-start (1+ context-start))
(string-to-number (TeX-match-buffer 1)))
;; nothing found
(t 1)))
;; Save a string of the error token, used to position the cursor
;; in the source file.
;; plain TeX example of what we are looking at:
;; ((point) is before ' aard...')
;; l.9 aardvark \undefinedINplainTEX
;; \n
;; ConTeXt:
;; ((point) is before ' aard...')
;; aardvark \undefinedINcontext
;; \n
;; The pdftex error message *always* has a line starting with
;; spaces after the error line.
;; ConTeXt LMTX (V 2026.02.12 anyway) does not have such a thing
;; when the error token is at the end of a line. Because of the
;; space chars on the line following the pdftex error message, the
;; (following) regexp in the previous version of this function
;; didn't include the \n at the end of the line(s).
;; However, since ConTeXt may have no space chars on the next
;; line, the match includes the \n, which causes TeX-next-error to
;; position the cursor incorrectly.
;; This regexp deals with this problem.
(string (progn
(beginning-of-line)
(re-search-forward " \\(\\([^ \t]*$\\)\\|\\($\\)\\)")
(TeX-match-buffer 1)))
;; (point) is now positioned at the end of the input line
;; which caused this error (TeX) or the beginning of the next
;; line (ConTeXt). To find the error context, rather than searching
;; just use (point) and locations we saved above.
(context (if context-available
(buffer-substring context-start (progn (forward-line 1)
(end-of-line)
(point)))
;; There is no real context available, so we
;; simply show the line with the error message.
(buffer-substring (1- (line-beginning-position))
context-start)))
;; We may use these in another buffer.
(offset (or (car TeX-error-offset) 0))
(file (car TeX-error-file))
info-list)
;; Remember where we were.
(setq TeX-error-point (point)
info-list (list 'error file line error offset context string nil nil
TeX-error-point nil))
(if store
;; Store the error information.
(add-to-list 'TeX-error-list info-list t)
;; Find the error point and display the help.
(apply #'TeX-find-display-help info-list))))