views:

591

answers:

4

I am using StatWeave to run SAS code from within LaTeX files. I would like to be able to open a file in a text editor and view the main LaTeX document with LaTeX syntax highlighting and the embedded SAS "code chunks" with SAS syntax highlighting. (The "code chunks" are real (working) code, not just for display, so I don't think the LaTeX listings package will work.) I'm sure there must be a way to do this with emacs, but I don't know what it is... At this point I'm open to any solution with any editor.

5 Feb 2010 update: I decided that the best approach was actually to stop trying to use StatWeave and SAS, and instead to learn to use Sweave and R. I'm happy with the Sweave highlighting in Vim, but I know other people who are very happy with ESS, so I'll go ahead and accept that answer so we can close this question.

+1  A: 

Perhaps one of the solutions on the MultipleModes page on the Emacs Wiki helps. Also, the manual for Sweave says that Emacs Speaks Statistics can handle Sweave files, so since StatWeave seems to be similar, perhaps you could hack ESS to handle those files as well. ESS seems to already have support for SAS, so the required changes might not be too big.

Jouni K. Seppänen
+1  A: 

Would it be an option not to have the sas files directly in the code but to use '\input' instead? One thing I frequently do with source examples is to put them in a subdirectory samples/ and then use "highlight" to create LaTeX files that can be included in the main file. That way I can execute the source files everytime they are changed to check if they work properly. The (re)generation of the tex code can be done by a shell script or from make if it is a bigger project. In most cases a simple generate_tex.sh script was enough for my projects that I called after I had changed any of my samples. The contents of it could be something like

#!/bin/bash
highlight -L -f --wrap-simple *.css --outdir ../input/
highlight -L -f --wrap-simple *.html --outdir ../input/
highlight -L -f --wrap-simple *.php --outdir ../input/

for a web project. In the main tex file then include the samples using

\input{filename.css}
Gerald Senarclens de Grancy
Thank you-- I do want to have the sas files directly in the LaTeX document in this case, but it's good to know about this other option-
Louisa Grey
A: 

UltaEdit might work but you would have to do a great deal of setup

A: 

If you are using xemacs you can use mmmmode to syntax highlight. I use ESS with statweave so as the first person answer why don't you use ESS to sybntax highlight and use statweave to do the work. I however do not use SAS but have used stata and R in the same file and they both work with their own syntax highlighting. (require 'mmm-mode) (require 'mmm-vars) (setq mmm-global-mode 'maybe) (setq mmm-submode-decoration-level 1)

(mmm-add-group 'latex-stats '((r-tag :submode r-mode :face mmm-code-submode-face :delimiter-mode nil :front "begin{Rcode}" :back "end{Rcode}" :back-offset (backward-char -1) :insert ((?R R-tag nil @ "\begin{Rcode}" @ "\n" _ "\n" @ "\end{Rcode}" @)) ) (sta-tag :submode STA-mode :face mmm-code-submode-face :delimiter-mode nil :front "begin{Statacode}" :back "end{Statacode}" :back-offset (backward-char -1) :insert ((?S STATA-tag nil @ "\begin{Statacode}" @ "\n" _ "\n" @ "\end{Statacode}" @)) )))

(add-to-list 'mmm-mode-ext-classes-alist '(nil "-swv.tex" latex-stats))

You just need to change to SAS-mode (I think if that is what it is called) and I have in my latex.el file (loaded with a lambda in the init.el) the following (local-set-key [ (control f1)]
'(lambda () (interactive) (if (string-match "\.tex" buffer-file-name) (progn (let (file-name file-name1 file-name-wihoutswv) (setq file-name (buffer-file-name)) (setq file-name1 (file-name-nondirectory file-name)) (setq file-name-wihoutswv (replace-regexp-in-string "-swv" "" file-name1)) (setq file-name-wihoutswv (replace-regexp-in-string "\.tex" "" file-name-wihoutswv)) (if (string-match "-swv\.tex" buffer-file-name) (find-file-other-window (concat file-name-wihoutswv ".tex")) (find-file-other-window (concat file-name-wihoutswv "-swv.tex")) ) ) ) (message "You are in Latex-mode and this is neither swv nor TeX file. Cannot open corresponding tex or sweave file") )))

and

(global-set-key [f1]
'(lambda () (interactive) (save-buffer) (if (string-match "-swv\.tex" buffer-file-name) (progn (interactive) (latex-mode) ;;(font-lock-fontify-buffer) (balance-windows) (mylatex-delete-indent) (save-buffer) (save-window-excursion (run-current-statweave-file)) ; (mylatex-clean) ) (if (string-match "\.tex" buffer-file-name) (progn (interactive) (latex-mode) ;(font-lock-fontify-buffer) (balance-windows) (do-pdf) ; (mylatex-clean) ) (message "this is not either TeX nor SWV file and I don't know what to do other than saving the buffer to the file. So I did") )) )

)

(defun run-current-statweave-file () "Execute statweave on a -swv.tex file and launch evince to view output" (interactive) (let (file-name file-name1 file-name-wihoutswv cmd1-str cmd2-str status) (setq file-name (buffer-file-name)) (setq file-name1 (file-name-nondirectory file-name)) (setq file-name-wihoutswv (replace-regexp-in-string "-swv\.tex" ".pdf" file-name1)) ;; (setq cmd1-str (concat "statweave " file-name1 " && evince " file-name-wihoutswv " &" )) (setq cmd1-str (concat "statweave " file-name1 )) (setq cmd2-str (concat "evince " file-name-wihoutswv " &")) ;; (shell-command cmd1-str) (setq output-buffer "swvoutput") (save-window-excursion (setq status (shell-command cmd1-str output-buffer))) ;;(message status) ;; (let (status) ((shell-command cmd1-str output-buffer))))) ))

It looks awful and I am not an lisp expert so I am sure it has a lot of inefficiencies but it works.

Jean