2 回答

TA贡献1772条经验 获得超8个赞
有一种方法可以通过一个名为listings的包向 python 添加代码。这个包允许显示代码甚至完整的文件。
为了实现这一点,conf.py您可以添加:
latex_elements = {
"papersize": "letterpaper",
"pointsize": "10pt",
"figure_align": "htbp",
"preamble": r"""
\usepackage{listings}
\lstset{
language=Python, % the language of the code
title=\lstname % show the filename of files included with \lstinputlisting; also try caption instead of title
}
""",
}
还有更多设置可以添加到lstset.
然后可以将代码添加到.rst文件中:
.. raw:: latex
\section{Code}
\lstinputlisting{../../../path/to/my/file.py}

TA贡献1828条经验 获得超4个赞
添加回答
举报