atan2のif分岐 を、書こうと思った。
まずは、文字列の準備や、基本的な使い方の確認。
python文字列→tex(pythonのモジュールを、少し編集して、Windowsでも使えるようにする。)→pdf→さらに、imagemagickなどで、pdf→pngファイル等の画像化 の手順で作成する。
“C:\Python27\Lib\site-packages\tex.py”を、
close_fds=True → close_fds=Noneにした。
(これで何か不具合起きたらゴメン。責任はとらない。)
pythonの文字列を用意する例
python文字列は、一行で書くので、(行末尾に\マークで改行できる。)
document = r"\documentclass{article}\pagestyle{empty}\usepackage{amsmath}" + ur"\begin{document}\parindent = 0pt$ \sin\theta$\end{document}"
と書いて、sinθとかになる。
# -*- coding: utf-8 -*-
from tex
import latex2pdf
import subprocess# 上のと同じ文字列。
document = r"\documentclass{article}\pagestyle{empty}\usepackage{amsmath}" + ur"\begin{document}\parindent = 0pt$ \sin\theta$\end{document}"
pdf = latex2pdf(document)#これで、pdfのバイナリデータができる。
# pdf = latex2pdf(unicode(document))#エラー時は、これを試してください。
fileName="tmp.pdf"
a = open(fileName,"wb")#バイナリモードで開く。通常のテキストでは、文字化けする。
a.write(pdf)#バイナリファイルに、バイナリデータを書く。
a.close()
文字列を、for構文やif分岐などで、操作して、数式アニメをするのが、自分の好きなやり方。
(その際は、ファイル名を連番などにする。)
(matplotlibでも、自分のtex環境を使用できるが、なぜかエラー起こるので、使わない。)
ur文字列
ユニコードかつ\→\\に自動変換してくれるur
ユニコードロー文字列を使いたくなる。
上のコードの抜粋の、
pdf = latex2pdf(document)
で、文字コードでエラーが起こったので、そういった工夫をしていた。
latex2pdfは、引数に、ユニコード文字列がいるからだ。
だから、
ur"abcd"
とか、urを文字列の横に書くことになる。
ur文字列の落とし穴
しかし、このやり方だと、下記のコードでエラーが出る。
理由 urでは、\uと表示できないから。(python3ではこうならないらしい)
ur"\u""""SyntaxError: (unicode error) 'rawunicodeescape' codec can't decode bytes in position : truncated \uXXXX"""
ユニコード文字列の落とし穴の回避策
\uが入っている部分だけ、r”\u”+とか分けて書く。分けて書く。
document = r"\documentclass{article}\pagestyle{empty}\usepackage{amsmath}" + ur"\begin{document}\parindent = 0pt$x^n = \begin{cases}1 & (n=0) \\ x \cdot x^{n-1} & (otherwise)\end{cases}$\end{document}"
pdf = latex2pdf(document)全てロー文字列(r”hogehoge\usepackage{hoge}”)のみで書いて、後からunicode。
document = r"\documentclass{article}\pagestyle{empty}\usepackage{amsmath}\begin{document}\parindent = 0pt$x^n = \begin{cases}1 & (n=0) \\ x \cdot x^{n-1} & (otherwise)\end{cases}$\end{document}"
pdf = latex2pdf(unicode(document))
これでできた。
arrayをbeginして、場合分けを書こうとする。
アレイ使用上の注意点
アレイにとって、()は両方とも必要で、カッコがない時は、ピリオドを付けるべきだと。
良し悪し | LaTeXコード | ダメな理由 |
---|---|---|
〇 | $$\left[\begin{array}{lll} a & a & a \\ b & b & b \\ c & c & c \end{array}\right]$$ | |
× | $$\left[\begin{array}{lll} a & a & a \\ b & b & b \\ c & c & c \end{array}\right$$ | 右側のカッコない |
× | $$\left\begin{array}{lll} a & a & a \\ b & b & b \\ c & c & c \end{array}\right]$$ | 左側のカッコない |
〇 | $$\left[\begin{array}{lll} a & a & a \\ b & b & b \\ c & c & c \end{array}\right.$$ | |
〇 | $$\left.\begin{array}{lll} a & a & a \\ b & b & b \\ c & c & c \end{array}\right]$$ |
()、[]や、ピリオドが無いと、
! Missing delimiter (. inserted).
で止まる。
atan2
document = r"\documentclass{article}\pagestyle{empty}\usepackage{amsmath}" + ur"\begin{document}\parindent = 0pt$\rm\tan\theta = Imaginary(\it{z}\rm)/Real(\it{z}\rm) = " + r"\u" + ur"nderline{\tan (\theta + \pi)\ or\ \tan (\theta - \pi)}\\\\ \rm Argument\ \theta (-\frac{1}{2}\pi\leq\theta\leq\frac{1}{2}\pi)\\ \rm Using\ inverse\ trigonometric\ function\\ \rm\theta = arctangent(\tan (\theta ))\\ \rm= \frac{1}{2}\it{i}\rm(log_{\it{e}}\rm(1-\it{i}\rm\tan (\theta ))-log_{\it{e}}\rm(1+\it{i}\tan (\theta )))\\\rm It\ can\ be\ \theta,\ \theta+\pi,\ \theta-\pi\ or\ 0\ \\\\ \rm Argument\ \phi (-\pi\leq\phi\leq\pi)\\ \left. \begin{array}{ll}\rm if\ x>0, & arctangent(\frac{Im(F(k))}{Re(F(k))}) \\\rm if\ x<0\ and\ y\geq0, & arctangent(\frac{Im(F(k))}{Re(F(k))})+\pi \\\rm if\ x<0\ and\ y<0, & arctangent(\frac{Im(F(k))}{Re(F(k))})-\pi \\\rm if\ x=0\ and\ y>0, & +\frac{\pi}{2} \\\rm if\ x=0\ and\ y<0, & -\frac{\pi}{2} \\\rm if\ x=0\ and\ y=0, & Undefined\end{array}\right\} =Argument\phi$\end{document}"
pdf = latex2pdf(document)
fileName="tmp1.pdf"
a = open(fileName,"wb")
a.write(pdf)
a.close()
のソースコードとしては読みにくくても、htmlにコピペして、mathjaxする分には使いやすいという特典付きの、一行LaTeX文字列。英語は、合っているかは知らん。数学も怪しい。
- 中速フーリエ変換 ~離散フーリエ変換より..
- 断面二次モーメントを、座標点の配列から計..
- 断面二次モーメントを、座標点の配列から計..
- fontファイルの文字データ(グリフ)を..
- matplotlibのpyplot.pl..
- 計算力学技術者試験の問題集 自炊(裁断→..
- pythonで、ホワイトノイズやピンクノ..
- 脳ドッグに行ってきた。→MRIの画像デー..
- matplotlibのimshowで円を..
- matplotlibの、cmapを、徐々..
- matplotlibのmake_axes..
- matplotlib floatinga..
- matplotlib plotの色を、値..
- Pythonで、「二次元フーリエ変換した..
- matplotlibのlinestyle..
- どちらが正しいRGBか。(matplot..
- matplotlibのannotateの..
- matplotlibで、x軸とy軸の数字..
- VBAで、pythonのrangeとか、..
- matplotlibのaxes3Dで、a..