无法正确放置 lstinputlisting

无法正确放置 lstinputlisting

我在文档中有此代码:

\begin{figure}
    \center
    \includegraphics[scale=0.5]{images/image.jpg}
    \caption{image}
\end{figure}

The content of file.sh :
\begin{figure}
    \lstinputlisting[language=Bash,caption={mycaption}]{file.sh}
\end{figure}
Blablabla

源代码放在正文后面啦啦啦并且位于一页上。此文本包含经典文本,一个部分。它应该放在图像之后。此外,源代码在页面末尾被截断,并且缺少一半代码……这是日志:

<images/image.jpg, id=122, 442.65375pt x 492.84125pt>
File: images/image.jpg Graphic file (type jpg)
<use images/image.jpg>
Package pdftex.def Info: images/image.jpg used on input line 292.
(pdftex.def) Requested size: 221.32632pt x 246.42001pt.
(/usr/share/texlive/texmf-dist/tex/latex/listings/lstlang1.sty
File: lstlang1.sty 2015/06/04 1.6 listings language file
)
(/usr/share/texlive/texmf-dist/tex/latex/listings/lstlang1.sty
File: lstlang1.sty 2015/06/04 1.6 listings language file
)
Package hyperref Info: bookmark level for unknown lstlisting defaults to 0 on i
nput line 298.
LaTeX Font Info: Try loading font information for T1+cmtt on input line 298.
(/usr/share/texlive/texmf-dist/tex/latex/base/t1cmtt.fd
File: t1cmtt.fd 2014/09/29 v2.5h Standard LaTeX font definitions
)
(./file.sh)
LaTeX Warning: Float too large for page by 705.87885pt on input line 299.
[9]

我快要疯了。希望你能帮助我。

答案1

如果 LaTeX 应该保持严格的顺序 image-listing-Blablabla,则不要使用任何figure环境。您包装到图形环境中的所有内容都会有一定的倾向,移动到 TeX 认为最适合的位置;此外,figure环境的内容不会跨页面中断。

在此处输入图片描述

\documentclass{article}
\usepackage{caption}% For the \captionof command, to be used outside of figures
\usepackage{graphicx}
\usepackage{listings}
\begin{document}
\begin{center}
    \includegraphics[scale=0.5]{example-image}
    \captionof{figure}{image}
\end{center}

The content of file.sh :
    \lstinputlisting[language=Bash,caption={mycaption}]{file.sh}
Blablabla
\end{document}

相关内容