2012年1月28日 星期六

[Blogger] SyntaxHighlighter

參考來源:http://blog.lyhdev.com/2010/09/bloggersyntaxhighlighter.html
Syntax Highlighter可以讓你在Blogger中顯示美觀的程式碼~
安裝的方法
  • 首先在<b:skin>前加入以下內容
<!-- SyntaxHighlighter  3.0.83-->
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushAS3.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushBash.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushColdFusion.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushDelphi.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushDiff.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushErlang.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushGroovy.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJavaFX.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPlain.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPowerShell.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushScala.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>
  • 接著在</body>前加入以下內容
<!-- SyntaxHighlighter  3.0.83-->
<script type='text/javascript'>
     SyntaxHighlighter.all()
</script>
<pre class="brush: groovy">
println 'Hello World'
println 1 + 2 + 3
</pre>
同時由於在blogger上貼程式碼時需要先經過轉換,不然大於小於的符號會不見的~可以用這個網站來達成
HTML Encoder:http://www.opinionatedgeek.com/DotNet/Tools/HTMLEncode/Encode.aspx
Firefox使用者可以參考這一篇:http://blog.lyhdev.com/2010/08/bloggerhtmlhtml-encode-button-firefox.html

2011年10月19日 星期三

[Software] Synergy


使用前請先注意電腦是在同一個區域網路(aka. IP前三碼相同)下且是連接同一個IP分享器
然後,兩台電腦都要裝 Synergy 軟體才能用,但是 MAC 環境下要裝的軟體名字不太一下,
for Windows 叫 Synergy
for MAC 叫 SynergyKM
網址:http://sourceforge.net/projects/synergykm/

Linux的話也叫synergy,不過command方面會分成synergyc(Client端)和synergys(Server端)

確認 OK 以後,
我要知道你是要
1.用 PC 的鍵盤同時控制 PC、MAC  (以PC為 Server 端)
還是 
2.用 MAC 的鍵盤同時控制兩台 (以 MAC 為 Server )
我自己是用 PC 鍵盤控制兩台電腦啦,一台PC 裝 Windows 7 x64,一台 iMac,OS 是 10.5.8 (Leopard)
居然連64位元的Windows都可以用是我最高興的地方!
所以先講 狀況 1 的設定方法。
假設你兩台電腦都已經裝好 Synergy 而且 位於同一個區域網路底下。
我的 PC 端 IP 是 192.168.1.1 (可以打開 Synergy 後,點左下 info 的按鍵看到)
MAC ip 是 192.168.1.3 (系統偏好設定-->網路 再找一下就看到了)。
打開 PC 上 Synergy,
點 Share this computer's keyboard and mouse (server) --> 以 PC 為 server 端。
按 Screens 下面的 "+"
Screen Name 可以隨便打,認得出來就可以,我輸入 PC
Aliases 不能隨便打,打錯就抓不到電腦!
Aliases 要跟你電腦的"網路名稱"完全一樣!!!! --> 第一個最容易設錯不能用的地方。 
網路名稱:控制台-->系統-->完整電腦名稱,ex: Twinkle-PC
改網路名稱:
(XP)     控制台-->系統-->變更                   
(Win 7) 控制台-->系統-->進階系統設定-->變更
(MAC)  系統偏好設定-->共享-->編輯  
輸入完 Screen Name 和 Aliases 按 OK。
同樣的動作再做一次,但這次輸入 MAC 的 Aliases,Screen Name 可以隨便打,我輸入 MAC。
接著,是設定下面 Links (螢幕位置) 的部份:
這邊是讓我搞很久,第二個最容易出問題的地方,不過點破就沒什麼稀奇了。
我的螢幕擺的位置是 PC 在左,MAC 在右,同高。
把下拉選單拉成: 0 to 100 % of the right of PC goes to 0 to 100% of MAC
然後按 "+"  --> 出現 PC is left of MAC.
我原本以為這樣就好了,結果是,我滑鼠往右移到MAC上之後就移不回 PC 了 ^o^"
沒想到還要設讓遊標移回來的位置:
把下拉選單拉成: 0 to 100 % of the left of MAC goes to 0 to 100% of PC
然後按 "+"  --> 出現 MAC is left of PC.
兩個 Links 都出現在框框裡之後按 OK. 
註:要是螢幕不同高,就調整 0 to 100 的選項,但我沒調過,可以自己試試看。
如此 Server端 就設定完成了。
轉戰到 MAC 吧...
打開 系統偏好設定-->最下面 SynergyKM
點上面 Location 的選單,選 New Location,打入一個名稱後ok
在 General 標籤下,選 Connect to a shared keyboard and mouse,
到 Client Configuration 標籤,輸入 Server 端 IP, ex: 192.168.1.1 
回到 General ,按 Turn Synergy On
Status 出現 Connected 表示連接成功!  可以移動滑鼠過去囉!
移過去之後打字也ok。
預設按鍵 Alt (PC) = Command (MAC)

