[PYTHON] How to output a document in pdf format with Sphinx

Introduction

With the Python document creation tool "Sphinx", you can output in pdf format in addition to html. This entry describes the procedure to output in pdf format.

Explain in this entry

--How to output a document in pdf format with Sphinx on macOS

Not explained in this entry

--How to install Sphinx * Since the introduction method was introduced in the past entry, please see here. --How to output on other than macOS --Internal mechanism until Sphinx outputs in pdf format

Premise

--Installing python3 system --Sphinx 1.5 or above must be installed --You have the latest homebrew installed - When executing sphinx-quickstart of Sphinx, separate "source" directory and "build" directory </ font>

Reference (sphinx-If you answer the question asked when executing quickstart as follows, the directory will be separated. )



#English edition
$ sphinx-quickstart
> Separate source and build directories (y/N) [n]:y

#Japanese version
$ sphinx-quickstart
>Separate source directory and build directory (y/ n) [n]:y

Writer's environment

  • macOS Catalina 10.15.3
  • Python 3.7.4
  • Sphinx 2.4.4
  • Homebrew 2.2.11

Reference: Python version confirmation procedure

$ python -V
Python 3.7.4

Reference: Sphinx version confirmation procedure

$ pip show sphinx
Name: Sphinx
Version: 2.4.4
(The following is omitted)

Reference: Homebrew version confirmation procedure

$ brew --version
Homebrew 2.2.11
(The following is omitted)

Flow until output in PDF format

  1. Build a TeX environment on macOS
  2. Rewrite conf.py so that it can be output in Japanese
  3. Run make latexpdf

Build a TeX environment on your mac

Sphinx can output reStructuredText in pdf format via a format called TeX (LaTeX to be exact).

  • Tex is pronounced "tefu".
  • This entry does not explain TeX and the detailed conversion mechanism.

Type the following command to build a TeX environment on macOS.

"Break cask install mactex-no-gui" takes a lot of time. By the way, it took about 6 hours in my environment.

$ brew cask install mactex-no-gui

The next "sudo tlmgr update --self --all" Depending on your environment, you may get the error "sudo tlmgr command not found". In that case, close the terminal, reopen it, and then try typing the command.

By the way, this command also takes time. It took about 2 hours in my environment.

$ sudo tlmgr update --self --all

Rewrite conf.py so that it can be output in Japanese

Open conf.py under the "source" directory, change / add as follows, and save.

#language part'ja'Change to
language = 'ja'

#Add the following line to the end of the file
latex_docclass = {'manual': 'jsbook'}
  • It seems that there are various other specifications in latex_docclass, but we are currently investigating.

Run make latexpdf

If you execute the following command in the directory where the Makefile is located, pdf will be created in "build / latex".

$ make latefpdf

References

-Create PDF file with Sphinx

Recommended Posts

How to output a document in pdf format with Sphinx
How to put a hyperlink to "file: // hogehoge" with sphinx-> pdf
PDF output with Latex extension in Sphinx
How to write a docstring to create a named tuple document with sphinx
How to write a named tuple document in 2020
How to convert / restore a string with [] in python
[Introduction to Python] How to output a character string in a Print statement
How to create a heatmap with an arbitrary domain in Python
[ROS2] How to play a bag file with python format launch
How to build a sphinx translation environment
How to add a package with PyCharm
How to work with BigQuery in Python
How to get a stacktrace in python
How to convert DateTimeField format in Django
[Small story] How to save matplotlib graphs in a batch with Jupyter
[Introduction to Python] How to write a character string with the format function
How to format a list of dictionaries (or instances) well in Python
How to study until a beginner in statistics gets started with Bayesian statistics
How to deal with memory leaks in matplotlib.pyplot
How to read a CSV file with Python 2/3
How to send a message to LINE with curl
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to draw a 2-axis graph with pyplot
How to embed a variable in a python string
How to develop a cart app with Django
How to create a JSON file in Python
How to make a dictionary with a hierarchical structure.
How to deal with run-time errors in subprocess.call
How to implement a gradient picker in Houdini
How to notify a Discord channel in Python
How to use tkinter with python in pyenv
How to create a multi-platform app with kivy
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
How to create a Rest Api in Django
How to count numbers in a specific range
How to read a file in a different directory
How to Mock a Public function in Pytest
To output a value even in the middle of a cell with Jupyter Notebook
How to drop Google Docs in one folder in a .txt file with python
How to count the number of elements in Django and output to a template
How to get a list of files in the same directory with python
Output PDF with Django
Output PDF with WeasyPrint
How to specify a schema in Django's database settings
[Python] How to draw a line graph with Matplotlib
I want to transition with a button in flask
Explain in detail how to make sounds with python
How to create a submenu with the [Blender] plugin
How to get a logged-in user with Django's forms.py
How to convert a class object to a dictionary with SQLAlchemy
How to deal with pyenv initialization failure in fish 3.1.0
How to do zero-padding in one line with OpenCV
How to run tests in bulk with Python unittest
[Python] How to expand variables in a character string
How to make a shooting game with toio (Part 1)
A memorandum on how to use keras.preprocessing.image in Keras
How to load files in Google Drive with Google Colaboratory
Convert the image in .zip to PDF with Python
How to access with cache when reading_json in pandas