[Tex/LaTex] Adjust matlab-prettifier to page width

listingsmatlab-prettifier

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

Best Answer

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

Related Question