2011年10月16日 星期日

Latex Code Listing


11. How can I get source code listings nicely printed?


Answer

Sorry, you are on your own, more or less.
I know that there are third-party LATEX packages that are tailored towards formatting and displaying of source code listings in LATEX documents. I did not have the time to investigate these packages for my own purposes, and so I was stuck with something really basic I wrote myself. Just in case that this quick 'n dirty hack might be all you need, too, I include it here.

Example usage

In the preamble, or in an appropriate include file:
\newenvironment{mylisting}
{\begin{list}{}{\setlength{\leftmargin}{1em}}\item\scriptsize\bfseries}
{\end{list}}

\newenvironment{mytinylisting}
{\begin{list}{}{\setlength{\leftmargin}{1em}}\item\tiny\bfseries}
{\end{list}}

In the manuscript:
\begin{mylisting}
\begin{verbatim}
  double a[n], b[n], c[n];

  for (i = 0; i < n; i++)
      c[i] = a[i] + b[i];
\end{verbatim}
\end{mylisting}


Result, before

'Before' screenshot

Result, after

'After' screenshot

2011年2月7日 星期一

時間

又是一段時間沒寫網誌了

反而像把網誌當筆記本來用了 = =

過年在家閒著

也好不容易有些時間想想事情

這一年

發生了不少事呢

而開學後還有更多的挑戰

人生嘛

只能繼續走下去:)

2010年11月12日 星期五

Parse Tree in Latex


Latex 真的是什麼都有XD


