Wednesday, June 5, 2013

Remove string postfix in shell

Can't believe I missed this tip for years (and was using all kinds of ugly workarounds).

To quickly remove a postfix of a string in shell:

test=file.ext; echo ${test%.ext}

On the other hand, if ext is not in test, the original string is returned.

PS: There are some more standard ways to deal with extensions, like

FN=$(basename $1 .tex)

But to deal with a string is more general and has more widely usage.

Saturday, March 2, 2013

Put LaTeX citations in correct order

In a research paper, if references are not ordered by the order they are referred in the main text, the paper is (by a subset of people) considered not well written. I found ordering references to be painful for large papers. Thus I wrote a tool to do it automatically.

Usage:

or.py yourpaper.tex

A file named "ref_ordered.txt" is generated with ordered reference. The ordered reference list is in this file.

Code: http://dl.dropbox.com/u/49916494/or.py

Monday, January 14, 2013

A mode for writing derived from org mode

Didn't find some features among the sea of el's in the emacs world. Thus implemented them myself:

(1) (Almost) real time character counting. This includes counting of the whole buffer, and the counting of the present section (defined in the org-mode sense).

As I am mostly writing Chinese, character counting instead of word counting is good enough for me. Word counting should be possible following a similar way.

The counting is displayed in the title bar. I am actually hiding the mode line to avoid distraction. Thus the title bar is the only place where indicators can be shown.

(2) Focus edit on one section, hide all other parts of the document. Of course one can re-focus on the whole document with another key stoke.

The code is shared .


Thursday, November 29, 2012

Latex with UTF-8 alias in emacs

Recently I felt I would need something more than auctex in emacs. Then I ended up writing a custom LaTeX-mode myself. Now I guess I no longer need LaTeX-preview anymore, and it's no longer painful to edit long formulas!

This is the .tex file, displayed in a plain mode:


 In my custom LaTeX-mode, it displays as (but it still saves as above):


And of course this is the compile result, as you could expect from the first screenshot figure:


The lisp code is shared. As usual for free shares, it is without warranty and try-outs are at your own risk. On the other hand, the modification on display is based on only font-lock-mode and compose-region. No dirty hacks. Thus should be safe.

https://github.com/tririver/wyTex

I also put some custom settings of auctex in the same file. They are not necessary. The only essential part is the function wyTex-key. This function is actually bi-directional alias. If you can input α, β, etc directly from keyboard instead of \alpha, \beta, etc, then directly input them and in saved file they will be saved as  \alpha, \beta, etc and thus can compile using latex or pdflatex directly.

Here is the wyTex-key function:
(defun wyTex-key (utf8-char tex-command)
        (local-set-key utf8-char 
      `(lambda () (interactive) (insert ,tex-command))) 
(font-lock-add-keywords
'wyTex-mode `((,(concat "\\(\\" tex-command "\\)")
(0 (progn (compose-region (match-beginning 1)
 (match-end 1) ,utf8-char)
 nil)))))
  )
Acknowledgment: Thank Francesco for answering my elisp question. And Thank +Xah for suggesting writing an own mode to learn elisp!

Wednesday, October 31, 2012

Use tasker to create simple applications

Tasker is a powerful tool for Android phones/tablets. It is actually a programming language (without language) that can obtain full control of your device. I am a beginner and just tried a simple task: input arxiv identifier, and let tasker download the paper.

 First create a scene (with name arxiv-get). Add TextEdit element, with "Text Changed" task: "Variable Set" -> Name %arxivId to %new_val .

 Then call this scene: Add a task doing the following things:

 (1) Show Scene. Name: arxiv-get;

 (2) HTTP Get. Server: arxiv.org; Path: pdf/%arxivId; Output File: Download/%arxivId.pdf

(3) Open File. File: Download/%arxivId.pdf

 Choose a icon and add a task shortcut. That's it.

Thursday, October 18, 2012

One key compile latex documents

Here I want press one key to compile latex document, and launch viewer.

The first question you would ask: Isn't auctex's binding C-c C-c doing it?
-- Not quite. The biggest problem is that, preview-latex breaks the generated pdf file. Thus I cannot have preview and the generated pdf at the same time. Also, finding configuration methods is (sometimes) harder than do it yourself.

