1

I have several sections in my article, each with a sizable (~22 lines) code listing explaining the concept of that specific section.

I do want to float my listings, as they look nasty if broken off to another page. However, LaTeX has decided that I can only fit one code listing to a page when I float. By increasing the top and bottom margins I should be able to fit at least two floats per page, but I obviously only want to change the margins of the page holding the floats. How can I do this?

EDIT: My (reworked) MWE:

% !TeX spellcheck = af_ZA
\documentclass[]{article}

\usepackage{listings}
\usepackage[font=bf,skip=\baselineskip]{caption}
\usepackage{xcolor}
\begin{document}

%--------------------------------------
% All document settings
\definecolor{code_grey}{gray}{0.9}
\lstset
{
    language=c++,
    frame=single,
    backgroundcolor=\color{code_grey},
    numbers=left,   
    breaklines=true,
    tabsize=1,
    basicstyle=\ttfamily,
    columns=flexible,
    captionpos=b,   
    xleftmargin=-.15\textwidth,
    xrightmargin=-.15\textwidth,
    float,
}
\renewcommand{\lstlistingname}{\textbf{Kodelys}}
\captionsetup[lstlisting]{font={footnotesize, it}}
\renewcommand{\figurename}{\textbf{Figuur}}
\captionsetup[figure]{font={footnotesize, it}}

\section{Die verskillende soorte plagiaat in programkode}
Some paragraph

\lstinputlisting
[
    label=plag_original,
    linerange=1-22,
    caption=The first listing,
    float=p
]{code.cpp}

\subsection{Woord-vir-woord kopie}
Another paragraph

\subsection{Kodekommentaar verandering}
The last paragraph

\lstinputlisting
[
    label=plag_method2,
    linerange=1-22,
    caption=The second listing - it must preferably share the same page with the first listing!,
    float=p
]
{code.cpp}

\end{document}

And code.cpp - I include this because many of the solutions I've tried does not work for \lstinoutlisting

// Original
void BubbleSort(vector<int> &num)
{
    int i, j, flag = 1;// set flag to 1 to start first pass
    int temp;// holding variable
    int numLength = num.length( ); 
    for(i = 1; (i <= numLength) && flag; i++)
    {
        flag = 0;
        for (j=0; j < (numLength -1); j++)
        {
            if (num[j+1] > num[j])// ascending order simply changes to <
            {
                temp = num[j];// swap elements
                num[j] = num[j+1];
                num[j+1] = temp;
                flag = 1;// indicates that a swap occurred.
            }
        }
    }
    return;//arrays are passed to functions by address; nothing is returned
}
  • Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – jub0bs Oct 28 '14 at 13:15
  • latex will put more than one listing per page if the float parameters set for your document allow that (and if there are no intervening \clearpage but without a working example it's impossible to say which parameter is causing them to float to different pages. – David Carlisle Oct 28 '14 at 14:18
  • See also tex.stackexchange.com/questions/111390/… – John Kormylo Oct 28 '14 at 14:29
  • 1
    Done - this should work as-is and it reproduces the results I do not want – getack Oct 28 '14 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.