Using Colours in LaTeX

A brief departure from the bookdesign series, as some of my future posts within and outside the series will be dealing with the use of colours, so I might as well get it out of the way first. Summarily, we’ll take a look at how to use colours in LaTeX with the xcolor package. This will work with all outputs, i.e. .dvi, .ps and .pdf.

The most useful commands for applying colours made available by xcolor are:

  • \color{color} (applies color to the text in the currrent group)
  • \textcolor{color}{text} (applies color to the specified text only)
  • \pagecolor{color} (colors the entire page to be color)
  • \colorbox{color}{text} (creates a box containing the specified text, with color as the background)
  • \fcolorbox{frame color}{bg color}{text} (same as above, but with a coloured border around the box)

See Section 2.6 of the xcolor manual for full list and more details of commands for colour application. Alternatively, if you’re using emacs, you can look it up with M-x list-colors-display.

The xcolor package has quite a collection of pre-defined colours; they are listed in Section 4 Colors by Name in the manual. Apart from the 19 “base” colours that are always available, you can also access more pre-defined colours via the dvipsnames, svgnames and x11names options. Here’s a quick example:

\documentclass[a6paper,12pt]{article}
\usepackage[x11names]{xcolor}

\begin{document}

% ‘LemonChiffon1’ from x11names
\pagecolor{LemonChiffon1}

% ‘magenta’ is a base colour
\textcolor{magenta}{Hello World!}

% ‘CadeBlue1’, ‘Firebrick2’ and ‘Goldenrod1’
% are available via x11names
What a \colorbox{CadetBlue1}{wonderful}
\fcolorbox{Firebrick2}{Goldenrod1}{world}.
\end{document}

You can mix up your own colours, too. \color{LemonChiffon1!80} means 80% LemonChiffon1 and 20% white, while \color{lime!30!yellow!60!Mahogany} would be 30% lime, 60% yellow, and 10% Mahogany. You can also give names to the colours you concoct, so that you can reuse them at various points in your document. For example:

  • \definecolor[named]{CoolBlack}{cmyk}{.3,0,0,1} defines the CMYK ‘Cool Black’ (useful if your printer asks for CMYK black instead of ‘rich black’)
  • \definecolor[named]{AquaBlue2}{rgb}{.553,.769,.98} defines ‘AquaBlue2’ with decimal RGB values (range [0,1])
  • \definecolor[named]{Hazelnut}{HTML}{BDA59B} defines ‘Hazelnut’ with HTML RGB values. Useful if you’re re-using colour definitions from CSS styesheets.

If you’re like me i.e. you just can’t hit the right figures to get a colour just right, head over to COLOURlovers for lotsa inspirations and colour schemes. I got almost all my colours for the Grid Computing Cluster report from this website.

Have fun and happy LaTeXing!

Setting page size and margins

There’s quite a bit of interest in the .tex code behind the Grid Computing Cluster report, but I think posting the raw code in its entirety would be a bit too overwhelming to quickly glean useful tips from it. (Also to avoid getting into any issues with my university… etc…)

So instead, I’ll be doing a series of short posts on how I achieved certain effects and book design, starting with this one. It’s probably faster for people to “zero-in” on what they need. 😉 Posts in this series will be tagged “book design”. But before you implement your next major book project with LaTeX, do check with your printing company about the printing process used. If you’ll be using on-demand laser printing, everything should go fine since the RGB colour model of your PDF should be adequate. However, if you’re going with offset printing, your printing company will most likely prefer CMYK colour model to be used instead, and I haven’t quite figured out how to tweak that perfectly with (pdf)LaTeX. In our case, only the book cover was printed with offset printing, and the effect was acceptable — so that ended well…

IMPORTANT DISCLAIMER: I am NOT a graphics designer, so my choice of colours/illustrations/etc probably sucks. Don’t shoot me. 🙂

Now there are multiple ways to achieve the same effect in LaTeX (that’s FOSS for you). For this report, I’ve decided to use the memoir document class, which incorporates the functionalities of many other packages. (Do take some time to browse or search through the excellent manual with texdoc or mthelp.)

The flowfram package looks like a very interesting solution for designing dynamic layout, but I didn’t find out about it until too late into my own project. Do find some time to look at it yourself. If you want to go with the standard document classes (report, book) or the koma-script classes, I’ll indicate what extra packages you’ll need where relevant.

