Thursday, April 12, 2012

Software blog

It has been a while since my last post. My wife was fighting bouts with cancer which took up a lot of energy. Besides that I started a new blog at select * from ideas.
I intend the keep blogging on mathmatics and other stuff and concentrate on software related blogs on select * from ideas.

Monday, July 11, 2011

A usefull Postscript Trick

In this blog post I will share a Postscript trick which I use a lot.

Postscript is best known as a page description language. It is a very powerful language, in fact, Postscript is Turing-complete. The language is stack-oriented so you will see a lot of stack operators like dup, exch and roll. These operators can be used to perform all necessary (and possible) stack operations. But it can be very tasking for a programmer to use them correctly.

For example, the following code draws a rectangle perpendicular to the coordinate axis with its lower left vertex at (x,y) and a width of w and height of h. In the comments an impression of the stack is given.

/rectangle {     % stack: h w x y
  moveto         % stack: h w
  dup            % stack: h w w
  3 -1 roll      % stack: w h w
  0 rlineto      % stack: w h
  0 exch rlineto % stack: w
  neg 0 rlineto  % stack:
  closepath      % stack:
  stroke         % stack:
} def
You can easily get lost in all these stack manipulations. Make one mistake and it is likely that your expectations are not met.

Luckily you can solve this problem within postscript. If you look at the above code again, you may notice that a definition is made. The symbol "/rectangle" is associated with the code-block by the def operation. One can associated other values with this operator. You can use it to simplify the above code.

/rectangle {      % stack: h w x y
  /y exch def     % stack: h w x
  /x exch def     % stack: h w
  /w exch def     % stack: h
  /h exch def     % stack: 
  x y moveto      % stack: 
  w 0 rlineto     % stack:
  0 h rlineto     % stack:
  w neg 0 rlineto % stack:
  closepath       % stack:
  stroke          % stack:
} def
There is one problem with this solution. It defines, and possibly overrides, associations for the symbols "/x", "/y", "/w" and "/h". This method leaks its variable in to the context in which it is called. Again, postscript can solve this problem. Initially the associations are made in the dictionary userdict. By using an other dictionary and throwing the dictionary away after it's service, we avoid polluting the userdict. (In the following code examples I will omit the impression of the stack.)
/rectangle {
  4 dict begin
    /y exch def
    /x exch def
    /w exch def
    /h exch def
    x y moveto 
    w 0 rlineto
    0 h rlineto
    w neg 0 rlineto
} def
Here we first define a dictionary for four keys with 4 dict. Start using it with begin. When we are done we discard it with end. This nicely allows the usage a variable with out polluting the userdict. Because I used this trick so often I polished it up a little. I left out the "body" of this method so you can concentrate an the scaffolding.

/rectangle {
  [/y /x /w /h] dup length 0 add dict begin {exch def} forall
} def
That one-liner signals the variable names used in this method, creates a dictionary and initialises it with values from the stack. The 0 add is there to easily add local variable. I the body of the method is using a local variable add one the the integer to ensure the size of the dictionary.

This wraps up this Postscript type. I explained how you can use variable in methods without polluting the userdict.

Postscript Brush SyntaxHighlighter

I found myself working with Postscript again and I wanted to share some tips and tricks. Syntax highlighting is a great clue in communications about software. I set out to provide syntax highlighting for Postscript.

I have some experience with Alex Gorbatchev's SyntaxHighlighter. It is a javascript based syntax highlighter with customizable brushes. I created a Smalltalk brush for my Smalltalk Galore blog account. I took that experience and used it to provide a Postscript brush for SyntaxHighlighter. It can be found in the git repository at SyntaxHighlighter-Postscript-Brush.

So you can expect some syntax highlighted Postscript code on this blog soon.

Sunday, July 10, 2011

Postscript Syntax Highlighting

In this blog post I will outline the GtkSourceView language definition for Postscript.

Postscript is best know as a page description language. I found myself using it to produce a set of pictures, but was disappointed that no syntax highlighting was available. Luckily I had prior experience

Just like the the blog post about MAGMA syntax highlighting I used GtkSourceView to create a language definition file for Postscript. The language definition file can be found on github. For instructions about installing the language definition file I refer to the post about MAGMA. (Or you can find instructions in the README file.)

Have fun with Postscript!

Tuesday, June 21, 2011

Graphviz: dot

In this blog post I will discuss the programming language dot.

Dot is a graph description language. I will dive right in and show you an example. The following code:

graph example {
    a [label="root"]
    b [shape="box"]
    c [color="red"]
    a -- b -- d
    a -- c -- e
    c -- f [label="indirect"]
produces this output:

This clearly shows the capabilities of dot. For more information about dot, and it companion programs visit the Graphviz website.

Thursday, June 16, 2011

MathJax: the gory details

In this blog post I will outline the steps needed to display LaTeX on blogger via MathJax.

In this post I announced the use of a new LaTeX rendering engine that I started to use. A comment asked for more information how I achieved this. Unfortunately it took me a while before I noticed the comment. In this blog post I will provide the steps I performed to start using MathJax in blogger.

Step 1: Hosting
This is by far the hardest step in the configuration of MathJax. MathJax relies on a server which provides all the resources MathJax needs. So you need a host which you can control. I looked around for a hosting service and picked a local hosting service.

Step 2: Installing MathJax
There are various ways of installing MathJax depending on your host. For various reason I opted for a local installation.
Get a copy of the latest MathJax distribution of the download page of MathJax. Place the distribution on the host and follow the installation instructions. Other options are described as well.

Step 3: Add reference to your MathJax in blogger
In order to use MathJax in blogger you have to refer to the MathJax installation. Go to the design section of blogger and edit the html. In the head section of the html add the following section of code.

     <script src='http://[yourhost/your-installation/]MathJax.js' type='text/javascript'/>

Step 4: Start using LaTeX
Now you can use LaTeX in blogger.
e^{i\pi} + 1 = 0

Above I outlined the steps I performed to start using MathJax in blogger. Since the time that I started using MathJax some things changed. For example, It is now possible to use MathJax directly from their Content Distribution Network (CDN). In effect making steps 1 and 2 unnecessary.

Before you can use the CDN you will have to read the terms of service. After that use the alternate step 3 to reference Mathjax in blogger as outlined below and on the documentation page.

Step 3': Add reference to the CDN Mathjax in blogger
Go to the design section of blogger and edit the html. In the head section of the html add the following section of code.

     <script src='' type='text/javascript'/>

This wraps up the explanation of the use of MathJax in Blogger.

Friday, June 3, 2011

MAGMA Syntax Highlighting

This blog post points out how to add syntax highlighting for MAGMA in gedit.

MAGMA is a very good computer algebra system, but not very well known outside a select mathematical circle. So it is not very surprising that gedit does not know how to syntax highlight MAGMA code out of the box.

Gedit uses GtkSourceView as its syntax highlighting engine. GtkSourceView can be extended by providing a language definition file.

So I set out to create a language definition file for MAGMA. The results can be found at github. In order to use is, you have to place the magma.lang file in the following directory:


Below you can find a screenshot of a snippet of MAGMA code highlighted by gedit.