0

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

  • No :( I used this as a template and added only "language=rust" which causes this problem – JustAHamster Jan 21 '22 at 16:03
  • Please write a minimal working example. See link for more details what it means. – user202729 Jan 21 '22 at 16:06
  • I updated my question - below is a working example reproducing the problem – JustAHamster Jan 21 '22 at 16:17
  • 1
    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 – David Carlisle Jan 21 '22 at 16:17
  • Wow that works!!! Thank you. Now my code line numbers are too close to the text. How can I fix this? – JustAHamster Jan 21 '22 at 16:21
  • Well it's strongly recommended to read the documentation of wrapfig to figure out what [5]{r}{.6\textwidth} does before asking though. My guess is that the last argument is the amount to shift in. By the way use @ to notify people. – user202729 Jan 21 '22 at 16:27
  • you also presumably did not test your updated code but replacing \documentclass[letterpaper]{article} with \documentclass[a4paper,12pt]{article} causes the problem implies that the document posted would work with the other document class, which is clearly not the case, the fact that you have npre than 5 lines to insert does not depend on the documentclass. – David Carlisle Jan 21 '22 at 16:38

1 Answers1

3

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

David Carlisle
  • 757,742