wiki_tex.py

I’ve written a simple python script called wiki_tex.py to help convert LaTeX to the wiki-tex used on MediaWikis like the Polymath1 wiki, and Wikipedia. It’s a stripped-down port of a similar script I use to put LaTeX on my blog – the original script is heavily customized, which is why I haven’t made it generally available.

A zipped version of the script can be downloaded here.

To use the script, put a copy of the script in the directory you’re writing LaTeX in. You also need to make sure Python is installed on your machine. Mac and most Linuxes have it pre-installed. Windows doesn’t, but you can get a one-click installer at the python site. I wrote the script under Python 2.5, but would be surprised if it doesn’t work under Python 2.x. I don’t know how it runs under the recently released Python 3.

To run the script, you need a command line prompt, and to have the directory set to be whatever directory you’re writing LaTeX in. Once in the right directory, just run:

python wiki_tex.py filename

where filename.tex is the file you’re texing. Note the omission of the .tex suffix is intentional. The script will extract everything between \begin{document} and \end{document}, convert to wiki-tex, and output a file filename0.wiki which you can cut and paste into the MediaWiki.

As a convenience, if you put lines of the form “%#break” into the file (these are comments, so LaTeX ignores them) then the script breaks up the resulting output into multiple files, filename0.wiki, filename1.wiki, and so on. This is useful for compiling smaller snippets of LaTeX into wiki-tex.

The script is very basic. There’s many conversions it won’t handle, but there’s quite a bit of basic stuff that it copes with just fine. I’d suggest starting with some small examples, and gradually working up in complexity.

The script is also not very battle-tested. It’s undergone a little testing, but this is well and truly alpha software. Let me know in comments if you find bugs, and I’ll try to fix them.

4 comments

  1. Dear Michael

    Thank you for the fine code!

    A LaTeX idiom that works nicely with your script is “\immediate\write18{}”; which immediately executes (provided, that is, that the LaTeX compiler has been invoked with the “–shell-escape” option).

    A simple LaTeX example is wiki_tex_example.tex, which (on my OS X system) runs your python conversion script directly from the LaTeX source file.

    All you fans of literate programming will appreciate that \write18 allows “pdftex” to run “make” (and therefore, any other program or script) as easily as “make” runs “pdftex”.

    The result is nearly-infinite flexibility in creating one-step literate programming environments that have all the nice graphical interfaces that we associate with LaTeX … Nirvana!

  2. Hmmmm … some key characters were stripped out of the above post; it should have read:

    “A LaTeX idiom that works nicely with your script is ‘\immediate\write18{<shell commands>}’, which immediately executes <shell commands>.”

    I hope people have fun with this … it has been my experience that literate programming becomes a lot more enjoyable (and practical too) when typesetting, compiling, testing, packaging … and now blogging and wiki-ing too! … is a one-click process. 🙂

  3. Nice one, John. As you say, every little bit of automation helps. With my workflow I probably wouldn’t use this, but someone doing a great deal of LaTeX-wiki might find it helpful. Another way of getting a similar effect would be through a simple shell script which combined LaTeX and wiki_tek steps.

Comments are closed.