1

I have this code (javascript from language option supported in listings)

\lstset{language=javascript}

\begin{lstlisting}
  $('#term').terminal(function(command, terminal) {
    var cmd = $.terminal.parseCommand(command);
    if (cmd.name == 'echo') {
      terminal.echo(cmd.rest);
    } else {
      terminal.error('Invalid Command');
    }
  });
\end{lstlisting}

and the result is this (using pdflatex):

$ ( ’#term’ ) . terminal ( function ( command , terminal ) {
    var cmd = $ . terminal . parseCommand ( command ) ;
    if ( cmd . name == ’echo’ ) {
        terminal . echo ( cmd . rest ) ;
    } else {
        terminal . error ( ’Invalid Command’ ) ;
    }
});

My configuration:

\lstset{
  language=JavaScript,
  showspaces=false,
  extendedchars=false,
  showstringspaces=false,
  showtabs=false,
  keepspaces=false,
}

How can I make it show exactly as in tex file? Without spaces (and also if possible ' isntead of )

  • While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. – Peter Grill Apr 5 '14 at 17:38
1

This works for me:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\begin{document}
\lstdefinelanguage{JavaScript}{
  keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
  keywordstyle=\color{blue}\bfseries,
  ndkeywords={class, export, boolean, throw, implements, import, this},
  ndkeywordstyle=\color{darkgray}\bfseries,
  identifierstyle=\color{black},
  sensitive=false,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  commentstyle=\color{purple}\ttfamily,
  stringstyle=\color{red}\ttfamily,
  morestring=[b]',
  morestring=[b]"
}

\lstset{
   language=JavaScript,
   backgroundcolor=\color{lightgray},
   extendedchars=true,
   basicstyle=\footnotesize\ttfamily,
   showstringspaces=false,
   showspaces=false,
   numbers=left,
   numberstyle=\footnotesize,
   numbersep=9pt,
   tabsize=2,
   breaklines=true,
   showtabs=false,
   captionpos=b
}

\begin{lstlisting}
  $('#term').terminal(function(command, terminal) {
    var cmd = $.terminal.parseCommand(command);
    if (cmd.name == 'echo') {
      terminal.echo(cmd.rest);
    } else {
      terminal.error('Invalid Command');
    }
  });
\end{lstlisting}
\end{document}

enter image description here

If you want to change it, I'd remove one part at a time, or simply use the verbatim environment (no colors, exactly as in the file, typewriter font).

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.