For code listings, I often change the listings parameter basicstyle to adjust the font size so that listing fit within the container it is in.

I mainly use a cell in a table to display small listing in. But the container could be a minipage or the whole page, or anything else.

So in a table cell, I use \linewidth or it could be some percentage of the textwidth.

I also know the number of characters of the widest (longest) line of the listing. I wrote a small lua function which finds this value.

I just do not know how to use the above 2 pieces of information, in order to map it to the correct font size to tell listing to use such that it fits.

Assuming for now that 11pt class is used. But it will be nice if this would work for arbitrary value and also any font family that happens to be used.

Here is a MWE showing an example listing, which I want to show in a table. I know the space needed, which is \linewidth, and I calculate that the longest line is 56 characters for this example, and I know I am using 11 pt.

What should the font size be then so that the listing would fit in? For example, when using \small it does not fit

Mathematica graphics

Code for the above is (MWE)


function getMaxLineWidth()
local maxLineLength = 0;

   local r    
   for line in io.lines("code.m") do
       r = string.len(line)
       if r>maxLineLength then
\newcommand{\getMaxLineWidth}{\directlua{ getMaxLineWidth() }}


clear all; 
syms s t
f = (s^4+5*s^3+6*s^2+9*s+30)/(s^4+6*s^3+21*s^2+46*s+30);

space available for the listing is     \the\linewidth and the 
The maximum line width is \getMaxLineWidth \ then
what should be the font size below so it will fit?



By trial and error scriptsize is found to work:

Mathematica graphics

And this manual adjustment is what I am trying to avoid. I think knowing the space available in pt and knowing the number of characters, and also the font size used for the document, it should be possible to calculate what font size is needed to pass to the listing command so this is all automated?

I am also not sure if one can specify an arbitrary font size value, or is one limited to the values given by \normalsize,\small, etc... ?

Can this be automated?

For reference, thanks for answer what-point-pt-font-size-are-large-etc

Mathematica graphics

  • Suppose you have an extremely long line by accident and the calculation you are asking for returns \fontsize{0.6pt}{.07pt}\selectfont ... Do you think the result will be useful? ;-)
    – Johannes_B
    Jun 27, 2016 at 7:33
  • That's what breaklines of listings and minted is for ... Additional to the example of Johannes_B: Imagine that you have many listings with different line lengths. This would result in many different font sizes for your listings. And that is what I would call bad typography ;) Jun 27, 2016 at 7:41

1 Answer 1


The idea for this comes from David Carlisle in chat. Basically, you put the listing in a box that will be scaled down if wider than some width. The adjustbox package provides an easy interface for doing just this.

Note however, as Ulrike Fischer mentions in chat, that this may well end up looking horrible (such as my example below), but if looks are irrelevant, then by all means.


\begin{adjustbox}{max width=\textwidth}
x = a + b + c + d + e + f + g + h + i + j + k + l + m +n + o + p + q + r + s + t + u + v + x + y

\begin{adjustbox}{max width=\textwidth}
x = a + b + c + d + e + f
  • 2
    i'd recommend always using \small for listing material to try to cut down on the number of instances that need to be adjusted; this will make the output appear much more uniform than it would if a majority of instances are adjusted. unless the main text is already very small, the smaller listing inserts make them easily recognizable, and not that hard to read with a monospace font. Jun 27, 2016 at 15:58

You must log in to answer this question.

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