Useful LaTeX Tools

LaTeX is a great typeset software in writing academic papers.

Here I give out some useful tools bundled with LaTex (details can be found via man in Linux/Unix systems).

latexmk – generate LaTeX document

latexmk -c Clean up (remove) all regeneratable files generated by latex and bibtex or biber except dvi, postscript and pdf.

latexdiff – determine and markup differences between two latex files

A typical usage of latexdiff is
latexdiff old.tex new.tex > diff.tex
Before running the command, do not forget to run bibtex to generate bbl files if you use bibtex. If you have multiple files with main.tex files, you can run the following command,
latexdiff --append-safecmd=subfile v1/main.tex v2/main.tex --flatten > diff.tex
Finally, you can typeset diff.tex and see the differences. This functionality is extremely useful if you work with others.

Tunnelling via SSH

Sometimes, the access to the lab/office computer is necessary. However, the university/company/organisation blocks my direct connection by its firewall, which is not good 🙁

Nevertheless, we can SSH to some computers in DMZ and then tunnelling to the lab/office computer for some specific applications.

Here, we leverage the forward function of SSH as following:
ssh -L port:host:hostport user@hostname

For example, we connect to a RDP server via SSH tunnel.

First, start the tunnel by
ssh -L 3389:123.123.123.123:3389 username@example.com
Above code starts a tunnel to 123.123.123.123:3389 where 123.123.123.123 is the RDP server ip address and 3389 is the RDP port.

Secondly, connect to 127.0.0.1:3389 via any RDP client.

Way to rollback or revert SVN repository

Sometimes we want to revert a svn repository because of bad commits.

Here is a quote from: http://stackoverflow.com/questions/2324999/revert-a-svn-folder-to-a-previous-revision

Assuming you want to revert from current HEAD (last commited) version to revision 268:

cd folder svn up svn merge -r HEAD:268 . 

Then resolve any conflicts manually (there should be nothing if there is no local change) and:

svn commit "- reverted to revision 268" 

To revert single change (e.g. made in revision 666):

cd folder svn merge -c -666 . 

To revert local changes (not committed yet):

cd folder svn revert -R . 

Let a mac application run in default language as you like

To do that, just use ‘defaults’ command.

The usages is simply:

default [command] [domain] [attribute] [value]

If I want to change the default language of com.apple.mail to be English while the whole system is in Chinese, just type in terminal:

defaults write com.apple.mail AppleLanguages ‘(“en”, “zh”)’

It will search for English language pack first, then Chinese.

error: ‘ptrdiff_t’ does not name a type

When I was compiling htmlcxx under Ubuntu 11.10, a compile error occurs. It shows

In file included from ParserDom.h:5:0,
                 from ParserDom.cc:1:
tree.h:118:21: error: 'ptrdiff_t' does not name a type

The error is occurred because Ubuntu 11.10 is using GCC 4.6.3.
Here is the common issues with GCC4.6/G++4.6: https://wiki.edubuntu.org/GCC4.6

To fix the error in tree.h:118:21 for ptrdiff_t, just add “#include <cstddef>” to the header.

SMS? Makes a disk hard…

… to be capable with apple portables.

Remember I have purchased a hard disk last year? (Link in Chinese)

Now this hard is broken since my mac fallen to the ground… After several days, I find out that may be the broken hard disk has problem with the Sudden Motion Sensor. Because it seems that the broken hard disk cannot resume after the disk drive head disengaged.

Here are 2 ways to solve it:
1. Replace a hard drive without the same functionality of SMS.
2. Disable the SMS

It is not safe to turn off the SMS for a hard drive without the same functionality of SMS. Hence, I will buy a new hard disk.

For those who use SSD or a hard disk has the same functionality as SMS, you can disable the SMS by
sudo pmset -a sms 0
or you just want to check by
sudo pmset -g
If you want to enable is just type in terminal
sudo pmset -a sms 1

What happens when new fails in C++

When malloc() fails, it will return a null pointer. But… What about new? It throws an exception… correct! But… What about the pointer? Any pointers returned?

See following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Author Name: Need-Being
// Modification date: 22/03/2012
// File description: Sample code to show what happens when throw exception in
//                   constructor.
 
#include <iostream>
using namespace std;
 
class Object
{
public:
	Object()
	{
		cout << "Object: Constructor called" << endl;
	}
 
	~Object()
	{
		cout << "Object: Destructor called" << endl;
	}
 
};
 
class Test
{
public:
	Test()
	{
		cout << "Test: Constructor called" << endl;
		throw 0; // Throw a exception by force
	}
 
	~Test()
	{
		cout << "Test: Destructor called" << endl;
	}
private:
	Object object;
};
 
int main()
{
	Test* test = (Test*)1;
 
	printf("Test pointer: %p\n", test);
 
	try {
		test = new Test();
	} catch (...) {
		cout << "Exception caught" << endl;
	}
 
	printf("Test pointer: %p\n", test);
 
	return 0;
}

Running result:
Test pointer: 0x1
Object: Constructor called
Test: Constructor called
Object: Destructor called
Exception caught
Test pointer: 0x1

In this case you can find out that the Test instance is freed without calling its destructor. Also, the value of test is not changed.