LaTeX error "Couldn't load requested language": cause and fix
The language=... option of listings names a language for which the package has no definition. A second error follows in the log, for example "language javascript undefined".
Why it happens
listingsdoes not ship the language. Version 1.9 has no definition for JavaScript, TypeScript, Rust, Kotlin, JSON, YAML or Dockerfile.- The language exists under another name or as a dialect. LaTeX source is
language={[LaTeX]TeX}, notlanguage=LaTeX, and C# islanguage={[Sharp]C}. - The name has a typo.
How to fix it
- Define the language yourself in the preamble with
\lstdefinelanguage{JavaScript}{...}: a keyword list, the comment signs and the string delimiters, as in the example below. - Use a language with similar syntax that
listingsknows, for exampleJavafor JavaScript code. Upper and lower case in the name do not matter. - Leave
languageout. The code is then printed without highlighting. mintedknows more languages, but it calls an external program and needs shell escape.
A document that produces the message
\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[language=JavaScript]
const answer = 42; // a comment
\end{lstlisting}
\end{document}The corrected document
\documentclass{article}
\usepackage{listings}
\lstdefinelanguage{JavaScript}{
keywords={const, let, var, function, return, if, else, for, while},
sensitive=true,
comment=[l]{//},
morecomment=[s]{/*}{*/},
morestring=[b]",
morestring=[b]'
}
\begin{document}
\begin{lstlisting}[language=JavaScript]
const answer = 42; // a comment
\end{lstlisting}
\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.
Related pages
- Package
listings - Package
minted - How-to Insert source code in LaTeX
- How-to Include source code from a file in LaTeX