Printing code with syntax highlighting? [closed]
Asked Answered
D

14

33

I have occasion to need to print code (Horrors!!! ;) ), and i was wondering what editor or tool would i use to print that code out with proper formatting and syntax highlighting? If it's important, it will be c# code but ideally the tool will work for as many languages as possible. Can Notepad++ or something handle this?

Darwindarwinian answered 1/11, 2009 at 11:19 Comment(1)
This superuser question has several suggestions, including one for Notepad++: superuser.com/questions/39571/…Embellish
A
15

Yes, Notepad++ can certainly print code with syntax highlighting.

Colour printing would obviously be preferable, but on the occasions when I've printed in black and white, the subtle differences in colour [rendered as shades of grey, of course] can be difficult to distinguish.

However, I think a little customisation of the colour schemes should make this less of a problem.

Aerograph answered 1/11, 2009 at 11:32 Comment(0)
P
41

You can use Vim! It's probably installed already if you're on modern Linux/MacOS and an easy install if not.

:syntax will turn syntax highlighting on and :hardcopy will print it. There's syntax highlighting definitions for many languages out there. The default look is usually optimised for screen display, but you can fix that.

Simply open the file on command line with vim <filename>, type :syntax on<ENTER>, then :hardcopy<ENTER> to print it. Quit Vim with :q!<ENTER>.

There's also the :TOhtml command which will open the current selection as HTML in a new Vim window. Capture the entire document with :%y<ENTER> followed by :TOhtml<ENTER> to open it.

Pruitt answered 1/11, 2009 at 11:30 Comment(4)
@Darwindarwinian Don't say sorry--we're sorry for you!Camarillo
You could also issue :TOhtml command which converts source code into HTML file with colors and everything.Edva
I prefer to preview the results before printing. You can print to a .ps file via :hardcopy > %.ps If you want a pdf file instead, use ps2pdf: hardcopy > %.ps | !ps2pdf %.ps && rm %.ps To edit the font: set printfont=Courier:h8Otway
to add line numbers in the printed files run set printoptions=number:yBop
A
15

Yes, Notepad++ can certainly print code with syntax highlighting.

Colour printing would obviously be preferable, but on the occasions when I've printed in black and white, the subtle differences in colour [rendered as shades of grey, of course] can be difficult to distinguish.

However, I think a little customisation of the colour schemes should make this less of a problem.

Aerograph answered 1/11, 2009 at 11:32 Comment(0)
V
15

New Answer:

Use TextMate. It prints colored code automatically. There's no setup. Just print. In case previous or newer versions can't do this I'm using TextMate version 2.0.23

Old Answer, and the answer for people who don't have Macs:

Use vim. Its the easiest method to do it in my experience by far, that is, once you know how.

Vim comes pre-installed on Macs, btw. And I know how you special people like colors, so I'm going to make this impatientbusinessman-proof for the benefit of all.

1.) open file

vim filename.m

2.) enable syntax coloration (mine did not have enabled by default)

:syntax on

3.) print

:hardcopy

Vim will choose your system's default printer without asking you so make sure you set that up first.

4.) exit the program (this is actually not a given)

:q
Vaal answered 23/2, 2014 at 21:30 Comment(0)
F
8

http://pygments.org/ is one option. It supports a ton of languages, and since it's written as a python library, you can script the conversion process however you want.

Frame answered 1/11, 2009 at 11:24 Comment(0)
H
3

Visual Studio will, and allows you have a completely separate configuration for printing.

Hector answered 1/11, 2009 at 12:3 Comment(2)
Where? I can only print in B/WBatts
I cannot recall, but based on the age of the answer I would have been referring to Visual Studio 2008 which had native support for colour printing. VS2010 and VS2012 can use the Color Printing[sic] extension. There does not seem to be an equivalent for VS2013.Hector
B
3

I recently compared the 2 solutions already mentioned : vim & pygments. They both give great results, but there is how you can practically use them quickly:

  • pygments does not provide direct export to PDF. Hence, the simplest solution I found was to export to HTML and then use wkhtmltopdf. You can combine both operations using the following bash script:
src2pdf () {
    local noext="${1%.*}"
    pygmentize -O full -o "$noext.html" "$1"
    # enabling line wrapping in <pre> blocks
    perl -i -wpe '/<style.*>$/&&($_.="pre{white-space:pre-wrap;}\n")' "$noext.html"
    wkhtmltopdf "$noext.html" "$noext.pdf"
    rm "$noext.html"
}
  • for vim, it's as simple as this: TERM=xterm-256color vim '+hardcopy >out.ps' +q code.src I found out that the $TERM environment variable can affect the output colors, so I prefer to set it explicitly. And finally, you may need to tweak your .vimrc a little:
set printfont=:h9  
set printoptions=number:y,left:5pc  
Beller answered 26/2, 2015 at 11:6 Comment(0)
T
3

The tool called enscript is very much the tool for doing this. It is very powerful, is not tied to an editor nor a language and you can create PDF's with syntax highlighting.

The documentation pretty much says it all.

enscript man page

Trisa answered 17/4, 2015 at 3:8 Comment(0)
B
1

Under unix you might want to try a2ps. It is flexible and produces nice results.

Blague answered 1/11, 2009 at 14:16 Comment(0)
R
1

I while ago I created a household python script that wraps pygments into a small console utility. It works with any language supported by pygments.

Also if you happen to use eclipse, you could simply copy the selected text in the editor and then paste it in a RTF-aware editor like MS Word - it will preserve all the colors, fonts and formatting.

Rematch answered 1/2, 2011 at 14:20 Comment(1)
haven't tested it, sounds nice, hope it works ok :) however, in case of more batch-oriented printing (for larger number of files) I'm still interested in some HTML-to-printer tool that could be used as a companion?Upend
A
1

If you have problems with Visual Studio 2012 concerning the highlighted printing an handeling the described problem:

  • Download and install this Power Tool which implements the color printing, besides some other features and bug fixes. Works for me!
Archivolt answered 25/2, 2014 at 19:1 Comment(0)
E
1

Solution For Bash Shell

  1. Add this line to ~/.bashrc if you are using UBUNTU
    or, to ~/.bash_profile if you are using MAC
    If that file does not exists, create it.

    alias lprc='vim -me -c ":syntax on" -c ":hardcopy" -c ":q"'

  2. source ~/.bashrc or source ~/.bash_profile

  3. To print colored hello.py just do this:
    lprc hello.py instead of lpr hello.py

Entertain answered 17/1, 2017 at 17:5 Comment(0)
T
1

Use ConTEXT programming editor (which is free). I am using it for both generating .pdf-s with syntax highlighted source code and printing to paper.

There are many ConTEXT syntax highlihting definitions to download and you can make your own highligher file which will BTW be highlighted using the ConTEXT Highlighter Files highlighter definition.

Tallith answered 12/8, 2020 at 4:40 Comment(0)
B
0

I do it downloading js and css files from

https://prismjs.com/

There are so many 5-7 options to select the theme and language highlighter. Once you select a theme and download the tiny js/css files the next thing you need to do is rename the code file to html, and call the css/js files. Open the html in a browser and print it. Done!

Bahamas answered 2/1, 2021 at 19:22 Comment(0)
B
0

You can also use this in case you've to print with bnw syntax highlighting https://github.com/SGI-CAPP-AT2/code-highlight-n-print

Barman answered 24/11, 2022 at 0:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.