Wednesday, July 11, 2012

StatWeave in LyX

While it's pretty easy to integrate R into LyX documents (using Sweave or knitr) I hadn't found any native way of integrating Stata code. I've hacked StatWeave (which allows Stata, R, and a bunch of other stat languages) to work in my LyX (in Windows 7 with LyX 2.0.4) and thought that others might like this as well. Here's the steps:

  1. Copy statweave.module to the "Resources/layouts" of your user directory (See HelpAbout LyX).
  2. Open LyX and go to  Tools→Reconfigure
  3. Restart LyX and go to  Tools→Preferences
  4. On the left choose File HandlingFile Formats.
  5. Click the New... button and then fill in  Format: StatWeave, check Document format, Short Name: statweave, Extension: swv. Then Apply.
  6. On the left choose File HandlingConverters
  7. Using the drop-down menus, create a new one from "StatWeave" to "LaTeX (pdflatex)" with the command "statweave --target tex $$i" (make the changes and then click "Add" in the upper right). Then Save.
Once you get that setup you should be able to compile: stata-test-bq.lyx. Notes:
  • LyX disables the enter key from inserting a line-break in insets (anyone who knows about this please let me know why or a work around). You can paste them, though, so copying several lines of code into a Stata Code Chunk insert works fine (if you are writing it in LyX then just copy a line break from somewhere and paste as needed). 
  • If you don't want to use the custom insets you can always just use plain TeX code (e.g. You can always just use TeX code inserts \begin{Statacode} ... \end{Statacode} or \Stataexpr{...})
  • The StatWeave manual is helpful for the non LyX part.
  • When configuring your Stata executable do not include "do " before "%codefile%".
  • You may need to edit the MiKTex path given the default ones are for version 2.6.
  • If you want to use relative paths for input files then you can pass the directory of the LyX file to Stata as an environment variable. In the converter replace the statweave command with "set orig_PWD=$$r && statweave --target tex $$i" (then ModifySave). Then in Stata you can change do that directory with -global orig_PWD =trim("`:environment orig_PWD'")"- (it will have the final slash). Don't cd around in the program or StatWeave wont' know where it's graphics files are for conversion.
Suggestions very appreciated.

Update: I've made a newer version the just focuses on Stata and allows for code block options. You can easily show figures now right-clicking in the block, choosing opts, and then putting in a string like fig, height=4.5in, width=9in, dispw=4in

3 comments:

Trang Le said...

Hello,

I followed exactly what you posted but still got an error message saying that the required package is missing
statweave -> Latex when running that stata-test-bq.lyx.

Could you direct me how to fix it?

bdamasio said...

Hello,

I have the same problem than Trang Le.
How can I fix it?

bequw said...

That message is harmless. The PDFs should still be able to be produced. I'm not sure what has to be edited to remove that warning message.