Jason Earl
Personal Website
BLOG
LaTeX/PDF formatting in RedCloth4
About RedCloth
Due to the various issues with the old RedCloth 3.x branch I was one of the early adopters of RedCloth 4. RedCloth is a Ruby implementation of the Textile mark up / micro language for text formatting. For those who don’t know, Textile is a lightweight alternative to XHTML which is often used on the web in place of WYSIWYG HTML editors, mainly because it is simple and makes it easy to filter the text so there is no harmful (i.e. libale to XSS attack through injecting SCRIPT tags) or bad (i.e. Anything pasted from Micro$oft Office).
Differences From Version 3.x
RedCloth 4 should be a lot faster, while eliminating most of the bugs known in On top of this it has very basic support for LaTeX and a simple system for support custom formatters. This proves to be very handy for me as I can use Textile both for web pages and PDF documents in a seamless way.
Creating PDF’s
Here’s the nitty gritty details for those wanting to render PDF’s in Textile, here’s a quick summary how to do it.
- Install Tetex/pdflatex, instructions will vary for this according to what distro / operating system you use. In Debian/Ubuntu you can use
sudo apt-get install texlive
- Install the latest RedCloth, using
gem install RedCloth
. You will need a C compiler installed for this, and note the capitalisation of the package name.
Using the following code as a starting point / example, you should be get a basic PDF rendered:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
require 'rubygems' require 'redcloth' text = "h1. Here is a test heading\n\n" + "p. Here is some paragraph content. " + "Obviously you will want to change this text." latex = RedCloth.new(text).to_latex tex_document = "\\documentclass[a4paper]{article}" + "\\RequirePackage{graphicx}" + "\\RequirePackage{hyperref}" + "\\author{ Site Author }" + "\\date{Published Date}" + "\\title{Site Name Here}" + "\\pagestyle{headings}" + "\\begin{document}" + "\\maketitle" + "\\newpage" + "#{latex}" + "\\end{document}" File.open "latex_test.tex", "wb" do |file| file.write tex_document end system "pdflatex latex_test.tex" |
As you can gather we need to generate a bit of extra meta data after call the to_latex method to let pdflatex know how to format things on the page. The resulting PDF will be in your home folder, again this will probably need to change to somewhere different, however this puts across the main points across for getting PDF documents out of RedCloth using Textile.
Also note that images are not fully supported at the moment.
Listening to Techno / Breakbeat:
Do Not Break
- Ellen Allien & Apparat