Saturday, February 27, 2010

Converting Excel tables to LaTeX tables

Creating a table in Microsoft Excel is far more easier than creating a table in LaTeX. Thus in this post, I will show you two ways on how you can convert the tables you produce in Excel to LaTeX format.

Method 1: excel2latex, an Excel plugin

Download the excel2latex plugin from here. Once downloaded, double click on it and it will install itself as an Excel plugin.

Once installed, you will be able to access it from the Add-ins tab in Excel:


Then you can select a table and click on the 'Convert Table to Latex' plugin to render the LaTeX format:


Note that if you enable the Booktabs-style formatting checkbox, you must include the following package in preamble of your LaTeX document:

\usepackage{booktabs}

This is the full code of the table it generated (without Booktabs-style formatting):

% Table generated by Excel2LaTeX from sheet 'Sheet2'
\begin{table}[htbp]
  \centering
  \caption{Add caption}
    \begin{tabular}{|cccc|}
    \hline
    \multicolumn{ 4}{|c}{Fully Random Arrays} \\
    \hline
          & Heap Sort & Quick Sort & Merge Sort \\
    \hline
    Input Size & Average Time & Average Time & Average Time \\
    \hline
    2     & 0     & 0     & 0 \\
    3     & 0.95  & 0     & 0 \\
    4     & 1.934498 & 0.477121 & 0.477121 \\
    5     & 3.158061 & 1.672098 & 2.075289 \\
    \hline
    \end{tabular}
  \label{tab:addlabel}
\end{table}

Note: I did encounter some very minor issues with the resultant LaTeX code from excel2latex, mainly when it came to rendering the outlines of certain tables.

For example, to render the table with the same borders it had in Excel, this line generated by the plugin:

multicolumn{ 4}{|c}{Fully Random Arrays} \\

must be changed to the following (notice the extra pipeline | in {|c|}):

multicolumn{ 4}{|c|}{Fully Random Arrays} \\

Method 2: Using GNOME's Project, Gnumeric

Gnumeric is another Spreadsheet application, but unlike Microsoft Excel, it supports the functionality of exporting spreadsheets to .tex format.



Gnumeric offers two possibilities. You can either save the spreadsheet as a table fragment, ie generating the following:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                                                  %%
%%  This is a LaTeX2e table fragment exported from Gnumeric.        %%
%%                                                                  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Fully Random Arrays & & &\\
 &Heap Sort &Quick Sort &Merge Sort\\
Input Size &Average Time &Average Time &Average Time\\
2 &0 & &\\
3 &0.95 &0 &0\\
4 &1.93449845124357 &0.47712125471966 &0.47712125471966\\
5 &3.1580607939366 &1.67209785793572 &2.07528852905168\\

Or you can save the whole spreadsheet as a complete LaTeX document, in which case it is probably an overkill for our current scenario of just rendering a single table.

Why can you write arr[n] == n[arr] in C ?

Try the following program in C :

#include <stdio.h>

int main () {
  int arr[] = {42};
  printf("%d", arr[0] == 0[arr]);
  return 0;
}

The above program prints out 1...ie, true.

But why...?

Well the reason the above works is because of how arrays are represented in C. In C, when referencing an array, you're just referencing a pointer to the first element :

int arr[] = {42, 39};
int x = arr[0];
int y = *(arr + 0);
// x == y

In the *(arr + 0) example, we are accessing the first element of the array by dereferencing the pointer to the first element arr plus the 0 offset.

int arr[] = {42, 39};
int x = arr[1];
int y = *(arr + 1);
// x == y

In the above example, we are accessing the second element with index 1.

Therefore, if *(arr + 1) evaluates to arr[1], then surely *(1 + arr) evaluates to 1[arr] according to elementary math...because addition is commutative, which means that the order of the operands does not matter, as it produces the same result.

The reason for this is because since C was designed back in the 70s, computers did not have much memory and thus the C compiler didn't do much syntax checking. Therefore, something like arr[i] was blindly translated to *(arr + i)

Wednesday, February 10, 2010

Finding the logarithm of any base in Java

Unfortunately, the only methods in Java that compute the logarithm of a number are java.lang.Math.log and java.lang.Math.log10. The former returns the natural logarithm (base e) of the number and the latter returns the base 10 logarithm of the number.

To calculate the logarithm of any base in Java, we thus have to use the following method:

public double logOfBase(int base, int num) {
    return Math.log(num) / Math.log(base);
}

Why does it work?


Let's say we want to find the base 2 logarithm of 32 (method call would be logOfBase(2, 32)), which is 5.


Now if we divide the base e logarithm (ln) of our number (32) by the base e logarithm of our base (2), we get the answer we wanted:



Wednesday, February 3, 2010

Scrolling page title with JavaScript and jQuery

The following is a jQuery function I wrote that allows you to make a scrolling page title.

I admit...it's a pretty useless "feature" but I just made it to kill some time =D

Usage

The simplest usage is as follows:

$.marqueeTitle();

The above snippet will use your existing title text to scroll.

You can also pass in an object with options to alter the script's behavior. The options are the following:
  • text - Use this parameter to set custom text if you don't want the scrolling text to be taken from the title
  • dir - "left" or "right"; by default, it's set to "left"
  • speed - The time it takes, in ms, for one character rotation

Here's another example, now demonstrating the parameters:

$.marqueeTitle({
  text: "This my custom text",
  dir: "right",
  speed: 500
});

Source

(function ($) {
    var shift = {
        "left": function (a) {
            a.push(a.shift());
        },
        "right": function (a) {
            a.unshift(a.pop());
        }
    };
    $.marqueeTitle = function (options) {
        var opts = $.extend({},
        {
            text: "",
            dir: "left",
            speed: 200
        }, options),
            t = (opts.text || document.title).split("");
        if (!t) {
            return;
        }
        t.push(" ");
        setInterval(function () {
            var f = shift[opts.dir];
            if (f) {
                f(t);
                document.title = t.join("");
            }
        }, opts.speed);
    };
}(jQuery));