Bo Peng wrote:
Dear list,

I am trying to add a lstlisting environment to my layout file (using
listings package) and arrive at the following:

Style Listings
 Margin        Static
 LatexType        Environment
 LatexName            lstlisting
 NextNoIndent        1
 LeftMargin        MMM
 RightMargin        MMM
 TopSep        0.5
 BottomSep        0.5
 Align            Left
 AlignPossible        Block, Left, Right, Center
 LabelType        No_Label

 # define the environment listings
 Preamble
   \usepackage{listings}
   \lstset{language=Python,
  showspaces=false,
  basicstyle=\footnotesize,
  showstringspaces=false,
  showtabs=false
  }
 EndPreamble

 # standard font definition
 Font
   Family          Typewriter
   Size            small
 EndFont
End


My problem is that

Line1
Line2

appears in latex as

\begin{lstlisting}
Line1

Line2
\end{lstlisting}

so I get an extra line.

If I do Ctrl-enter after Line1, the latex code would be

\begin{lstlisting}
Line1 ~\\
Line2
\end{lstlisting}

~\\ will be shown in lstlisting as it is.

How can I correct this? Basically, I would like the code to be in
latex as it is.

Many thanks in advance.
Bo


Two possible solutions, neither necessarily ideal.

Method 1: Add emptylines=0 to the package load in the preamble, then just use Enter to terminate lines. The blank lines will be vaporized. Downside: _all_ blank lines will be vaporized, so if you wanted to leave empty lines, you'll need to put a Python comment character (whatever that may be) as a placeholder.

Method 2: Invoke the environment with \begin{lstlisting}[escapechar=\\] and use C-Enter to terminate lines. The double backslash you would otherwise get will be vaporized. So will any other backslashes (don't know if Python uses them or not), except those in strings, which get screwed up for reasons separate from this.

/Paul

Reply via email to