如何从 IDLE 物理打印彩色 python 代码? [英] How to physically print python code in color from IDLE?
问题描述
我已经搜索了这个问题的答案,但相关的解决方案似乎涉及解释器中的 'print'
ing.
I have searched for an answer to this but the related solutions seem to concern 'print'
ing in the interpreter.
我想知道是否可以从 IDLE 打印(在纸上物理)彩色 python 代码?
I am wondering if it is possible to print (physically on paper) python code in color from IDLE?
我去了:<代码>文件>在IDLE中打印窗口,它似乎只是打印出黑白版本,而没有提示是否打印彩色等.
I have gone to: File > Print Window
in IDLE and it seems to just print out a black and white version without prompting whether to print in color etc.
似乎这可能不可用,因此选项是将代码复制到像 SciTE 这样的文本编辑器并从那里打印 - 虽然很像默认的 IDLE 语法突出显示.
It seems like this might not be available so the option is to copy code to a text editor like SciTE and print from there - quite like the default IDLE syntax highlighting though.
推荐答案
IDLE 无法做到,但您可以通过间接方式做到:使用 LaTeX 将您的脚本设置为 IDLE 颜色.
IDLE can't do it, but you can do it in an indirect way: Use LaTeX to format your script with the IDLE colors.
列表
包支持 Python 的类似 IDLE 的着色,但它隐藏得很好:您必须知道包含 listings
手册中提到的首选项文件",但没有对其功能的描述.以下几乎最小的示例会将您的脚本 myscript.py
转换为 IDLE 颜色的彩色 PDF.
The listings
package supports IDLE-like colorizing for python, but it's pretty well hidden: You must know to include a "preference file" that is mentioned in the listings
manual, but without a description of its features. The following almost-minimal example will turn your script myscript.py
into color PDF in the IDLE colors.
\documentclass{article}
\usepackage{listings}
\input{listings-python.prf} % defines the python-idle-code style
\usepackage{textcomp} % Needed for straight quotes
\lstset{
basicstyle=\normalsize\ttfamily, % size of the fonts for the code
language={Python},style={python-idle-code},
showstringspaces=false,
tabsize=4,
upquote=true, % Requires textcomp
}
\oddsidemargin=-0.5cm
\textwidth=7in % This just fits 80 characters at 10pt
\begin{document}
\lstinputlisting{myscript.py}
\end{document}
您还可以在真实的 LaTeX 文档中包含代码片段.
You could also include code snippets in a real LaTeX document.
可用性:您至少需要 listings
1.5b 版 (2013/08/26).Texlive 2013 的用户必须从 CTAN 下载它(例如使用命令行 texlive 管理器,tlmgr
).它是 TexLive 2016 的一部分.
Availability: You'll need at least listings
version 1.5b (2013/08/26). Users of Texlive 2013 must download it from CTAN (e.g. with the command line texlive manager, tlmgr
). It is part of TexLive 2016.
不幸的是,CTAN 版本似乎仍然存在问题:首选项文件 listings-python.prf
位于包的文档文件夹中,而不是 tex/latex/listings
TeX 可以在哪里找到它.您需要手动移动它才能包含工作.
Unfortunately there still seems to be a problem with the CTAN version: The preferences file listings-python.prf
is in the documentation folder for the package, not in tex/latex/listings
where TeX can find it. You'll need to move it manually for the inclusion to work.
这篇关于如何从 IDLE 物理打印彩色 python 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!