The parsetree Package for Drawing Trees in LaTeX
[ LaTeX for Linguists.dvi.ps.pdfTestFile]





  • Basic Use



  • Interaction with other packages
  • Boxes, Subscripts, Unlabelled Nodes



  • AVMs on Nodes



  • Special Characters in Node Labels



  • Non-Local Lines








  • This is my favoured and recommended method for drawing standard linguistic trees where the nodes have less than three daughters. It is very simple, and interacts well with other packages. Here is a simple example.

    \begin{parsetree}
        ( .S. 
            (.NP.  `we')
            ( .VP.
                (.V.    `gave' )
                (.NP.   `them')
                (.NP. ~ `a toy')
             )
        )
    \end{parsetree}
    
    Apart from information on basic usage you will find information about the use of the package for more complex examples, and use in conjunction with avm.sty, and tree-dvips) for drawing really rather complicated things.
    The package is available from the usual archive sites, but I have not been able to find documentation other than what is in the source code. So I have made this up myself. My apologies to the author (Eirik Hektoen) for misrepresentations.
    This documentation is available in printed form: PostScript and DVI. The style/package file parsetree.sty is also available here.

    Basic Use

    In the preamble, put:
    \usepackage{parsetree}
    
    In the text put: 
    \begin{parsetree}
        ( .S. 
            (.NP.  `we')
            ( .VP.
                (.V.    `gave' )
                (.NP.   `them')
                (.NP. ~ `a toy')
             )
        )
    \end{parsetree}
    
    Basic usage is as follows:
    • \begin{parsetree}...\end{parsetree}
    • A (sub-) tree consists of a "(", a node label, some daughters, and a ")".
    • A terminal node consists of just the node label. Terminal node labels are written in quotes: `baby'`gave'
    • Non-terminal node labels are surrounded by dots, e.g. .S.,
    • Putting a ~ before a node label puts it under a triangle, rather than a vertical line.
    • Node labels can be arbitrarily complex (see examples below).
    • Warning: no more than tree daughters are allowed per node.
    • Warning: don't use "(", or ")", or "." or "~" inside node labels (see below on how to avoid this restriction).
    • The following commands can be used to vary the appearance of trees, see below for examples of their use.
      \pthorgaphorizontal gap between sisters (default 12pt)
      \ptvergapvertical gap between mother/daughter (default 12pt)
      \ptnodefontfont and strut height/depth of non-terminal nodes
      \ptleaffontfond and strut height/depth of leaves
    Here are some more simple examples: 
    \begin{parsetree}
            (.S. .NP. .VP.)
            \end{parsetree}
    

    \begin{parsetree}
            (.NP. `Sam')
            \end{parsetree}
    

    Interaction with other packages

    Node labels can be arbitrarily complicated. Just to give you an idea, the following examples show nodes containing arrays, co-indexing subscripts, and frame boxes, and nodes without node lables; nodes labelled with feature structures (made with the avm.sty package); node labels containing round brackets and other characters that parsetree treats specially; and nodes with lines linking remote parts of the tree (made with the tree-dvips package).

    Boxes, Subscripts, Unlabelled Nodes

    An example with some nodes boxed, and some nodes unlabelled (just put nothing between the `.' and `.'):
    \begin{parsetree}
        ( .\begin{tabular}{c}S\\TNS=pres\\asp=simple\end{tabular}. 
            (.\fbox{NP$_i$}. .the baby.)
            ( . .
                (. . .gave. )
                (.\fbox{NP$_{j , {j \neq i}}$}. 
                    .the baby. )
                (. . .a toy.)
             )
        )
    \end{parsetree}
    

    AVMs on Nodes

    For making trees labelled with AVMs (Feature Structures), a useful hint is to first use \newcommand to define commands to draw the AVMs (this means you can check they are correct before you try to combine them into a tree), and makes the tree much easier to read.
    We define three commands to draw the AVMs: \fsA\fsB, and \fsC, e.g.
    \newcommand{\fsB}{\begin{avm}
               \sort{f}{\[ cat & det \\
                           agr & \@1   \\
                           def & \@2   \] }
                      \end{avm} }
    
    One can, of course test these just by putting:
    \fsA
            \fsB
            \fsC
    
    To draw the tree, we put these commands in the node labels, preceded by some commands to adjust the space between nodes (see below):

    \begin{parsetree}
            \pthorgap{75pt}
            \ptvergap{40pt}
         ( .\fsA .
               .\fsB .
               .\fsC .
         )
    \end{parsetree}
    
    This example uses the parsetree declarations: \pthorgap and \ptvergap:
    • The gap between sisters is controlled by the \pthorgap declaration (default is 12pt)
    • The vertical distance between mother and daughters is controlled by the \ptvergap declaration (default is 12pt).
    These can be set globally, or anywhere within individual parsetree environments.You can also alter the font and height/depth allowed for non-terminal node labels and leaves with \ptnodefont and \ptleaffont. The following are the defaults:
    \ptnodefont{\normalsize\rm}{11pt}{3pt}  % font and strut height/depth: nodes
    \ptleaffont{\normalsize\it}{11pt}{3pt}  % font and strut height/depth: leaves
    

    Special Characters in Node Labels

    Parsetree treats some characters specially (e.g. round brackets, dot, tilde), using them as commands to draw trees. This can be a problem if you want these characters in your trees. However, there is a simple solution.
    The parsetree environment is simply a wrapper that: (a) makes these special characters `active' (i.e. special), and (b) calls the commands\ptbegtree \ptendtree at the beginning and end of the environment respectively. If we use these latter commands directly, then these special characters retain their normal meanings, and can appear in node lables. However, we must now use the underlying parsetreecommands to draw the tree:
    • replace "(" with \ptbeg, and ")" with \ptend
    • replace .N. by \ptnode{N}.
    • replace `N' by \ptleaf{N}.
    • replace ~ with \pttritrue
    Here is our original example:

    \ptbegtree
        \ptbeg \ptnode{S} 
            \ptbeg \ptnode{NP}  \ptleaf{we} \ptend
            \ptbeg \ptnode{VP}
                \ptbeg \ptnode{V}    \ptleaf{gave}  \ptend
                \ptbeg \ptnode{NP}   \ptleaf{them}  \ptend
                \ptbeg \ptnode{NP} \pttritrue  \ptleaf{a toy} \ptend
             \ptend
        \ptend
    \ptendtree
    
    And here is an example, with `special' characters thrown in:

    \ptbegtree
      \ptbeg \ptnode{(VP)}
          \ptbeg \ptnode{(V)}    \ptleaf{(`saw)}  \ptend
          \ptbeg \ptnode{NP}     \ptleaf{Sam's~~~toy.} \ptend
       \ptend
    \ptendtree
    
    Here is a more complicated and realistic example (to simplify things, I have defined \npile to produce a node label consisting of an array -- of course, with something this complicated, you would probably want to simplify things even further, but this is just for exemplification).
    \newcommand{\npile}[1]{%
      \ptnode{               %
        \( \begin{array}{c}#1%
           \end{array}       %
        \)   }}  
    

    \ptbegtree
     \ptbeg
     \npile{S\\
            see' (s, k)\\
            \lambda y see' ( y,k ) (s)}
        \ptbeg
        \npile{NP\\s}
          \npile{Sam\\s}
        \ptend
        \ptbeg
        \npile{VP\\ \lambda y see' ( y,k )\\
                     \lambda x \lambda y see' ( y,x ) (k)}
                \ptbeg
                \npile{V\\ \lambda x \lambda y see' ( y,x ) }
                    \npile{saw\\ \lambda x \lambda y see ( y,x )}
                \ptend
                \ptbeg
                \npile{NP\\k}
                      \npile{Kim\\k}
                \ptend
         \ptend
    \ptend
    \ptendtree
    

    Non-Local Lines

    The tree-dvips package can be used to draw `non-local' lines, as in the following example, supposed to show something like "Quantifier Raising":

    \begin{parsetree}
    ( .S.
      (.NP. ~ .\node{1}{\strut every baby}. )
      (.S.
          (.NP. .a baby.)
          ( .{VP}.
                (.V. .{gave}. )
                (.NP. .every child.)
                (.NP. .\node{2}{\strut t}.)
             )
        )
    )
    \abarnodeconnect[-10pt]{2}{1}
    \end{parsetree}
    
    Here is a more complicated example: 
    \begin{parsetree}
        ( .\node{1}{S}. 
            ( .\node{2}{NP$_i$}. .the baby.)
            ( .\node{3}{VP}.
                (.\node{4}{V}. .\node{5}{gives}. )
                (.NP$_j$. .the baby.)
                (.NP. .a toy.)
             )
        )
    \anodecurve[tl]{2}[bl]{1}{20pt}
    \anodecurve[r]{1}[t]{3}{20pt}
    \anodecurve[bl]{3}[t]{4}{20pt}
    \anodecurve[bl]{4}[l]{5}{20pt}
    \end{parsetree}
    

    LaTeX for Linguists,
    Doug Arnold,
    doug@essex.ac.uk,
    November 30, 2009.

    2010年11月10日 星期三

    C code in latex

    \documentclass[a4paper,10pt]{article}
    \usepackage[utf8x]{inputenc}

    \usepackage{listings}

    \begin{document}


    \lstset{language=C,frame=single,numbers=left,tabsize=2}
    \begin{lstlisting}
    #include
    #include
    #include

    int
    max(int a, int b)
    {
    if(a>b) return a;
    else return b;
    }

    void
    reconstruct(char *x, char *y, 
                int table[strlen(x)+1][strlen(y)+1])
    {
    int n=strlen(x),m=strlen(y),i,j,currentChar;
    int maxlcs=table[n][m];
    char* lcschar=(char*)malloc((maxlcs+1)*sizeof(char));
    *(lcschar+maxlcs)='\0';

    currentChar=0;
    i=0;
    j=0;

    while(currentChar
    {
      if( *(x+i) == *(y+j) )
      {
       *(lcschar+currentChar)=*(x+i);
       currentChar++;
       //printf("%c\n",*(x+i));
       i++;
       j++;
      }
      else if(table[i+1][j] >= table[i][j+1]) i++;
      else j++;
    }
    printf("%s\n",lcschar);
    free(lcschar);
    }


    int
    lcs(char *x,char *y)
    {
    //printf("%zu\n",strlen(x));
    int n=strlen(x), m=strlen(y), i, j, table[n+1][m+1];
    for(i=0;i<=n;i++) table[i][0]=0;
    for(j=0;j<=m;j++) table[0][j]=0;
    for(i=1;i<=n;i++)
      for(j=1;j<=m;j++)
       if( *(x+i) == *(y+j) ) table[i][j]=1+table[i-1][j-1];
       else table[i][j]=max(table[i-1][j], table[i][j-1]);

    reconstruct(x,y,table);
    return table[n][m];
    }


    void
    main()
    {
    int res=lcs("abdebcbb","adacbcb");
    //printf("%d\n",res);
    }
    \end{lstlisting}


    \end{document}

    2010年10月25日 星期一

    Gas Tex TXT

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%
    %% GasTeX : Graphs and Automata Simplified in TeX

    GasTex Example(For Automata)

    http://www.liacs.nl/~hoogeboo/praatjes/gastex/

    GasTeX (Graphs and Automata Simplified in TeX, by Paul Gastin) is a package for drawing figures with a LaTeX interface and the help of Postscript. Everyone that needs to draw automata in his LaTeX documents on a regular basis should have a look at this package.
    The examples below are mainly to remind me of the possible settings for figures I sometimes need.


    \begin{figure}[H]
      \begin{center}
        \unitlength=4pt
        \begin{picture}(15, 28)(0,-10)
          \gasset{Nw=5,Nh=5,Nmr=2.5,curvedepth=3}
          \thinlines
          \node(A0)(7.5,0){$0$}
          \node[Nmarks=i,iangle=90](A1)(0,13){$1$}
          \node[Nmarks=if,iangle=90,fangle=90](A2)(15,13){$2$}
          \drawloop[loopangle=180](A1){$a$}
          \drawloop[loopangle=0](A2){$b$}
          \drawedge(A1,A2){$a$}
          \drawedge(A2,A1){$b$}
          \drawedge(A2,A0){$a$}
          \gasset{curvedepth=-3}
          \drawedge[ELside=r](A1,A0){$b$}
          \drawloop[loopangle=270](A0){$a, b$}
        \end{picture}
      \end{center}
      \caption{La complétion de l'automate.}
    \end{figure}
    Example of GasTeX output



    Code
    binary relation

    Code
    binary tree (post order)

    Code
    undirected graph (a)

    Code
    undirected graph (b)

    Code
    undirected graph (c - linear)

    Code
    fsa (final 0 or even 1's)

    Code
    fsa (subword 00 or even 1's)

    Code
    fsa (ends in a - - , deterministic)

    Code
    cube (strings Hamilton dist 1)

    Code
    CMB Euler path

    Code
    CMB probes (A..G) and clones (1..6)

    Code
    CMB PQ trees

    Code
    Hilbert curve (tiles)