2

This question already has an answer here:

My preamble is as follows:

\documentclass{llncs}
\usepackage{xcolor}
\usepackage{harvard}
\usepackage{listings}
\usepackage{chngcntr}
\makeatletter
\lst@InstallKeywords k{attributes}{attributestyle}\slshape{attributestyle}{}ld
\makeatother

\definecolor{bluekeywords}{rgb}{0,0,1}
\definecolor{greencomments}{rgb}{0,0.5,0}
\definecolor{redstrings}{rgb}{0.64,0.08,0.08}
\definecolor{xmlcomments}{rgb}{0.5,0.5,0.5}
\definecolor{types}{rgb}{0.17,0.57,0.68}
\definecolor{cyan}{rgb}{0.168,0.568,0.686}

%\counterwithin{lstlisting}{section}

\lstset{language=C++,
basicstyle=\ttfamily,
literate={~} {$\sim$}{1},
showspaces=false,
showtabs=false,
breaklines=true,
showstringspaces=false,
breakatwhitespace=true,
escapeinside={(*@}{@*)},
commentstyle=\color{greencomments},
keywordstyle=\color{bluekeywords}\bfseries,
stringstyle=\color{redstrings},
basicstyle=\ttfamily,
morekeywords=
{  abstract, event, new, struct,
   as, explicit, null, switch,
   base, extern, object, this,
   bool, false, operator, throw,
   break, finally, out, true,
   byte, fixed, override, try,
   case, float, params, typeof,
   catch, for, private, uint,
   char, foreach, protected, ulong,
   checked, goto, public, unchecked,
   class, if, readonly, unsafe,
   const, implicit, ref, ushort,
   continue, in, return, using,
   decimal, int, sbyte, virtual,
   default, interface, sealed, volatile,
   delegate, internal, short, void,
   do, is, sizeof, while,
   double, lock, stackalloc,
   else, long, static,
   enum, namespace, string, var, \#region, \#endregion
},
moreattributes={Assert, Test, GeneticAlgorithmHarmonizer, ExpectedException,
InvalidElitismRate,
InvalidMutationRate,
InvalidPopulationSize, 
ScoreManagementUnitTests, TestFixture, Double, Int32, MusicalStyle, KeySignature,     TimeSignature}, % etc...
attributestyle = \bfseries\color{cyan}, % (for instance)
backgroundcolor=\color{black!5}, % set backgroundcolor
basicstyle=\footnotesize% basic font setting
}

I have the following listing in my document:

\begin{lstlisting}[basicstyle=\ttfamily, caption={class foo managing private memory},label={lst:baseFoo}]

class foo
{
  public:
    foo() { aReallyLargeArray = new char[123456789]; }
    ~foo() { delete [] aReallyLargeArray; }
  private:
    char* aReallyLargeArray;
};

int main(int argc, char** argv)
{
   foo bar;  
}

\end{lstlisting}\bigskip

However, indentation in the input is not preserved in the output. Note how the ~foo() destructor is indented much more than foo on the previous line. This improper indentation occurs in other listings of mine as well. I suspect it has to do with the ~... Any thoughts?

enter image description here

marked as duplicate by jub0bs, Jesse, Masroor, user31729, Claudio Fiandrino May 16 '14 at 14:09

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.

  • No problem here: i.stack.imgur.com/k5Dcl.png . May be you need an update for the tex distro. – user11232 May 16 '14 at 2:09
  • The problem most likely stems from ~foo being preceded by a tab character, whereas foo (on the previous line) is preceded instead by 4 spaces; see tex.stackexchange.com/questions/166585/…. To avoid any surprises like this one, I recommend using spaces throughout your listing. – jub0bs May 16 '14 at 12:10
  • 1
    In the future, please don’t break your code apart: keep the preamble and body in one minimal working example (MWE), so that people can copy & paste it easily and start working on an answer more quickly. – jub0bs May 16 '14 at 12:11