Tag Archives: TeX

How to use textpath with Metapost


It took me more than a few hours to get this working and as the manual is not at all fully clear on what you need to do (or rather, it assumes more knowledge of LaTeX than I had), I thought it would be useful to set this out.

Textpath allows one to draw text on a path (i.e., not just on a straight line). Here’s an example I prepared earlier:

Hardware Virtualisation

Decus et tutamen as the coins say.

But using it is not as simple as I hoped it would be and even the very fine The LATEX Graphics Companion does not cover it – this is despite textpath being in the standard free TeX distributions and the alternative they describe – TXP – requiring a manual install.

Here are the key steps you need to get it to work.

Firstly, you have to configure your metapost file as a full LaTeX file and not just a piece of Metapost.

So at the top:

input latexmp;
setupLaTeXMP(preamblefile="mypreamble");
input textpath;
filenametemplate "%j-%c.mps";

And then you need to create your LaTeX preamble – i.e. the file I have called “mypreamble.tex” here but you can call it anything.

Here is mine:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[osf]{mathpazo}
\usepackage{textpathmp}

This is what constrains your file as LaTeX.

This also means you have to end your file with an end after the endfig of the Metapost.

If you get some of this wrong you may find that you get an output but the strings (labels) are messed up – it is quite sensitive.

Here’s the file that generated the above graphic:


input latexmp;
setupLaTeXMP(preamblefile="mypreamble");
input textpath;
filenametemplate "%j-%c.mps";
beginfig(1);
prologues:=3;

%some colours
color lightgrey;
lightgrey:=(4/5, 4/5, 4/5);
color midgrey;
midgrey:=(3/5, 3/5, 3/5);
color darkgrey;
darkgrey=(2/5, 2/5, 2/5);
color gloom;
gloom=(1/5, 1/5, 1/5);

color azure;
azure:=(12/13, 1, 1);
color baby;
baby:=(12/13, 24/25, 1);
color lavender;
lavender=(23/26, 23/26, 49/50);
color ultra;
ultra=(8/13, 23/26, 12/13);
color ukraine;
ukraine=(66/255, 173/255, 222/255);
color turquoise;
turquoise=(0, 191/255, 1);

path hardware, virtlayer, kernel, user;
path hardwareL, virtlayerL, kernelL, userL;

hardware = fullcircle scaled 100;
virtlayer = fullcircle scaled 150;
kernel = fullcircle scaled 250;
user = fullcircle scaled 350;

hardwareL := reverse fullcircle scaled 75 rotated 180;
virtlayerL := reverse fullcircle scaled 125 rotated 180;
kernelL := reverse fullcircle scaled 225 rotated 160;
userL := reverse fullcircle scaled 325 rotated 160;

fill virtlayer withcolor lavender;
draw user;
fill user withcolor midgrey;
draw kernel;
fill kernel withcolor lightgrey;
draw virtlayer;
fill virtlayer withcolor lavender;
draw hardware;
fill hardware withcolor white;
draw textpath("Virtualisation", virtlayerL, 0);
draw textpath("Hardware", hardwareL, 0);
draw textpath("Kernel", kernelL, 0);

label.urt(btex Hardware virtualisation etex, (-200,-200));

endfig;
end;

There are lots of tricks to get the text looking the right way – e.g., inside or outside the path, left to right and so on. But they are described in the manual and I will leave them to readers to work out – getting the output at all is the most fundamental task!

How do I do this?


I am preparing a computer science slide presentation and (as I am sane) do not want to use Powerpoint.

So I have been using LyX and the Beamer class.

The slides look great. But how do I add notes?

If I add notes “by hand” e.g. \note{Here is a note} – I see nothing in the PDFs.

And if I use the LyX “Noteitem” I get something like this “Note: Here is a note” on screen, but again nothing in the PDFs.

What am I doing wrong? I have looked at the LyX wiki and it is not illuminating.

Anyone got any thoughts on the LaTeX companion?


