使用语法突出显示打印代码?

发布于 2024-08-09 19:24:10 字数 1539 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

安人多梦 2024-08-16 19:24:10

你可以使用 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.

錯遇了你 2024-08-16 19:24:10

是的,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.

娇柔作态 2024-08-16 19:24:10

新答案:

使用 TextMate。它自动打印彩色代码。没有设置啊只需打印即可。如果以前或较新的版本无法做到这一点,我正在使用 TextMate 版本 2.0.23

旧答案,以及针对没有 Mac 的人的答案:

使用 vim。根据我的经验,这是迄今为止最简单的方法,也就是说,一旦你知道如何做。

顺便说一句,Vim 预装在 Mac 上。我知道你们这些特殊的人是如何喜欢颜色的,所以为了所有人的利益,我将让这个不耐烦的商人免受攻击。

1.) 打开文件

vim 文件名.m

2.) 启用语法着色(我的默认没有启用)

:语法

3.) print

:hardcopy

Vim 会在不询问你的情况下选择你系统的默认打印机,所以一定要先设置它。

4.) 退出程序(这实际上不是给定的)

:q

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
标点 2024-08-16 19:24:10

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.

偏闹i 2024-08-16 19:24:10

Visual Studio 将允许您拥有完全独立的打印配置。

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

送你一个梦 2024-08-16 19:24:10

我最近比较了已经提到的两种解决方案:vim & pygments。它们都给出了很好的结果,但是您可以如何快速实际使用它们:

  • pygments 不提供直接导出为 PDF 的功能。因此,我发现的最简单的解决方案是导出为 HTML,然后使用 wkhtmltopdf。您可以使用以下 bash 脚本组合这两个操作:
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"
}
  • 对于 vim,就像这样简单:TERM=xterm-256color vim '+hardcopy >out.ps' +q code.src
    我发现 $TERM 环境变量会影响输出颜色,所以我更喜欢显式设置它。
    最后,您可能需要稍微调整一下 .vimrc
set printfont=:h9  
set printoptions=number:y,left:5pc  

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  
疯了 2024-08-16 19:24:10

名为 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

变身佩奇 2024-08-16 19:24:10

在 unix 下你可能想尝试 a2ps。它很灵活并且可以产生很好的结果。

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

鸩远一方 2024-08-16 19:24:10

我不久前创建了一个家庭 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.

风尘浪孓 2024-08-16 19:24:10

如果您在使用 Visual Studio 2012 时遇到突出显示打印和处理所描述问题的问题:

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!
以酷 2024-08-16 19:24:10

Bash Shell 的解决方案

  1. 如果您使用的是 UBUNTU,请将此行添加到 ~/.bashrc
    或者,如果您使用的是 MAC,则为 ~/.bash_profile
    如果该文件不存在,请创建它。

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

  2. source ~/.bashrcsource ~/. bash_profile

  3. 要打印彩色 hello.py 只需执行以下操作:
    lprc hello.py 而不是 lpr hello.py

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

风吹雪碎 2024-08-16 19:24:10

使用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.

泅人 2024-08-16 19:24:10

下载 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!

帅冕 2024-08-16 19:24:10

如果您必须使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文