schussman.com logo

Embed LaTeX in Wordpress

I don’t run Wordpress, but this is pretty cool: you can embed LaTeX right into your Wordpress posts. (Official announcement at WP.com)

Sweave and typeface control

Sweave has a tendency to preclude latex packages from setting typefaces, apparently. Here’s a much simpler fix (R 2.3.0+) than my hacky solution of using \input or \include to typeset the sweave-generated file in a wrapper file. (I suppose I should upgrade my R installation so that I can actually use this, but I shudder at the thought of updating all those packages again. I’ll put it off for just a few more weeks, I think.)

Sweave and complex projects

A thread on R-Help recently discussed using Sweave/LaTeX for complicated projects. Two really useful tips were highlighted in that conversation—I use the first of them regularly: In the beginning of a Rnw/Snw file, use the prefix.string option to set the location of an includes directory: \SweaveOpts{prefix.string=/Path/to/directory}

This is really useful for organizing all the files that built by your project. (Mine are all directed to an includes directory that lives beneath my main manuscript directory.)

The second tip is to use a makefile to build a project that consists of multiple Sweave files. I like makefiles as much as the next guy, but here is my TextMate-specific solution for the same dilemma: Within the TM project, use the TM_SWEAVE_MASTER variable to name a master file, and in this file, simply plug in a single Sweave section that invokes source (for R-only files) or Sweave for your project’s various files. When you want to build the whole project (for example when you begin work for the day and need to load up all your data) all you do is open up the project and invoke the Sweave -> Sweave Project in R command.

For example, my dissertation project sets TM_SWEAVE_MASTER to “diss-master.Snw,” and that file looks like this:


<<echo=false,results=hide>>=
source(file="˜/data/diss/R/clean-summary.R")
source(file="˜/data/diss/R/tables.R")
source(file="˜/data/diss/R/figures.R")
source(file="˜/data/diss/R/citystats.R")
Sweave(file="˜/docs/diss/manuscript/data-collection.Snw")
Sweave(file="˜/docs/diss/manuscript/longevity.Snw")
@

The several R files do a little bit of data tweaking and build some tables/figures. Once all that data is loaded up, the two Sweave files can be built. I do this once per work session, using the Sweave Project in R command, which makes sure everything in the project is up to date. Subsequently, I can simply Sweave any individual Snw file (using the corresponding TextMate command), without having to recompile the entire project. (This all of course integrates well with using TM_LATEX_MASTER, also set at the project level, to order LaTeX to typeset the overall document.) I have found it to be a really nice and functional workflow.

Posting other peoples' hints: Watermarks in LaTeX

I found myself today with the desire to incorporate a bit of watermark-ish text and graphic into a LaTeX document. The regular graphics commands don’t work very well with this, so I went googling and found the code below.

Note that the following isn’t my tip, but the page that references the code points to a 404 link. I tracked down the google cache to get the original link back out and thought I’d share.



   \usepackage{eso-pic}    \usepackage{color}    \usepackage{type1cm}    \makeatletter     \AddToShipoutPicture*{%     \setlength{\@tempdimb}{.5\paperwidth}%     \setlength{\@tempdimc}{.5\paperheight}%     \setlength{\unitlength}{1pt}%     \put(\strip@pt\@tempdimb,\strip@pt\@tempdimc){%    %\makebox(0,0){\rotatebox{45}{\textcolor[gray]{0.75}{\fontsize{5cm}{5cm}\selectfont{Draft}}}}    \makebox(0,0){\includegraphics[scale=1]{../figures/LETTERHEAD.PDF}}     }    }    \makeatothe

There are two makebox lines present. Uncomment the first to use a text or stamp the output of another command on the page; or use the second (replace the filename) to use a graphical watermark. Also, remove the asterisk from the opening AddToShipoutPicture command to have your image stamped on every page, rather than just the first.

Sweave Bundle update

Note (mostly) to self: I made a few small updates to the Sweave Bundle for TextMate. Interested parties, you know what to do.

Convert LaTeX tables to Word

I have recently found myself needing to convert some lovely LaTeX-typeset tables into a format that MS Word can handle more gracefully than pasting in as a graphic. Kieran last year pointed to tools such as Hevea (along with a set of regular expressions—all hail—) to do the work. I’ve heard from others that tools such as latex2rtf work quite well. Both of those tools are pretty heavy duty, however (the Hevea manual is a 500K PDF which is still downloading from a slow server, and latex2rtf has a slew of config files and options), especially for my lightweight task.

