1

According to Richard Johnson's Elements of MATLAB Style paragraph 6, it is a best practice to keep content to within 80 characters. Although matlab-prettifier wraps lines nicely, in my opinion, wrapping should not be necessary if the included .m-file adheres to this best-practice line length. Is it possible to rescale the matlab-prettifier environment, such that the first 80 characters will not wrap?

A minimum example (with default wide margins) is shown below:

codeExample.m

%% This is a comment spanning 80 characters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
L = +(1/3)*sqrt((2*sqrt(2)*sqrt(G^4*(2*R^2-5*R+2))+G^2*(4-5*R))/(R));

texExample.tex

\documentclass[a4paper]{article}
\usepackage{inputenc}
\usepackage[numbered,]{matlab-prettifier}
\usepackage{geometry}
\begin{document}
\lstinputlisting[style=Matlab-editor]{codeExample.m}
\end{document}

Result Unnecessary line wrapping

3
  • Welcome to TeX.SX! Not an answer but some hints: Your margins are to big. To visualize them use \usepackage{showframe}. Try \usepackage{geometry} \geometry{ a4paper, total={170mm,257mm}, left=20mm, top=20mm, }
    – Bobyandbob
    Commented Oct 27, 2017 at 9:41
  • You can change the basicstyle to use a smaller font (see section 8.2 of the matlab-prettifier manual). But this would not improve readability … Commented Oct 27, 2017 at 9:55
  • I updated the question to \usepackage{geometry} for more realistic margins. Thanks for the suggestions - adapting margins for certain pages, or manually adapting font size to the current margins does the trick for now, but a solution though would rescale the environment regardless of current margin settings, might still be desirable.
    – ktolbol
    Commented Oct 28, 2017 at 11:15

1 Answer 1

0

Rescaling can be done with the resizebox command from the graphicx package. Syntax: \resizebox{width}{height}{content}, with size value ! indicating proportional scaling. Note that this scales down, but also up, so short lines will appear bigger. You can also make a resizebox that scales only down and not up, cf. https://tex.stackexchange.com/a/327889. Another possibility is to just switch off line breaking, which may look fine with 80 columns.

MWE:

\documentclass[a4paper]{article}
\usepackage[numbered,]{matlab-prettifier}
\usepackage{geometry}
\usepackage{graphicx}
\begin{document}
\noindent default:
\lstinputlisting[style=Matlab-editor]{codeExample.m}
no linebreaks:
\lstinputlisting[style=Matlab-editor,breaklines=false]{codeExample.m}
resizebox:\\
\resizebox{\textwidth}{!}{\lstinputlisting[style=Matlab-editor,breaklines=false]{codeExample.m}}

\vspace{1em}\noindent
resizebox short lines:\\
\resizebox{\textwidth}{!}{\lstinputlisting[style=Matlab-editor,breaklines=false]{codeExampleShort.m}}
conditional resizebox:\\ % from https://tex.stackexchange.com/a/327889
\resizebox{%
      \ifdim\width>\textwidth
        \textwidth
      \else
        \width
      \fi
    }{!}{\lstinputlisting[style=Matlab-editor,breaklines=false]{codeExampleShort.m}}
\end{document}

Result:

enter image description here

2
  • The conditional resize box exactly solves the issue described by the MWE! Very nice! However, whereas the MWE contained only 2 lines of code, using a resize-box, the code can no longer span several pages. I suppose it may not be possible to have both, though.
    – ktolbol
    Commented Oct 30, 2017 at 9:13
  • @ktolbol you can try to split the source code into different parts (see tex.stackexchange.com/a/34324) and display each part on a page, combined with packages like forloop or pgffor, check automatically the number of lines in a file (e.g. tex.stackexchange.com/a/2377), etc. Depending on the application you may be able to do many things automatically. But it will not be easy :D
    – Marijn
    Commented Oct 30, 2017 at 10:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .