Showing posts with label latex. Show all posts
Showing posts with label latex. Show all posts

Mar 28, 2017

高品質的數學文書排版是怎麼做到的?給國高中同學的一點介紹

高品質的數學文書處理與排版 - 給國高中同學的一點介紹

(修改中)

讀者一般在電腦上處理數學文件的時候,比如說做報告的時候,都是用微軟的Office Word。不過若是你遇到很多的數學,相信你應該用方程式處理器打得很痛苦,我以前就是這樣。要不然就是花錢請出版社打,不過他們也是用Word打,因此他們也很痛苦,打出來錯誤百出,無比醜陋,還要反覆校稿,搞得大家都不愉快。或者是遇到需要加入或者畫出高品質繪圖的狀況,不管是自己用小畫家或powerpoint畫,或是找出版社畫,都是無比醜陋,就會覺得,怎麼這麼難搞。這邊就是告訴你,你找錯人了。這個問題屬於"電腦排版"的領域,簡稱"電排",如同電腦繪圖,電腦繡字我們簡稱電繪,電繡。電排跟以前鉛字活字排版是有一定程度的關係。電繪電繡這些領域,你必須要去找這些領域的專家來,電排也是。這邊我會稍微介紹一下這個領域,與這些專家使用的工具,以及若是想要學習這門技術,想排出高精密且高品質的數理與圖案的文件,需要付出多少的努力。

有一些軟體是專門用來處理數學文件電排的。我們這邊介紹一個電腦程式LaTeX,他是一種電腦程式化的活字疊格排版技術,疊格的意思,就跟以前手動鉛字活字排版時一格一格疊上去的意思是一樣的,只是現在把它電腦程式自動化,在電腦中去排版。LaTeX中文我叫他"活字疊格",簡稱"疊格",取其近似於TeX的發音。活字疊格這個程式就是專門用來處理專業美觀並且公式數量很多(如一本數學書籍)的數理化文件,他做出來的PDF文件絕對是最精美最高品質,而且在數學或圖案文件量大的時候絕對是你的唯一選擇,因為真正專業的不會選擇用Word。因此LaTeX的全文我傾向叫他,電腦精密活字疊格排版技術(或程式)。

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

May 1, 2014

direction of friction force of rolling objects 有外力作用的的純滾動摩擦的作用力方向討論

這是一題常見於研究所考試試題的延伸討論。

試題舉例:國立成功大學96學年度碩士班招生考試試題,編號224,系所:醫學工程研究所甲組,科目:工程力學

這邊詳細的討論幾種較難的延伸型態,並審視磨擦力作用於滾動物體的方向,因摩擦力的方向很容易弄錯。有趣的是這些型態在日常生活中都很常見。

A sectional material I wrote up for university physics. These are more advanced problems for 1st and 2nd year engineering in university.

重新將我的手稿排版(latex): 排版完成2016/6/18



我的部落格內的相關文章:
http://whymranderson.blogspot.tw/2013/06/3-dimentional-rolling-coin-simulation.html
http://whymranderson.blogspot.tw/2013/02/two-coins-roll-without-slipping-puzzle.html


舊手稿


Mar 9, 2014

Typeset mix Chinese and English TeX documents with Scientific Workplace 5.0 and TEX live 2013

  You can typeset Chinese documents with Scientific Workplace (5.0) if you have just a few Chinese paragraphs. But if you have a long Chinese document or a document with mix Chinese and English characters, the software will suffer. The SWP's compiler for international characters, Omega, is just not good enough for this purpose. You will run into a lot of overfull hbox problems during typesetting. We will need another latex compiler for long documents of mix Chinese and English. But we will want to use the convenient editing features of Scientific Workplace, what should we do?

Here is something you can try. Still prepare your international document with SWP. But typeset it with another latex compiler. Here I use a popular one, XeLATEX in TEX live 2013. But there are a few problems. First, the file prepared by SWP has a character encoding that is not recognized by other latex compiler. So I wrote a small Python program to convert the encoding. Second, the figure insertion syntax generated by SWP is not recognized  by XeLatex. But this can be done by inserting TEX fields containing the correct Xelatex figure insertion commands into the SWP file by using SWP's TEX OBJECTS buttons. You won't be able to preview the figures SWP window but that doesn't really hurt. Here are step-by-step details:

  • How to convert SWP 5.0 special unicode format file to unicode correct characters:
