1

I need to put my code for a software I have written in my thesis appendix. There's about 4000 lines of code across multiple files, so I'd like to have 100 lines of code per page.

Using these settings, I get about 30 lines and the comments wrap which takes up extra space (even though in my matlab code I already wrapped comments).

%matlab stuff
\usepackage[framed, numbered]{matlab-prettifier}
\usepackage{listings, color}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
    backgroundcolor=\color{white}, 
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\fontsize{6}{5}\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\fontsize{6}{5},
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2   
}
\lstset{style=mystyle}

and in the appendix:

\lstinputlisting[style = Matlab-editor]{GINA_Analyser.m}

enter image description here

Using resizebox, I end up with the whole code attempting to fit on one page, like this. This code is within the appendix section:

\resizebox{\textwidth}{!}{\lstinputlisting[style = Matlab-editor]{FileValidator.m}}

enter image description here

Is there something I can do?

  • Have you tried to further scale the fontsize down? This might prevent the code from wrapping. – Tim Hilt Jan 14 at 11:54
  • Hi Tim. I have. For some reason, regardless of how I scale the code in the header section for lstdefinestyle, the font size doesn't actually change. – Corinne Jan 14 at 15:57
  • If you aren't obliged to print the code, you could attach it to the pdf using attachfile2 or embedall. This has the advantage of promoting reproducibility and saving trees. – Ross Jan 14 at 16:48
  • Hi Ross, thanks for the suggestion. My supervisor has asked that I have it included in the thesis unfortunately! Saving trees is part of the reason I want to make the text as small as possible while keeping it legible. – Corinne Jan 14 at 16:51
0

I used Latin Modern TT Light Condensed for my code listings - it's narrower than normal TT fonts for the same font size so you'll get more characters per line, and fewer lines wrapping. I also reduced the line spacing just a little using setspace, which gave me a bit of trouble (the link is to my question about that).

Unfortunately the biggest gain may be from manual effort: Reworking the line-wrapping in the comments, for example. Your lines 1-9 actually take up 17 lines; they should go down to about 12 if you don't break each one twice. You can also reduce the tab stops to perhaps 2 spaces, and remove the indent on block comments so more characters fit on the line. Even adjusting whitespace in the code itself is an option if your individual lines tend to be long; your example shows generous use of space.

With a few lines wrapping on each page, I got to ~62 lines/page. P.186 of my thesis (Dropbox) is a good example.

Your Answer

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

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