# Inserted images come before text and code [duplicate]

I am typing up some loose ends in my LaTeX code, and I have come far with help from here. The last things I need help with are some formatting subtleties that I cannot solve.

Basically, I am trying to figure out how to stop my images from coming before my MATLAB code and text, when clearly it is coming after the MATLAB code/text in the .tex file.

\documentclass{article}
\usepackage[margin=0.75in]{geometry}
\usepackage{graphicx}
\usepackage{float}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{listings}
\PassOptionsToPackage{breakindent=0pt}{listings}
\usepackage[useliterate, autolinebreaks, framed]{mcode}
\usepackage{mcode}
\usepackage{flafter}
\makeatletter \def\lst@breakindent{0pt} \makeatother
\makeatletter \def\lst@prebreak{} \makeatother

\begin{document}

\begin{figure}[htpb]
\centering
\begin{subfigure}{.5\textwidth}
\centering
\rule{2cm}{3cm}
\label{fig:sub1}
\end{subfigure}%
\begin{subfigure}{0.5\textwidth}
\centering
%\includegraphics[width=1.0\linewidth]{case2Black.png}
\rule{2cm}{3cm}
\label{fig:sub2}
\end{subfigure}
\caption{A figure with two subfigures}
\label{fig:test}
\end{figure}

This text comes before the MATLAB code.

\begin{lstlisting}

% Scanned Response Function (Spatial Frequency for linear array)

%This function will compute the spatial frequency response of a spatially sampled signal across a linear array. 'snapShots' is a matrix, where each column consists of one snapshot of the spacially sampled field. Now, each column is first shaded with a window, given my params.windowUsed. Then, each column is FFT'd, and fftShifted. Finally, the spatial frequency axis is then computed, based on whether we want the phi, k, u, or theta space.

function [sR, axisUsed, allAxes] = scannedResponse(snapShots, params)

%We perform an element by element multiply of each snapshot with the chosen window.
Xwindowed = params.windowUsed*ones(1,params.N.totalSnapShots) .* snapShots;

\end{lstlisting}

May musical arrival beloved luckily adapted him. Shyness mention married son she his started now. Rose if as past near were. To graceful he elegance oh moderate attended entrance pleasure. Vulgar saw fat sudden edward way played either. Thoughts smallest at or peculiar relation breeding produced an. At depart spirit on stairs. She the either are wisdom praise things she before. Be mother itself vanity favour do me of. Begin sex was power joy after had walls miles.

Up branch to easily missed by do. Admiration considered acceptance too led one melancholy expression. Are will took form the nor true. Winding enjoyed minuter her letters evident use eat colonel. He attacks observe mr cottage inquiry am examine gravity. Are dear but near left was. Year kept on over so as this of. She steepest doubtful betrayed formerly him. Active one called uneasy our seeing see cousin tastes its. Ye am it formed indeed agreed relied piqued.

May musical arrival beloved luckily adapted him. Shyness mention married son she his started now. Rose if as past near were. To graceful he elegance oh moderate attended entrance pleasure. Vulgar saw fat sudden edward way played either. Thoughts smallest at or peculiar relation breeding produced an. At depart spirit on stairs. She the either are wisdom praise things she before. Be mother itself vanity favour do me of. Begin sex was power joy after had walls miles.

Up branch to easily missed by do. Admiration considered acceptance too led one melancholy expression. Are will took form the nor true. Winding enjoyed minuter her letters evident use eat colonel. He attacks observe mr cottage inquiry am examine gravity. Are dear but near left was. Year kept on over so as this of. She steepest doubtful betrayed formerly him. Active one called uneasy our seeing see cousin tastes its. Ye am it formed indeed agreed relied piqued.

May musical arrival beloved luckily adapted him. Shyness mention married son she his started now. Rose if as past near were. To graceful he elegance oh moderate attended entrance pleasure. Vulgar saw fat sudden edward way played either. Thoughts smallest at or peculiar relation breeding produced an. At depart spirit on stairs. She the either are wisdom praise things she before. Be mother itself vanity favour do me of. Begin sex was power joy after had walls miles.

Up branch to easily missed by do. Admiration considered acceptance too led one melancholy expression. Are will took form the nor true. Winding enjoyed minuter her letters evident use eat colonel. He attacks observe mr cottage inquiry am examine gravity. Are dear but near left was. Year kept on over so as this of. She steepest doubtful betrayed formerly him. Active one called uneasy our seeing see cousin tastes its. Ye am it formed indeed agreed relied piqued.

$\theta = 0$

\begin{figure}[htpb]
\centering
\begin{subfigure}{.5\textwidth}
\centering
%\includegraphics[width=1.0\linewidth]{case2Rect.png}
\rule{2cm}{3cm}
\label{fig:sub1}
\end{subfigure}%
\begin{subfigure}{0.5\textwidth}
\centering
%  \includegraphics[width=1.0\linewidth]{case2Black.png}
\rule{2cm}{3cm}
\label{fig:sub2}
\end{subfigure}
\caption{A figure with two subfigures}
\label{fig:test}
\end{figure}

\end{document}


Anyway, what happens is that the images here come before the texts/MATLAB, when in fact I want them to come after the texts/MATLAB.

How to remedy this?

• figure is a float. So it may come at the top of the page. You could do \begin{figure}[hbp] in this case. – Qrrbrbirlbel Oct 1 '13 at 15:31
• @Qrrbrbirlbel How did you fix the formatting on the post here for future reference? – Spacey Oct 1 '13 at 15:31
• You have inserted your code as Blockquote (the “), you need to format your code as Code (the {} or Ctrl + K) button. See also How do I mark code blocks? – Qrrbrbirlbel Oct 1 '13 at 15:32
• @Qrrbrbirlbel Ah thanks! That worked. :-) One last thing, how do stop the MATLAB code rendering from showing all those three dots '...' in the comments part? – Spacey Oct 1 '13 at 15:33
• I don’t know what you are talking about (I commented out (both!) the reference to the mcode because I don’t have that package). Also I don’t know Matlab. The lstlisting environment is also a float, here you need to use the key float, e.g. \begin{lstlisting}[float=b] to put the code at the bottom. Reference: How to influence the position of float environments like figure and table in LaTeX? – Qrrbrbirlbel Oct 1 '13 at 15:36

Adding hbp prevents the float going to the top of any page even if it floats to the next page. What you actually want is to prevent the float floating backwards to the. top of the current page. The flafter package (part of the core latex release) changes the float algorithm so floats always appear after their position in the source.

\usepackage{flafter}

• @TyranaSaur I still can't run the example as I don't have mcode package or the included images, for the MWE you could just use \rule{2cm}{3cm} etc to make a rectangle that has no dependencies. You might want to include h so [htpb] Note if you just use \includegraphics images appear as placed the only reason to use figure is to allow latex to move it: it is a floating environment. – David Carlisle Oct 1 '13 at 16:17
• @TyranaSaur If I ignore the errors from not having mcode then I see the second float floats to the second page and is a p float. (which is vertically centred by default) it can not be t as there is no text yet on that page. If you want page floats top aligned use \makeatletter\setlength\@fptop{0pt}\makeatother – David Carlisle Oct 1 '13 at 16:44