Showing posts with label emacs. Show all posts
Showing posts with label emacs. Show all posts

Friday, January 10, 2014

Processing org-mode -> latex-beamer -> pdf in the background

I have enjoyed making slides using org-mode greatly. However, it could be very slow. I currently have 30-page slides (but complicated code blocks and frames on them) and it takes me 10 seconds to compile.

Even worse, Emacs is not concurrent. Thus I have to wait those 10 seconds without being able to continue typing. This is annoying.

The fix is actually simple: launch another Emacs process to compile the file. Now everything works in the background in a non-blocking way. The code is very simple. Bind it with a key and that's it.

  (setq wy-obc-eval-str 
 "--eval=(progn (find-file \"%s\") (org-beamer-export-to-pdf) (kill-emacs))")
   
  (defun wy-org-beamer-concurrent ()
    (interactive)
    (save-buffer)
    (let ((eval-str (format wy-obc-eval-str buffer-file-name)))
      (call-process "emacs" nil 0 nil eval-str "--geometry" "60x5")))

PS: legoscia told me that there is emacs-async package (see question), which can free me from launching another emacs explicitly. This may be a better solution but I haven't tried that out.

Thursday, January 9, 2014

Colored code in org-mode export (Minted way)

In the emacs configuration file, add

(setq org-latex-listings 'minted)

And in the org file, add

#+LATEX_HEADER: \usepackage{minted}
#+LATEX_HEADER: \usepackage{color}

Then minted should work (in principle).

Fixes:

(1) I have to replace the pdflatex command to pdflatex --shell-escape to make it work (as I can read from the org code, seems org tries to add --shell-escape automatically. But on my machine it doesn't work out of the box.)

(2) Add support for Mathematica: Here is the plugin:

https://github.com/benjamin-hodgson/pygments-mathematica

It almost works. But there are a few special operators missing: $, `, !. I added them here and made a pull request.

https://github.com/tririver/pygments-mathematica

The looking is considerably better than Listings way:


Wednesday, January 8, 2014

Colored code in org-mode export

To export org-mode code with more style controls, use (note the var name has changed recently and the old one doesn't work any more!):

(setq org-latex-listings t)

One can then tweak the styles. For example, mine for exporting to beamer is:

\definecolor{dkgreen}{rgb}{0,0.5,0}
\definecolor{dkred}{rgb}{0.5,0,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{blue(pigment)}{rgb}{0.2, 0.2, 0.6}
\lstset{
  basicstyle=\ttfamily\bfseries\footnotesize\color{blue(pigment)},
  morekeywords={virtualinvoke},
  keywordstyle=\color{blue}\bf,
  ndkeywordstyle=\color{red},
  commentstyle=\color{dkred},
  stringstyle=\color{dkgreen},
}

\makeatletter
\def\verbatim{\vspace{-0.3cm}\footnotesize\@verbatim \@vobeyspaces \@xverbatim}
\makeatother

Save the above code to a file, and then input this file to the org-beamer export:

#+LATEX: \input{the_above_file.tex}

And This is how it looks like:


Tuesday, January 7, 2014

Evaluate Mathematica code in emacs org-mode

This is yet another attempt of running Mathematica in emacs. For the previous one, see
http://cosmosimple.blogspot.co.uk/2013/12/run-mathematica-commands-in-emacs.html

However, in org-mode, there are greater expectations -- we would like emacs not only to be able to run the Mathematica code, but also "knows" which are code blocks, which are results. Such knowledge enables clever things like calling a Mathematica function from emacs lisp, or other org-supported language. Also, one can easily export the org output to pdf or html, with code and result in the desired styles.

I didn't find org support for Mathematica. Thus I created it myself:

https://github.com/tririver/wy-els/blob/master/ob-mathematica.el

In principle, I could do better by using sessions. However, it's beyond my current need thus I leave it for (possible) future work.

Note that the rather complicated structure of org code blocks can be simplified by yasnippet of emacs.

Example: working on a org-table
(Note that the lines above "#+RESULTS:" are inputs. After pressing C-c C-c inside the SRC block, the result emerges.)

#+NAME: example-table
          | 1 | 4 |
          | 2 | 4 |
          | 3 | 6 |
          | 4 | 8 |
          | 7 | 0 |

#+BEGIN_SRC mathematica :var x=example-table :exports both
1+Transpose@x
#+END_SRC

#+RESULTS:
| 2 | 3 | 4 | 5 | 8 |
| 5 | 5 | 7 | 9 | 1 |

Example: call Mathematica function from org-mode

#+NAME: add-one
#+BEGIN_SRC mathematica :var x=1
  x+1
#+END_SRC

#+RESULTS: add-one
: 2

#+CALL: add-one(x='(1 2 3 4))

#+RESULTS:
| 2 | 3 | 4 | 5 |

Tuesday, December 17, 2013

Run mathematica commands in emacs -- updated

Sometime ago I posted how to run Mathematica code in Emacs. Now this is improved -- faster and cleaner version:


(defun wy-rl-beginning () ; "rl" denotes "region or line"
  (if mark-active (region-beginning) (line-beginning-position))) 
(defun wy-rl-end () ; "rl" denotes "region or line"
  (if mark-active (region-end) (line-end-position)))
(defun wy-get-rl ()
  (buffer-substring-no-properties (wy-rl-beginning) (wy-rl-end)) )
(defun wy-remove-comment (s)
  "Remove comment symbol (on the left only)."
  (if (string-match (concat "\\`[ \t\n\r]*" comment-start "+") s)
      (replace-match "" t t s)
    s))
