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 
      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.

  • 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. – Peter Grill Mar 22 '12 at 19:30
  • so what other option do I have? – epsilon8 Mar 22 '12 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. – Peter Grill Mar 22 '12 at 19:36

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.

      void setPorts(int recvHere 
      , int sendFrom

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.


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

\subsection{P2P library}
The public methods it has are 
      void setPorts(int recvHere &\newline& int sendFrom
  • 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. – Augustus Kling Nov 6 '13 at 23:07

another escape way is to define escapeinside field.


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

For example,

  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...)

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.