We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(14)
你可以使用 Vim!如果您使用的是现代 Linux/MacOS,那么它可能已经安装,如果没有,则安装也很简单。
:syntax
将打开语法突出显示,:hardcopy
将打印它。许多语言都有语法高亮定义。默认外观通常针对屏幕显示进行了优化,但您可以修复 那个。只需使用
vim
在命令行上打开文件,输入:syntax on
,然后输入:hardcopy
即可打印它。使用:q!
退出 Vim。还有
:TOhtml
命令,它将在新的 Vim 窗口中以 HTML 形式打开当前选择。使用:%y
捕获整个文档,然后使用:TOhtml
将其打开。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.是的,Notepad++ 当然可以打印带有语法高亮的代码。
彩色打印显然更可取,但在我以黑白打印的情况下,颜色的细微差别(当然,呈现为灰色阴影)可能很难区分。
然而,我认为对配色方案进行一些定制应该可以减少这个问题。
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.
新答案:
使用 TextMate。它自动打印彩色代码。没有设置啊只需打印即可。如果以前或较新的版本无法做到这一点,我正在使用 TextMate 版本 2.0.23
旧答案,以及针对没有 Mac 的人的答案:
使用 vim。根据我的经验,这是迄今为止最简单的方法,也就是说,一旦你知道如何做。
顺便说一句,Vim 预装在 Mac 上。我知道你们这些特殊的人是如何喜欢颜色的,所以为了所有人的利益,我将让这个不耐烦的商人免受攻击。
1.) 打开文件
2.) 启用语法着色(我的默认没有启用)
3.) print
Vim 会在不询问你的情况下选择你系统的默认打印机,所以一定要先设置它。
4.) 退出程序(这实际上不是给定的)
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
2.) enable syntax coloration (mine did not have enabled by default)
3.) print
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)
http://pygments.org/ 是一种选择。它支持多种语言,并且由于它是作为 Python 库编写的,因此您可以根据需要编写转换过程的脚本。
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.
Visual Studio 将允许您拥有完全独立的打印配置。
Visual Studio will, and allows you have a completely separate configuration for printing.
我最近比较了已经提到的两种解决方案:
vim
&pygments
。它们都给出了很好的结果,但是您可以如何快速实际使用它们:pygments
不提供直接导出为 PDF 的功能。因此,我发现的最简单的解决方案是导出为 HTML,然后使用wkhtmltopdf
。您可以使用以下 bash 脚本组合这两个操作:vim
,就像这样简单:TERM=xterm-256color vim '+hardcopy >out.ps' +q code.src
我发现
$TERM
环境变量会影响输出颜色,所以我更喜欢显式设置它。最后,您可能需要稍微调整一下 .vimrc:
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 usewkhtmltopdf
. You can combine both operations using the following bash script: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:
名为
enscript
的工具非常适合执行此操作。它非常强大,不依赖于编辑器或语言,您可以创建带有语法突出显示的 PDF。文档几乎说明了一切。
enscript 手册页
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
在 unix 下你可能想尝试 a2ps。它很灵活并且可以产生很好的结果。
Under unix you might want to try a2ps. It is flexible and produces nice results.
我不久前创建了一个家庭 python 脚本,将 pygments 包装到一个小型控制台实用程序中。它适用于 pygments 支持的任何语言。
另外,如果您碰巧使用 Eclipse,您可以简单地在编辑器中复制选定的文本,然后将其粘贴到支持 RTF 的编辑器(如 MS Word)中 - 它将保留所有颜色、字体和格式。
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.
如果您在使用 Visual Studio 2012 时遇到突出显示打印和处理所描述问题的问题:
打印,除了一些其他功能和错误修复。对我有用!
If you have problems with Visual Studio 2012 concerning the highlighted printing an handeling the described problem:
printing, besides some other features and bug fixes. Works for me!
Bash Shell 的解决方案
如果您使用的是 UBUNTU,请将此行添加到
~/.bashrc
或者,如果您使用的是 MAC,则为
~/.bash_profile
如果该文件不存在,请创建它。
alias lprc='vim -me -c ":syntax on" -c ":hardcopy" -c ":q"'
source ~/.bashrc
或source ~/. bash_profile
要打印彩色 hello.py 只需执行以下操作:
lprc hello.py
而不是lpr hello.py
Solution For Bash Shell
Add this line to
~/.bashrc
if you are using UBUNTUor, to
~/.bash_profile
if you are using MACIf that file does not exists, create it.
alias lprc='vim -me -c ":syntax on" -c ":hardcopy" -c ":q"'
source ~/.bashrc
orsource ~/.bash_profile
To print colored hello.py just do this:
lprc hello.py
instead oflpr hello.py
使用ConTEXT编程编辑器(免费)。我用它来生成带有语法突出显示的源代码的 .pdf-s 并打印到纸张上。
有许多ConTEXT语法高亮定义可供下载,您可以制作自己的高亮文件,顺便说一句使用 ConTEXT 荧光笔文件荧光笔 定义突出显示。
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.
下载 js 和 css 文件,
我从https://prismjs.com/
有很多 5-7 个选项选择主题和语言荧光笔。选择主题并下载微小的 js/css 文件后,您需要做的下一件事是将代码文件重命名为 html,并调用 css/js 文件。在浏览器中打开 html 并打印它。完毕!
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!
如果您必须使用 bnw 语法突出显示进行打印 https://github.com/SGI-CAPP-AT2/code-highlight-n-print
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