[Tex/LaTex] Set the lstlisting at the exact place in the code (like “h” for figure)


When using lstlisting, my displayed code is sent back on the top of my page.
I would like to have the same kind of option as:


ie having displayed my code exactly where I put it in my latex code.

My current code for lstlisting is as follow:



Second question:
How to display lstlisting on 2 followed pages, when the code is too long?

Best Answer

Don't use figure environment. Generally you don't need to use figure as listings provides a float option by itself. But for your case, you shouldn't use that either (as you do not want floating). The following code works as you wanted:

\begin{lstlisting}[caption=Listing,language=c,escapechar=+] %% don't use float option here
+{public void}+ Method () {
  try {
    .. Original code
    .. After returning
  catch (...) {
    .. After throwing
  finally {
    .. After

enter image description here

As you can see, the code runs in to second page as well.

Related Question