I have a java lstlisting and it is too big and does not mach a single page.

Is there any way to specify font for specific listing, not all listings in document?


You can set the option directly with the lstlisting environment option basicstyle

    basicstyle=\tiny, %or \small or \footnotesize etc.
int isJava = 1;

See also this answer

  • 3
    I really like this approach, but one needs to remember that other attributes of basicstyle (such as the font colour), will be lost when the size is changed this way. – user4417 Nov 22 '18 at 11:40
  • 1
    Actually, the colours seem to stay the same, but one needs to specify \ttfamily as well so as not to lose monospace font. – oarfish Jan 7 '19 at 14:19

The best way to do this is by using the lstdefinestyle functionality which is built into the listings package. Encapsulate your style as follows (I've used a crummy MATLAB style definition as an example):


Then, use it like so:

% A "large" listing


% A "tiny" listing


The results of the two different lstlisting's will be different sizes, as set by your basicstyle command.

  • 4
    Be careful with \lstset, though; its effects are global. – jub0bs May 25 '14 at 8:42

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.