1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PythonでLaTeX文書とPDFを作成する

Posted at

はじめに

PyLaTeXというライブラリを使うことで、PythonプログラムからLaTeXおよびPDFを作成できます.

方法

以下のサンプルプログラムがこの記事の全てなので,ご覧いただくと早いと思います.

プログラム

# インストール
%pip install pylatex
!apt-get update
!apt-get install latexmk
!apt-get install -y texlive-lang-japanese

# ホームディレクトリのパスを取得
home_dir = os.path.expanduser("~")
latexmkrc_path = os.path.join(home_dir, ".latexmkrc")

# latexmkrcファイルの中身の定義
content = '''$latex = 'platex -synctex=1 -halt-on-error -interaction=nonstopmode -file-line-error %O %S';
$bibtex = 'pbibtex %O %S';
$biber = 'biber --bblencoding=utf8 -u -U --output_safechars %O %S';
$makeindex = 'mendex %O -o %D %S';
$dvipdf = 'dvipdfmx %O -o %D %S';

$max_repeat = 5;
$pdf_mode = 3;'''

# latexmkrcファイルを作成して内容を書き込む
try:
    with open(latexmkrc_path, "w") as file:
        file.write(content)
    print(f".latexmkrc file has been created successfully at {latexmkrc_path}")
except IOError as e:
    print(f"An error occurred while creating the file: {e}")

# PyLaTeXによりTexおよびPDFの作成
from pylatex import (
    Document,
    Section
)

doc = Document()
with doc.create(Section("はじめに")):
    doc.append("このファイルは,Google Colabから作成されたものです.")

doc.generate_pdf("sample", compiler="latexmk") # PDFファイルの作成

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?