escreen instead of elscreen for screen-like features in emacs

I’ve been using elscreen for screen-like features in emacs the last couple of years. However, I have a few complaints. Elscreen has issues when used with emacsclient -c: a new frame might not be created from the -c argument, which messes up my current screen in emacs, and the “Opening Server Files Always in a New Frame” tip on this did not resolve this issue perfectly with elscreen. Also, when used with emacs’ Desktop mode, I get an error when launching emacs with an Rnw file (Rweave, or Sweave) is in the file list; the launch actually does not load all files, and I have to exit and delete my desktop file (which I’ll lose the list of files).

I discovered escreen from this post a long time ago, and thought escreen was pretty lightweight and worked pretty well. However, I never switched to it because it didn’t have “tabs” for me to know which screen I was at. I didn’t realize the very same post had customizations that would show which screen you are at. I’ve copied his customizations, and added a few more of my own, which I’ll list here:

 <pre class="example">;; escreen is simpler than elscreen...but elscreen spawns new 0-9 set of screens for a new frame whereas escreen does not

;; it is better than elscreen because: elscreen has issues when using with emacsclient -c (does not always create new frame, messes up existing frame); in escreen, each screen has its own ring of recently visited files (good!) ;; http://blog.tapoueh.org/news.dim.html#%20Escreen%20integration ;; http://www.splode.com/~friedman/software/emacs-lisp/#ui (load “escreen”) (escreen-install) (setq escreen-prefix-char “\C-z”) ;; http://www.macs.hw.ac.uk/~hwloidl/cool-el.html (global-set-key escreen-prefix-char ‘escreen-prefix) ;; add C-\ l to list screens with emphase for current one (defun escreen-get-active-screen-numbers-with-emphasis () “what the name says” (interactive) (let ((escreens (escreen-get-active-screen-numbers)) (emphased “”))

(dolist (s escreens) (setq emphased (concat emphased (if (= escreen-current-screen-number s) (propertize (number-to-string s) ;;’face ‘custom-variable-tag) ” “) ‘face ‘info-title-3) ;;’face ‘font-lock-warning-face) ;;’face ‘secondary-selection) (number-to-string s)) ” “))) (message “escreen: active screens: %s” emphased)))

(global-set-key (kbd “C-\ l”) ‘escreen-get-active-screen-numbers-with-emphasis)

(defun escreen-goto-last-screen-dim () (interactive) (escreen-goto-last-screen) (escreen-get-active-screen-numbers-with-emphasis))

(defun escreen-goto-prev-screen-dim (&optional n) (interactive “p”) (escreen-goto-prev-screen n) (escreen-get-active-screen-numbers-with-emphasis))

(defun escreen-goto-next-screen-dim (&optional n) (interactive “p”) (escreen-goto-next-screen n) (escreen-get-active-screen-numbers-with-emphasis))

(define-key escreen-map escreen-prefix-char ‘escreen-goto-last-screen-dim)

(defun escreen-create-screen-dim () (interactive) (escreen-create-screen) (escreen-get-active-screen-numbers-with-emphasis))

(defun escreen-kill-screen-dim () (interactive) (escreen-kill-screen) (escreen-get-active-screen-numbers-with-emphasis))

(add-hook ‘escreen-goto-screen-hook ‘escreen-get-active-screen-numbers-with-emphasis)

(define-key escreen-map “c” ‘escreen-create-screen-dim) (define-key escreen-map “k” ‘escreen-kill-screen-dim)

