亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將源代碼添加到使用 sphinx 生成的 LaTeX 文檔中

如何將源代碼添加到使用 sphinx 生成的 LaTeX 文檔中

HUH函數 2021-11-16 14:52:55
我正在用 Sphinx 生成我的 python 庫的文檔。我使用擴展sphinx.ext.viewcodeextensions = [    "sphinx.ext.autodoc",    "sphinx.ext.doctest",    "sphinx.ext.intersphinx",    "sphinx.ext.ifconfig",    "sphinx.ext.viewcode",  # Add links to highlighted source code    "sphinx.ext.napoleon",  # to render Google format docstrings]這會生成一個指向源文檔的鏈接,類似于:class MyClass(param1, param2)[source]像這張圖片:如果我按源代碼,我會在 HTML 頁面中正確地看到源代碼。我想在生成 LaTeX 文件時做完全相同的事情。當您從 LaTeX 創建 pdf 時,我在文檔中找不到如何添加源代碼。任何提示?
查看完整描述

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}


查看完整回答
反對 回復 2021-11-16
?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

根據文檔sphinx.ext.viewcode

此擴展僅適用于與 HTML 相關的構建器,例如html、applehelp、devhelp、 等htmlhelp,qthelpsinglehtml.


查看完整回答
反對 回復 2021-11-16
  • 2 回答
  • 0 關注
  • 264 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號