SWP version 5.0 produced Chinese character in its special unicode codepoint format (if you open the tex file with any text editor, you will see every Chinese character has a form like \U{6211} instead of displaying 我 in the tex file.) This is due to the extra parenthesis {} after \U. So I wrote a small python program to automatically remove all {} in the tex file. So that the file can then be prepared by other tex programs (Tex Life 2013 in my case).

The program is very simple:
import re
import codecs
def dashrepl(matchobj):
    return chr(int(matchobj.group(1),16))
f1 = open('rotationV4pngtest.tex', 'r')   # name of your origianl file
f2 = codecs.open('rotationV4pngtest.tex.temp', "w", "utf-8")   #  name of the new file
p= re.compile(r"\\U{([\w]{,4})}")
for line in f1:
    m = re.sub(p,dashrepl,line)
##    print(m)
    f2.write(m)
f1.close()
f2.close()


  • For the SWP tex file to be readable by other compiler, you will also need to change the preamble (the packages and stuff)  in the beginning of the tex document so that other tex program can use it. Here is my case:
Original preamble generated by SWP:
\documentclass[12pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{sw2unicode}
\usepackage[UT1]{fontenc}
\usepackage{pmingliu}
\usepackage[left=0.95in,right=0.95in,top=2cm,bottom=2.54cm]{geometry}
Change the above to the following so that Xelatex can complie:
\documentclass[12pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{fontspec}
\usepackage{xeCJK}
\setmainfont[Mapping=tex-text]{Times New Roman} % rm
\setsansfont[Mapping=tex-text]{Arial}           % sf
\setmonofont{Courier New}                       % tt
\setCJKmainfont{微軟正黑體}
\usepackage[left=0.95in,right=0.95in,top=2cm,bottom=2.54cm]{geometry}
\usepackage{unicode-math}
\usepackage{graphicx}
\usepackage[hidelinks]{hyperref}


  • The file generated by SWP has symbols that use SWP's "tcilatex" macro, so you need to copy tcilatex.tex file into the same directory as your tex file. The location of tcilatex.tex in SWP's path has the following structure "D:\swp50\TCITeX\TeX\LaTeX\SWmacros\". Locate this file and copy it into your working directory. Then make sure your tex file has the \input{tcilatex.tex} line. You should have this line because you prepared your file with SWP.

  • Change the TEX figure insertion command:

The figure insertion command for Xelatex should look like this:
\begin{figure}[th]
\caption{{}}
\label{firstfig}
\begin{center}
\fbox{\includegraphics[scale=0.7]{cordtrans.JPG}}
\end{center}
\end{figure}

So open the SWP tex file with Notepad and replace all things look like the following with the above

\FRAME{fhF}{5.5097in}{3.7135in}{0pt}{}{}{cordtrans.JPG}{\special{language
"Scientific Word";type "GRAPHIC";maintain-aspect-ratio TRUE;display
"USEDEF";valid_file "F";width 5.5097in;height 3.7135in;depth
0pt;original-width 9.135in;original-height 6.1436in;cropleft "0";croptop
"1";cropright "1";cropbottom "0";filename
'
cordtrans.JPG';file-properties "XNPEU";}}

There could be other commands you need to change before you can run your tex document with TEX life Xelatex. Here I only show three points, which is enough for me to typeset this document. Good luck!

Mar 1, 2014

使用Scientific Workplace 5.0 + XeLatex軟體做中英文混雜並夾帶數學公式的長篇圖文文章電腦排版

  這邊介紹如何利用Scientific Workplace 5.0的編輯方便性,加上XeLatex (TeX life 2013)對中英文混雜的完美支援,來對中英文混雜並夾帶數學公式的長篇圖文文章做自動排版。

Nov 9, 2012

Scientific Workplace + Endnote Tricks

Last update 2/13/2013

Share some recent discoveries with the software Scientific Workplace 5.0
  1. To work with Endnote. - this includes adding a 'Label' to the bib file using a convenient small Matlab function and in the Endnote the export 'bibtex style' you can remove the unwanted fields (like the 'note' field) that you don't want it to appreare in the typesetting bibliography library.
 Alright it is time to give the full story. Sorry for the late reply :p! Anyway. I don't know if bibDB in SWP(scientific workplace) fixed this problem yet. If not, then I guess it is still useful for me to write this down for those people fighting with this problem. If they did fix it, that's even better. Actually after I checked Mackichan website I found this written in 2006, which means they haven't fix it or it is still not a complete solution. So I think my post will still be useful.