Saturday, October 21, 2017

Compiling Latex with latexmk with tikz externalization and pre-compiled preamble (which includes tikz)

Compilation of LaTeX projects that contain tikz images can be sped up by an astronomical amount by properly separating the static and dynamic preamble and issuing the right commands.

A minimum-working-example that I wrote that demonstrates this in great detail can be found at:

Sunday, July 2, 2017

Ubuntu - Copy large number of files easily to Android Phone


The MTP protocol for transferring files to and from an Android Phone is utter garbage, especially when transferring a large number of files (like your Music collection for the first time - on my Google Pixel). The connection inevitably fails in the middle of the transfer, which is slow to boot.


USB tether your phone to your computer (read if you get stuck). You don't even need internet pass-through, just an IP Address (so you can use sudo dhclient usb0 instead of sudo dhcpcd usb0 if you are using the terminal).

Next follow to set up an ssh connection. Once you have that you are home free.

I use lftp over ssh to copy all the files I want. I even have a simple lftp bookmark set up:
pixel sftp://abid:xx@pixel
where my ssh config section is titled "pixel" and the password "xx" is just a red-herring since ssh is configured to use an RSA key for authentication. This bookmark allows me to access my phone's files using a simple lftp pixel.

Ubuntu - SSH in to Android Phone (Google Pixel)

The ability to SSH in to your (unrooted) Android Phone is a very powerful one to have, and it is surprisingly simple.

  1. Install the "SimpleSSHD" app on your phone (form the Google Play Store).
  2. Copy your PUBLIC ssh key (form your computer) to the "/sdcard/ssh" (default) folder on your phone and rename it authorized_keys.
  3. Run the "SimpleSSHD" app and click start (uses port 2222 be default since it runs in user space). The app will tell you the IP address of the phone.
  4. SSH in to your phone by issuing:
    ssh -o StrictHostKeyChecking=no -p 2222 -i <private key location> <phone ip address>
    You will need to issue StrictHostKeyChecking=no the first time only.
  5. To avoid having to type all of this out every time you can add a section to the ssh config file (~/.ssh/config).
    Host pixel
        Port 2222
        IdentityFile ~/.ssh/pixel
    and simply run ssh pixel to connect.
Nore: This technique works with USB tethering as well. SimpleSSHD will show both the WiFi and the USB Tethering IP Addresses and you can use the latter for super-fast ssh.

Ubuntu - USB Tethering with the Google Pixel


When I connected my Google Pixel to my Ubuntu (14.04) laptop with the USB cable and selected the option to USB tether on the phone the connection would fail. The Network Manager applet would show a greyed out option "Ethernet Network (Google Pixel)" with the status "disconnected" underneath it, also greyed out.

Interestingly running "ifconfig" revealed that a "usb0" section was present but no IP address had been assigned.
usb0      Link encap:Ethernet  HWaddr 32:a8:e6:6c:fe:9d  
          inet6 addr: fe80::30a8:e6ff:fe6c:fe9d/64 Scope:Link
          RX packets:5 errors:0 dropped:0 overruns:0 frame:0
          TX packets:13 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:560 (560.0 B)  TX bytes:1802 (1.8 KB)

This clue led to a long trawl across the internet that led to a surprisingly simple solution.


Install the "dhcpcd" package.
sudo apt-get install dhcpcd
Now after connecting the Google Pixel simply run:
sudo dhcpcd usb0
And confirm by running:
ifconfig usb0
route -n
ping -c3

Thursday, June 22, 2017

Compiling Latex with scons with tikz externalization and pre-compiled preamble (which includes tikz)

Edit: I have created another MWE that uses latexmk instead of scons (which I found to be using more resources and not recompiling correctly when citations were added):

Compilation of LaTeX projects that contain tikz images can be sped up by an astronomical amount by properly separating the static and dynamic preamble and issuing the right commands.

A minimum-working-example that I wrote that demonstrates this in great detail can be found at:

Ubuntu - Connecting to a PPTP VPN server using the terminal


I was away from Islamabad and needed to download some scientific papers. This required me to connect to my University's VPN which was of the PPTP variety. I tried using the Network Manager applet in Ubuntu 14.04 which would successfully connect but the moment I would open a page on Chrome the VPN connection would fail.


