0

The listings package font is somehow different from the rest of the document. I want to see exactly same font.

E.g. in the example below, I want to see both "Hello World"s to look exactly same (except the indentation).

\documentclass[]{article}
\usepackage{listings}
\begin{document}

Hello

World

\begin{lstlisting}
Hello
World
\end{lstlisting}

\end{document}

Below is when I zoom in using an image program. Left is the usual article text, right is the listing. I can see following differences: 1- listing characters have one more pixels between them. 2- Somehow they have subtle differences, e.g. look to the bottom of "d", or top of "W".

comparing fonts

  • please read its package document – Mogic Mar 22 '18 at 8:50
1

It's not the font that's different, it's the default letter spacing. With no extra options, the font is treated as a monospaced font, i.e. each letter gets the same width. As the letters in most standard fonts have different widths, monospacing them doesn't look good. But it is useful in listings.

The option 'flexiblecolumns' will treat the font as proportional (i.e. use the design width of each character as defined in the font).

The extra indents in the first "Hello world" are due to your paragraph settings -- specifically the value of parindent. You can force 'listings' to add extra margin space as well with the 'xleftmargin' and 'xrightmargin' options.

See the listings manual on how to use those options (there are several ways, which to use depends on what exactly you need to do).

And if you want to change the actual font family used by listings, have a look at this question.

  • The code looks better now, thanks, this is a very nice package. – an apprentice Mar 22 '18 at 9:47

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.