Should I shell out £23 for The Latex Companion to ensure I can most effectively write my documents and design my slides for university? I have The LATEX Graphics Companion and there is no doubt it is a good book, but the number of books I could buy increases exponentially the more I think about the work I need to do. So, anyone have any practical experience with the book’s usefulness to a computer science research student with a middling level of LaTeX experience, who is likely to use LyX for a lot of his work?

I bought a book


“Programming Android: Java Programming for the New Generation of Mobile Devices” to be precise.

Now I just have to decide what I want to do with it! I had thought about a LaTeX based application, as both LaTeX and LyX are thinly supported on Android. But that looks very ambitious at the moment.

We’ll see.

LaTeX and O’Reilly


the tarsier featured on the cover of Learning ...
Image via Wikipedia

I have been in touch with Anslem Lingnau, the author of the (German-language) LaTeX Hacks: Tipps und Techniken für professionellen Textsatz – and he tells me that while he would love to see an English translation of his book there seems to be no sign of one forthcoming.

Indeed a quick scan seems to suggest that O’Reilly are not so great at publishing books on LaTeX.

Anslem is also a Scottish country dancing enthusiast of note, if Dashing White Sergeants are your thing…

LaTeX w00t!


The LaTeX logo, typeset with LaTeX
Image via Wikipedia

I owe Professor Paul A. Rubin another apology – turns out I can display LaTeX natively in a wordpress.com blog after all – as the following wave equation shows:

 

i\hbar\frac{\partial}{\partial t}\left|\Psi(t)\right>=H\left|\Psi(t)\right>

Red-black trees


Example red-black tree
Image via Wikipedia

Binary trees are seen and used frequently in computing science and computing. They are a good abstraction for many naturally occurring relationships (most of our mathematics is based on binary operations, for instance) and have O(log n) complexity (ie if you went from searching a tree of 1000 elements to a tree of 100,000 elements then the search should not take 100 times longer but about 10.)

Of course that log n goodness requires the tree to be “balanced” ie for any given node there should be roughly equal numbers to the left and the right. One way of doing this is through a “red black tree” – here nodes in the tree are assigned a colour: the root is always black. The rule is that any traversal from the root to the leaves should always go through an equal number of black nodes and to ensure this is possible red nodes may be inserted in the tree, but no red node may have another red node as an immediate descendant. (A full explanation is in Introduction to Algorithms though one can also work off various explanations on the internet, though they tend to be less than complete.)

The Linux kernel natively implements a red black tree (in C) and the first bit of work I started on my MSc project was to write my own, userland, implementation so I could see processes in the same way the kernel did.

As I had got a less than glorious mark (still a pass, so that’s what counts) in the C++ exam last year I also decided that I would write this in C++ using templates. A few days after I started I discovered that actually the writers of the STL had got there before me, but as this was an academic exercise I ploughed on with my own implementation.

Essentially this is what I did on my two week summer holiday in Scotland last year! When I was there I also started (though completed when I got home) a couple of helper applications to position the tree according to Reingold and Tilford’s algorithm (which I had to translate from PASCAL) for “better drawing of trees” and a Qt application to display it all.

In fact I had a nagging problem with the Reingold-Tilford algorithm which I finally got around to fixing last night.


LaTeX to PNG representation of processes

(Interestingly the code also allows you to use the Turing-complete capabilities of LaTeX by specifying a TeX output that uses LaTeX’s own positioning algorithm – something I picked up from The LATEX Graphics Companion – that is what the example shown above uses, though unfortunately for even moderately loaded systems the LaTeX processor objects to the width of the output).

Fancy trying it? I could do with someone giving it a bash on a BSD system – not needed for my course but interesting none the less.

The code is all at GitHubhttp://github.com/mcmenaminadrian: memball gives the basic GraphML or TeX or plaintext output, treedraw will convert the GraphML to an SVG or serialiszed stream using Reingold and Tilford’s algorithm and treeqt will use Qt to display the tree using the serialized class. You may have to download various libraries to get it to work (certainly the libproc-dev package on Ubuntu/Debian) – I couldn’t get memball to work on a Windows machine using Cygwin but maybe even that is fixable.

