Ipe: Using a different font

Text included in Ipe figures are rendered using the default LaTeX font which can sometimes look pretty thin. To use a different font for a Ipe figure, open Edit → Document Properties and add the font package in the Latex preamble section. For example, the left figure was generated using default font and the one on the right using this font package:

\usepackage[T1]{fontenc}
\usepackage[math]{iwona}

LaTeX: Import & Subimport for Document Organization

Large LaTeX documents can be organized nicely by using the \input command. For more on that see this post. However, the \input command is not intelligent, it blindly includes the content of its files.

For example, a Report.tex includes a 01-IntroDir/01-Intro.tex file that uses lots of images stored in 01-IntroDir. All of the \includegraphics commands in 01-IntroDir/01-Intro.tex would have to include the 01-IntroDir/ prefix:

% 01-IntroDir/01-Intro.tex
\includegraphics{01-IntroDir/Image0.png}
\includegraphics{01-IntroDir/Image1.png}
\includegraphics{01-IntroDir/Image2.png}

To intelligently include the content in other directories such that all their content paths are also relative, the \import and \subimport commands from the import package can be used.

For example, Report.tex can be changed to:

% Report.tex
\usepackage{import}
\subimport{01-IntroDir/}{01-Intro.tex}

When \import or \subimport are used, the imported files can refer to their images/files relative to themselves. So, the above 01-IntroDir/01-Intro.tex can be simplified to:

% 01-IntroDir/01-Intro.tex
\includegraphics{Image0.png}
\includegraphics{Image1.png}
\includegraphics{Image2.png}

LaTeX: Using input command for document organization

Creating and maintaining a large LaTeX document using a single .tex file can be painful. It would be wise to organize it as sections, with one or more .tex files for each section.

For example, a large Report.tex could be broken into 2 smaller sections: 01-Intro.tex and 02-Results.tex. These files can be included into the main Report.tex using the \input command like this:

% Report.tex
\input{01-Intro.tex}
\input{02-Results.tex}

If 01-Intro.tex and 02-Results.tex include a lot of images, then it might be nice to move them and their respective images/files into their own subdirectories, say 01-IntroDir and 02-ResultsDir. In that case, Report.tex is changed to:

% Report.tex
\input{01-IntroDir/01-Intro.tex}
\input{02-ResultsDir/02-Results.tex}

Note that the \input command just pulls the text from 01-IntroDir/01-Intro.tex into the main Report.tex for compilation. Thus, inside 01-IntroDir/01-Intro.tex commands like \includegraphics still need to provide the full path of their image relative to the main Report.tex:

% 01-IntroDir/01-Intro.tex

% Image IntroPic.png is in 01-IntroDir

% This does not work:
\includegraphics{IntroPic.png}

% This is how to correctly include:
\includegraphics{01-IntroDir/IntroPic.png}

Notes from Effective STL

I recently read Effective STL by Scott Meyers. My review of the book can be found here. Below are my notes from the book.

  • One guideline that is missing in the book: Do not extend the STL containers. It is possible, but brings about nothing but misery.
  • Item 1: Choose your containers with care

    The standard sequence containers are: vector, list, deque, string
    The standard associative containers are: set, multiset, map, multimap

    string is typedef of basic_string<char>
    wstring is typedef of basic_string<wchar_t>

    Scott does not mention this, but I find this useful because when you get compilation errors on string, they will mention basic_string<> and not string.

  • Item 2: Beware of the illusion of container-independent code

    This item mentions the most important trick to cut the verbosity of STL: Typedef everything possible.

    For example:
    class Foo;
    typedef std::vector<Foo> FooVec;
    typedef FooVec::iterator FooVecIter;
    
  • Item 4: Call empty() instead of checking size() against zero

    empty() is a constant time operation on all containers. size() can sometimes be a linear time operation on certain containers, like std::list.
  • Item 5: Prefer range member functions to their single element counterparts

    Instead of doing:
    for (int i = 0; i < MAX; ++i)
        vec.push_back(arr[i]);
    

    Try this:

    std::copy( arr, arr, std::back_inserter(vec) );
    

    For an example usage of std::back_inserter see this post.

  • Item 9: Choose carefully among erasing options

    To me, this is STL’s biggest gotcha! std::remove() does not remove elements from the container.

    Use the erase-remove idiom to achieve actual removal:
    fooVec.erase( std::remove(), fooVec.end() );
    
  • Item 14: Use reserve to avoid unnecessary reallocations
  • Item 16: Know how to pass vector and string data to legacy APIs

    if ( !fooVec.empty() )  // This is important!
        someCFunctionCall( &v[0], v.size() );
    
  • Item 18: Avoid using vector<bool>

    It is a pseudo-container with a compressed representation of bools. Avoid using std::vector<bool>, use std::deque<bool> instead. It offers everything that the former does.
  • Item 21: Always have comparison functions return false for equal values
  • Item 23: Consider replacing associative containers with sorted vectors

    If the container is used in a phased manner, first phase only for insertions and the second phase only for lookups, then a sequence container might offer better performance than an associative container.
  • Item 27: Use distance and advance to convert a container’s const_iterator to iterator

    STLContainer v;
    STLContainer::const_iterator ci = SomeFunction();
    STLContainer::iterator i( v.begin() );
    std::advance( i, std::distance<STLContainer::const_iterator>( i, ci ));
    
  • Item 28: Understand how to use a reverse_iterator’s base iterator

    The base() iterator of a reverse_iterator points 1 element in front of the reverse_iterator position. Use with care.
  • Item 29: Consider istreambuf_iterator for character-by-character input

    Useful for unformatted input from files:

    // Read file to string
    std::ifstream iFile("haha.txt");
    std::string fileData( std::istreambuf_iterator<char>( iFile ) ), std::istreambuf_iterator<char>() );
    
  • Item 30: Make sure destination ranges are big enough

    This usually leads to bugs. Instead, whenever possible use one of the inserters: std::inserter, std::back_inserter or std::front_inserter.

    For an example usage of std::back_inserter see this post.
  • Item 31: Know your sorting options

    Ordered by performance, best to worst:
    partition
    stable_partition
    nth_element
    partial_sort
    sort
    stable_sort
  • Item 34: Note which algorithms expect sorted ranges

    These are:
    binary_search
    lower_bound
    upper_bound
    equal_range
    set_union
    set_intersection
    set_difference
    set_symmetric_difference
    merge
    inplace_merge
    includes
  • Item 37: Use accumulate or for_each to summarize ranges
  • Item 39: Make predicates pure functions

    Predicate is a function that returns bool.
    Pure function is a function whose return value depends only on its input parameters. It does not have any side effects.
  • Item 40: Make functor classes adaptable

    Inherit functors from std::unary_function or std::binary_function so that the necessary types are defined nicely for you.

    Defining functors as classes or structs is purely a matter of style. STL uses structs. Some may find it better since there is no need to declare it as public.
  • Item 43: Prefer algorithm calls to hand written loops
  • Item 44: Prefer member functions to algorithms with the same names
  • Item 45: Distinguish among count, find, binary_search, lower_bound, upper_bound and equal_range
  • Item 46: Consider function objects instead of functions as algorithm parameters

    Passing functors is actually faster than passing pointer to a function! :-)

