1

I have this code in a document :

\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

The source code is placed after the text Blablabla and on a single page. This text containt a classic text, a section. It should be placed just after the image. Morever, the source code is cut at end of page and it's missing the half of code... This are the logs :

<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]

I am going crazy. Hope you can help me.

naphaneal
  • 2,614
Anonymous
  • 113
  • 4
    Welcome to TeX.SX! Don't go crazy, it isn't worth it. Why do you wrap the listing in a figure environment? It tells TeX that it is allowed to place it wherever there is enough space. Moreover, figures are not broken across pages. – gernot May 01 '17 at 13:33
  • For the cut off lines, maybe try breaklines=true – samcarter_is_at_topanswers.xyz May 01 '17 at 13:35
  • see this for further reference on how to use the listing-package: https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings as an alternative, you can also use the minted-package – naphaneal May 02 '17 at 13:15
  • @gernot, I place it into a wrapper because if I don't do that, the picture is over the text. – Anonymous May 02 '17 at 20:24
  • What do you mean by over the text? Do you mean that the order in which they appear in the PDF is different from that in your code? If yes, then that is because figure is a floating environment, meaning it can move around to get better page breaks. See https://tex.stackexchange.com/questions/2275 and https://tex.stackexchange.com/questions/8625 for some suggestions on how to influence that. – Torbjørn T. May 08 '17 at 07:37

1 Answers1

1

If LaTeX is supposed to keep the strict order image-listing-Blablabla, don't use any figure environments. Everything that you wrap into a figure environment will have a certain inclination to move around to where TeX thinks it fits best; moreover, the contents of a figure environement does not break across pages.

enter image description here

\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}
gernot
  • 49,614
  • Thanks, It solved my problem ! Do you think if I use this mean to include images I will never again be bothered with this kind of problem ? I started to use LaTeX to focus me on the content of my document but finnally, I am often bothered with this kind of problem. – Anonymous May 08 '17 at 20:03
  • @Anonymous I don't know with which problems you are confronted. But I'm sure that all (well most) problems can be solved when using LaTeX correctly. – gernot May 08 '17 at 20:42