5

I am new to LaTeX and am using a template provided by a friend:

 \subsection{P2P library}
The library is implemented in C-Sharp and provides two basic facilities - maintaining a member list at every instance and the support for receving and multicasting UDP data. 

The public methods it has are 
\begin{lstlisting}
      void setPorts(int recvHere \newline, int sendFrom

Why doesn't ,int sendFrom move to a new line in the PDF output? In fact it just shows up in the PDF output.

3
  • The listings is a verbatim environment so that the usual macros are not executed. You can specify a character to escape you to latex and you will need to insert that character around \newline. Mar 22, 2012 at 19:30
  • so what other option do I have?
    – epsilon8
    Mar 22, 2012 at 19:30
  • 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. Mar 22, 2012 at 19:36

3 Answers 3

10

As commented by Peter Grill, lstlisting is a verbatim environment, meaning that its contents are printed exactly as they appear in the code. As this also includes linebreaks, you could simply add a linebreak in the code.

\begin{lstlisting}
      void setPorts(int recvHere 
      , int sendFrom
\end{lstlisting}
0
5

You can escape to latex using escapechar=\&. Then enclose the LaTeX macros you want executed within a & pair:

enter image description here

For simply inserting a new line, best to probably manually do it as per TorbjørnT's solution. But if you want to imbed more useful macros then specifying an escapechar will be needed.

\documentclass{article}
\usepackage{listings}

\lstset{% Add other global options here
  basicstyle=\small\sffamily,
  escapechar=\&% char to escape out of listings and back to LaTeX
}


\begin{document}
\subsection{P2P library}
The public methods it has are 
\begin{lstlisting}
      void setPorts(int recvHere &\newline& int sendFrom
\end{lstlisting}
\end{document}
1
  • It worth noting that simply typing a line break affects the line numbers. Your solution with &\newline& performs the line break without incrementing lstlisting's line numbers. Therefore both solutions have their own uses. Nov 6, 2013 at 23:07
3

another escape way is to define escapeinside field.

\lstset{escapeinside={\%*}{*)}

means that between %* and *) characters you can write latex commmands.

For example,

\begin{lstlisting}
  void setPorts(int recvHere,  %*\newline*) int sendFrom)

The answer is similar to Peters' but i couldn't add the comment to his answer since i have not enough reputation...)

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .