Wrapfigure: lstlisting (code block) overlaps text

listingswrapfigure

As shown in the picture below, the lstlisting overlaps text. I defined my own lststing "style". I think that causes the problem.
How can I fix it? 🙂

enter image description here

Code:

\section{ New Section }

\begin{wrapfigure}[5]{r}{.5\textwidth}

\begin{lstlisting}[language=rust, caption=Test, label={lst:test}]
fn main(){
    let a = 42;
    let b = 32;
    let c = 42;
    let d = 32;
}
\end{lstlisting}  

\end{wrapfigure}

hello hello hello hello hello hello hello hello ...

Language definition:

\lstdefinelanguage{rust}
{   
    columns=fullflexible,
    keepspaces=true,
    frame=single,
    framesep=0pt,
    framerule=0pt,
    framexleftmargin=4pt,
    framexrightmargin=4pt,
    framextopmargin=5pt,
    framexbottommargin=3pt,
    xleftmargin=4pt,
    xrightmargin=4pt,
    backgroundcolor=\color{GrayCodeBlock},
    basicstyle=\footnotesize\color{BlackText},
    keywords={
        true,false,
        unsafe,async,await,move,
        use,pub,crate,super,self,mod,
        struct,enum,fn,const,static,let,mut,ref,type,impl,dyn,trait,where,as,
        break,continue,if,else,while,for,loop,match,return,yield,in
    },
    keywordstyle=\color{PurpleKeyword},
    ndkeywords={
        bool,u8,u16,u32,u64,u128,i8,i16,i32,i64,i128,char,str,
        Self,Option,Some,None,Result,Ok,Err,String,Box,Vec,Rc,Arc,Cell,RefCell,HashMap,BTreeMap,
        macro_rules
    },
    ndkeywordstyle=\color{RedTypename},
    comment=[l][\color{GrayComment}\slshape]{//},
    morecomment=[s][\color{GrayComment}\slshape]{/*}{*/},
    morecomment=[l][\color{GoldDocumentation}\slshape]{///},
    morecomment=[s][\color{GoldDocumentation}\slshape]{/*!}{*/},
    morecomment=[l][\color{GoldDocumentation}\slshape]{//!},
    morecomment=[s][\color{RedTypename}]{\#![}{]},
    morecomment=[s][\color{RedTypename}]{\#[}{]},
    stringstyle=\color{GreenString},
    string=[b]"
    basicstyle=\footnotesize,
    numbers=left,
    stepnumber=1,
    showstringspaces=false,
    tabsize=1,
    breaklines=true,
    breakatwhitespace=false,
}


Update:

Problem has something to do with
\documentclass[a4paper,12pt]{article} copying the code from Floating text around listing works
but replacing \documentclass[letterpaper]{article} with
\documentclass[a4paper,12pt]{article} causes the problem

Entire code:

\documentclass[a4paper,12pt]{article} % was \documentclass[letterpaper]{article}

\usepackage{listings}
\usepackage{blindtext}
\usepackage{wrapfig}

\begin{document}

\begin{wrapfigure}[5]{r}{.6\textwidth}
\begin{lstlisting} 
public void Main (string[] args)
{
   // here goes some code
   // here goes some code
   // here goes some code
   // here goes some code
   // here goes some code
   
}
\end{lstlisting}
\end{wrapfigure}

\blindtext
\end{document}

enter image description here

Best Answer

You used [5] so only 5 lines are cut out but you have 6 lines of code and the caption and a specified top margin, so there is no chance of it fitting, delete [5] and wrapfig will cut as many lines as needed

Related Question