0

There are many questions (and answers) on this site about packages for showing listings of source code. But all the answers I have found are at least five years old, and I was wondering if any newer packages have been developed that are in some way or other superior to the older ones like "listings"?

Ideally, I'd like to have listings with the following characteristics ...

  1. Typeset in a fixed-width font, preferably a fairly narrow one that doesn't take up too much space.
  2. Enclosed in a box with a nice border, maybe even with a drop shadow
  3. A colored background.
  4. Syntax-driven color coding for languages like C/C++/C#.
  5. Some control over where page breaks occur, maybe.

Some of these are just icing on the cake that I could live without.

So, any new-and-improved solutions today in 2016? If the answer is "no", that will still be helpful, because it tells me that I can stop looking.

10
  • See CTAN (or here) for many different packages.
    – jon
    Feb 20, 2016 at 3:27
  • 8
    Uhmm... here is a list of packages that do most of what you're after and there "last updated" time stamps: listings, 2015/06/04; minted, 2015/09/09; tcolorbox, 2015/11/27 (via tcblisting)... All of these have been updated in the last year, let alone the last 5 years. Where do you get your information from?
    – Werner
    Feb 20, 2016 at 3:37
  • 2
    Don't they pretty much all use fixed-width font? I can imagine using non-fixed typewriter font, just about. But that's hardly likely to be default. And which typewriter font is used is most likely a question of \ttfamily's default, which will not be determined by the package responsible for formatting the source code (generally) but by your document-wide font configuration.
    – cfr
    Feb 20, 2016 at 3:48
  • 5
    The standard approach to using a package to do something is to read its documentation about how to do that thing. If the package is newer than the question you have in mind, surely that suggests start with the documentation, then look at the question, and then (finally) ask a question if the first two steps do not provide enough help. Any thing else is bound to lead to 'cargo cult programming', which is usually ill-advised.
    – jon
    Feb 20, 2016 at 4:17
  • 1
    I see. I would've thought the questions you refer to were about 'how to do X?', not 'which package should I use?' (which are usually closed here as they are considered to solicit only open-ended, opinion-based answers). My advice about 'blindly follow the advice' is: don't do it, even if it is from last month. (But don't take my word for it!)
    – jon
    Feb 20, 2016 at 6:25

1 Answer 1

1

The various comments gave me the impression that it wasn't worth looking for anything new, so I just used the time-honoured solution, which is the listings package. I was able to produce the following result without too much effort:

enter image description here

It has a nice colored box with a drop shadow, a narrow font (squished Consolas, courtesy of fontspec FakeStretch), and C# syntax highlighting. Good.

The relevant part of the setup code is:

\usepackage{unicode-math}
\usepackage{color}
\usepackage{listings}

\setmonofont[Scale=MatchLowercase, FakeStretch = 0.9]{Consolas}

\definecolor{bluekeywords}{rgb}{0.13,0.13,1}
\definecolor{greencomments}{rgb}{0,0.5,0}
\definecolor{redstrings}{rgb}{0.9,0,0}
\definecolor{codebackground}{rgb}{1,1,0.85}
\definecolor{codeframecolor}{rgb}{0.65,0.65,0.65}
\definecolor{shadowcolor}{rgb}{0.85,0.85,0.85}

\lstset{language=[Sharp]C,
    showspaces=false,
    showtabs=false,
    lineskip = -0.5pt,
    xleftmargin = 1pt,
    framesep = 5pt,
    frame = shadowbox,
    framerule = 1pt,
    backgroundcolor=\color{codebackground},
    rulecolor = \color{codeframecolor}, 
    rulesepcolor = \color{shadowcolor},
    rulesep = 3pt,
    aboveskip = 12pt,
    belowskip = 4pt,
    commentstyle=\itshape\color{greencomments},
    keywordstyle=\color{bluekeywords}\bfseries,
    stringstyle=\color{redstrings},
    basicstyle=\ttfamily
}

The minted and fancyvbr packages seem to be newer, and were not mentioned in the older answers I found. But newer isn't necessarily better, and listings seems to do what I want, so I stopped looking.

2
  • Er... you don't think including the code would make this a more useful answer, then?
    – cfr
    Feb 20, 2016 at 23:18
  • Nobody seemed very interested (except possibly you). Anyway, I added the code.
    – bubba
    Feb 21, 2016 at 11:24

You must log in to answer this question.

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