My FAQ

Table of Contents

Emacs

How to install Emacs in Windows?

  • Download Emacs (windows version) and extract the zip file to a directory.
  • Create .emacs file in that directory.
  • Create an environment variable with name = HOME and value = ~/path/to/that/directory
  • Or, try EmacsW32.

How to use Emacs key bindings in all Windows applications?

Use XKeymacs.

How to enable spell check in Emacs?

  • Download the flyspell.el and copy it into the directory of the Emacs load path.
  • Declare the flyspell-mode function as auto-load by adding the following expression to .emacs file.
(autoload 'flyspell-mode "flyspell" "On-the-fly spelling checker." t)
(autoload 'flyspell-delay-command "flyspell" "Delay on command." t)
(autoload 'tex-mode-flyspell-verify "flyspell" "" t)

Add the following expression for spell check in \({\LaTeX}\) editing.

(add-hook 'LaTeX-mode-hook 'flyspell-mode)

How to get doc-view mode to work on Windows Emacs?

  • Firstly, a clip on YouTube for how doc-view mode works.
  • Install Cygwin.
  • Visit this website.
  • The ghostscript program comes with CTeX bundle seems not to work. Instead, set doc-view-ghostscript-program to c:/cygwin/bin/gs.exe

How to input date and time to current buffer?

C-5 M-! date

How to speed up Emacs start-up?

  • Add the following to .emacs. Now when you "exit" Emacs with the familiar key sequence, what actually happens is that the server buffers (from emacsclientw) are saved and deleted and then the frame (Emacs window) is made invisible. You can check the task manager to see that Emacs is indeed still running. (See here)
(require 'server)
(defun my-done ()
    (interactive)
    (server-edit)
    (make-frame-invisible nil t))
(global-set-key (kbd "C-x C-c") 'my-done)
  • Invoke emacsclientw in a proper way. See here.
  • Change Desktop to some frequently used directory.
  • Setting up Emacs Daemon is not easy in Windows.

Where to find interesting Emacs tricks?

LaTeX

How to install AUCTeX?

  • Install MikTeX or CTeX bundle.
  • Download the zip file and extract it to the load path of Emacs.
  • Add the following to .emacs.
(autoload 'reftex-mode "reftex" "RefTeX Minor Mode" t)
(autoload 'turn-on-reftex "reftex" "RefTeX Minor Mode" nil)
(autoload 'reftex-citation "reftex-cite" "Make citation" nil)
(autoload 'reftex-index-phrase-mode "reftex-index" "Phrase mode" t)
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)       ;; with AUCTeX LaTeX mode
(setq preview-scale-function 1.7)                 ;; enlarge equation preview
(setq preview-auto-cache-preamble t)              ;; auto cache preamble
(setq preview-image-type (quote pnm)))            ;; image type for preview (not sure of why)
  • AUCTeX makes \(\LaTeX\) editing in Emacs much simpler, see a clip on YouTube.

How to install cdlatex?

  • Download cdlatex.el and put it to the load path of Emacs.
  • Add the following to .emacs to autoload cdlatex for AUCTeX mode.
(autoload 'cdlatex-mode "cdlatex" "CDLaTeX Mode" t)
(autoload 'turn-on-cdlatex "cdlatex" "CDLaTeX Mode" nil)
(add-hook 'LaTeX-mode-hook 'turn-on-cdlatex) ;with AUCTeX LaTeX mode
  • Add the following to .emacs to make math typing more comfortable.
(setq cdlatex-env-alist
 '(("equation*" "\\begin{equation*}\n?\n\\end{equation*}" nil)
   ("frame" "\\begin{frame}\n\\frametitle{?}\n\\end{frame}\n" nil)
   ("alignsplit" "\\begin{align*}\n&\\hspace{3ex} ?\n\\end{align*}\n" nil)
   ("ex" "\\begin{ex}\n?\n\\end{ex}" nil)
  )
)   ;; define several frequently-used environments

(setq cdlatex-command-alist
'(("eq" "Insert equation* env"   "" cdlatex-environment ("equation*") t nil)
  ("fm" "Insert frame env"   "" cdlatex-environment ("frame") t nil)
  ("al" "Insert alignsplit env" "" cdlatex-environment ("alignsplit") t nil)
  ("ex" "Insert example env" "" cdlatex-environment ("ex") t nil)
 )
)  ;; define alias of environment, type alias [TAB] to get the structure of environment

(setq cdlatex-math-symbol-alist
 '((?\; ("\\ldots"))
   (?h ("\\hat{?}"))
   (?T ("\\tilde{?}"))
   (?j ("\\{? \\}"))
   (?x ("\\text{?\}"))
   (?p ("o_P\\left(?\\right)"))
   (?P ("O_P\\left(?\\right)"))
   )
) ;; define several frequently math symbols, type `symbol to input symbol
  • cdlatex simplifies math typing in \(\LaTeX\) editing. e.g., type `h`s to get \(\hat{\sigma}\), type `P to get \(O_P()\). Type `h`m[TAB]-`m=`Pfr1[TAB]sq[TAB]n. (a total of 21 keystrokes) to get \[\hat{\mu}-\mu=O_P\left(\frac{1}{\sqrt{n}}\right).\] Without using cdlatex, the above expression needs 50 keystrokes: \hat{\mu}-\mu=O_P\left(\frac{1}{\sqrt{n}}\right).

How to install \({\LaTeX}\) packages in Linux?

  • Download the package from CRAN.
  • Extract the .zip file to get .dtx and .ins
  • latex *.ins to get .sty
  • Create a folder in /usr/share/texmf-texlive/tex/latex and move .sty to the new folder by using root permission (M-F2 gksu nautilus).
  • Refresh the package tree by sudo texhash.

R

How to clear the history of a R session?

rm(list=ls(all=TRUE))

How to debug a R code using ESS-mode in Emacs?

  • Download ess-tracebug.el and put it into emacs load-path.
  • Put the following in .emacs.
(require 'ess-tracebug)
(add-hook 'ess-post-run-hook 'ess-tracebug t)
  • Use the following keybings.
    M-c bset break point
    M-c wwatch window
    print(n)show the value of "n"
    nnext line
    ccontinue
    qexit debugging
  • For more information, see ess-tracebug google code.

How to run matlab and R codes in my department's server?

nohup /unsup/R/bin/R CMD BATCH script.R output_name.out&
nohup matlab -nodisplay <script.m> output_name.out&

org-mode

How to evaluate R code chunk in org-mode?

  • Add the following to .emacs, by default org-mode only enables emacs-lisp code evaluation.
(org-babel-do-load-languages
 'org-babel-load-languages
 '((R . t)
   (emacs-lisp . t)))
  • Add /path/to/R.exe to system environment variable "path", so that org-mode knows where to find R.
  • C-c C-c to evaluate R code chunk and insert result to .org file. C-' to edit the current code chunk in a R major-mode edit buffer.

How to use Org-babel to sweave R code and LaTeX code?

  • See an org-mode demo.
  • The following header arguments are useful.
    argumentvaluefunction
    :exportscode/results/both/nonecontrol which part to export
    :resultsvector/latex/codespecify how they will incorporated into the Org-buffer
    :filefilenameresults are saved to the named file
    :cacheyes/no"yes" ensures the code chunk are only re-run when inputs changed
  • Refer to org-mode manual for more header arguments.

Org version 7.8.09 with Emacs version 23