2

I am trying to create some styling for my javascript snippets. My configuration looks like:

\documentclass[letterpaper, dvipsnames]{article}
\usepackage[utf8]{inputenc}
\usepackage{listings} % Package for adding javascript code with code highlighting
\usepackage[dvipsnames]{xcolor} % Package for adding colors
\definecolor{purple}{rgb}{0.65, 0.12, 0.82}
\definecolor{main-color}{rgb}{0.6627, 0.7176, 0.7764}
\definecolor{back-color}{rgb}{0.1686, 0.1686, 0.1686}

\lstdefinestyle{codeSection}
{
  language= JavaScript,
  basicstyle = {\ttfamily \color{main-color}},
  backgroundcolor = {\color{back-color}},
  commentstyle = {\color{OliveGreen}},
  stringstyle = {\color{green}},
  keywordstyle = {\color{orange}\bfseries},
  keywordstyle = [2]{\color{orange}},
  keywordstyle = [3]{\color{purple}},
  keywordstyle = [4]{\color{lime}},
  keywordstyle = [5]{\color{orange}},
  keywords = {typeof, new, true, false, function, return, null, switch, var, if, in, while, do, else, case, break, class, export, boolean, throw, implements, import, this, constructor, string, number, public, private, static, const, var, let, void},
  morekeywords = [2]{class, export, boolean, throw, implements, import, this, interface},
  morekeywords = [3]{Promise, Observable},
  morekeywords = [4]{console},
  otherkeywords = {;},
  comment = [l]{//},
  morecomment = [s]{/*}{*/},
  morestring = [b]',
  morestring = [b]",
  sensitive = false,
  breaklines = true,
  showstringspaces= false,
  showspaces= false,
  extendedchars= true
}

Now when I try to add some Code it renders with the correct styling, but it gives me the error:Could not load requested styling. The Code-insertion looks like:

\begin{document}
\begin{figure}[H]
\begin{lstlisting}[style=codeSection]
export interface FakeHttpResponse {
    code: string;
    message: string;
}

const scndPromise = new Promise<FakeHttpResponse>((resolve, reject) => {
    setTimeout(() => {
        resolve({
            code: '200',
            message: 'Promise kept!'
        });
    }, 10 * 1000);
});

console.log(scndPromise);
setTimeout(() => console.log(scndPromise), 10 * 1000);
\end{lstlisting}
\end{figure}
\end{document}

Can anyone help me out on fixing this error?

6
  • Make a complete example that can be tested. – Ulrike Fischer Nov 14 '18 at 18:10
  • Extented my config to a reproducable example. – Fantasia Nov 14 '18 at 18:18
  • I can find no javascript definitions in the listings files. Why do you think that the language exists? – Ulrike Fischer Nov 14 '18 at 18:30
  • I am fairly new to latex so... what should I add to prevent this error? – Fantasia Nov 14 '18 at 21:10
  • If the language doesn't exist, you will have to create it (or adjust an existing one), see the documentation of listings. – Ulrike Fischer Nov 14 '18 at 21:16
1

I fixed it by the following configuration:

\lstdefinelanguage{JavaScript}
{
  keywords = {typeof, new, true, false, function, return, null, switch, var, if, in, while, do, else, case, break, class, export, boolean, throw, implements, import, this, constructor, string, number, public, private, static, const, var, let, void},
  morekeywords = [2]{class, export, boolean, throw, implements, import, this, interface},
  morekeywords = [3]{Promise, Observable},
  morekeywords = [4]{log},
  otherkeywords = {;},
  comment = [l]{//},
  morecomment = [s]{/*}{*/},
  morestring = [b]',
  morestring = [b]",
}

\lstset
{
  language= JavaScript,
  commentstyle = {\color{OliveGreen}},
  stringstyle = {\color{green}},
  keywordstyle = {\color{orange}\bfseries},
  keywordstyle = [2]{\color{orange}},
  keywordstyle = [3]{\color{purple}},
  keywordstyle = [4]{\color{lime}},
  keywordstyle = [5]{\color{orange}},
  basicstyle = {\ttfamily \color{main-color}},
  backgroundcolor = {\color{back-color}},    
  sensitive = false,
  breaklines = true,
  showstringspaces= false,
  showspaces= false,
  extendedchars= true
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.