Enforcing LaTeX macro expansion orders and unwritten rules about formatting a macro

LaTeX document is actually a program, with text to be formatted. However they are not particularly friendly to program on. At least it takes a lot of trial and error…


I recently had to generate a rather complex table with colouring rules. (essentially a ‘heatmap’ table) To do this, I have used several packages: \colortbl, \color and \fp.

colortbl and color was used to set the colour of the cell itself using \cellcolor{} command. In addition, fp was needed to compute a colour based on the value by simply doing ‘1 – decimal value’ to get the ratio between the two colours in RGB. While LaTeX document is a program, is really not designed for us, endusers, to write a program that requires computation. By default, decimal point calculation is a nightmare, thus fp was imported to make it less painful.

One of the troublesome aspect is how LaTeX documents are compiled. The gist of this is basically a series of token expansion. It expands top to bottom, left to right, through a series of commands. And in the context of macros, something like this becomes a nightmare:


Something like this will probably cause an error indicating this is an invalid argument for \cellcolor{} as they expect three values in the range of 0.0-1.0 representing red, green, and blue.
To solve this, you need to prevent LaTeX compiler to attempt expanding \cellcolor{} before \mycommand . My initial attempt was to insert \expandafter to make LaTeX expand the later tokens to expand first. However, that did not solve this.


The solution ended up being rather hacky but almost similar to C macro:


UPDATE: The solution (I believe) does the following:

  • Define temporary macro using \edef
  • Avoid the expansion of \cellcolor by prepending \noexpand when \edef is being expanded to define a macro
  • But that did not stop expanding \mycommand
  • Calls the temporary macro defined with the result of \mycommand inserted

I don’t claim to understand perfectly how it was expanded, but I believe this is what has happened. This idea came from [2]. In addition, I found [1] to be useful in understanding how LaTeX parses the commands. (I also saw a great table summarising different commands used to instruct expansion order, which now I can’t find…)

In the process of trying this, I have also discovered that macros should have escapes for new lines (\n) as part of the macros by appending % at the end of each lines. As similar to macros, it will paste the whole block when being expanded. This lead to an interesting problem in the context of \tabular{} command where the text inside the cell had an odd padding in the cell, breaking centering column alignment, which was caused by this new lines interpreted and inserted. Thus if you want a sensible looking macros in LaTeX, where not everything is in a single line, end of line has to be escaped.

In short:

  • If \expandafter fails, try defining a temporary macro and use \noexpand.
  • Remember to escape the end of line when changing line in your multi-line LaTeX macro by using %.

[1] Overleaf – How does \expandafter work: An introduction to TeX tokens
[2] A Tutorial on \expandafter – Stephan v. Bechtolsheim http://www.tug.org/TUGboat/Articles/tb09-1/tb20bechtolsheim.pdf

UPDATE (2020 08 05): Added bit of explanation of the solution. Also fixed some typos…

Ubicomp 2019 September London Trip

image of ilnp testbed at ubicomp 2019

September has been a rather eventful month.

In the earlier post, talked about spontaneous star photography, but couple weeks before, I was away in London for a week attending conference and workshops. This time, it was for Ubicomp 2019.

Ubicomp is a conference about ubiquitous computing. Simply put, it’s about ‘computers everywhere, in our everyday life’. Sensor networks, IoT devices, wearable devices and etc. My supervisor, Prof. Saleem Bhatti, and I have submitted a paper to a workshop called Pervasive Urban Application Workshop (PURBA). Fortunately, our paper was accepted, so I took some of our kit to show them working.

