2

This question already has an answer here:

My Javascript code has a large amount of whitespace in its block in the output pdf file. How do I get rid of this without un-indenting my latex code? Thanks in advance.

\documentclass{article}
\title{Javascript Cheatsheet}
\author{Puvendran Pillay}
% Taken from Lena Herrmann at
% http://lenaherrmann.net/2010/05/20/javascript-syntax-highlighting-in-the-latex-listings-package
\usepackage{listings}
\usepackage{color}
\definecolor{yellow1}{rgb}{.9,.86,.45}
\definecolor{red1}{rgb}{.97,.15,.47}
\definecolor{purple1}{rgb}{0.2, 0.2, 0.2}
\definecolor{blue1}{rgb}{0.68, 0.5, 1}
\definecolor{grey1}{rgb}{0.45, 0.44, 0.37}
\definecolor{black1}{rgb}{0.2, 0.2, 0.2}
\definecolor{orange1}{RGB}{253, 151, 31}
\lstdefinelanguage{JavaScript}{
  keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
  keywordstyle=\color{red1}\bfseries,
  ndkeywords={class, export, boolean, throw, implements, import, this},
  ndkeywordstyle=\color{red1}\bfseries,
  identifierstyle=\color{orange1},
  sensitive=false,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  commentstyle=\color{grey1}\ttfamily,
  stringstyle=\color{yellow1}\ttfamily,
  morestring=[b]',
  morestring=[b]"
}

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

\begin{document}
  \maketitle
  \section{Comments}
    \begin{lstlisting}
      // in-line comment
      /* multi-line
      comment */
    \end{lstlisting}
  \section{Datatypes}
    \begin{enumerate}
      \item undefined
      \item  null
      \item  boolean
      \item  string
      \item  number
      \item  object
      \item  array
      \item  float
    \end{enumerate}
  \section{Variables}
    \begin{enumerate}
      \item Declare variable
        \begin{lstlisting}
          var ourName;
        \end{lstlisting}
      \item Assign value to a variable
        \begin{lstlisting}
          ourName = "something";
        \end{lstlisting}
      \item Can also be done once
        \begin{lstlisting}
          var ourName = "something";
        \end{lstlisting}
        \item Variable should be initialized when declared.
        \item Javascript variables names are \emph{case-sensitive}.
    \end{enumerate}
  \section{Operators}
      \begin{enumerate}
        \item Arithmetic Operators
          \begin{enumerate}
            \item Addition
              \begin{lstlisting}
                var x = 5;     // assign the value 5 to x
                var y = 2;     // assign the value 2 to y
                var z = x + y  // assign the value 7 to z (x + y)
                alert(z);      // outputs the value in an alert box
              \end{lstlisting}
            \item Subtraction
              \begin{lstlisting}
                var x = 5;     // assign the value 5 to x
                var y = 2;     // assign the value 2 to y
                var z = x - y  // assign the value 3 to z (x - y)
                alert(z);      // outputs the value in an alert box
              \end{lstlisting}
            \item Multiplication
              \begin{lstlisting}
                var x = 5;     // assign the value 5 to x
                var y = 2;     // assign the value 2 to y
                var z = x * y  // assign the value 10 to z (x * y)
                alert(z);      // outputs the value in an alert box
              \end{lstlisting}
            \item Division
              \begin{lstlisting}
                var x = 5;     // assign the value 5 to x
                var y = 2;     // assign the value 2 to y
                var z = x / y  // assign the value 2.5 to z (x / y)
                alert(z);      // outputs the value in an alert box
              \end{lstlisting}
            \item Modulus \textit{(returns the remainder of division)}
              \begin{lstlisting}
                var x = 5;     // assign the value 5 to x
                var y = 2;     // assign the value 2 to y
                var z = x % y  // assign the value 1 to z (x % y)
                alert(z);      // outputs the value in an alert box
              \end{lstlisting}
            \item Increment
              \begin{lstlisting}
              //same as var x = x + 1;
              var x = 0;       // assigns the value of 0 to x
              x++;             // increases the value by 1
              alert(x);        // outputs the value in an alert box
              \end{lstlisting}
            \item Decrement
              \begin{lstlisting}
              //same as var x = x - 1;
              var x = 0;       // assigns the value of 0 to x
              x--;             // Decreases the value by 1
              alert(x);        // outputs the value in an alert box
              \end{lstlisting}
          \end{enumerate}
        \item Assignment Operators
        \begin{enumerate}
          \item assignment (=)
            \begin{lstlisting}
              var x = 10;
              alert(x);  // outputs the value in an alert box
            \end{lstlisting}
          \item Addition assignment
            \begin{lstlisting}
              var x = 10;
              x += 5;    // same as x = x + 5;
              alert(x);  // outputs the value in an alert box
            \end{lstlisting}
          \item Subtraction assignment
            \begin{lstlisting}
              var x = 10;
              x -= 5;    // same as x = x - 5;
              alert(x);  // outputs the value in an alert box
            \end{lstlisting}
          \item Multiplication assignment
            \begin{lstlisting}
              var x = 10;
              x *= 5;    // same as x = x * 5;
              alert(x);  // outputs the value in an alert box
            \end{lstlisting}
          \item Division assignment
            \begin{lstlisting}
              var x = 10;
              x /= 5;    // same as x = x / 5;
              alert(x);  // outputs the value in an alert box
            \end{lstlisting}
          \item Modulus assignment
            \begin{lstlisting}
              var x = 10;
              x %= 5;    // same as x = x % 5;
              alert(x);  // outputs the value in an alert box
            \end{lstlisting}
        \end{enumerate}
      \end{enumerate}
\end{document}

marked as duplicate by Werner, Arun Debray, Jesse, Maarten Dhondt, Stefan Pinnow May 31 '16 at 4:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Welcome! Put the code in an external file and input it, maybe? – cfr May 31 '16 at 0:37
  • i don't use lstlisting, and am not in a position to test right now, but most verbatim-like environments observe initial spaces in a line. your code is indented quite a number of spaces, and each space occupies the width of a monospaced letter. that is quite likely the source of your indentation. remove the initial spaces from a couple of lines, and see if that makes the difference you are looking for, – barbara beeton May 31 '16 at 0:42
  • @cfr I would if the code was a large one but this is just small snippet of JavaScript code. At maximum only 5-6 lines. Is there a way that I can just get rid of the whitespace in front of it? – Puvendran Pillay May 31 '16 at 0:42
  • @barbarabeeton Hmm looks like that is the only option I am left with right now. But it makes my source code a bit messy. – Puvendran Pillay May 31 '16 at 0:44
  • the only alternative i can think of is to patch the lstlisting definition to ignore initial spaces. but i'm in a situation right now without a tex installation, so i can't check to see how that might be done. (and it's also an approach that might be i bit chancy, so would have to be done with great care.) maybe someone else can come up with good patch code. for myself, i'd accept the fact that messy code is preferable to an unwanted indent. – barbara beeton May 31 '16 at 0:50