7

I am trying to insert some piece of code into my document, using the \lstinputlisting command. However the fonts displayed do not look too good. Is it possible to change the font size and style?

Martin Scharrer
  • 262,582

2 Answers2

11

You can use the optional argument to provide any listings options:

\lstinputlisting[basicstyle=\ttfamily\scriptsize,language=somelang]{filename}

For multiple listings you can set this settings globally (or locally to the current group) using \lstset{<options>}. See the listings manual for more details. https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings

Y00
  • 103
Martin Scharrer
  • 262,582
5

\lstinputlisting obeys to the same options that are set with \lstset for the environment form. So something like

\lstset{basicstyle=\ttfamily,columns=fullflexible}

should do.

egreg
  • 1,121,712
  • What does columns=fullflexible do? Could we also set its text width for new line? – alper Oct 02 '22 at 14:32
  • 1
    @alper Basically, it sets every letter in a box at its natural width. – egreg Oct 02 '22 at 14:38
  • Is it a good idea to reduce box's width? I have a long sentences where it breaks into two due to a single character at the end. I have tried to reduce the space in between using basewidth=0.5em since I was not able to use \! inside the listing text – alper Oct 02 '22 at 14:46
  • @alper Long sentence in a lstlisting? – egreg Oct 02 '22 at 14:48
  • yes sir, its actually a code piece like shown here https://tex.stackexchange.com/q/643425/127048 // but even there is empty space at the end of some lines , it is breaked into two. I can ask a new question related to this to not make noise in the comment section – alper Oct 02 '22 at 14:58
  • @alper We need details such as the line width and many others. Better showing the real case in a new question. – egreg Oct 02 '22 at 15:00
  • https://tex.stackexchange.com/questions/660295/how-can-i-have-most-compact-code-inside-a-lstlisting-frame thanks – alper Oct 02 '22 at 16:28