I really like the pxfont package for displaying code in listings. I would usually just do this to load pxfont for the entire document; \usepackage{pxfonts}.

However, this time I am required to use a template with mandatory fonts for the text. I would still like to use pxfonts for the code listings, but can not set it globally, as it would overwrite the mandatory non-listings font settings.

Is there any way to isolate the usage of pxfonts to listings only? I have tried various combinations of \newcommand and \renewcommand to 'capture' the font settings, reset them to default, and use the captured version to set the listings' basicstyle. Sadly, I lack the experience and I didn't get anything working.

Any ideas?

  • 1
    It should be possible, but you should add, how the listings should be printed: only in set in \ttfamily or in the default way listings works? Side remark: Very, very recently a new package newpx was published “meant to be a replacement for Young Ryu’s pxfonts”.
    – Speravir
    Apr 9, 2013 at 0:24
  • 2
    If you mean the typewriter font coming with pxfonts, it's a matter of saying \renewcommand{\ttdefault}{pxtt} in your preamble.
    – egreg
    Apr 9, 2013 at 0:32
  • @agreg thanks for the suggestion! I didn't know I could do that without including the pxfonts package. It's not perfect, as it's still a document-wide substitution, but it's certainly a solution for the document I'm working on now. Thanks!
    – peanutman
    Apr 9, 2013 at 8:50
  • @peanutman It's certainly possible to "localize" that effect, but you should be more explicit about your needs.
    – egreg
    Apr 9, 2013 at 9:43
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers.
    – jub0bs
    Mar 2, 2014 at 14:36

1 Answer 1


If all you need of PXfonts is the typewriter type font, then


in the preamble will tell LaTeX to use it instead of the default.

If you need the font only inside lstlisting environments, the easiest solution is to say


You must log in to answer this question.

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