0

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?

Spacey
  • 1,159

1 Answers1

4

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.

adding

\usepackage{flafter}

Should fix things.

David Carlisle
  • 757,742
  • Thanks David, so I removed the [hbp], and just added the usepackage, however this simply makes my code/text appear on the top, and then the set of images appear below it. I simply want to images to appear as they are ordered, one set before, then text, then one set after. (I edited the post btw). Right now adding this package doesnt seem to fix it. Perhaps I have misunderstood something you have said? – Spacey Oct 01 '13 at 16:07
  • @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 01 '13 at 16:17
  • David, your solution works if it was just figure->text->figure. Then everything is fine. But when I use MATLAB code, etc, then it doesnt seem to work. – Spacey Oct 01 '13 at 16:31
  • David, please bear with me as I am quite new to LaTeX. :) Yes, basically what is now happening, is that the last figure, simply doesnt go 'to the top' of the second page. I am wondering how this might be remedied in this context. Thanks! :) (PS I edited the post with new code). – Spacey Oct 01 '13 at 16:37
  • @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 01 '13 at 16:44
  • Beautiful, that seems to have done the trick! :-) So, what exactly do that [htpb] or [hpb] etc do? – Spacey Oct 01 '13 at 16:48
  • @TyranaSaur http://tex.stackexchange.com/questions/8652/what-does-t-and-ht-mean/8654#8654 – David Carlisle Oct 01 '13 at 16:51