In this first post we’ll just look at how (custom) page sizes and margins can be specified. Basically my page design is:

  • Stock paper size is 303mm x 216mm. The stock paper is the raw original paper that a commercial printer will print the page on, which will be trimmed down to the page size
  • Page size is A4 i.e. 297mm x 210mm.
  • The printer advised us to leave a good 10mm from the page edges around any printed text.
  • I decided the main typeblock will be 20mm from the top, bottom and spine (inner) edge of the page, and 15mm from the outer edge.
  • Also, the running header should occupy the height of a line of text, 7mm from the top edge of the typeblock. Whereas the baseline of the running footer is 7mm from the bottom edge of the typeblock.

These are not necessarily good measurements for an aesthetically-pleasing book design, but more because I was trying to strike a balance between aesthetics and to fit everything in (Lesson: Get in touch with the printing company early to check with their printing specifications per minimum page margins, etc!)

These can be implemented using commands from the memoir class. (The notion of ‘stock’, although a common concept in publishing and printing industries, is only available in memoir, not in the standard document classes.)

\documentclass[twoside]{memoir}

%% The stock paper size
\setstocksize{303mm}{216mm}

%% The ‘real’ page size
\settrimmedsize{297mm}{210mm}{*}

%% The stock paper will be trimmed 3mm from the
%% top edge and 3mm from the left edge

\settrims{3mm}{3mm}

%% Spine and trim page margins from main typeblock
\setlrmarginsandblock{20mm}{15mm}{*}

%% Top and bottom page margins from main typeblock
\setulmarginsandblock{20mm}{20mm}{*}

%% Header occupies height of a single line;
%% Bottom edge of footer is 7mm from bottom edge
%% of typeblock

\setheadfoot{\baselineskip}{7mm}

%% Bottom edge of header is 7mm from top edge of
%% typeblock

\setlength\headsep{7mm}

%% Apply and enforce layout
\checkandfixthelayout

In my case, due to some later issues with colouring the 3mm bleeding edges too (as per our printing company’s request), I decided to make the trimmed page size the same as my stock size and increased the page margins instead:

\documentclass[twoside]{memoir}

%% Custom stock paper and page size
\setstocksize{303mm}{216mm}
\settrimmedsize{\stockheight}{\stockwidth}{*}

%% Adjust margins accordingly to achieve
%% same previous layout

\setlrmarginsandblock{23mm}{18mm}{*}
\setulmarginsandblock{23mm}{23mm}{*}

\setheadfoot{\baselineskip}{7mm}
\setlength\headsep{7mm}

\checkandfixthelayout

If you just need to print on exactly A4-sized paper, you can forget about setting the stock and trimmed size explicitly and just do

\documentclass[a4paper,twoside]{memoir}

\setlrmarginsandblock…
\setulmarginsandblock…

\checkandfixthelayout

One last thing: if you’re not using the memoir class, you can still change the page margins via a high-level interface with the geometry package:

\documentclass{article}
\usepackage{geometry}
\geometry{
   paperwidth=216mm, paperheight=303mm,
   left=23mm,  %% or inner=23mm
   right=18mm, %% or outer=18mm
   top=23mm, bottom=23mm,
   headheight=\baselineskip,
   headsep=7mm,
   footskip=7mm
}

Again, if you’re just going to print on A4 (or letter) paper, you can just do

\usepackage[a4paper]{geometry}
\geometry{
   left=20mm,  %% or inner=20mm
   right=15mm, %% or outer=15mm
   top=20mm, bottom=20mm,
   headheight=\baselineskip,
   headsep=7mm,
   footskip=7mm
}

i.e. just pass the a4paper option to the geometry package; there’s then no need to set the paper height and width explicitly.

Unfolding vim-latex fold

I don’t know about you guys but I found it is very annoying to have my sections and subsections to be folded in vim/gvim , since somehow I mistakenly put “i” since I want to change to insert mode to unfold (although it should be :za) .

So now, the cruelest method is by

:set foldlevel = 5

usually I put 10 heh.

(since section,subsection and subsubsection may =3 .. so play safe with above that level).

