Skip to content

Instantly share code, notes, and snippets.

@hamukichi
Created August 10, 2014 13:58
Show Gist options
  • Save hamukichi/c5c9db5efa142f7e31cf to your computer and use it in GitHub Desktop.
Save hamukichi/c5c9db5efa142f7e31cf to your computer and use it in GitHub Desktop.
PythonTeXの動作を確認するために、簡単な文書を書いた。
\documentclass[a4j]{jsarticle}
% Unicode文字を使用できるようにする
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
% 数式の記述に用いる
\usepackage{amsmath}
% PythonTeXを利用する
\usepackage[]{pythontex}
% Pythonの関数をLaTeXのコマンドで呼び出せるようにする
% 関数は以下で定義
\newcommand{\iseven}[1]{\py{iseven(#1)}}
\title{Python \TeX のテスト}
\author{はむ吉(のんびり)}
\date{\today}
\begin{document}
\maketitle
\section{Hello, Python \TeX !}
\begin{pycode}
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
print(r"Hello, Python \TeX !")
print(ur"こんにちは、Python \TeX !" + "\n")
print(u"Pythonのバージョンは{0}です。".format(sys.version))
\end{pycode}
\section{Python側で定義した関数を\LaTeX 側から使う}
\begin{pycode}
def iseven(n):
if type(n) is not int:
return u"{0}は整数ではありません。".format(n)
elif n % 2 == 0:
return u"{0}は偶数です。".format(n)
elif n % 2 == 1:
return u"{0}は奇数です。".format(n)
\end{pycode}
\begin{itemize}
\item \iseven{1024}
\item \iseven{59049}
\item \iseven{8.314}
\end{itemize}
\section{SymPyを利用する}
\begin{sympycode}
x = symbols("x")
func = x**4 + x**3 + x**2 + x + 1
print(r"\begin{align}")
for n in range(1, 5):
deriv = Derivative(func, x, n)
print("{0} &= {1}".format(latex(deriv), latex(deriv.doit())))
if n < 4:
print(r"\\")
print(r"\end{align}")
\end{sympycode}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment