lstlisting inside figure Latex Error: There' s no line here to end

175 views Asked by At

Problem is, that Latex is telling me, that there is following Error at line \end{lstlisting}:

68: LaTeX Error: There's no line here to end.

\begin{figure}
    \caption{Paragraph 1 - Code}
    \begin{lstlisting}
    [firstnumber=125]
    def recextract(obj, arr, prefix):
        #Recursively search for values of key in JSON tree.
        if isinstance(obj, dict):
            for k, v in obj.items():
                if isinstance(v, (dict, list)):
                    recextract(v, arr, prefix+'_'+k)
                else:
                    arr[prefix+'_'+k] = v
        elif isinstance(obj, list):
            for item in obj:
                recextract(item, arr, prefix)

    def parse_json(array_of_json):
        arr = dict()
        es_row = recextract(json.loads(array_of_json), arr, '')
        return arr
     
    prdd = rdd.map(lambda x: parse_json(x[1]))
    df = prdd.toDF(sampleRatio=0.01)
    df = df.cache()
    df = df.toDF(*(c.replace('.', '_') for c in df.columns))

    print(df.count())
    \end{lstlisting}
    \source{Zeppelin}
    \label{fig:abb1}
\end{figure}

please help :)

I tried using the lstlisting as a figure, so I can have it numbered etc

1

There are 1 answers

0
AudioBubble On

You don't need the figure environment to have the listing numbered, see here. If you delete the figure environment (including label and caption) and change \begin{lstlisting}[firstnumber=125]to \begin{lstlisting}[firstnumber=125, caption=Paragraph 1 - Code, label=fig:abb1] then you should no longer get an error message.

Mini example:

\documentclass{article}

\usepackage{listings}
\usepackage{lipsum} %not needed here, for filler text

\begin{document}
% This prints a list of your listings
\lstlistoflistings

% Caption sets the caption of the listing and label can be used to set a label that can be called with \ref{}
\begin{lstlisting}[language=Python, caption=Python example, label=ABC]
import numpy as np
    
def idle():
    pass
\end{lstlisting}

\lipsum[1]

\begin{lstlisting}[language=Python, caption=A second example, label=DEF]
import pandas as pd
\end{lstlisting}
See algorithm \ref{ABC} and \ref{DEF}.
\end{document}