珞珈山水BBS电脑网络Linux & Unix → 单文区文章阅读

单文区文章阅读 [返回]
发信人: bigben (早起的鸟儿debug), 信区: Linux_Unix
标  题: emacs-wiki-latex.el (pp版)
发信站: 珞珈山水BBS站 (Sat Oct 22 18:19:24 2005), 转信


(require 'emacs-wiki)

(
defcustom emacs-wiki-latex-template
"\\documentclass[12pt]{article}
\\pagestyle{empty}
\\usepackage{latexsym}
\\begin{document}
%s
\\end{document}"

"LaTeX file template")

(
defcustom emacs-wiki-latex-image-url
  
"latex"
  
"relative url to latex images directory in the web")

(
defcustom emacs-wiki-latex-directory
  (concat emacs-wiki-publishing-directory 
"/" emacs-wiki-latex-image-url)
  
"local latex images directory")

(
defcustom emacs-wiki-latex-temp-dir
  
"~/wiki/temp"
  
"latex work dir. Temporary files are generated in this dir")

(
defcustom emacs-wiki-latex-command "latex"
  
"latex command location")

(
defcustom emacs-wiki-convert-command "convert"
  
"convert command location")

(
defvar emacs-wiki-convert-density "150x150")

;; setup inline-markup
(add-to-list 'emacs-wiki-publishing-markup
             '[
"\\(\\$\\$\\)\\([^$]+?\\)\\(\\$\\$\\)" 2
               emacs-wiki-latex-inline-markup])

;; setup <latex> tag
(add-to-list 'emacs-wiki-markup-tags
             '(
"latex" t nil t emacs-wiki-latex-tags))

(
defmacro with-temp-dir (DIR &rest body)
  
"Do something in DIR and return to current dir finally."
  (
let ((pwd (make-symbol "pwd")))
    `(
let ((,pwd default-directory))
       (cd ,DIR)
       (
progn ,@body)
       (cd ,pwd)
       nil)))

(
defun emacs-wiki-latex-tags (beg end highlight-p)
  (
if highlight-p
      (
progn
        (remove-text-properties
         beg end '(face nil font-lock-multiline nil
                        invisible nil intangible nil display nil
                        mouse-face nil keymap nil help-echo nil))
        (goto-char end))
    (
let* ((text (buffer-substring beg end))
           (image (emacs-wiki-latex-to-png text)))
      (delete-region beg end)
      (insert 
       (format 
"<img src=\"%s\" class=\"latex-display\" alt=\"%s\" />"
               image
               text))
      (add-text-properties beg (point) '(rear-nonsticky (read-only)
                                                        read-only t)))))

(
defun emacs-wiki-latex-inline-markup ()
  (
let* ((beg (match-beginning 2))
         (end (match-end 2))
         (text (buffer-substring-no-properties beg end)))
    (delete-region (- beg 2) (+ end 2))
    (insert
     (format 
"<img src=\"%s\" class=\"latex-inline\" alt=\"$%s$\" />"
             (emacs-wiki-latex-to-png (concat 
"$" text "$"))
             text))))

(
defun emacs-wiki-latex-to-png (text)
  
"Convert text to png image use `latex' and `convert'.
Returns the png file name."

  (
let ((file (md5 text))
        (content (format emacs-wiki-latex-template text)))
    (
unless (file-exists-p emacs-wiki-latex-directory)
      (make-directory emacs-wiki-latex-directory))
    (
unless (file-exists-p emacs-wiki-latex-temp-dir)
      (make-directory emacs-wiki-latex-temp-dir))
    (
unless (file-exists-p (format
                            
"%s/%s.png"
                            emacs-wiki-latex-directory
                            file))
      (with-temp-dir
       emacs-wiki-latex-temp-dir
       (
with-temp-file (concat file ".tex")
         (insert content))
       (message
        (shell-command-to-string
         (format 
"%s %s.tex"
                 emacs-wiki-latex-command
                 file)))
       (shell-command-to-string
        (format 
"%s -trim -density %s %s.dvi %s.png"
                emacs-wiki-convert-command
                emacs-wiki-convert-density
                file file))
       (copy-file (concat file 
".png")
                  emacs-wiki-latex-directory t t)))
    (format 
"%s/%s.png" emacs-wiki-latex-image-url file)))

(
provide 'emacs-wiki-latex)
【 在 bigben (早起的鸟儿debug) 的大作中提到: 】
: * emacs-wiki-latex.el
: emacs-wiki-latex.el是一个给emacs-wiki加入pp的latex公式图片的工具。
: 不管是在EmacsWikiMode还是PlannerMode中,都可以很方便地输入latex公式,
: ...................

--
天空不留下鸟的痕迹,但我已经飞过.


※ 来源:·珞珈山水BBS站 bbs.whu.edu.cn·[FROM: 202.114.79.*]
[返回单文区目录]

武汉大学BBS 珞珈山水站 All rights reserved.
wForum , 页面执行时间:16.343毫秒