图片破列

图片破列

当使用 lstlisting 在页面上放置长代码时,它会中断,这很正常。当在浮动图形环境中放置图片时,它会进入列表的两个部分之间,这显然不是应该的。很抱歉无法更好地解释我的问题;因此,这里有一张图片:

Picture for better explanation

与文档不同部分相关的 MWE,但结果相同:

\documentclass[11pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[german]{babel}

\usepackage{graphicx}
\usepackage{listings}


\begin{document}
\section*{B.3}
\begin{lstlisting}[language=Java, caption=B.3a)]
    float s = 1.8f;
    int tx = 20;
    int ty = 50;
    int r = 50;

    public void setup() {
        size(500, 500);
        background(255, 255, 0); // aus draw zu setup

    } // setup

    public void draw() {
        scale(s);
        translate(tx, ty);
        rotate(radians(r));

        stroke(0, 0, 255);
        strokeWeight(4);
        fill(0, 255, 0);
        triangle(5, 20, 55, 3, 105, 20);
        fill(255, 0, 0);
        rect(25, 20, 60, 60);

    } // draw
\end{lstlisting}

\begin{figure}[htb]
\caption{B.3a)} 
\includegraphics[scale=0.7]{Screenshots/B3a.PNG}
\end{figure}

\begin{lstlisting}[language=Java, caption=B.3b)]
    float s = 1.8f;
    int tx = 20;
    int ty = 50;
    int r = 50;

      public void setup() {
        size(500,500);
        background(255,255,0); //aus draw zu setup


      } //setup


      public void draw() {
          translate(tx,ty);
          scale(s);
          rotate(radians(r));

          stroke(0,0,255);
          strokeWeight(4);
          fill(0,255,0);
          triangle(5,20,55,3,105,20);
          fill(255,0,0);
          rect(25,20,60,60);



    } //draw  
\end{lstlisting}
\end{document}

我已经尝试改变图形的参数(使用 h、htbp 或 !h)但没有帮助。

所以我的问题是:我必须做哪些改变才能将列表放在一个块中 - 分页符是可以的 - 并且不会有图像干扰?

答案1

B3a.PNG我的硬盘上没有,但是使用\rule{1cm}{10cm}占位符,可以通过将 a)figure环境第二个lstlisting环境 b) 大卫·卡莱尔的“魔法代码” ( \global\csname @topnum\endcsname 0)之间 lstlistingfigure

\documentclass[11pt]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[german]{babel}
% \usepackage{graphicx}
\usepackage{listings}

\begin{document}

\section*{B.3}

\begin{lstlisting}[language=Java, caption=B.3a)]
    float s = 1.8f;
    int tx = 20;
    int ty = 50;
    int r = 50;

    public void setup() {
        size(500, 500);
        background(255, 255, 0); // aus draw zu setup

    } // setup

    public void draw() {
        scale(s);
        translate(tx, ty);
        rotate(radians(r));

        stroke(0, 0, 255);
        strokeWeight(4);
        fill(0, 255, 0);
        triangle(5, 20, 55, 3, 105, 20);
        fill(255, 0, 0);
        rect(25, 20, 60, 60);

    } // draw
\end{lstlisting}

\begin{lstlisting}[language=Java, caption=B.3b)]
    float s = 1.8f;
    int tx = 20;
    int ty = 50;
    int r = 50;

      public void setup() {
        size(500,500);
        background(255,255,0); //aus draw zu setup


      } //setup


      public void draw() {
          translate(tx,ty);
          scale(s);
          rotate(radians(r));

          stroke(0,0,255);
          strokeWeight(4);
          fill(0,255,0);
          triangle(5,20,55,3,105,20);
          fill(255,0,0);
          rect(25,20,60,60);



    } //draw  
\end{lstlisting}

\global\csname @topnum\endcsname 0

\begin{figure}[htb]
\caption{B.3a)} 
\rule{1cm}{10cm}% placeholder for graphic
\end{figure}

\end{document}

相关内容