Connect to the VPN from the terminal using instructions that were written for Ubuntu 7.04 ( and were supposed to have become outdated 7 years ago. But what is in the command line can never die.

Based on help from and with a little tweaking I was able to successfully connect to the VPN in a stable fashion.

  1. Install pptp package: sudo apt-get install pptp-linux

  2. Create a peers file. I named mine 'comsats' so I created: /etc/ppp/peers/comsats with the following content
        pty "pptp <vpn server ip address> --nolaunchpppd"
        logfd 2
        ipparam comsats
        remotename comsats
        name <vpn username>
    This file needs to be executable (sudo chmod +x /etc/ppp/peers/comsats). Note the 'remotename' and the 'ipparam' which are set to 'comsats'. These will be used to activate the connection and to run automated scripts when the connection is setup and torn down.

  3. Add your VPN credentials to /etc/ppp/chap-secrets in the following format:
        * <vpn username> * <vpn password>
    At this point you can check your vpn connection by executing:
        sudo pon comsats
    For debugging the connection look at /var/log/syslog. At this stage your internet will NOT be forwarded over the vpn because the routes haven't been set up for it.

    To close the connection Ctrl+C is supposed to work but it didn't for me so I had to use ps -A | grep ppp to find the pid of the connection process and then sudo kill -KILL <pid> to kill it.

  4. To forward your internet packets over the VPN you must first find the ip addresses of the gateways for your internet interface (wlan0 in my case) and the vpn interface (ppp0). Simply use ipconfig. In my case my wifi gateway was and the vpn gateway was To take a look at the current routes issue the route command, it will show you the current default gateway.
    The first step is the delete the current default gateway:
        sudo route delete gw wlan0
    Note the use of my wlan0 gateway ip address (
    Now switch to using the ppp0 gateway as the default so that all outgoing internet traffic is sent via the vpn:
        sudo route add gw ppp0
    You can confirm the switch to using the VPN by looking at your external IP address (I simply navigate to and a whois to confirm).

  5. After closing the vpn connection you will have to revert the changes to the routes to get your normal internet access back (the Network Manager applet does all of this automatically, when it works) as follows:
        sudo route delete gw ppp0
        sudo add gw wlan0

  6. To automate the route changes one can add files to the /etc/ppp/ip-up.d and /etc/ppp/ip-down.d folders that are automatically run when the connection is setup and torn down respectively.
    Create and populate /etc/ppp/ip-up.d/comsats:
        [ "$PPP_IPPARAM" = "comsats" ] || exit 0
        route delete default gw wlan0
        route add default gw ppp0
    Note how we use the $PPP_IPPARAM to match against the same we defined in /etc/ppp/peers/comsats to make the code run only when this connection is set up.
    Similarly create and populate /etc/ppp/ip-down.d/comsats:
        [ "$PPP_IPPARAM" = "comsats" ] || exit 0
        route delete default gw ppp0
        route add default gw wlan0
    Finally comment out nodetach in /etc/ppp/peers/comsats to run the connection as a deamon and use sudo poff comsats to close the connection when you need to. This connection is not as stable as I would like it (usable however) so I use tail -F /var/log/syslog | ccze -A to keep an eye on the connection while I am using it, restarting it as needed.

Wednesday, June 21, 2017

Highlighted (syntax) code snippets in Blogger

My blogs being technical in nature often contain snippets of code. The old technique I was using for correctly highlighting them no longer works because the library I was using has gone the way of the dinosaurs. Thankfully, however, there is a much better (and slightly easier) way to achieve this using the highlight.js library (

  1. In your main Blogger page, from the pane on the left (which contains 'Posts', 'Stats', etc.) click on 'Theme'.
  2. In the frame that reloads on the right first choose 'Backup / Restore' and then 'Download theme' to save a backup of the unmodified theme before we mess with it.
  3. Press 'Close' to close the dialog/pop-up window and choose 'Edit HTML'.
  4. Before the </head> tag paste the following lines:
    <link href='//' rel='stylesheet'/>
    <script src='//'/>
  5. Choose 'Save Theme'

Note:   I chose to use the 'agate' theme (CSS), there are many others. You can take a look at the available options at To choose another one simply replace "agate" in the link with the theme you like.

Similarly, 'highlight.min.js' only supports a core of 35 common languages. To highlight other languages add additional scripts you can find at


Now to place a code snippet in your blog simply switch to "HTML" editing (instead of "Compose") and place the snippet inside a <pre><code> </code></pre> block or simply inside <code> </code> if quoting code inline. You can specify the language inside the "class" element of the "code" block (visit the highlight.js website for more details).

To create the following highlighted python snippet:
    def factorial(n):
        if n == 0:
            return 1
            return n * factorial(n - 1)

I used the following code in HTML mode:
    <pre><code class="python">    def factorial(n):
        if n == 0:
            return 1
            return n * factorial(n - 1)

Simple and elegant. Enjoy.

Note: To place HTML itself inside a highlighted snippet (very meta indeed, I needed it for the last section of code) you will have to escape all of the HTML. I used

Tuesday, June 20, 2017

Linux - Switch display from terminal (virtual and emulated) using xrandr

Situation: I had my laptop (running Ubuntu) connected to my TV via an HDMI cable with the display set to TV only. I then unplugged the HDMI cable without switching the display to Internal (using the "Displays" system settings). The display is supposed to switch automatically but on my machine this sometimes fails to happen leaving me with a running X session which I can't see.

However by pressing Ctrl+Alt+F2 (through F6) I am able to switch to a virtual terminal which shows up on the internal screen just fine.

Solution: Use xrandr to switch the display. But this requires two little tricks because xrandr is an X command and it normally fails on virtual terminals.

sleep 5 && xrandr -d :0 --output LVDS1 --auto

and quickly switch to the terminal running X (Ctrl+Alt+F7 on Ubuntu) before the sleep timer runs out.

Explanation: By sleeping for 5 seconds we allow ourselves time to switch to the virtual terminal running X so that when the xrandr command is issued the X Server is awake to receive it.

Since we are running xrandr from the virtual terminal we have to explicitly specify which display to target. The default display is ":0" meaning the first (zero-indexed) display on the local machine (for remote machine its IP address would precede the ':').

Finally --output LVDS1 --auto tells xrandr to switch to the internal display with default settings.