2013-06-12 23:40:20 +02:00
|
|
|
;;==============================================================================
|
|
|
|
;; TeX and LaTeX
|
|
|
|
;;==============================================================================
|
2013-09-17 15:43:45 +02:00
|
|
|
;; The default tex-mode and AucTeX may seem quite disappointing. Let's use
|
|
|
|
;; custom KISS functions for everything.
|
2013-06-12 23:40:20 +02:00
|
|
|
|
2013-09-17 15:43:45 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; CUSTOM
|
|
|
|
(defcustom tex-compiler nil
|
|
|
|
"This is the name of the executable called upon TeX compilations.
|
2013-06-12 23:40:20 +02:00
|
|
|
Examples: pdftex, pdflatex, xetex, xelatex, luatex, lualatex...
|
|
|
|
|
2013-09-17 15:43:45 +02:00
|
|
|
If value is nil, the compiler will be tex-default-compiler for
|
|
|
|
TeX mode, and latex-default-compiler for LaTeX mode."
|
2013-06-12 23:40:20 +02:00
|
|
|
:safe 'stringp)
|
|
|
|
|
2013-07-04 10:37:18 +02:00
|
|
|
(defcustom masterfile nil
|
2013-09-17 15:43:45 +02:00
|
|
|
"The file that should be compiled. Useful for modular documents."
|
2013-06-12 23:40:20 +02:00
|
|
|
:safe 'stringp)
|
|
|
|
|
2013-09-17 15:43:45 +02:00
|
|
|
(defcustom tex-compiler-options "-file-line-error-style -interaction nonstopmode -synctex=1"
|
2013-06-12 23:40:20 +02:00
|
|
|
"The options to the tex compiler. Options are set between the
|
|
|
|
compiler name and the file name.
|
|
|
|
|
|
|
|
Interesting options:
|
|
|
|
|
|
|
|
* -file-line-error-style: change the style of error report to
|
|
|
|
display file name and line first.
|
|
|
|
|
|
|
|
* -halt-on-error: default.
|
|
|
|
|
|
|
|
* -interaction <mode>: like -halt-on-error, you can set the way
|
|
|
|
the compilers behave on errors. Possible values for <mode> are
|
|
|
|
'batchmode', 'errorstopmode', 'nonstopmode' and 'scrollmode'.
|
|
|
|
|
|
|
|
* -shell-escape: allow the use of \write18{<external command>}
|
|
|
|
from within TeX documents. This is a potential security issue.
|
|
|
|
|
|
|
|
* -synctex=1: enable SyncTeX support.
|
|
|
|
|
|
|
|
You may use file local variable for convenience:
|
|
|
|
|
2013-09-17 15:43:45 +02:00
|
|
|
% -*- tex-compiler-options: \"-shell-escape\"
|
2013-06-12 23:40:20 +02:00
|
|
|
|
|
|
|
Note that -shell-escape can also be toggled with universal
|
|
|
|
argument."
|
|
|
|
:safe 'stringp)
|
|
|
|
|
2013-09-17 15:43:45 +02:00
|
|
|
(defcustom tex-startcommands ""
|
2013-06-12 23:40:20 +02:00
|
|
|
"You can call a TeX compiler upon a string instead of a file.
|
|
|
|
This is actually useful if you want to customize your
|
|
|
|
compilation.
|
|
|
|
|
|
|
|
If this variable is not an empty string, the mandatory \" is
|
|
|
|
prepended and \\input\" is appended, so that the target file gets
|
|
|
|
read; otherwise the TeX compiler would stop there.
|
|
|
|
|
|
|
|
You may use it to act on the process, like the default behaviour:
|
|
|
|
\\nonstopmode
|
|
|
|
which will continue the process whenever an error is
|
|
|
|
encountered. There is an command-line argument for that on most
|
|
|
|
compilers, that is is rarely useful.
|
|
|
|
|
|
|
|
If you use a color theme, or any conditional variable inside your
|
|
|
|
document, you may define it here:
|
|
|
|
\\def\\myvar{mycontent}"
|
|
|
|
:safe 'stringp)
|
|
|
|
|
2013-09-17 15:43:45 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; VARIABLES
|
|
|
|
(defvar tex-viewer "zathura --fork -s -x \"emacsclient --eval '(progn (switch-to-buffer (file-name-nondirectory \"'\"'\"%{input}\"'\"'\")) (goto-line %{line}))'\""
|
|
|
|
"PDF Viewer for TeX documents. You may want to fork the viewer
|
|
|
|
so that it detects when the same document is launched twice, and
|
|
|
|
persists when Emacs gets closed.
|
|
|
|
|
|
|
|
Simple command:
|
|
|
|
|
|
|
|
zathura --fork
|
|
|
|
|
|
|
|
We can use
|
|
|
|
|
|
|
|
emacsclient --eval '(progn (switch-to-buffer (file-name-nondirectory \"%{input}\")) (goto-line %{line}))'
|
|
|
|
|
|
|
|
to reverse-search a pdf using SyncTeX. Note that the quotes and
|
|
|
|
double-quotes matter and must be escaped appropriately.")
|
|
|
|
|
|
|
|
(defvar tex-default-compiler "pdftex"
|
|
|
|
"Default compiler for TeX mode. Used if tex-compiler is
|
|
|
|
empty.")
|
|
|
|
|
|
|
|
(defvar latex-default-compiler "pdflatex"
|
|
|
|
"Default compiler for LaTeX mode. Used if tex-compiler is
|
|
|
|
empty.")
|
|
|
|
|
|
|
|
(defvar tex-extension-list
|
|
|
|
'("aux" "glg" "glo" "gls" "idx" "ilg" "ind" "lof" "log" "nav" "out" "snm" "synctex" "synctex.gz" "tns" "toc" "xdy")
|
|
|
|
"List of known TeX exentsions. This list is used by 'tex-clean
|
|
|
|
to purge all matching files.")
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; FUNCTIONS
|
2013-07-04 10:37:18 +02:00
|
|
|
(defun tex-set-compiler ()
|
2013-06-12 23:40:20 +02:00
|
|
|
"Use compile to process your TeX-based document. Use a prefix
|
|
|
|
argument to call the compiler along the '-shell-escape'
|
|
|
|
option. This will enable the use of '\write18{<external
|
|
|
|
command>}' from within TeX documents, which need to allow
|
|
|
|
external application to be called from TeX.
|
|
|
|
|
|
|
|
This may be useful for some features like GnuPlot support with TikZ.
|
|
|
|
|
|
|
|
WARNING: the -shell-escape option is a potential security issue."
|
|
|
|
(interactive)
|
2013-09-17 15:43:45 +02:00
|
|
|
(hack-local-variables)
|
2013-06-12 23:40:20 +02:00
|
|
|
(let (
|
2013-09-17 15:43:45 +02:00
|
|
|
;; Set compiler to be tex-compiler if not empty, or a default
|
2013-06-12 23:40:20 +02:00
|
|
|
;; compiler otherwise.
|
|
|
|
(local-compiler
|
2013-09-17 15:43:45 +02:00
|
|
|
(if (not tex-compiler)
|
2013-06-12 23:40:20 +02:00
|
|
|
(cond
|
2013-09-17 15:43:45 +02:00
|
|
|
((string= "latex-mode" major-mode) latex-default-compiler)
|
|
|
|
((string= "plain-tex-mode" major-mode) tex-default-compiler)
|
|
|
|
(t (message "Warning: unknown major mode. Trying pdftex.") "pdftex"))
|
|
|
|
tex-compiler))
|
2013-06-12 23:40:20 +02:00
|
|
|
|
2013-09-17 15:43:45 +02:00
|
|
|
;; Master file.
|
2013-06-12 23:40:20 +02:00
|
|
|
(local-master
|
2013-09-17 15:43:45 +02:00
|
|
|
(if (not masterfile) buffer-file-name masterfile))
|
2013-06-12 23:40:20 +02:00
|
|
|
|
2013-09-17 15:43:45 +02:00
|
|
|
;; If tex-startcommands has some content, we make sure it is a string
|
2013-06-12 23:40:20 +02:00
|
|
|
;; that loads the file.
|
|
|
|
(local-start-cmd
|
2013-09-17 15:43:45 +02:00
|
|
|
(if (not (string= "" tex-startcommands))
|
|
|
|
(concat "\"" tex-startcommands "\\input\"")))
|
2013-06-12 23:40:20 +02:00
|
|
|
|
|
|
|
;; Support of prefix argument to toggle -shell-escape.
|
|
|
|
(local-shell-escape
|
|
|
|
(if (equal current-prefix-arg '(4)) "-shell-escape" "")))
|
|
|
|
|
2013-07-04 10:37:18 +02:00
|
|
|
(set (make-local-variable 'compile-command)
|
2013-09-17 15:43:45 +02:00
|
|
|
(concat local-compiler " " local-shell-escape " " tex-compiler-options " " local-start-cmd " \"" local-master "\""))))
|
2013-06-12 23:40:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
(defun tex-clean ()
|
|
|
|
"Remove all TeX temporary files. This command should be safe,
|
|
|
|
but there is no warranty."
|
|
|
|
(interactive)
|
2013-09-17 15:43:45 +02:00
|
|
|
(hack-local-variables)
|
2013-06-12 23:40:20 +02:00
|
|
|
(let (
|
|
|
|
;; Master file.
|
|
|
|
(local-master
|
2013-09-17 15:43:45 +02:00
|
|
|
(if (not masterfile) buffer-file-name masterfile)))
|
2013-06-12 23:40:20 +02:00
|
|
|
|
|
|
|
(let (
|
|
|
|
;; File name without extension.
|
|
|
|
(file
|
|
|
|
(replace-regexp-in-string "tex" "" (file-name-nondirectory local-master))))
|
|
|
|
|
|
|
|
;; Concatate file name to list.
|
|
|
|
(mapcar
|
|
|
|
;; Delete file if exist
|
|
|
|
(lambda (argfile) (interactive)
|
|
|
|
(when (and (file-exists-p argfile) (file-writable-p argfile))
|
|
|
|
(delete-file argfile)
|
|
|
|
(message "[%s] deleted." argfile)))
|
|
|
|
(mapcar
|
|
|
|
;; Concat file name with extensions.
|
|
|
|
(lambda (arg) (interactive) (concat file arg))
|
2013-09-17 15:43:45 +02:00
|
|
|
tex-extension-list)))))
|
2013-06-12 23:40:20 +02:00
|
|
|
|
|
|
|
(defun tex-pdf-compress ()
|
2014-02-18 12:13:18 +01:00
|
|
|
"Use `masterfile' variable as default value for `pdf-compress'."
|
2013-06-12 23:40:20 +02:00
|
|
|
(interactive)
|
2013-09-17 15:43:45 +02:00
|
|
|
(hack-local-variables)
|
2014-02-18 12:13:18 +01:00
|
|
|
(let ((local-master (if (not masterfile) buffer-file-name masterfile)))
|
|
|
|
(pdf-compress local-master)))
|
2013-06-12 23:40:20 +02:00
|
|
|
|
|
|
|
(defun tex-pdf-view ()
|
|
|
|
"Call a PDF viewer for current buffer file. File name should be
|
|
|
|
properly escaped with double-quotes in case it has spaces."
|
|
|
|
(interactive)
|
2013-09-17 15:43:45 +02:00
|
|
|
(hack-local-variables)
|
2013-06-12 23:40:20 +02:00
|
|
|
(let (
|
|
|
|
;; Master file.
|
|
|
|
(local-master
|
2013-09-17 15:43:45 +02:00
|
|
|
(if (not masterfile) buffer-file-name masterfile)))
|
2013-06-12 23:40:20 +02:00
|
|
|
|
|
|
|
(shell-command
|
2013-09-17 15:43:45 +02:00
|
|
|
(concat tex-viewer
|
2013-06-12 23:40:20 +02:00
|
|
|
" \""
|
|
|
|
(replace-regexp-in-string "\.tex$" "\.pdf" (file-name-nondirectory local-master))
|
|
|
|
"\" &" ))
|
|
|
|
(delete-windows-on "*Async Shell Command*")))
|
|
|
|
|
2013-09-21 13:03:55 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; MACROS
|
|
|
|
|
2013-09-26 12:07:33 +02:00
|
|
|
(defun latex-itemize ()
|
|
|
|
"Prepend \\item to the beginning of the line if not already
|
|
|
|
there, otherwise insert it on next line. On region, append
|
2013-09-21 17:06:51 +02:00
|
|
|
\item to every line and surround the region by an `itemize'
|
|
|
|
environment. If bound to M-RET, you can then easily apply this
|
|
|
|
command on the paragraph at point with M-h M-RET."
|
2013-09-21 13:03:55 +02:00
|
|
|
(interactive)
|
2013-09-30 19:11:22 +02:00
|
|
|
(let (min max case-fold-search)
|
|
|
|
(if (not (region-active-p))
|
|
|
|
|
|
|
|
(if (string-match "\\item" (buffer-substring (line-beginning-position) (line-end-position)))
|
|
|
|
(progn
|
|
|
|
(goto-char (line-end-position))
|
|
|
|
(newline)
|
|
|
|
(insert "\\item "))
|
|
|
|
(goto-char (line-beginning-position))
|
|
|
|
(insert "\\item")
|
|
|
|
(just-one-space))
|
|
|
|
|
|
|
|
(replace-regexp "^ *\\([^
|
2013-09-21 13:03:55 +02:00
|
|
|
]\\)" "\\\\item \\1" nil (region-beginning) (region-end))
|
2013-09-30 19:11:22 +02:00
|
|
|
(goto-char (region-end))
|
|
|
|
(goto-char (line-end-position))
|
|
|
|
(newline)
|
|
|
|
(insert "\\end{itemize}")
|
|
|
|
(goto-char (region-beginning))
|
|
|
|
(goto-char (line-beginning-position))
|
|
|
|
(insert "\\begin{itemize}")
|
|
|
|
(newline))))
|
2013-09-21 13:03:55 +02:00
|
|
|
|
2013-09-17 15:43:45 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2014-02-18 12:13:18 +01:00
|
|
|
;; TeX setup
|
2013-09-26 12:07:33 +02:00
|
|
|
|
2014-02-18 12:13:18 +01:00
|
|
|
(defun tex-setup ()
|
2013-06-12 23:40:20 +02:00
|
|
|
(dolist (key '("\C-c\C-f" "\C-c\C-b"))
|
|
|
|
(local-unset-key key))
|
2013-11-11 13:51:16 +01:00
|
|
|
(set-face-attribute 'tex-verbatim nil :family "freemono")
|
2013-07-04 10:37:18 +02:00
|
|
|
(set (make-local-variable 'compilation-scroll-output) t)
|
|
|
|
(set (make-local-variable 'compilation-hide-window) t)
|
2013-10-30 15:57:50 +01:00
|
|
|
(set (make-local-variable 'paragraph-start) "
|
|
|
|
")
|
|
|
|
;; (set (make-local-variable 'use-hard-newlines) t)
|
2013-09-21 13:03:55 +02:00
|
|
|
(local-set-key (kbd "<f9>") 'tex-pdf-view)
|
2014-02-18 12:13:18 +01:00
|
|
|
(tex-set-compiler))
|
|
|
|
|
|
|
|
(add-hook 'tex-mode-hook 'tex-setup)
|
|
|
|
(tex-setup)
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; LaTeX setup
|
2013-09-26 12:07:33 +02:00
|
|
|
|
2014-02-18 12:13:18 +01:00
|
|
|
(defun latex-setup ()
|
|
|
|
(local-set-key (kbd "M-RET") 'latex-itemize)
|
|
|
|
(turn-on-orgtbl))
|
2013-09-30 19:11:22 +02:00
|
|
|
|
2014-02-18 12:13:18 +01:00
|
|
|
(add-hook 'latex-mode-hook 'latex-setup)
|
|
|
|
(latex-setup)
|
2013-11-11 13:51:16 +01:00
|
|
|
|
2013-09-30 19:42:07 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; The magnificent latex-math-preview mode!
|
|
|
|
;; TODO: some symbols are not generated properly.
|
|
|
|
(autoload 'latex-math-preview-expression "latex-math-preview" nil t)
|
|
|
|
(autoload 'latex-math-preview-insert-symbol "latex-math-preview" nil t)
|
|
|
|
(autoload 'latex-math-preview-save-image-file "latex-math-preview" nil t)
|
|
|
|
(autoload 'latex-math-preview-beamer-frame "latex-math-preview" nil t)
|
|
|
|
(autoload 'latex-math-preview-text-symbol-datasets "latex-math-preview" nil t)
|
|
|
|
|
|
|
|
(setq latex-math-preview-cache-directory-for-insertion
|
|
|
|
(concat emacs-cache-folder "latex-math-preview-cache"))
|
2013-09-30 19:11:22 +02:00
|
|
|
|
|
|
|
;; Extra for latex-math-preview-mode.
|
|
|
|
;; TODO: latex-math-preview-mode extra does not work.
|
|
|
|
(require 'latex-math-preview-extra-data nil t)
|
2013-09-30 19:42:07 +02:00
|
|
|
(add-hook
|
|
|
|
'latex-mode-hook
|
|
|
|
(lambda ()
|
|
|
|
;; (local-set-key (kbd "C-c p") 'latex-math-preview-expression)
|
|
|
|
;; (local-set-key (kbd "C-c C-p") 'latex-math-preview-save-image-file)
|
|
|
|
(local-set-key (kbd "C-c j") 'latex-math-preview-insert-symbol)
|
|
|
|
(local-set-key (kbd "C-c C-j") 'latex-math-preview-last-symbol-again)
|
|
|
|
;; (local-set-key (kbd "C-c C-b") 'latex-math-preview-beamer-frame)
|
2013-10-06 19:56:55 +02:00
|
|
|
;; (add-to-list 'latex-math-preview-text-symbol-datasets
|
|
|
|
;; latex-math-preview-textcomp-symbol-data)
|
|
|
|
;; (add-to-list 'latex-math-preview-text-symbol-datasets
|
|
|
|
;; latex-math-preview-pifont-zapf-dingbats-symbol-data)
|
|
|
|
;; (add-to-list 'latex-math-preview-text-symbol-datasets
|
|
|
|
;; latex-math-preview-pifont-symbol-fonts-symbol-data)))
|
|
|
|
))
|
2014-02-12 17:48:29 +01:00
|
|
|
|
|
|
|
(provide 'mode-tex)
|