For quickly making word-acceptable (ugly, but editable by your editor or colleagues) tables, I found a reasonably easy process: 1) output the table to a small standalone LaTeX file; 2) use TTH to convert the table to HTML; 3) paste the table into Word; 4) use Word’s “convert text to table” command. You’ll then have a useable table, ready for whatever formatting tweaking you want to do. This all takes about 30 seconds per table.

This has worked just fine for my purposes—specifically in this case, building a set of summary tables in R that need to be brought into a Word document (Word turned out not to handle converting into a table the text pasted straight from R). The estimable Frank Harrell has some further instructions, as well as a small wrapper script, to make TTH work with Sweave documents, too.

Jumping Ship: Moving from emacs to TextMate

Update: Not quite ready to give up all the nice authoring features of emacs, I built, with some tinkering, a reftex-style citation command for the TextMate/LaTeX bundle. It has since been incorporated into the main LaTeX bundle.

Update: The Sweave bundle is updated as of Oct 5 2006. Thanks to Haris for the contributions and improvements.


Yesterday I linked to a screencast that shows off some the neat things that one can do with the math bundle in TextMate. TextMate continues to get better, and it has become my primary editor on OSX. Kieran posted a comment about TextMate’s relative lack of functionality with regard to LaTeX and R:

I was looking into TextMate but its latex and R support is still fairly basic — there’s no real equivalent to auctex/reftex’s functionality, and the R bundle is rudimentary. This is a pity, as it seems like a really powerful environment, and for some time I’ve been looking for a way to escape from Emacs and use an OS X native, modern editor/IDE. Maybe soon.

Kieran is right in part: Auctex and Reftex are excellent additions to emacs and TextMate can’t yet match them. But it does offer some nice advantages over emacs, so I thought I’d write up a few thoughts on my transition to TextMate and the ways I’ve found to compensate for no longer having access to my beloved C-c C-c RET.

Why switch

OS X is a pleasant working environment, and even builds of emacs that are meant to fit nicely in that environment still don’t feel native. Aquamacs is one such attempt. For brand new users of emacs, Aquamacs may be a good tool, but for those of us with pre-existing byzantine .emacs files, Aquamcs adds a whole additional level of confusion by changing keybindings and introducing a whole new set of configuration options. Despite all the attempts to make it more modern, setting one’s typeface in the editor remains a frustrating exercise. My machine is relatively modern and speedy, and emacs still takes a good long time to load fully, even after pruning unnecessary cruft from my config file. And once loaded, there are just enough interface differences to be jarring: scroll bars, for example, are something that most emacs builds have never really sorted out. It’s 2006; can I please get a scroll bar that works the same way as every other scroll bar on my machine?

It’s not about bling, however. As projects such as my dissertation grew in size — multiple data, LaTeX, R, and Sweave files spread around the place — the organization of all that material started to occupy an increasing chunk of my cognitive space. “Where’s file goober, and how is it related to file data?” Although emacs handles lots of files just brilliantly, and switching between them is a snap if you’ve loaded the right iswitch-b package, it doesn’t help much with the organization end of things. That was my real original incentive to switch: I wanted my software to take a little of the load off of my brain, and perhaps to do it a little more quickly.

Finally, switching is a nice opportunity to review the way I get work done and think conscientiously about how to improve. On the flip side, it’s also a nice way to structurally procrastinate.

Why not switch?

Emacs is powerful. It can read email, browse the web, make a fully-functional wiki right on your desktop, and, on those occasions when appropriate, it can edit text files with championship ability. It is a mature working environment with brilliant integration with LaTeX, BibTeX, and R. Just using it can make one feel like a ninja, albeit a meek, deskbound one with rapidly deteriorating vision and nascent repetetive stress disorder in the wrists. As Kieran commented, nothing quite approaches the combination of AucTeX and RefTeX. I’m still reaching for those key-bindings that, alas, don’t work in TextMate no matter how many times I C-c [ them.

Built-in magic

TextMate immediately addressed my core reason to switch with its handling of projects. It has a project drawer into which you can simply drag files and folders, create separators, and arbitrarily organize them all. It seems like a small thing, but the ability to see all the files that comprise a project, and then navigate them easily, is something that’s a) really important in order to have a clear sense of what I’m working on, and b) remarkably difficult in emacs.

 