(defun wy-math2 ()
  (interactive)
  (let ((expr) (code) (res))
    (setq expr (wy-remove-comment (wy-get-rl)))
    (setq code (concat "echo 'Print[" expr "]'>/tmp/Meval2.tmp.m;"
      "MathematicaScript -script /tmp/Meval2.tmp.m"))
    (setq res (shell-command-to-string code))
    (kill-new res)
    (princ res)))


Tuesday, November 5, 2013

Chrome edits in emacs

Noticed a plugin in chrome -- Edit with Emacs.

When using Chrome and editing some (but unfortunately not all) text area, this plugin can connect to emacs and let the edit done therein.

This works great for gmail. Especially that I find it works together with org-mode. With the following bit of code in .emacs, one can use all powerful org edits, such as org-tables and send them back to gmail.

(require 'edit-server)
(edit-server-start)

(add-hook 'edit-server-start-hook (lambda () 
    (org-mode)
    (local-set-key (kbd "C-d") '(lambda () 
        (interactive)
     (mark-whole-buffer)
     (org-html-convert-region-to-html)
 (goto-char (point-max))
 (insert "Here is signature")
     (edit-server-done)))
))

Monday, January 14, 2013

A mode for writing derived from org mode

Didn't find some features among the sea of el's in the emacs world. Thus implemented them myself:

(1) (Almost) real time character counting. This includes counting of the whole buffer, and the counting of the present section (defined in the org-mode sense).

As I am mostly writing Chinese, character counting instead of word counting is good enough for me. Word counting should be possible following a similar way.

The counting is displayed in the title bar. I am actually hiding the mode line to avoid distraction. Thus the title bar is the only place where indicators can be shown.

(2) Focus edit on one section, hide all other parts of the document. Of course one can re-focus on the whole document with another key stoke.

The code is shared .


Thursday, November 29, 2012

Latex with UTF-8 alias in emacs

Recently I felt I would need something more than auctex in emacs. Then I ended up writing a custom LaTeX-mode myself. Now I guess I no longer need LaTeX-preview anymore, and it's no longer painful to edit long formulas!

This is the .tex file, displayed in a plain mode:


 In my custom LaTeX-mode, it displays as (but it still saves as above):


And of course this is the compile result, as you could expect from the first screenshot figure:


The lisp code is shared. As usual for free shares, it is without warranty and try-outs are at your own risk. On the other hand, the modification on display is based on only font-lock-mode and compose-region. No dirty hacks. Thus should be safe.

https://github.com/tririver/wyTex

I also put some custom settings of auctex in the same file. They are not necessary. The only essential part is the function wyTex-key. This function is actually bi-directional alias. If you can input α, β, etc directly from keyboard instead of \alpha, \beta, etc, then directly input them and in saved file they will be saved as  \alpha, \beta, etc and thus can compile using latex or pdflatex directly.

Here is the wyTex-key function:
(defun wyTex-key (utf8-char tex-command)
        (local-set-key utf8-char 
      `(lambda () (interactive) (insert ,tex-command))) 
(font-lock-add-keywords
'wyTex-mode `((,(concat "\\(\\" tex-command "\\)")
(0 (progn (compose-region (match-beginning 1)
 (match-end 1) ,utf8-char)
 nil)))))
  )
Acknowledgment: Thank Francesco for answering my elisp question. And Thank +Xah for suggesting writing an own mode to learn elisp!

Monday, April 16, 2012

Move personal bindings to Hyper- in emacs

I guess everybody who heavily use emacs has a number of his / her own key-bindings. Unfortunately, personal key-bindings (if not too long) could conflict with system ones. If not in one mode, it would happen in another mode.

Now I am happy with my solution: I map left alt to Hyper, and  then bind everything personal to Hyper. The mapping can be done in xmodmap: create a file, say, .Xmodmap, with
clear Mod1
add Mod1 = Alt_L
add Mod3 = Hyper_L
add Mod4 = Super_L
keycode 108 = Hyper_L
Then run xmodmap .Xmodmap (and put it to startup applications so it runs every time when login).

Then in .emacs, set every key-binding you like with Hyper (I set a lot of them. Here are only some samples):
(global-set-key (kbd "H-g") 'goto-line)
(global-set-key (kbd "H-l") 'forward-char)
(global-set-key (kbd "H-h") 'backward-char)
(global-set-key (kbd "H-j") 'next-line)
(global-set-key (kbd "H-k") 'previous-line)
(global-set-key (kbd "H-\\") 'math-eval)
(global-set-key (kbd "H-p") 'my-org-screenshot)
(global-set-key (kbd "<H-SPC>") 'save-buffer)
(global-set-key (kbd "<H-escape>") '(lambda ()   (interactive)   (kill-buffer (current-buffer))))
Another advantage is that I press left alt using thumb. Thumb is much stronger than little figure for frequent use :)

Monday, March 26, 2012

Run mathematica commands in emacs

I would like the following feature: When writing a document in emacs (say, a LaTeX paper), I can write the original expression in a comment line, then execute and get the answer. The idea is to send a string from emacs to a shell script, which runs Mathematica kernel

Here is the realisation:

Sunday, December 11, 2011

Taking notes using emacs org-mode

After years of using emacs, it's surprising that I got to know org-mode so late. Better late than never. The first thing I did is to switch to org-mode to arrange my notes, on research, computer technology and life.

My criteria for a good note taker is as follows:
  1. Easy to organize topics / create note
  2. Full text search
  3. Sync a copy to internet
  4. Todo list and other tags in note
  5. Mathematics formula support
  6. Simple item folding and expanding
  7. The editor is stable and feature rich
  8. At best plain text, to access anywhere
With the help of Dropbox and some shell scripts, emacs org-mode is good for all the above aspects: (3) is implemented by putting the note folder into Dropbox. (4) - (8) are the advantages of org-mode or emacs. While here I use some simple shell scripts to implement (1) and (2) on command line.

Saturday, December 10, 2011

Use emacs as a widget

Previously I used yakuake or tilda as a terminal -- press F12 to call or switch back from them. Then an idea comes: why not using emacs for this purpose, and a lot more?

Here is what I have now: press f12 to switch from a pre-configured full screen emacs window and the current window. I can run terminal commands, open frequently used files, take notes and do mathematical calculation (maxima) in emacs. Press f12 again brings me back to the previous application I was using.


The idea is as follows: