Nov 30, 2014

latex minted package "pygmentize not installed" and "undefined control sequence \PYGzus etc" error messages solution

之前minted用的都沒事,後來不知是否因為換了電腦,同樣的tex檔在另一台電腦compile後出現"pygmentize not installed"的錯誤。因為我python pygmentize與minted都有安裝好,查了一下發現可能是檔案path中有空格的緣故,因此minted在測試pygmentize套件階段時無法通過,算是minted的一個小問題。因此我把測試pygmentize的那一個指令comment取消掉,反正我知道我一定有裝。找到minted.sty檔,應該在你的latex安裝資料夾中,並且找到以下的程式碼,將AtEndPreamble裡的測試指令取消執行。這樣就可以順利compile並且順利排版。
\AtEndPreamble{%
% \ifthenelse{\boolean{minted@draft}}{}{%
% \TestAppExists{pygmentize}%
% \ifAppExists\else
% \PackageError{minted}%
% {You must have `pygmentize' installed
% to use this package}%
% {Refer to the installation instructions in the minted
% documentation for more information.}%
% \fi
% }%
}
另外一個問題是我也遇到undefined control sequence \PYGzus 或\PYG加各式符號 etc等等的一堆錯誤指令。查了一下發現要移除minted所產生的暫存資料夾
"_minted-yourlatexfilename",移除之後就可以順利compile了。

pretty code generation

\documentclass{article}
\usepackage[inner=1in,outer=1in,top=1in,bottom=1in]{geometry}
\usepackage{minted}
\usepackage{mdframed}
\definecolor{bg}{rgb}{0.95,0.95,0.95}
\begin{document}

\begin{mdframed}[leftline=false, rightline=false,backgroundcolor=bg]
\inputminted[linenos,fontsize=\footnotesize,gobble=0,breaklines,breakanywhere]{python}{thermal_ions.py}

\end{mdframed}
\end{document}



延伸閱讀:
http://whymranderson.blogspot.tw/2014/03/4th-runge-kutta.html

Oct 30, 2014

陀螺儀教學文件的文書排版製作

關於陀螺教學文件的製作:

此文件的排版及其內容及圖片的製作幾乎都是用免費的開源軟體所完成。唯一使用的付費軟體為Scientific Workplace,我因其TEX的快速指令鍵而受惠,因此使用它來編輯tex文件。由於有些軟體公司在學校便宜的大量提供校園版權而進而讓學生產生對付費軟體的過度依賴,這似乎會導致在畢業以後學生會因為此依賴而對原本不需要的東西不自知產生需要,造成畢業學生不自知的付出過多的隱藏成本,因此這邊也做了一個示範。這邊用到的免費開源軟體皆為許多人多年的心血結晶,因此我也特別在此一一列出所用到的軟體。

1. 由於需要排出中英混雜,並且帶有許多數學公式的文件,而Scientific Workplace無法達到我的排版要求,這裡用上排版規則最為完整的排版引擎Latex,完整的說是TexLive 2013中的XeLatex排版引擎。其中也非常感謝套件xeCJK,其中文排版能力非常專業。
2. 所有向量圖片皆由開源軟體Python Matplotlib與其pgf套件,PSTricks與其套件pst-node所製作,這些軟件製作向量圖形都非常成熟。
3. 模擬程式是由開源軟體Python及其3D繪圖程式套件mplot3D所製作完成。Python軟體的強大已不用我在此贅述。
4. latex的minted套件,非常方便的就可以給出漂亮專業的程式碼區塊。
5. Sphinx,自動擷取程式說明行並自動排版成漂亮的說明書。我用它來作陀螺儀程式的說明書文件。這樣我就不用每次改了程式就要去改說明書,now sphinx will take care of that for me!

如果沒有這些軟體作者無私的貢獻,沒有這些強大的免費套件軟體,我根本無法完成此文件的排版,說不定還是幾張亂糟糟的計算紙。並且,我認為正確美觀的排版在知識的傳承與創意的迸發上有其重要的角色,這也是為什麼我願意花了近半年的時間來學習這些軟體並且應用它們。

這邊提到的開源軟體雖然強大,不過免費的畢竟會有其不方便的地方,我接下來就列舉我所遇到需要克服的困難,以及如何解決他們。
1. 使用Scientific Workplace 5.0編輯中文文件,但使用Xelatex來做排版。由於Scientific Workplace產生的中文文件使用其特殊的unicode編碼,因此要讓Xelatex讀到正確的中文字,要做個簡單的編碼轉換,我在我的blog中這篇文章有詳述如何轉換。我是寫了一個Python小程序來達成這個動作。這裡我學習到了電腦文字unicode與程式語言中的regular expression。
  • (2016/六月: 由於此份文件已經開始越來越大,目前已近70頁,若每次更改後要看結果都要全部編譯後才看的到那會很麻煩會等很久,因此我必須將整個latex擋拆開成一個章節一個tex檔,並且每個章節都可以獨立編譯,獨立看到章節的pdf輸出結果,這樣就可以一個章節修改後很快看到結果。然後最後在全部編譯。目前我是用python程式語言配合windows batch執行檔來達成此目標,即在batch檔案可以選擇只編譯章節或編譯整份文件,然後用python去調整preamble,最後batch在執行xelatex。
    不過後來我學到latex本身有一些套件可以達到此目的,目前考慮用import package,這樣才可以獨立章節編譯。還有想用另一個套件使得subdocument中可以用相對於subdoc所處在的subfolder路徑。)
2. 由於不想買以前學校裡學習時常用的向量繪圖軟體Adobe Illustrator,Acdsee Canvas等昂貴的向量繪圖軟體,我花了一些時間去衡量如何經濟地劃出精美精準的向量繪圖圖形。不過最後還是決定使用程式語言土法煉鋼來完成此需求,其實不會太花時間。因為付費的的繪圖軟體其實很多功能都不需要用到,常用的就那幾個,自己寫比較快。因此我使用Python與其強大的繪圖庫Matplotlib,Matplotlib製作的圖形的精美程度已經是國際期刊等級,有此可見其作者群之用心與藝術程度。我在這一篇blog中有些說明。不過,不方便的是,Python Matplotlib的EPS轉換還不完整,而點陣圖形在latex處理後產生的PDF檔又會有圖案模糊錯位的問題。因此,我找到Matplotlib中有前輩撰寫的PGF套件,可以完美的跟latex做結合,來產生精美的PDF圖形檔案。只能說非常感謝PGF套件的提供者,真的是非常好用阿!
3. minted套件有一個問題,就是他不會自動分割程式區塊,所以跨頁的程式區塊就要稍微用點技巧,參考這邊的解說,使用mdframed套件,並且在minted中不要使用背景顏色,讓mdframed來負責背景顏色的處理,然後將listing環境捨棄,這樣就可以跨頁了。
4. Sphinx中的autodoc套件,似乎還沒有辦法自動取得物件的屬性(autoattribute我無法用成功),我得用autoclass加:member:指令來達到相同的目的。
5. 若有太多大的pgf圖片,約400KB each,似乎會造成Tex compile時超過其記憶體上限500000,不之是否跟我有用shaded有關,我後來沒有去細究,就直接用python將資料存成資料檔,然後用PStricks的3D套件去存取資料檔並劃出來3D圖形。
以下為2016/三月更新
6. GIT版本管理(請見我的另一篇部落格文章)
7. Django,我用python的套件在pythonanywhere.com建立了一個初步網站,想要能夠慢慢脫離google blogger的ubiquity。初步紀錄

我使用的PSTricks的參考書 (非常推薦!): The LaTeX Graphics Companion (2nd Edition) Paperback – August 12, 2007 by Michel Goossens (Author), Frank Mittelbach

相關部落格文章:
http://whymranderson.blogspot.tw/2014/11/latex-minted-package-pygmentize-not.html


Memo for myself:
http://www.chinabaike.com/t/30826/2014/0517/2213116.html

Sep 28, 2014

Diy 筆記簡易裝訂成書

要裝訂成書一定要跑影印店嗎?我拿一本工研院的成果彙編拆開當封面,用handpress小型金屬加工工具車洗在筆記頁邊打小洞,並且用銅線穿過打的洞並且綁緊,這樣就可以把筆記做成冊,然後拿個厚紙板將頁邊包起來。不漂亮,但是很實用,右邊翻頁邊我有注意有貼齊,因此很好翻。線也綁得很緊因此翻開完全不會鬆動。零成本的裝訂。





2016八月。哥不是開玩笑的,現在自己的筆記我都自己裝訂拉,哈哈哈。後來發現也不用鐵絲,只要用粗一點的線綁就可以了!

Jul 21, 2014

Use python matplotlib to create 3D vector-based graphics, 3D vector graphics DIY

I drew the following graphs using Python and it's Matplotlib. If you are interested in the python code, they are here (not including the first gyroscope figure, will be added later). I am trying to improve the code into many functions/modules so that drawing can be convenient and automated. The part related to plotting starts from line 128, because this is just a portion of another bigger project. Just want to show this is not as time-consuming as one may think it is. We can create or draw beautiful vector-based graphs without buying expensive software like Adobe Illustrator or Acdsee Canvas. However as you can guess with this method you will need the numerical x,y,z locations of everything! (which is generated from yet another portion of the program!)

Jun 25, 2014

Python code for 3D rotation simulation (core algorithm: iteration of direction cosine matrix)

(ongoing revision persists...)

Iteration of DCM with angular velocity in body frame. (This is the module. Import it in Python to use the iteration method.)
An example. (A test code.)

The background theory is in this post (in Chinese language though, I want to translate it into English but it takes time. Hope someone is interested in translating it!). And it has a python simulation of a rotating symmetric top in 3D motion.

The approximation of rotation by iteration of DCM is discussed in this textbook[1]. The approximation of rotation ωdt is also discussed. Here I use ω(t+dt)dt to approximate the rotation from t to t+dt. This is discussed in the same post.

[1]  D.H. Titterton and J.L. Weston, Strapdown inertial navigation technology, Peter Peregrinus
Ltd., London UK, 1997