There is a script in the treeqt repo to make it easier: download the sources form all three repos, build them and then run:

./setup | ./treeqt --r 1

Smaller PNG graphic files from your LaTeX


I use LaTeX (LyX as the front end) to generate the occasional bits of mathematical script that appear here. Is it bad to admit it gives me a small thrill to see how the LaTex is transformed into something mathematical – makes me feel like a proper scientist/mathematician. Well, I’ve done it now…

WordPress.com will not display postscript natively (or if it can I have never been able to get it to work), so I usually open the .ps files in GIMP and convert them to a PNG – PNGs being optimised to handle things like line drawings and graphical images of text.

The PNGs that come out of this are of a reasonable size but they could be smaller. Some years ago I wrote some Perl code to do that (I needed something that worked in a CGI environment and while Perl is not so fast for even integer maths it was the best option).

That code is available on CPAN as Image::Pngslimmer and on a typical Linux/BSD (OSX too?) install (I am assuming you have perl and the cpan module installed) you should be able to get it on to your box with sudo cpan Image::Pngslimmer – which may ask you a lot of questions before it installs if you have never used CPAN but while YMMV the default answers should probably work.

You can write your own script or just use this quick and dirty one for PNGs of 64K or less:

#!/usr/bin/perl -w
use Image::Pngslimmer();
my $srcfile = shift;
open INF, $srcfile;
binmode INF;
read(INF, $buffer, 65535);
my $blob2 = Image::Pngslimmer::indexcolours($buffer);
my $blob3 = Image::Pngslimmer::discard_noncritical($blob2);
print Image::Pngslimmer::zlibshrink($blob3);

To use this copy the script above into your editor and save it with a name eg shrinker.pl and then execute like this: perl shrinker.pl some.png > smaller.png which will create a new file smaller.png from some.png (which is untouched).

The results can be reasonably impressive. This file – created by cutting from the GIMP – comes in at 14.4KB (NB the graphics you see here are preprocessed by wordpress.com, click on the graphics to see the original):

Proof graphic, uncompressedBut after a bit of processing with the above script, this comes in at 9.7KB:

Compressed proof graphicFinally, please do not say this doesn’t matter because your pipe is so fat. Millions of these things are being pumped through the internet every minute and the collective saving of bandwidth really would matter if we could deliver it…

LaTeX frustration…


Knowledge of German as a foreign language (sec...
Image via Wikipedia

Anyone who works on software development and in the FOSS world generally is used to seeing books and documentation in English. It is certainly a great advantage to be a fluent speaker and reader.

But it is not always the case – as I have just found out.

Right now I have returned to writing my MSc project proposal and that means back to using the power of LyX and LaTeX. But with great power comes great complexity and it can be tough navigating all of this.

So I discovered there is an O’Reilly “Hacks” book for LaTeX – LaTeX Hacks.

Great! I was going to order it without even bothering to read a review, so sure was I that it would be helpful and useful: until I discovered it was in German and there is no sign of an English translation.

To make matters worse, it seems that the O’Reilly quick reference – LaTeX – is also auf Deutsch.

And there is even 100 neue Latex Hacks

This all has an odd, and unsettling feel to it. A century ago German domination of the physical and mathematical sciences was near-complete. Think of 1905 and Einstein just for starters.

But since the tragedy and disaster of Hitler we are used to thinking of the Germans as great engineers but the US clearly as the world’s leading centre of scientific research. And when a threat to that is identified it is usually seen as being from China (as Barack Obama said only a few weeks ago in his state of the union address). But maybe the LaTeX domination of Germany suggests there is life in the old world yet.

Either that or O’Reilly need to pull their fingers out on translating this stuff.

(The graph shows the numbers of people in EU members states who speak German as a foreign or second language: I did a year of it at High School but would not claim to know much beyond some very basic vocabulary and grammar).