You are not logged in.

#1 2010-02-15 15:17:17

keither
Member
Registered: 2009-07-31
Posts: 57

custom keybinding for shell command in emacs

Hello all,

I am a very frequent LaTeX user, and I often have BibTeX references and pstricks images, so I end up compiling my document several times for every document. To speed up this process, I wrote a short, simple shell script (below)

#!/bin/sh
file="$1"
latex \\nonstopmode\\input $file.tex
bibtex $file
latex \\nonstopmode\\input $file.tex
latex \\nonstopmode\\input $file.tex
dvips $file.dvi
ps2pdf $file.ps

This has been much nicer, since instead of typing all of those things, I simply type "mytex file."

Being the greedy person that I am, I would now like to compile it from emacs without typing the shell command, kind of like C-c C-f when I am just compiling as usual. I don't have very much experience with custom keybindings in emacs anyway, but I certaintly do not know how to call a particular shell command including the current file name.

Any tips?

Last edited by keither (2010-02-15 15:18:42)

Offline

#2 2010-02-15 22:11:08

chpln
Member
From: Australia
Registered: 2009-09-17
Posts: 361

Re: custom keybinding for shell command in emacs

Global binding (untested):

(global-set-key (kbd "C-c f")
                (lambda ()
                  (shell-command (concat "mytex " buffer-file-name))))

It is simple to make it specific to a major-mode, though exactly what is required depends on the mode in use.

Offline

#3 2010-02-16 00:03:17

keither
Member
Registered: 2009-07-31
Posts: 57

Re: custom keybinding for shell command in emacs

Thank you very much for you response. With this keybinding, I get the following error:

Wrong type argument: commandp, (lambda nil (shell-command (concat "mytex " buffer-file-name)))

Any idea what might be going on there?

Offline

#4 2010-02-16 02:00:11

chpln
Member
From: Australia
Registered: 2009-09-17
Posts: 361

Re: custom keybinding for shell command in emacs

Yes, sorry.  I realised the mistake shortly after posting, but didn't get chance to correct it.  It should have read:

(global-set-key (kbd "C-c f")
                      (lambda ()
                        (interactive)
                        (shell-command (concat "mytex " buffer-file-name))))

Offline

#5 2010-02-16 02:10:39

keither
Member
Registered: 2009-07-31
Posts: 57

Re: custom keybinding for shell command in emacs

Beautiful! Thank you!

This is also a good introduction to keybinding next time I want to teach emacs something.

Offline

#6 2010-02-16 03:36:59

keither
Member
Registered: 2009-07-31
Posts: 57

Re: custom keybinding for shell command in emacs

Actually, one more thing. My shell command is more successful if I don't type the .tex file extension.  I tried to remove it like this:

(global-set-key (kbd "C-c f")
                      (lambda ()
                        (interactive)
                        (shell-command (concat "mytex " file-name-sans-extension (buffer-file-name)))))

but I get "Symbol's value as variable is void: file-name-sans-extension"

Offline

#7 2010-02-16 07:22:44

chpln
Member
From: Australia
Registered: 2009-09-17
Posts: 361

Re: custom keybinding for shell command in emacs

The parens will also need to enclose `file-name-sans-extension'.

(global-set-key (kbd "C-c f")
                (lambda ()
                  (interactive)
                  (shell-command (concat "mytex " (file-name-sans-extension buffer-file-name)))))

Offline

#8 2010-02-16 08:27:00

Pank
Member
From: IT
Registered: 2009-06-13
Posts: 371

Re: custom keybinding for shell command in emacs

First of all, you will probably save some compilation time by using an "inteligent" script such as latexmk. You should look into it if you have not done so already.

Second of all, AUCTeX will also suggest the "correct" next step when using C-c C-c. Also, you should consider just adding latexmk or your own script to the C-c C-c cycle. Here are some examples from my .emacs

(eval-after-load "tex"
  '(progn 
     (add-to-list 'TeX-command-list
          (list "View in Acrobat Reader" "acroread %o"
            'TeX-run-command nil t))          
     (add-to-list 'TeX-command-list
          (list "Count Words" "texcount -inc %s.tex"
            'TeX-run-command nil t))
     (add-to-list 'TeX-command-list 
          '("Synctex" "/home/rasmus/.scripts/synctex-emacs %t %b %n" TeX-run-TeX nil t) t)
     ))

If you want to use a script as the one suggested by yourself you would want to look into the last exampe ("Synctex").

--Rasmus


Arch x64 on Thinkpad X200s/W530

Offline

#9 2010-02-16 11:58:15

keither
Member
Registered: 2009-07-31
Posts: 57

Re: custom keybinding for shell command in emacs

Hmm...I still get the same errors with the parentheses, actually.

Pank, good suggestions, though I'll have to go look at Synctex and make sure it's actually doing what I need. When I have pstricks images, for example, the order of operations (latex -> dvips -> ps2pdf) is sometimes important. I will definitely add all of that stuff though, I've been opening the terminal to use texcount.

Offline

#10 2010-02-16 20:33:55

Pank
Member
From: IT
Registered: 2009-06-13
Posts: 371

Re: custom keybinding for shell command in emacs

AUCTeX should detect pstricks and suggest the correct procedure. This should especially be true for simple, single-file documents. I have never used pstricks myself since pdftex is a priority to me. (I stick with PGF/TikZ instead).

On the error: My snip should be okay. Try to eval it in scratch.


Arch x64 on Thinkpad X200s/W530

Offline

#11 2010-02-16 20:42:03

keither
Member
Registered: 2009-07-31
Posts: 57

Re: custom keybinding for shell command in emacs

Sorry, the error referred to the post above yours, not your snip.

Offline

#12 2010-02-17 02:28:05

chpln
Member
From: Australia
Registered: 2009-09-17
Posts: 361

Re: custom keybinding for shell command in emacs

keither wrote:

Hmm...I still get the same errors with the parentheses, actually.

Strange -- I tested it before submitting and just now without any problems so long as the buffer corresponds to a file (which itself is a trivial error to solve).  Is it possible that the old command for that binding was still being used?

Last edited by chpln (2010-02-17 02:30:17)

Offline

Board footer

Powered by FluxBB