But so far I did not managed to get it work by putting in in $HOME/.vimrc . Any hints?

My first presentation with LaTeX

I spent my precious nites doing these stuffs. Everything went well except I didn’t compiled my *.tex twice so that my Tikz arrows drawn perfectly.

Next 6th Nov I’ll be going to present another talk.. I hope I can avoid such problem again.

Tex Source
My Presentation Slides [HTML]
My Slides [PDF, 70 slides since I did some overlays]

You can refer my .tex file and do whatever changes that you want… but I’ll be happy if you tell me where will you present your talk using my so called *masterpiece* 😀

By the way, I converted those Beamer PDFs using external command, that is gs

I tweak some recommendations here

So, what I did was;

gs -dNOPAUSE -g1024x768 -r205 -sDEVICE=pngalpha -sOutputFile=./temp/slide_%02d.png -dBATCH beamer-foss-my-09.pdf cd temp/new/

//02d since this will correctly numbers your slides sequence from 00,01,10,11.. otherwise it will assume 1,11,10 is within the same order(as you can see, in the link that I gave before it doesn’t stated that… so “man gs” did helped 🙂

for i in *.png; do convert -resize 600 $i temp/new/$i ; done

//you need to install ImageMagick package to do this.. “convert” command will resize the initially generated PNG from gs to smaller size. I did tried to set smaller size in gs command but seems it just bring partial of the slides into smaller pieces…

igal2 -r *.png -xy 300 -w 4
//this will create HTML galleries, initially the tool named “igal”

Some components of my *.tex file were adapted from texample.net …

KDE Beamer Themes

FOSS.my is this Friday, and even though I can’t attend, perhaps there are some presenters who’d be interested to use Beamer to prepare their presentations, and perhaps using KDE themes: you can download the theme packages (.sty and .png files) from that link there. Once downloaded, put them in your TEXMF tree (~/texmf/tex/latex on TeXLive and MikTeX, ~/Library/texmf/tex/latex on MacTeX), and refresh it (use the MikTeX control panel; or run texhash if you’re using TeXLive. If using MacTeX, you won’t need to manually run texhash or anything).

By the way, you’ll find templates for OOImpress and KPresenter via that link, too.

To use the Oxygen-style theme:

\documentclass{beamer}
\usetheme{oxygen}

\begin{document}
\begin{frame}

\end{frame}

\end{document}

Sample output:

To use the Air theme and the output:

\documentclass{beamer}
\usetheme{air}

There’s also a PNG image which can be used as background for presentations at the link above (desenho.png). To use this with the default beamer theme, do:

\documentclass{beamer}
\setbeamertemplate{background}{\includegraphics[width=\paperwidth,height=\paperheight]{desenho}}

Happy LaTeXing!

Background images

I’m sure every LaTeX user tried this code at least once to add a “background image” on a page:

\includegraphics[width=\pagewidth,height=\textwidth]{wallpaper_filename}

But the included graphics, even though it looks the right size, would refuse to budge beyond the page margins, leaving a white border all round it. And you’d probably have problem placing any text at all on the graphics, thus defeating the purpose of a “background” image.

The wallpaper package offers an easy way to add background images or wallpapers in LaTeX, including tiling. The following commands are available:

  • \CenterWallPaper{}{} where is a value between 0 and 1, for specifying the fraction of paper width or height. The aspect ratio of the graphics will not be changed, i.e. you won’t get a “stretched” wallpaper as you would with your desktop wallpapers.
  • \ULCornerWallPaper{}{}
  • \LLCornerWallPaper{}{}
  • \URCornerWallPaper{}{}
  • \LRCornerWallPaper{}{}
The above four commands take the same arguments as \CenterWallPaper, but puts the image at the Upper Left, Lower Left, Upper Right and Lower Right corners of the page respectively.

For tiling wallpapers, there are:

  • \TileWallPaper{}{}{}, where you can specify (in pt, cm, in…) the width and height of the included image.
  • \TileSquareWallPaper{}{}, where the included image would be re-shaped as a square, and is the number of “image tiles” that will fill up the width of the paper.

Note that all the commands above will apply the background images on every page after the command is issued. To clear the wallpaper, use \ClearWallPaper.