Our paper, “Seamless internet connectivity for ubiquitous communication” (https://dl.acm.org/citation.cfm?doid=3341162.3349315) featured ‘continuous mobility’ experiment. The experiment emulates the movement of mobile device while we send and receive data continuously. We took the position that ‘ubiquitous computing’ needs ‘ubiquitous communication’; the ability to use any connectivity the device has and move between them smoothly. I was given a chance to present the paper and a live demo during the workshop, which was a success. Thanks to very friendly crowd, I had a very pleasant conversations with them. On top of that, I have managed to take the opportunity to do a spontaneous demo during the main part of the conference in the ‘social/networking area’ of the venue.

Photo of a table with a laptop showing slides. Two desktop computers with one display on the far right. The computers are connected with various cables with some routers in between.
ILNP demo at Ubicomp 2019

The demo featured something different from what we described in the paper. While the paper used benchmarking software iperf and emulation of movement was done by a script, in the demo I have physically attached and detached cables while a video stream was sent from the one side to the another. I believe this was much more compelling demo for two reasons: 1. video stream is a real application where mobility can enhance the experience 2. physically removing cable is visually more convincing how the data flow has migrated. And as suspected this drew some attention amongst more smaller demo kits and or booths with just a poster.

The original intention was to present everything on the first day and to keep rest of the week little less intense than lugging 20+ kg case and running demo all day. However I felt I should make the best use of the opportunity, so during the day I was rather busy all week. Luckily, I have managed to squeeze some meetups with good friends and some photography in.

It is always refreshing to be elsewhere to do some photography. It gives me chance to try new things and see different perspectives. While I hate how busy London is, it’s what makes it so diverse and exciting. I look forward to be down there again!

I’d like to thank my supervisor and the school for the funding to go to the Ubicomp 2019.

I ended up liking the following so that went to the flickr:

Joyful Hour
Twilight Market

Little glimpse of star trail

During the summer – early autumn, it’s not the easiest time to capture star. The sun set is rather late. This alone was enough to put me off from attempting one. But a friend of mine had dragged me out to do one after checking the weather.

Turns out, the field around observatory is a great place to do this. (duh) This was my first time doing star shoot around that area so I was little unsure, but it turned out to be a great location. The only gripe about the location is that because it is right next to the sports centre, we had to wait till they close to turn off the over-head lighting for the outdoor track. Hence, it had to be rather late.

This time, we met up at 23:00 with all our gears ready near the sports centre to scout the location. This time of the year is when the temperature and humidity varies quite rapidly throughout the day. And that particular day was rather humid while the temperature was dropping rapidly. This created rather inconvenient fog.

After nervously looking for the right path in rather dark field, we’ve arrived at the location. As our eyes got used to the darkness, we started to see the stars. It was simply astonishing. I always forget how beautiful the night sky is in St Andrews. That is, if the clouds happen to be not there that night. But when it is clear, this is one of the best place I know to see the star.

As we took couple test shots figuring out focus, exposure settings etc, couple of other friends joined. As we chatted we dialled in the setting further. And I began my long exposure. As the temperature further dropped, those who joined us had left, and it was just myself and the other friend who initially came out.

After a long wait, of about 45 minutes, which is probably the longest I’ve done, I nervously checked the image. And this is what I saw on the back of my camera:

Raw image before developing

I was quite excited. I could see the longest trail I’ve captured on single exposure. While I do understand ‘stacking’ is much better for noise performance but the sheer fact that it is captured like this to me is fascinating.

I continued to shoot at different angle for short 25 min exposure after that and that looked promising as well.

The next day, I sat down at my laptop and processed it properly with Lightroom:

45-min exposure at the observatory in St Andrews after processing

I think this worked out quite well. Hopefully, my next step will be to remember to shoot with tighter aperture and or to shoot with different focusing point for focus stacking… There is always more to try.

Link to my Flickr Profile: https://www.flickr.com/photos/coconutryo/

26th Birthday

I have once again added +1 to my age.

I have started by birthday, at my desk around 9:30 cracking on with PhD related work, to a meeting with my supervisor. The day continued on with, talking to my girlfriend Asta, some more work, then dinner with friends, ice cream to follow (jannetta’s, obviously) then back to office for more work.
My supervisor and I are working on this paper and I’m still collecting results.

I have received kind words congratulating on my birthday from various places, various medium, and in very varying time (due to timezones).

But I think I definitely have to give some of the credit to my parents dealing with me.

Thank you all, to have dealt with me for another year, and thank you for your support.

It has been a great year being 25, now we shall see what 26 is going to be like.

‘Shot Story’ No. 2 – 10 Years


This is a photo I’ve taken back in 2005. This is the year I started playing with a DSLR. (Before that, I did play with a point and shoot and some times with a film SLR) And I’ve been taking photos with some degree of passion up till now.

10 years ago from today, I’ve started a flickr account, and started uploading my photos. Photos I thought I shot well, made in to it and today, I have 389 photos.

Continue reading “‘Shot Story’ No. 2 – 10 Years”

2017 12 Japan Trip – Quick Update Day 1

Some of you may have seen slightly panicky facebook post yesterday but here’s a quick update.

I’m now back in Japan at my parents place in Tokyo. I’ll be around Tokyo till mid January then back to PhD again! (Not that I won’t be logging into my machines remotely over my mum’s computer as I left CS laptop in UK)

Despite the 30-45 min delay on Edinburg -> Brussels flight, I made it to NH 232 for Tokyo, Narita with my check-in baggage.

In the context of going home, as computer science student, I will be asked to take a look around things at home. Like your mum’s computer and home network devices. And when I did so, there was some discovery I wanted to address with regards to the topic of the Internet. So here we go (sorry for the long intro):

Continue reading “2017 12 Japan Trip – Quick Update Day 1”

‘Shot Story’ No. 1 – The Northern Lights over St Andrews

This is my new blog segment called ‘Shot Story’, where I plan to talk about the photos I have shot. Feel free to give suggestions in the comment section below with a link to a photo on my flickr! Equally, if you have any question/topic you want me to discuss, feel free to comment below!

Aurora Over St Andrews
I’ve always wanted to talk about photography. Its been on my ‘blog-topic-list’ for far too long. So I shall start with one of the most memorable shots in St Andrews. This definitely is a milestones in my photography experience: I caught the northern lights.

Continue reading “‘Shot Story’ No. 1 – The Northern Lights over St Andrews”

‘ILNP Talk’ – Linux Kernel Code Porting

Disclaimer: This is a rather overdue blog post I wrote a couple of months ago. I’ve made more progress than this! (There shall be a follow-up post soon…)

This might be the first time I actually touch on my research in this blog. I guess the time has come.

As I mentioned before, I’m working on a project to develop and implement a protocol called Identifier-Locator Network Protocol v6 (ILNPv6). This is a network protocol which implements the architecture my supervisor has designed; the Identifier-Locator Network Protocol (ILNP).

I’ll leave out the actual explanation of the ILNP for this post. However to give you the gist of what ILNP is trying to achieve, here are 2 of features/goals  that are relevant to my PhD:

Continue reading “‘ILNP Talk’ – Linux Kernel Code Porting”

Quick Summary of June 2017

So, two months have gone by since my last post! And on a side note, we’re already half way through 2017…

The absence of articles in the last two months has to proven me that writing is hard, blogging is hard and life is hard.

But within the last two months, a lot has happened. I think now is a good time to do a quick summary of it. Maybe I can come back and write more about them too.

Continue reading “Quick Summary of June 2017”