# Splitting .tex file across pages when included as figure

I am trying to include a .tex file in my thesis. The file is some MATLAB code formatted to appear correctly in LaTeX. The compiler throws a warning that the code is too long for the page, but it doesn't automatically split it across multiple pages. The code I am using is:

\begin{figure}

\input{SUB_GRID_FLAG_ML.tex}

\caption{SUB GRID FLAG ML, \hl{See APPENDIX FOR FULL CODE}}

\label{SUB_GRID_FLAG_ML_CODE}

\end{figure}


Is there a way to do this inside my main LaTeX file rather than any included .tex files?

The content of SUB_GRID_FLAG_ML.tex

% This file was automatically created from the m-file
% "m2tex.m" written by USL.
% The fontencoding in this file is UTF-8.
%
% You will need to include the following two packages in
%
% \usepackage{color}
% \usepackage{fancyvrb}
%
% It is advised to use the following option for Inputenc
% \usepackage[utf8]{inputenc}
%

% definition of matlab colors:
\definecolor{mblue}{rgb}{0,0,1}
\definecolor{mgreen}{rgb}{0.13333,0.5451,0.13333}
\definecolor{mred}{rgb}{0.62745,0.12549,0.94118}
\definecolor{mgrey}{rgb}{0.5,0.5,0.5}
\definecolor{mdarkgrey}{rgb}{0.25,0.25,0.25}