What the code does:
* One key compile latex document
* Put temporary files into temporary paths
* Open reader (if not already open)
* On mistake, open log file in new frame
* On warning, send desktop-notification

Now implications

Friday, October 12, 2012

关于“自由意志”的一点思考


这些天两次和老师、朋友讨论“自由意志是否存在”的问题。连同自己之前的想法,这里做一些归纳:

1. 什么是“是否存在”。
   这应该是疑问最少的一点。“意志”不需要也不可能完全是“自由”的。只要存在自由的成分即可。不过,如何界定“存在”还是不明确的。下文会提到这一点。

2. 什么是“意志”?
   首先,我认为意志是完全由物质决定的。也就是说完全由大脑的微观状态决定的。同样,意识、潜意识也是完全由大脑的围观状态决定的。具有不同信仰的人可能在这一点上有不同的看法。对于采取不同看法的人而言,我下面的讨论是没有意义的。
   那么,是不是说,意志是幻觉,意志不存在,进而自由意志不存在呢?我想这个结论过于武断。我们应该在承认意志(以及意识、潜意识)存在的前提下讨论自由意志的存在与否,而不是简单地否认意志。
   其实,是否承认意志、意识、潜意识的存在,涉及到如何界定“存在”。我只是愿意界定“存在”,使得意志、意识和潜意识是存在的。这种界定的目的在于:
   a. 使自由意志的讨论有意义。
   b. 承认意志、意识、潜意识的存在有其实用的价值。否则精神层面上的一切讨论都变得没有意义。
   出于以上目的,我把存在界定为呈展(emergent)意义上的存在。这样,意志在大脑微观状态基础上的存在性,可以类比于(非零的)熵在微观状态基础上的存在性。统计意义上,我们只把熵定义为同样宏观状态的情况下,微观状态的计数。这样,即便我们完全知道微观状态,熵仍然是存在的。这种存在性不仅是可以定义的,而且是有用的(否则热力学层面上的讨论将变得不可行)。
   这样,意志应该确实是存在的。并且,简单地用还原论来否定自由意志的存在也是不够的。
   总之,我宁愿把“意志”界定为大脑微观状态的某种宏观体现。具体是哪种,我不知道。意识、潜意识也是相似的定义,只是“某种”的所指不同。

3. 什么是“自由”?
   我认为,自由是在约束之下的选择权。约束就是在一定条件下“无法选择”。包括:
   a. 外界条件的约束,包括当前的和过去的外界条件。体现为人无法选择外界条件完全不允许的选项,例如被切断氧气供应后无法仍然选择正常地继续生活。
   b. 初始状态的约束,包括自身的和外界的初始状态。这和a有交集,但不完全相同。
   c. 量子涨落的约束。从哥本哈根解释来看,量子涨落的约束体现为人无法选择测量过程出现的结果;从多世界解释来看,量子涨落的约束体现为人无法选择自己进入哪个分支世界。
   这里大概c是最有争议的。量子涨落究竟应该看成是自由的体现还是约束自由的体现?这取决于我们把“自由”界定为做出选择的权利,还是(外界对系统的)不可预测性。我认为前者是恰当的。不可预测性是自由的必要条件,但不是等价的。比如说,经典计算机完全按照程序运行,我们知道经典计算机不是自由的。但是,给经典计算机加上一个量子的随机数产生器(目前完全可以做到),经典计算机的结果将不可预测。但是这并不说明经典计算机变成了“自由的”。至少,这不是我理解的自由。

4. 什么是“存在自由意志”?这个问题可以分为几个子问题:
   对于自由的约束,是否完全剥夺了意识“做出选择”的权利(即摆脱约束的权利)?
   -- 是:那么,意志就不是自由的。我倾向于这个答案。这是因为,经典和决定论的意义上,意志的选择权被初始状态的约束剥夺了。而量子的意义上,意识的选择权又被量子涨落的约束剥夺了。于是没有自由。或许我这里过于武断。如果如此,我想从相对明确的定义出发,我的结论是可以明确地被驳倒的。
   -- 否:意识是否利用了被允许的“做出选择”的权利?
      -- 是:意志是自由的。
      -- 否:意志仍然不是自由的。