Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

marked as duplicate by Martin Schröder, Kurt, Stefan Kottwitz Oct 2 '13 at 9:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
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? –  TyranaSaur 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? –  TyranaSaur Oct 1 '13 at 15:33
1  
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

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
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? –  TyranaSaur Oct 1 '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 1 '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. –  TyranaSaur Oct 1 '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). –  TyranaSaur Oct 1 '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 1 '13 at 16:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.