;; (global-set-key (kbd “C-]”) ‘escreen-goto-next-screen) ;; (keyboard-translate ?\C-[ ?\H-[) ;; (global-set-key (kbd “H-[“) ‘escreen-goto-prev-screen) (global-set-key (kbd “C-]”) ‘escreen-goto-next-screen-dim) (keyboard-translate ?\C-[ ?\H-[) (global-set-key (kbd “H-[“) ‘escreen-goto-prev-screen-dim)

Whenever I create/kill/visit a screen, the minibuffer will always show the list of screens available, with emphasis on the current screen. I like escreen because it does not have the issues I listed before with elscreen. Also, in escreen, each screen has its own ring of recently visited files, so that if I move to a different screen and edit a buffer, move back to my original screen, kill the buffer, the recent buffer from the other screen will not be the default screen on this current screen.

I like escreen!

Creating even NICER, publishable, embeddable plots using tikzDevice in R for use with LaTeX

It’s true. I like to do my work in R and write using LaTeX (well, I prefer to use org-mode for less formal writing and/or if I don’t have to typeset a lot of math). I haven’t done a lot of LaTeX’ing or Sweaving in the last year since 1) I’ve been collaborating with scientists (stuck using Word) and 2) my simulations in R have been a little overwhelming to keep in one file a la literate programming. I have a feeling I’ll be going back to LaTeX soon since I have to write up my dissertation (and lectures if I end up at an academic institution, **crosses finger**).

Subconsciously I’ve always wanted a tighter integration between R and LaTeX. Sweave did a fantastic job bringing R to LaTeX, greatly improving my workflow and jogging my memory when I revisit projects (just look at the one file consisting of documentation/writing and code). Despite R’s outstanding capabilities in creating publishable plots, I always felt it needed work in the realm of typesetting math. Sure it supported mathematical expressions. I used them a few times, but whenever I included the generated plot in a LaTeX document, the figure appeared out of place. I’ve explored Paul Murrell’s solution by embedding Computer Modern Font into the R-generated plot; UPDATE 10/23/2010 I also explored the psfrag in this post. The required effort probably outweighs the cost in most situation in my opinion (I haven’t done it in a real life scenario). I also tried to create a simplistic plot and overlay LaTeX code afterwards; again, haven’t done much with this, although I expect this will come in useful when I have to write over a pdf file that I do not have access to the source code.

I’ve also explored how to draw in LaTeX using the Picture package and XY package. I didn’t do much with it after the exploration because I didn’t know the syntax well and because drawing in R, Google Docs, or OpenOffice suffices 99.9% of the time. I prefer to draw in R or LaTeX to have reproducible code.

I was recently introduced to tikzDevice by this post via the R blogosphere. What it does is translate an R plot to TikZ. That is, instead of creating the plot device via pdf(), you do it with tikz(). This creates a .tex file via three modes (well four but I don’t think I’ll use the barebones mode):

  1. Just tikz code so you can use the include{} command in LaTeX (default).
  2. Tikz code surrounded by the document skeleton so the tex file can be compiled (standAlone=TRUE).
  3. Console output mode where the code are sent to stdout for use with Sweave (console=TRUE). UPDATE 10/23/2010: use this with pgfSweave; builds on cacheSweave and Sweave.

Read the vignette; it’s fairly complete. The authors claim that the software is still in Beta stage (they’re still testing certain interface features), but my initial testing shows that it is ready for prime time, at least for my usage.

If you want the results in jpeg/png for use with the internet or Word documents, you can always convert the pdf to another format via the convert command.

Here is my example for the standalone (2) case:

 <pre class="src src-sh"><span style="color: #ff4500;">## </span><span style="color: #ff4500;">look at vignette for examples and how to's</span>

library(tikzDevice) f1 <- “tikzDevice_Ex1.tex” tikz(file=, standAlone=TRUE) set.seed(100) n <- 100 x <- rnorm(100) y <- 2*x + rnorm(n) fit <- lm(y ~ x) plot(x, y, xlab=“x”, ylab=“y”, main=“$E[Y] = \beta_0 + \beta_1 \times x$”) dev.off() system(paste(“rubber –pdf”, f1)) system(“convert tikzDevice_Ex1.pdf tikzDevice_Ex1.png”) system(“gnome-open tikzDevice_Ex1.png”)

Note I make use of the rubber command. Feel free to replace it with pdflatex.

UPDATE 10/23/2010: Make use of pgfSweave with this!

Emacs: AucTeX + Rubber + Sweave

I got rubber to work with AucTeX and Sweave (Rnw) files with the help of this.

Basically, combined with my other stuff, I tweaked my .emacs file to look like:

<pre class="src src-sh">;;following is AucTeX with Sweave -- works

;;http://andreas.kiermeier.googlepages.com/essmaterials (setq TeX-file-extensions ‘(“Snw” “Rnw” “nw” “tex” “sty” “cls” “ltx” “texi” “texinfo”)) (add-to-list ‘auto-mode-alist ‘(“\.Rnw\'” . Rnw-mode)) (add-to-list ‘auto-mode-alist ‘(“\.Snw\'” . Snw-mode)) (add-hook ‘Rnw-mode-hook (lambda () (add-to-list ‘TeX-command-list (“Sweave” “R CMD Sweave %s” TeX-run-command nil (latex-mode) :help “Run Sweave”) t) (add-to-list ‘TeX-command-list (“LatexSweave” “%l %(mode) %s” TeX-run-TeX nil (latex-mode) :help “Run Latex after Sweave”) t) ;; following 3 lines for rubber, taken from same site as next paragraph, http://www.nabble.com/sweave-and-auctex-td23492805.html, xpdf to open (add-to-list ‘TeX-command-list (“RubberSweave” “rubber -d %s && open ‘%s.pdf'” TeX-run-command nil t) t) (setq TeX-command-default “Sweave”)))

;; AucTeX with rubber ;;http://www.nabble.com/sweave-and-auctex-td23492805.html (eval-after-load “tex” ‘(add-to-list ‘TeX-command-list ;;‘(“Rubber” “rubber -d %t && xpdf ‘%s.pdf‘” TeX-run-command nil t) t)) ;; change by vinh (“Rubber” “rubber -d %t && open ‘%s.pdf'” TeX-run-command nil t) t))

Now, when an Rnw file is open, I can press C-c C-c, select Sweave. Then repeat, select RubberSweave (or LatexSweave).

Editing/adding on to Sweave features in ESS

I really like David Whiting’s Sweave methods in ESS: M-n s M-n P and I get a compiled pdf file. However, i wanted to change it so Sweave uses the cacheSweaveDriver() when sweaving. I knew the the functions were defined in the ess-swv.el file (in my carbon emacs in Mac OS X, it is at /Applications/Emacs.app/Contents/Resources/site-lisp/ess/ess-swv.el). Now, I edited the file to create a new way to Sweave:

<pre class="src src-emacs-lisp"><span style="color: #ff4500;">;;; </span><span style="color: #ff4500;">changed by vinh</span>

(defun ess-swv-run-in-R2 (cmd &optional choose-process) “Run \[cmd] on the current .Rnw file. Utility function not called by user.” (let* ((rnw-buf (current-buffer))) (if choose-process ;; previous behavior (ess-force-buffer-current “R process to load into: “) ;; else (update-ess-process-name-list) (cond ((= 0 (length ess-process-name-list)) (message “no ESS processes running; starting R”) (sit-for 1); so the user notices before the next msgs/prompt (R) (set-buffer rnw-buf) ) ((not (string= “R” (ess-make-buffer-current))); e.g. Splus, need R (ess-force-buffer-current “R process to load into: “)) ))

(save-excursion (ess-execute (format “require(tools)”)) ;; Make sure tools is loaded. (basic-save-buffer); do not Sweave/Stangle old version of file ! (let* ((sprocess (get-ess-process ess-current-process-name)) (sbuffer (process-buffer sprocess)) (rnw-file (buffer-file-name)) (Rnw-dir (file-name-directory rnw-file)) (Sw-cmd (format “local({..od <- getwd(); setwd(%S); %s(%S, cacheSweaveDriver()); setwd(..od) })” Rnw-dir cmd rnw-file)) ) (message “%s()ing %S” cmd rnw-file) (ess-execute Sw-cmd ‘buffer nil nil) (switch-to-buffer rnw-buf) (ess-show-buffer (buffer-name sbuffer) nil)))))

(defun ess-swv-weave2 () “Run Sweave on the current .Rnw file.” (interactive) (ess-swv-run-in-R2 “Sweave”))

(define-key noweb-minor-mode-map “\M-nw” ‘ess-swv-weave2)

Now, I can do M-n w to Sweave with cacheSweaveDriver(). Tried it, didn’t work. After many trials, i decided to put this code in my .emacs file. Works. Hmm, wierd. Is ESS using a different ess-swv.el file? Tried deleting it from where i think it is. M-n s still works. Weird!!!

After some more searching, found out that the stuff is going on in the ess-swv.elc file. Deleted both files, M-n s doesn’t work anymore. Googled what *.elc is. It is a byte-compiled version of .el (to make things run faster in emacs, sometimes). So I did it to mine.

Byte compile for emacs: http://www.cs.utah.edu/dept/old/texinfo/emacs18/emacs\_26.html

So I open my ess-swv.el file in emacs, typed M-x byte-compile-file

Voila! .elc file created. Close emacs and run it again!