0

I'm running into issues when using lstlisting to highlight code. In the latex code I have intended the listing to appear below the text, but it disregards the way the code is structured and puts it above the code. How do I force code listings to obey the structure and order I write in the latex code?

The core latex code:

\documentclass[12pt, a4paper]{article}
%% PACKAGES %%
\usepackage[a4paper,left=1.6cm, right=2cm, top=1.5cm, bottom=0.5cm,includefoot, footskip=30pt]{geometry}
\usepackage[backend=biber, style=iso-numeric, babel=other, urldate=short]{biblatex}
\addbibresource{ref.bib}
\usepackage{url}
\usepackage{float}
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{pdfpages}
\usepackage{setspace}
\usepackage{lipsum}

%% FORMATING %%
\DeclareCaptionType{code}[code][list of code] 
\renewcommand{\lstlistingname}{code}
\onehalfspacing

%% CODE COLORS %%
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.97,0.97,0.97}

\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegray},
    keywordstyle=\color{codegreen},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}

\lstset{style=mystyle}
\setcounter{section}{-1}

\begin{document}
%% START DOCC %%

\section{sample that should be on top}
\lipsum[1]

\begin{center}
    \begin{code}
    \begin{lstlisting}[language=python, frame=single]
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat
        ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget,
        consectetuer id, vulputate a, magna. Donec vehicula augue eu neque. Pellentesque habitant
        morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras    \end{lstlisting}
    \caption[bleh1]{bleh1}
    \end{code}
\end{center}

\begin{center}
    \begin{code}
    \begin{lstlisting}[language=python, frame=single]
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat
        ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget,
    \end{lstlisting}
    \caption[bleh2]{bleh2}
    \end{code}
\end{center}

%% END OF DOCC %%
\end{document}

Output:

Latex output

1 Answer 1

2

Welcome to TeX.SX!

When you use \DeclareCaptionType you seem to basically define a new float environment. Its content will, well, float! If you don't want it to float, you can use float's [H] specifier to put the float exactly where it is in your code. Warning: There are multiple reasons why you should not do it (https://tex.stackexchange.com/a/373265/237192 to name some).

Another way is to use placeins' \FloatBarrier which keeps floats from floating across that barrier, so you could place one after your section title.

Even better: If you don't want it to float, don't make it a float. You do not need to wrap it into a float, you can simply use \begin{lstlisting}[caption=test] ... \end{lstlisting}. This is the way to go.

Some more hints:

Edit after comment

To actually have a really minimum working example:

\documentclass[12pt, a4paper]{article}
%% PACKAGES %%
\usepackage{lipsum}
\usepackage{listings}
\lstset{basicstyle=\ttfamily\footnotesize,breaklines=true,captionpos=b}

\begin{document}
    \section{sample that should be on top}
    \lipsum[1][1-3]
    \begin{lstlisting}[language=python, frame=single, caption=This is code 1]
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, ...
    \end{lstlisting}
    \begin{lstlisting}[language=python, frame=single, caption=This is code 2]
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, ...
    \end{lstlisting}
    \lstlistoflistings
\end{document}

Result

2
  • >\begin{lstlisting}[caption=test] ... \end{lstlisting} I want to make a final table of codes, I did not find another way to do that without wraping it into another shell.
    – mamimom892
    Mar 12, 2023 at 19:53
  • How about \lstlistoflistings? Mar 12, 2023 at 19:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .