Thursday, 5 December 2013

[Latex] Math mode in Verbatim or Verb in math mode

Verb in math mode

  • Include
    \usepackage{fancyvrb} % math mode in verbatim
  • SaveVerb to define verb text
    \SaveVerb{verbtext}+verb text+
  • UseVerb
    \UseVerb{verbtext} within math mode

Examples

\usepackage{fancyvrb} % math mode in verbatim
\SaveVerb{verbtext}+verb text+
\begin{align*}
    \UseVerb{verbtext}
\end{align*}

Color the verbatim

Define new blockcode environment

\DefineVerbatimEnvironment{blockcode}
{Verbatim}
{formatcom=\color{blue}}

Examples

\begin{blockcode}
    text to be colored
\end{blockcode}

Break lines in verb

Include this new package: spverbatim

\usepackage{spverbatim} % break lines at spaces when vs. \verb

Use \spverb

\spverb+\langle \listarg \rangle+

Examples

Math in Verbatim

  • TBD

Source Code Syntax Highlight in Blogger

How to enable the source code syntax hightlighter for google blogger

SyntaxHighlighter

  • Go to Blogger's template
  • Click "Backup/Restore" in the upright of page to backup current template at first
  • Then "Edit Template"
  • Add the following lines just before the line
        
        
        

Examples

js

int function(int i)
{
    return i;
}

C/C++

int function(int i)
{
    return i;
}

References

Monday, 2 December 2013

[Latex] Cross-Reference and automatically increasing number in table cell

The below commands define two counters: cellcounter and cellcounter1, which are used for two tables seperately.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% New Command for Cross-Reference in Table cell
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcounter{cellcounter} % a new counter
% automatically increase the counter, print the value of counter, and label it for reference
\newcommand{\omegalabel}[1]{\refstepcounter{cellcounter}\arabic{cellcounter}\label{#1}}
% remember to reset before a new start
\newcommand{\cellcounterreset}{\setcounter{cellcounter}{0}} % automatically increase the counter

\newcounter{cellcounter1} % a new counter
% automatically increase the counter, print the value of counter, and label it for reference
\newcommand{\philabel}[1]{\refstepcounter{cellcounter1}\arabic{cellcounter1}\label{#1}}
% remember to reset before a new start
\newcommand{\cellcounter1reset}{\setcounter{cellcounter1}{0}} % automatically increase the counter
In the table cell, we can use it like this way.

\begin{tabular}[]{|p{0.6in}|p{1.0in}|p{1.5in}|p{3.0in}|}       
      \hline
       $\Omega_3$ Rule \omegalabel{omega3:rul_gen1} & \multicolumn{3}{p{5.5in}|}{In order to identify the state space, the initialisation and the operations, Z specification must have a schema named \I{Init} for initialisation and the only one schema included in \I{Init} schema defines the state space. A schema, which includes state schema and its decoration, and is not included by other schema, is treated as an operation.} \\ \cline{2-4}
      \hline
\end{tabular}
In another table, we can use it like this way too.
\begin{tabular}[]{|p{0.6in}|p{1.0in}|p{1.5in}|p{3.0in}|}       
        \hline
        $\Phi$ Rule \philabel{phi:rul_abbr} & $AbbrDef==exp$ & \verb+nametype AbbrDef=+ $\Phi(exp)$ & Abbreviation Definition \\
        \hline
\end{tabular}
Then, we can refer to it by

Rule~\ ref{phi:rul_abbr}
Rule~\ ref{omega3:rul_gen1}
The output will be like.

Tuesday, 15 October 2013

[git] usage (1)


Authors: - Kangfeng Ye
Title: Git Usage ...
Keywords: Git

Git Introduction

Git is a distributed version control system.

Understanding the key Terminologies

Commands

git add

  • add changes in the working tree to the staging area or called staging
  • allows you to incrementally modify files, stage them, modify and stage them again until you are satisfied with your changes

An example

  • show current working tree status
$ git status -s
  • add a new file, and edit it to add contents
$ echo "git add test" > readme.txt
  • query status and it is untracked
$ git status -s
?? readme
  • staging it and query status again. A - staged
$ git add readme
$ git status -s
A readme
  • modify it and query status again. AM - the file has been changed but the change is not staged
$ echo "staging, then modify again" >> readme
$ git status -s
AM readme
  • staging it again. It comes back to staged again.
$ git add readme; git status -s
A readme

Another example to test

  • query current status
$ git status -s
A readme
  • show file and it's staged content
$ cat readme
git add test
staging, then modify again
  • modify it, but no staging
$ echo "no staging" >> readme
$ git status -s
AM readme
  • commit it
$ git commit -m

Troubleshooting

A scenario

  • create a local git repository
$cd /mnt/share/rfid_tracker
$ git init
$ touch readme.txt
  • clone from repository
$cd ~/src/
$ git clone /mnt/share/rfid_tracker rfid_tracker - a clone is in rfid_tracker folder
  • modify the readme.txt file and commit in clone
$ git commit -m "change" readme.txt
  • push to original repository
$ git push
  • an error occurs
remote: error: refusing to update checked out branch: refs/heads/master
error: failed to push some refs to /mnt/share/rfid_tracker
  • solution (make original reposity as bare)
$ cd /mnt/share/rfid_tracker
$ mv .git .. && rm -fr *
$ mv ../.git .
$ mv .git/* .
$ rmdir .git
$ $ git config --bool core.bare true

Thursday, 22 August 2013

How Operators: Infix, Prefix, Postfix, or Mixfix (Distfix)

How Operators: Infix, Prefix, Postfix, or Mixfix (Distfix)
Infix, prefix, postfix and Mixfix operators are different ways to write expressions equally.

Infix Notation

Operators are written in the middle of the operands

Examples

  • (6-5)*7
  • A*B + C/D

Prefix Notation

Also knowns as Polish notion. Operators are placed in the left of the operands

Examples

  • *(- 6 5) 7 or * - 6 5 7
  • + * A B / C D

Postfix Notation

Also knowns as Reverse Polish notion. Operators are placed in the right of the operands

Examples

  • 6 5 - 7 *
  • A B * C D / +

Outfix Notation

Operators are placed out of the operands

Examples

  • |_| in Maude, e.g. | 1 0 1 1 0 | is 5

Mixfix Notation

Have several name parts and can be infix, postfix, prefix, and closed (a).

References

Examples

  • if n + n == n ! then n else (n + n - n) in Agda: several name parts, mixed fixity (prefix, infix, postfix)
  • L ? S : T in Maude: comination of outfix and infix

Definition

  • Holes: denoted by _ in _[_]
  • Precedence
  • Infix _ + _: two holes and one name part +
  • Prefix if _ then _ else _: three name parts (if, then, else), and three holes.

Thursday, 15 August 2013

[VIM] Insert the content of a file into current opened file


read a file and insert it into the position below the cursor of the current file

:read /path/to/file
:re /path/to/file
:r /path/to/file
  • insert into the beginning of the file
:0r /path/to/file
  • insert into the end of the file
:$r /path/to/file

read the output of system command and insert into the position below the cursor of the current file

  • read in the file list by ls command
      :r !ls
    
      :r !ls ~
  • read in the dir tree by tree command
      :r !tree

read a specific lines from a file and insert it into the position below the cursor of the current file

  • read the line no.10 to 20 into current file
:r !sed -n 10,20p /path/to/file

Friday, 26 July 2013

Match two marks in one line or two successive lines by grep or awk

Sometime we may need to check if a text file contains the wanted information which can be in one line or two sequential lines, but not others. For example we may check if a file contains the "operation system" and "Linux". It limits to the following format:

In one line:
        "operation system Linux" or "operation systems such as Unix, Linux, Windows..."
or
        "operation system
        Linux"
or
         "operation systems such
          as Unix, Linux, Windows... "
but not
         "Linux operation system"
not
         "Linux
          operation system"
not
         "operation system
         ......
         Linux"

pcregrep
pcregrep -M 'operation system.*(\n|.).*Linux' test.txt
awk
awk '/operation system/{k=1; ln=$0}  /Linux/{if(k == 1){print "--------"; print ;} if(k == 2){print "---------"; print ln; print;}} {if(k > 0){k++}}' test.txt
Problem: this awk will match the "Linux operation system"? How to avoid it?

Improvement: 
./match.awk test.txt
match.awk
#!/usr/bin/awk -f

BEGIN \
{
        print " - BEGIN - "
        m1 = 0; # mark1
        m2 = 0; # mark2
        ln = ""; # last line
}
{
        # two marks in one line
        if ($0 ~ /operation system.*Linux/) {
                        print "----1----";
                        print $0;
        }
        else {
                if ($0 ~ /operation system/) {
                        m1 = 1;
                        ln = $0;
                }
                else if ($0 ~ /Linux/ && m1 == 1) {
                # two marks in adjacent lines
                        m2 = 1;
                        print "----2----";
                        print ln;
                        print $0;
                }
                else {
                # clear all variables for unmatched line
                        m1 = 0;
                        m2 = 0;
                        ln = "";
                }
        }
}
END { print " - DONE - " }
Test Output
 - BEGIN -
----1----
operation system Linux
----2----
operation system
Linux
----1----
operation system such as Linux
----2----
operation system
such as Linux
 - DONE -
Test Input: test.txt
operation system Linux

operation system
Linux

operation system such as Linux

operation system
such as Linux

Linux
operation system

Linux operation system

operation system

Linux

operation system
such as
Linux