Also, for each command \XXXWallPaper command above, there is a \ThisXXXWallPaper command that takes the same arguments but would apply the background image on only the current page.

You can now run wild with your imagination to design letterheads, chapter headings, covers, etc. Have fun!

Latex beamer and blocks

Block

\begin{frame}
\frametitle{Portable Object (PO) files}

\begin{block}{Nature}

\begin{itemize}

\item It’s a raw, untranslated file

\item Created automatically using gettext package

\item Ready to be translated

\end{itemize}

\end{block}

\end{frame}

produce this :

Alertblock

\begin{frame}
\subsection{gettext}
\frametitle{gettext}
\begin{alertblock}{gettext in short}
\begin{itemize}
\item Part of GNU packages
\item Enables internationalization of software
\item Enables the creation of Portable Object (PO) file
\end{itemize}
\end{alertblock}
\begin{center}
\alert{\emph{Portable Object?}}
\end{center}
\end{frame}

produce this :

Exampleblock

\begin{frame}
\frametitle{Machine Object (MO) files}

\begin{exampleblock}{Description}

\begin{itemize}

\item \alert {Compiled file}, derived from PO

\item It’s a binary, thus \emph{machine readable}

\end{itemize}

\end{exampleblock}

\end{frame}

produce this:


Good luck !

Name that symbol!

If you wanted to insert some symbol in a word processing application, you’d probably open up a character palette or symbol map, and browse through various fonts (wingdings, dingbats…) to find that elusive symbol.

In LaTeX, the Comprehensive LaTeX Symbol List has just about every symbol imaginable in LaTeX, from mathematical operators and symbols (including Greek letters), the Euro symbol, wingdings, dingbats, blah blah blah. The list is accessible as a PDF on your local installation via the commands texdoc comprehensive on TeXLive, or mthelp –view comprehensive on MikTeX.

However, browsing through the list is still rather tedious, even if the document has a good index to make searching a leeeeeetle bit easier. Detexify is a free online tool that greatly simplifies the hunt for a LaTeX symbols. You sketch your symbol with the mouse, and detexify tries to classify it and returns the appropriate LaTeX package and command required for rendering it.

Here’s an example of a “null” symbol. The 2nd symbol in the list is the one I’m looking for, so I’m going to include the lines

\usepackage{amssymb} $… \varnothing … $
in my .tex file.

Here’s another example of a maths symbol.

Wingdings and dingbats work, too. In fact, I haven’t even finished drawing the envelope, and detexify has already showed me \Letter from the marvosym package.

Drawing diagram; the cryptic way

Well, this is not-so-latex stuffs, but I just want to share with you guys…

I managed to tried out 3 applications:

Let’s see for Tikz;

%modified from texample.net
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{mindmap,trees}
\usepackage{verbatim}

\begin{document}
\pagestyle{empty}

\begin{tikzpicture}
\path[mindmap,concept color=red!50!black,text=white]
node[concept] {Hidup}
[clockwise from=0]
child[concept color=green!60!blue]
{
node[concept] {Pertimbangan}
[clockwise from=90]
child { node[concept] {cari duit} }
child { node[concept] {buat amal ibadat} }
child { node[concept] {belajar sungguh-sungguh} }
child { node[concept] {bina keluarga bahagia} }
}
child[concept color=orange] {
node[concept] {research}
[clockwise from=-30]
child { node[concept] {jurnal} }
child { node[concept] {artikel} }
};
\end{tikzpicture}\end{document}

will produce

then how about ditaa;

Raw source snapshot:


will produce:


and finally graphviz;

Source, the “dot” file:

digraph process{
node [color=blue,fontcolor=black,font=helvetica,]
nodesep=1.5
A[label=”Machine Object (MO)”]
B[label=”Portable Object (PO)”]

C[label=”developer”]
D[label=”translator”]
E[label=”tools”]
F[label=”POedit”]
G[label=”kbabel”]

C->B[label=”prepares PO”,style=dashed]
C->A[label=”compiles MO”,style=dashed]
C->B->D [label=”send PO files/i18n”,fontcolor=red]
D->B->C[label=”return translated files/l10n”,fontcolor=green ]
D->E[label=”translate using”,fontcolor=blue,style=dashed]
E->F
E->G
}

returns;