C++: Timing the code

Timing C++ code is easy using the clock function call from the <ctime> header file:

#include <ctime>
clock_t begin = clock();
doSomething();
clock_t end = clock();
double timeSec = (end - begin) / static_cast<double>( CLOCKS_PER_SEC );

CLOCKS_PER_SEC is typically defined as 1000 in most C++ library implementations. So, the clock resolution is typically 1 millisecond.

If you need higher resolution timing on Windows, check out the high-resolution performance counter described here.

Tried with: Visual C++ 2008

C++ STL: Find common elements of 2 sorted vectors

Finding the elements common among two sorted vectors and storing the (common) result in a third vector can be done using the std::set_intersection algorithm as follows:

std::set_intersection(	vec0.begin(), vec0.end(),
						vec1.begin(), vec1.end(),
						vec2.begin()	);

Note that std::set_intersection expects the result vector to be of enough size, i.e. of size max( vec0.size(), vec1.size() ). I find this ugly since the result vector needs to be filled with junk elements and its space is wasted depending on the result of the intersection.

Thankfully, STL has std::back_inserter which can handle this situation:

std::set_intersection(	vec0.begin(), vec0.end(),
						vec1.begin(), vec1.end(),
						std::back_inserter( vec2 )	);

The std::back_inserter acts as an iterator to std::set_intersection while it uses std::vector.push_back() to insert each new element given by it. So, the resulting vector does not need to be initialized to an appropriate size and after std::set_intersection it has only the result elements and has exactly that size.

C++ STL: Copy vector to array

The array behaves like a vector and so can be used almost everywhere a vector is used. So, a vector can be copied into an array using std::copy algorithm. But, make sure that the array is big enough to hold the elements of the vector when you do this:

#include <vector>

std::vector<Foo> fooVec;
Foo fooArr[FOO_MAX];

std::copy( fooVec.begin(), fooVec.end(), fooArr );

Dell Vostro 3300

My new Dell Vostro 3300 laptop arrived yesterday. It took about 10 days from ordering online to delivery. This is my second laptop and will be replacing my 4-year old dinosaur Fujitsu S7021.

The Vostro 3300 ships with a Intel Core i3 330M (2.13 GHz) processor, 2GB RAM and 320 GB harddisk. The display is 13-inch widescreen. I will be using 64-bit versions of Windows 7 and Ubuntu 10.04 (Lucid Lynx) on this baby.

The top and side exterior is brushed metal and the rest is plastic. The lid is super thin and the sits sleekly over the nicely polished sides. The plastic surrounding the inside display looks a bit out of place in the middle of all this. The keyboard is chiclet and the font used for the keys is large and cute.

LaTeX: Color Text

Adding color to text in LaTeX is super easy!

Include the color package:

\usepackage{color}

Specify the color and the text you want colored to the textcolor command wherever needed:

\textcolor{red}{This is colored in red!}

To see other predefined color names go here.

Beamer: Divide Frame into Columns

In a Beamer frame, you might need to present 2 or more figures or a figure and textual content beside each other. The columns environment of Beamer can be used to achieve this by subdividing the frame into columns.

To do this enclose all the frame content within a columns environment:

\begin{frame}{Fixing an Optical Mouse}
\begin{columns}
% Frame content here
\end{columns}
\end{frame}

To create a column of content inside this environment of say, 0.6 width of the total frame width use:

\column{0.6\textwidth}
% Content of this column here

The content of this column follows after this. Do this for each column you want. The widths of all the columns should add up to 1.0.

The frame shown above has 2 columns, of width 0.6 and 0.4 respectively, holding a figure and some text. It was generated from this snippet of LaTeX code:

\begin{frame}{Fixing an Optical Mouse}
\begin{columns}
	\column{0.6\textwidth}
	\includegraphics[scale=0.3]{mouse.png}

	\column{0.4\textwidth}
	\begin{itemize}
	\item{Open mouse.}
	\item{Fix internals of mouse.}
	\item{Close mouse.}
	\end{itemize}
\end{columns}
\end{frame}