Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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? –  user494461 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
add comment

2 Answers 2

up vote 3 down vote accepted

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}
share|improve this answer
add comment

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}
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.