\DefineShortVerb[fontfamily=courier,fontseries=m]{\$} \DefineShortVerb[fontfamily=courier,fontseries=b]{\#} \noindent \hspace*{-1.6em}{\scriptsize 1}$  $\color{mgreen}$%Set up a flag to alternate the nodes updated on different time steps.$\color{black}$$\\ \hspace*{-1.6em}{\scriptsize 2} subGridFlag = 0;\\ \hspace*{-1.6em}{\scriptsize 3} \\ \hspace*{-1.6em}{\scriptsize 4} \color{mgreen}%Set up a flag to alternate the nodes updated on different rows.\color{black}$$\\ \hspace*{-1.6em}{\scriptsize 5}$  rowFlag = 0;$\\ \hspace*{-1.6em}{\scriptsize 6}$  $\\ \hspace*{-1.6em}{\scriptsize 7}$  $\color{mblue}$for$\color{black}$ t = 1:Ns$\\ \hspace*{-1.6em}{\scriptsize 8}$      $\color{mgreen}$%% RED NODES$\color{black}$$\\ \hspace*{-1.6em}{\scriptsize 9} if(subGridFlag == 0)\\ \hspace*{-2em}{\scriptsize 10} \color{mblue}for\color{black} l = 2:Nx-2\\ \hspace*{-2em}{\scriptsize 11} \color{mblue}for\color{black} m = 2:Ny-2\\ \hspace*{-2em}{\scriptsize 12} if(rowFlag == 0)\\ \hspace*{-2em}{\scriptsize 13} p(l,m) = (0.5*(p1(l-1,m)+p1(l+1,m)+p1(l,m-1)...\\ \hspace*{-2em}{\scriptsize 14} +p1(l,m+1))-p2(l,m));\\ \hspace*{-2em}{\scriptsize 15} rowFlag = 1;\\ \hspace*{-2em}{\scriptsize 16} \color{mblue}else\color{black}$$\\ \hspace*{-2em}{\scriptsize 17}$                      p(l+1,m) = (0.5*(p1(l,m)+p1(l+2,m)+p1(l+1,m-1)...$\\ \hspace*{-2em}{\scriptsize 18}$                                              +p1(l+1,m+1))-p2(l+1,m));$\\ \hspace*{-2em}{\scriptsize 19}$                      rowFlag = 0;$\\ \hspace*{-2em}{\scriptsize 20}$                  $\color{mblue}$end$\color{black}$$\\ \hspace*{-2em}{\scriptsize 21} \color{mblue}end\color{black}$$\\ \hspace*{-2em}{\scriptsize 22}$              $\\ \hspace*{-2em}{\scriptsize 23}$          $\color{mblue}$end$\color{black}$$\\ \hspace*{-2em}{\scriptsize 24} subGridFlag = 1;\\ \hspace*{-2em}{\scriptsize 25} rowFlag = 0;\\ \hspace*{-2em}{\scriptsize 26} \\ \hspace*{-2em}{\scriptsize 27} \color{mgreen}% Get Output Value\color{black}$$\\ \hspace*{-2em}{\scriptsize 28}$          out(t) = p(Outx,Outy);$\\ \hspace*{-2em}{\scriptsize 29}$          $\color{mgreen}$% Update mesh history$\color{black}$$\\ \hspace*{-2em}{\scriptsize 30} p2 = p1;\\ \hspace*{-2em}{\scriptsize 31} p1 = p;\\ \hspace*{-2em}{\scriptsize 32} \color{mgreen}%% BLUE NODES\color{black}$$\\ \hspace*{-2em}{\scriptsize 33}$      $\color{mblue}$else$\color{black}$$\\ \hspace*{-2em}{\scriptsize 34} \color{mblue}for\color{black} l = 2:Nx-2\\ \hspace*{-2em}{\scriptsize 35} \color{mblue}for\color{black} m = 2:Ny-2\\ \hspace*{-2em}{\scriptsize 36} if(rowFlag == 0)\\ \hspace*{-2em}{\scriptsize 37} p(l,m+1) = (0.5*(p1(l-1,m+1)+p1(l+1,m+1)+p1(l,m)...\\ \hspace*{-2em}{\scriptsize 38} +p1(l,m+2))-p2(l,m+1));\\ \hspace*{-2em}{\scriptsize 39} rowFlag = 1;\\ \hspace*{-2em}{\scriptsize 40} \color{mblue}else\color{black}$$\\ \hspace*{-2em}{\scriptsize 41}$                      p(l+1,m+1) = (0.5*(p1(l,m+1)+p1(l+2,m+1)+p1(l+1,m)...$\\ \hspace*{-2em}{\scriptsize 42}$                                                +p1(l+1,m+2))-p2(l+1,m+1));$\\ \hspace*{-2em}{\scriptsize 43}$                      rowFlag = 0;$\\ \hspace*{-2em}{\scriptsize 44}$                  $\color{mblue}$end$\color{black}$$\\ \hspace*{-2em}{\scriptsize 45} \color{mblue}end\color{black}$$\\ \hspace*{-2em}{\scriptsize 46}$          $\color{mblue}$end$\color{black}$$\\ \hspace*{-2em}{\scriptsize 47} subGridFlag = 0;\\ \hspace*{-2em}{\scriptsize 48} rowFlag = 0;\\ \hspace*{-2em}{\scriptsize 49} \color{mgreen}% Get Output Value\color{black}$$\\ \hspace*{-2em}{\scriptsize 50}$          out(t) = p(Outx,Outy);$\\ \hspace*{-2em}{\scriptsize 51}$          $\color{mgreen}$% Update mesh history$\color{black}$$\\ \hspace*{-2em}{\scriptsize 52} p2 = p1;\\ \hspace*{-2em}{\scriptsize 53} p1 = p;\\ \hspace*{-2em}{\scriptsize 54} \color{mblue}end\color{black}$$\\ \hspace*{-2em}{\scriptsize 55}$  $\color{mblue}$end$\color{black}$$\\ \UndefineShortVerb{\$}
\UndefineShortVerb{\#}


Update: I just had my flushleft in the wrong place and have regained the formatting. Unfortunately LaTeX now splits all code no matter what, even small 10 line chunks are split when called near the bottom of the page, rather than being at the top of the next page, losing much of the layout qualities of the float environment. Are there any other techniques I could try?

-

## migrated from stackoverflow.comMay 31 '12 at 16:11

This question came from our site for professional and enthusiast programmers.

That said, you should edit your question and add the content of SUB_GRID_FLAG_ML.tex. –  Alexis Pigeon May 31 '12 at 14:34
You might be able to do this by redefining the figure environment. See this answer –  Scribblemacher May 31 '12 at 17:58
I gave the advice in that answer a go, but I lost all of my captions through errors and could not find the appropriate way of putting them back. It does however split the code figures across multiple pages where necessary, but it also destroys the formatting by centering every line \begin{flushleft} does not rectify this either. –  Dean8020 Jun 1 '12 at 9:32
Update: I just had my flushleft in the wrong place and have regained the formatting. Unfortunately LaTeX now splits all code no matter what, even small 10 line chunks are split when called near the bottom of the page, rather than being at the top of the next page, losing much of the layout qualities of the float environment. Are there any other techniques I could try? –  Dean8020 Jun 1 '12 at 9:38

Something like this makes TeX try hard not to break between lines but does allow it and allows space to stretch before or after the figure to make it easier not to break. I used a value of 50, using bigger values (up to 10000) make it harder to break lines.

SG.tex is your file but using

\catcode\$\active \def${\verb\$}


instead of MakeShortVerb as I don't know whet package that came from (you did not supply a MWE) and SG2 was the same but cut off at line number 9, to check it didn't break the small example.

The code below has captions at the top, if you want them at the bottom then:

\newcommand\minput[3]{%
\begin{flushleft}%
\filbreak
\interlinepenalty50
\input{#3}%
\par
\nobreak
\captionof{figure}{#1\label{#2}}%
\filbreak
\end{flushleft}}


Original version with caption first:

\documentclass{article}

\usepackage{color}

\usepackage{capt-of}

\newcommand\minput[3]{%
\begin{flushleft}%
\filbreak
\captionof{figure}{#1\label{#2}}%
\par
\nobreak
\interlinepenalty50
\input{#3}%
\filbreak
\end{flushleft}}

\begin{document}

\minput{SUB GRID FLAG ML}{SUB_GRID_FLAG_ML_CODE}{SG.tex}

\minput{SUB GRID FLAG ML 2}{SUB_GRID_FLAG_ML_CODE2}{SG2.tex}

aa\\
aa\\
aa\\
aa\\
aa\\aa\\
aa\\aa\\
aa\\
aa\\
aa

\minput{SUB GRID FLAG ML 3}{SUB_GRID_FLAG_ML_CODE3}{SG2.tex}

\end{document}

-
Thank you @DavidCarlisle, This seems to work how I was hoping LaTeX would anyway. One small thing, I wanted my caption at the bottom of the figure so changed it to: \usepackage{capt-of} \newcommand\minput[3]{% \begin{flushleft}% \filbreak \par \nobreak \interlinepenalty50 \input{#3}% \filbreak \captionof{figure}{#1\label{#2}}% \end{flushleft}} ` As this has moved my caption to the bottom I am working with it, but am I likely to have messed anything else up, I am not 100% sure how it is working anyway. –  Dean8020 Jun 1 '12 at 14:06
no I think you want a nobreak before the caption as otherwise you may get a page break and then a caption o its own on the next page. I'll put it in my answer as code formatting in comments is rubbish –  David Carlisle Jun 1 '12 at 14:23
Thanks again, I had notice that happen actually and was planning to work around it somehow. With the new implementation you provided I just noticed on of my pieces of code has now spanned two pages (about half and half, as expected) but LaTeX has put another float on a separate page between the two halves. Can I stop that? or will I need to be careful with positioning and check everything once It's finalised? –  Dean8020 Jun 1 '12 at 14:53
get donald arseneau's placeins package and put a \floatbarrier (I think it's called) at the start of the definition of \minput then no floats will be allowed to float past it –  David Carlisle Jun 1 '12 at 15:01