安装sphinx
pip install sphinx
安装texlive
在清华大学开源软件镜像站下载镜像文件:https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/Images/texlive2018-20180414.iso
创建sphinx项目
在项目文件夹中shift+鼠标右键,打开命令窗口。
输入创建项目命令,并且选择项目配置,大部分直接回车选择默认配置即可。
G:\test>sphinx-quickstart
> Separate source and build directories (y/n) [n]:
> Name prefix for templates and static dir [_]:
> Project name: pdftest # 输入项目名称
> Author name(s): 123 # 作者
> Project release []:
> Project language [en]:
> Source file suffix [.rst]:
> Name of your master document (without suffix) [index]:
> autodoc: automatically insert docstrings from modules (y/n) [n]: y # 重要,输入y
> doctest: automatically test code snippets in doctest blocks (y/n) [n]:
> intersphinx: link between Sphinx documentation of different projects (y/n) [n]:
> todo: write "todo" entries that can be shown or hidden on build (y/n) [n]:
> coverage: checks for documentation coverage (y/n) [n]:
> imgmath: include math, rendered as PNG or SVG images (y/n) [n]:
> mathjax: include math, rendered in the browser by MathJax (y/n) [n]:
> ifconfig: conditional inclusion of content based on config values (y/n) [n]:
> viewcode: include links to the source code of documented Python objects (y/n) [n]: y # 重要,输入y
> githubpages: create .nojekyll file to publish the document on GitHub pages (y/n) [n]:
> Create Makefile? (y/n) [y]:
> Create Windows command file? (y/n) [y]:
项目配置
修改conf.py文件
-
要使用latex生成pdf,修改latex_elements选项。
latex_elements = { # The paper size ('letterpaper' or 'a4paper'). # 'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). # 'pointsize': '10pt', # Additional stuff for the LaTeX preamble. 'preamble': ''' \\hypersetup{unicode=true} \\usepackage{CJKutf8} \\AtBeginDocument{\\begin{CJK}{UTF8}{gbsn}} \\AtEndDocument{\\end{CJK}} ''', # Latex figure (float) alignment # 'figure_align': 'htbp', }
-
修改主题样式
修改sphinx_rtd_theme主题要先在文件头部添加:import sphinx_rtd_theme
再设置html_theme选项:
html_theme = 'sphinx_rtd_theme'
生成pdf
写好rst文档后,添加到index.rst。
然后在项目根目录打开命令窗口,输入命令make latexpdf,之后在_build文件夹中找到生成的pdf文档。
参考
https://blog.csdn.net/suzyu12345/article/details/52923464
https://www.cnblogs.com/zzqcn/p/5096876.html#_label6