click-enlarge

 

Navigating those files is easy, as well: You can find and click in the project list, switch tabs with the keyboard, or hit cmd-T to bring up a file browser that finds files as you type: “cmt-T cl” narrows the list to those files that match the pattern “cl.” It pretty nicely approximates the autocompletion of switching buffers in emacs.

 

Many pieces loosely joined

TextMate is, like emacs, extensible almost to the point of absurdity. The architect of this extensibility built TextMate to hook into virtually any programming language, shell command, and external application. TextMate comes with built-in support for LaTeX and BibTeX compilation, as well as completing citations and labels within LaTeX documents. The latter function isn’t nearly as slick as using RefTeX, but it works fairly well. There are a few useful screencast demonstrations of those features. Haris, the author of those screencasts, has contributed tremendously to cite key and label completion — they work pretty well, thanks much to him.

I’ve made what I think are a few improvements to existing bundles in order to faciliate my own work: I’ve modified the LaTeX compile command to switch to xelatex if necessary, for example.

More in depth, but still fairly simple, is my rudimentary Sweave bundle for TextMate.1 TextMate allows one to set environment variables at the global or project level, so, for instance, I can assign a “master document” variable to my dissertation. This allows me to generate LaTeX output from a single in-process Sweave file, or, with an alternate command, to re-run the entire Sweave project through R and then begin LaTeX compilation. With the bundle TextMate (mostly; it’s still a work in progress) correctly parses Sweave files, allowing for context-sensitive actions depending on the position of the caret in a file: Within a Sweave document, I can generate LaTeX, compile that associated LaTeX file, send selected code to R, or build the entire master document. It works pretty slick now that it’s set up. Whereas in Emacs, the ties between various files was frequently opaque, I’ve found that keeping track of those relationships and compiling documents is more transparent and much easier.

Is it worth it?

For me, it has been worth it. The tinker to work ratio starts out pretty high, but that’s not unusual. Breaking emacs habits is tougher, even after four months, and I’d love a citation mode that works more akin to that found in RefTeX — the ability to invoke the command and then choose citation types, for example — and I still miss some of the enhancements from AucTeX; it trained me too well to C-c C-s to insert a section, for example. The ease with which one can build bundles and interface with external applications suggests that it won’t be long before someone may start building equivalent tools, and that will be a happy day.

In the meantime, TextMate is fast, allows me to visualize my projects, and works well enough with the other applications I use, as well as within my workflow, to justify the switch. It’s a good app, and it has improved my work.

Revise and extend (ie, updated)

I forgot to mention another issue about switching. Up until now, my tools have been almost entirely cross-platform for the past five or six years. TextMate is OS X-specific, so I can’t smoothly use the same set of tools on the Windows laptop like I could with emacs. This gives me some pause: It’s nice to have a mostly universal workflow, in which I could sit down at a PC, Mac, or linux machine, sync some files, and work. But over the past year with the iMac, I’ve picked up a few other non cross-platform tools: BibDesk is a great BibTeX manager, and I’ve been doing a ton of stuff using OmniOutliner Pro in the past handful of months, so switching away from emacs on one platform isn’t as much of a transition on that front as it might have been a year ago. Besides, the Mac is a nice platform to work on. And, hey, one of these days I’ll be able to trade up the Toshiba for a shiny new *Book of some kind.


1 The SWeave bundle is now distributed as a regular TextMate Bundle via the subversion bundle repository. [ return to text ]

Nice find: Dataninja

Dataninja was just the right site to stumble across tonight. The production of an “economist and (future) economics PhD student,” Dataninja is packed full of good data and workflow stuff: Techniques to convert from spreadsheets to LaTeX code, tips for working with Stata, R pointers (including homemade reference cards), applescripts, programming tools, links to data sets, and more. As they say, read the whole thing.

Packed full. It’s a great resource.

De-lovely

... or, what happens to your recently-completed syllabus after the correspondence office gets done re-typing it in Microsoft Word.


About, the short version

I’m a sociologist-errant. This site is powered by Textpattern, Pair Networks and the sociological imagination. For more about me and this site, see the long version.

RSS feed