4

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.

  • 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? – zcqwevb 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
6

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

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.