Skip to content

Instantly share code, notes, and snippets.

@hamukichi
Created February 16, 2015 12:05
Show Gist options
  • Save hamukichi/d057440e9d9a10d9c362 to your computer and use it in GitHub Desktop.
Save hamukichi/d057440e9d9a10d9c362 to your computer and use it in GitHub Desktop.
PythonTeXを用いて、図の作成と文書への埋め込みを一度に行う例。とりあえず動作を確認した程度。
\documentclass[a4j]{jsarticle}
% Unicode文字を使用できるようにする
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
% 数式の記述に用いる
\usepackage{amsmath}
% 図の埋め込みに用いる
\usepackage[dvipdfmx]{graphicx}
% PythonTeXを利用する
\usepackage{pythontex}
\title{Python\TeX を用いた図の作成と埋め込み}
\author{はむ吉(のんびり)}
\date{\today}
\begin{document}
\maketitle
図\ref{fig:graph}は関数$f(x)=|x|/x$とその$n=5$までのFourier級数展開を図示したものである。
\begin{sympycode}
#!/usr/bin/env python
# -*- coding: utf-8 -*-
filename = "graph.pdf"
l = [-mpmath.pi, mpmath.pi] # 定義域
n = 5 # 項の数
f0 = lambda x: abs(x)/x # もとの関数
cs = mpmath.fourier(f0, l, n) # Fourier級数の係数
f1 = lambda x: mpmath.fourierval(cs, l, x) # f0のFourier級数展開
mpmath.plot([f0, f1], xlim=l, file=filename) # グラフを出力
# 出力したグラフを文書に埋め込む
output = r"""\begin{figure}[htbp]
\centering
\includegraphics[clip, width=10.0cm]{./[FILENAME]}
\caption{Fourier級数展開の例}
\label{fig:graph}
\end{figure}""".replace("[FILENAME]", filename)
print(output)
\end{sympycode}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment