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 |

Wednesday, January 1, 2014

Change default stylesheet in Mathematica

The whole thing starts when I wanted to change the default font in Mathematica (the default is buggy on my Linux box if ms-fonts are not installed). Later I also modified some other defaults. It's not very trivial thus let me note it down the steps:

(1) Open a notebook, in the menu choose "Format" -> "Edit Stylesheet".

(2) Choose a style to change. For example, "Text", in the menu at the top left.

(3) Modify fonts, etc.

(4) Save this stylesheet to a .nb file. Optionally one can also click Install StyleSheet so that the stylesheet can be found at "Format" -> "Stylesheet..." menu.

(5) Open menu "Format" -> "Option Inspector", make sure "Global Preference" is selected (where the default is "Selection") in the popup menu.

(6) Search for DefaultStyleDefinitions, change it to the saved stylesheet file. Click apply.

Close Mathematica and launch it again. The default font is changed.

Another change I made is added an opener for a group of cells. This can be done at steps (4). Here, search ShowGroupOpener and set it to True, and save. I also changed the color so that it does not look annoying.

Saturday, December 28, 2013

Better copy - paste in LaTeX generated PDF files

Research papers are often read in pdf format, with a lot of equations. One annoying fact is that copying the pdf equation and pasting it elsewhere makes almost no sense.

For example, it would be better to copy equations in the pdf file directly in the LaTeX source form, and then it can be pasted into presentations, using beamer, or via plugins to PowerPoint or LibreOffice.

As another example, as I am updating a manual for Mathematica code, it would be very useful for the user to copy the equations (which are Mathematica code) into Mathematica directly.

The solution is mentioned here and tested to work well.

\usepackage{accsupp}

\begin{equation}
\BeginAccSupp{ActualText={The text to paste}}
The text to display
\EndAccSupp{}
\end{equation}

Here \begin{equation} and \end{equation} are not necessary -- if advanced copy - paste is needed  outside the math environment, this can and should be omitted.

As pointed out by Oberdiek, if the paste text is the LaTex source code (to be used, for example, in making presentations), one can further ease the above process by

\documentclass{article}
\usepackage{accsupp}

\newcommand*{\copyable}[1]{%
  \BeginAccSupp{%
    ActualText=\detokenize{#1},%
    method=escape,
  }%
  #1%
  \EndAccSupp{}%
}

\begin{document}
\[
  \copyable{\int x\sin ax\;\mathrm{d}x = \frac{\sin ax}{a^2}-\frac{x\cos ax}{a}+C}
\]
\end{document}

Friday, December 27, 2013

Customizing Input in Mathematica Notebook

Here we customize the Mathematica Notebook frontend by the following example: bind key ctrl + ] into [[]] (which is array label in Mathematica), and let the cursor stay in the middle of the double brackets.

The way is to modify KeyEventTranslations.tr. This file is located at

Linux:
$MATHEMATICA_HOME/SystemFiles/FrontEnd/TextResources/X

Windows:
$MATHEMATICA_HOME/SystemFiles/FrontEnd/TextResources/Windows

Mac OS:
$MATHEMATICA_HOME/SystemFiles/FrontEnd/TextResources/OSX

One can directly modify this file. But the recommended method (at least under Linux) is to copy this file to user directory. For example, in Linux, this file should be copied to (create the directory before copying)

.Mathematica/SystemFiles/FrontEnd/TextResources/X/

The above copying step can be omitted if one prefers to edit directly system-wide.

After this, insert the following code before the final closing "}]" near the end of the file. Note that one may also need to add a comma in front of this code block, to make this entry an element of the whole list.

Item[KeyEvent["[", Modifiers -> {Control}], FrontEndExecute[{
    FrontEnd`NotebookWrite[FrontEnd`InputNotebook[], "[", After],
    FrontEnd`NotebookWrite[FrontEnd`InputNotebook[], "]", Before]
}]]