muse-latex: Make images look better.
[muse-el.git] / lisp / muse-latex.el
blob7df12041a058d20e7d6f56fae5b1efbf5b2d4110
1 ;;; muse-latex.el --- publish entries in LaTex or PDF format
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
7 ;; Emacs Muse is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published
9 ;; by the Free Software Foundation; either version 2, or (at your
10 ;; option) any later version.
12 ;; Emacs Muse is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Emacs Muse; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;; Li Daobing (lidaobing AT gmail DOT com) provided CJK support.
28 ;; Trent Buck (trentbuck AT gmail DOT com) gave valuable advice for
29 ;; how to treat LaTeX specials and the like.
31 ;;; Code:
33 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 ;; Muse LaTeX Publishing
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 (require 'muse-publish)
41 (defgroup muse-latex nil
42 "Rules for marking up a Muse file as a LaTeX article."
43 :group 'muse-publish)
45 (defcustom muse-latex-extension ".tex"
46 "Default file extension for publishing LaTeX files."
47 :type 'string
48 :group 'muse-latex)
50 (defcustom muse-latex-pdf-extension ".pdf"
51 "Default file extension for publishing LaTeX files to PDF."
52 :type 'string
53 :group 'muse-latex)
55 (defcustom muse-latex-header
56 "\\documentclass{article}
58 \\usepackage[english]{babel}
59 \\usepackage[latin1]{inputenc}
60 \\usepackage[T1]{fontenc}
61 \\usepackage{hyperref}
62 \\usepackage[pdftex]{graphicx}
64 \\newcommand{\\comment}[1]{}
66 \\begin{document}
68 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
69 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
70 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
72 \\maketitle
74 <lisp>(and muse-publish-generate-contents
75 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
76 "Header used for publishing LaTeX files. This may be text or a filename."
77 :type 'string
78 :group 'muse-latex)
80 (defcustom muse-latex-footer "\n\\end{document}\n"
81 "Footer used for publishing LaTeX files. This may be text or a filename."
82 :type 'string
83 :group 'muse-latex)
85 (defcustom muse-latexcjk-header
86 "\\documentclass{article}
88 \\usepackage{CJK}
89 \\usepackage{indentfirst}
90 \\usepackage[CJKbookmarks=true]{hyperref}
91 \\usepackage[pdftex]{graphicx}
93 \\begin{document}
94 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
96 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
97 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
98 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
100 \\maketitle
102 <lisp>(and muse-publish-generate-contents
103 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
104 "Header used for publishing LaTeX files (CJK). This may be text or a
105 filename."
106 :type 'string
107 :group 'muse-latex)
109 (defcustom muse-latexcjk-footer
110 "\n\\end{CJK*}
111 \\end{document}\n"
112 "Footer used for publishing LaTeX files (CJK). This may be text or a
113 filename."
114 :type 'string
115 :group 'muse-latex)
117 (defcustom muse-latex-markup-regexps
118 `(;; numeric ranges
119 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
121 ;; be careful of closing quote pairs
122 (10100 "\"'" 0 "\"\\\\-'"))
123 "List of markup regexps for identifying regions in a Muse page.
124 For more on the structure of this list, see `muse-publish-markup-regexps'."
125 :type '(repeat (choice
126 (list :tag "Markup rule"
127 integer
128 (choice regexp symbol)
129 integer
130 (choice string function symbol))
131 function))
132 :group 'muse-latex)
134 (defcustom muse-latex-markup-functions
135 '((table . muse-latex-markup-table))
136 "An alist of style types to custom functions for that kind of text.
137 For more on the structure of this list, see
138 `muse-publish-markup-functions'."
139 :type '(alist :key-type symbol :value-type function)
140 :group 'muse-latex)
142 (defcustom muse-latex-markup-strings
143 '((image-with-desc . "\\begin{figure}[h]\\centering\\includegraphics[width=0.75\\textwidth]{%s}\\caption{%s}\\end{figure}")
144 (image-link . "\\begin{figure}[h]\\centering\\includegraphics[width=0.75\\textwidth]{%s}\\end{figure}")
145 (url-with-image . "%% %s\n\\includegraphics[width=0.75\\textwidth]{%s}")
146 (anchor-ref . "\\ref{%s}")
147 (url . "\\url{%s}")
148 (url-and-desc . "\\href{%s}{%s}\\footnote{%1%}")
149 (link . "\\href{%s}{%s}\\footnote{%1%}")
150 (link-and-anchor . "\\href{%1%}{%3%}\\footnote{%1%}")
151 (email-addr . "\\verb|%s|")
152 (anchor . "\\label{%s}")
153 (emdash . "---")
154 (comment-begin . "\\comment{")
155 (comment-end . "}")
156 (rule . "\\bigskip")
157 (no-break-space . "~")
158 (enddots . "\\ldots{}")
159 (dots . "\\dots{}")
160 (part . "\\part{")
161 (part-end . "}")
162 (chapter . "\\chapter{")
163 (chapter-end . "}")
164 (section . "\\section{")
165 (section-end . "}")
166 (subsection . "\\subsection{")
167 (subsection-end . "}")
168 (subsubsection . "\\subsubsection{")
169 (subsubsection-end . "}")
170 (section-other . "\\paragraph{")
171 (section-other-end . "}")
172 (footnote . "\\footnote{")
173 (footnote-end . "}")
174 (footnotetext . "\\footnotetext[%d]{")
175 (begin-underline . "\\underline{")
176 (end-underline . "}")
177 (begin-literal . "\\texttt{")
178 (end-literal . "}")
179 (begin-emph . "\\emph{")
180 (end-emph . "}")
181 (begin-more-emph . "\\textbf{")
182 (end-more-emph . "}")
183 (begin-most-emph . "\\textbf{\\emph{")
184 (end-most-emph . "}}")
185 (begin-verse . "\\begin{verse}\n")
186 (end-verse-line . " \\\\")
187 (verse-space . "~~~~")
188 (end-verse . "\n\\end{verse}")
189 (begin-example . "\\begin{quote}\n\\begin{verbatim}")
190 (end-example . "\\end{verbatim}\n\\end{quote}")
191 (begin-center . "\\begin{center}\n")
192 (end-center . "\n\\end{center}")
193 (begin-quote . "\\begin{quote}\n")
194 (end-quote . "\n\\end{quote}")
195 (begin-uli . "\\begin{itemize}\n")
196 (end-uli . "\n\\end{itemize}")
197 (begin-uli-item . "\\item ")
198 (begin-oli . "\\begin{enumerate}\n")
199 (end-oli . "\n\\end{enumerate}")
200 (begin-oli-item . "\\item ")
201 (begin-dl . "\\begin{description}\n")
202 (end-dl . "\n\\end{description}")
203 (begin-ddt . "\\item[")
204 (end-ddt . "] "))
205 "Strings used for marking up text.
206 These cover the most basic kinds of markup, the handling of which
207 differs little between the various styles."
208 :type '(alist :key-type symbol :value-type string)
209 :group 'muse-latex)
211 (defcustom muse-latexcjk-encoding-map
212 '((utf-8 . "{UTF8}{song}")
213 (japanese-iso-8bit . "[dnp]{JIS}{min}")
214 (chinese-big5 . "{Bg5}{bsmi}")
215 (mule-utf-8 . "{UTF8}{song}")
216 (chinese-iso-8bit . "{GB}{song}")
217 (chinese-gbk . "{GBK}{song}"))
218 "An alist mapping emacs coding systems to appropriate CJK codings.
219 Use the base name of the coding system (ie, without the -unix)."
220 :type '(alist :key-type coding-system :value-type string)
221 :group 'muse-latex)
223 (defcustom muse-latexcjk-encoding-default "{GB}{song}"
224 "The default Emacs buffer encoding to use in published files.
225 This will be used if no special characters are found."
226 :type 'string
227 :group 'muse-latex)
229 (defun muse-latexcjk-encoding ()
230 (when (boundp 'buffer-file-coding-system)
231 (muse-latexcjk-transform-content-type buffer-file-coding-system)))
233 (defun muse-latexcjk-transform-content-type (content-type)
234 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
235 system to an associated CJK coding system."
236 (let ((match (and (fboundp 'coding-system-base)
237 (assoc (coding-system-base content-type)
238 muse-latexcjk-encoding-map))))
239 (if match
240 (cdr match)
241 muse-latexcjk-encoding-default)))
243 (defcustom muse-latex-markup-specials-document
244 '((?\\ . "\\textbackslash{}")
245 (?\_ . "\\textunderscore{}")
246 (?\< . "\\textless{}")
247 (?\> . "\\textgreater{}")
248 (?^ . "\\^{}")
249 (?\~ . "\\~{}")
250 (?\@ . "\\@")
251 (?\$ . "\\$")
252 (?\% . "\\%")
253 (?\{ . "\\{")
254 (?\} . "\\}")
255 (?\& . "\\&")
256 (?\# . "\\#"))
257 "A table of characters which must be represented specially.
258 These are applied to the entire document, sans already-escaped
259 regions."
260 :type '(alist :key-type character :value-type string)
261 :group 'muse-latex)
263 (defcustom muse-latex-markup-specials-example
265 "A table of characters which must be represented specially.
266 These are applied to <example> regions.
268 With the default interpretation of <example> regions, no specials
269 need to be escaped."
270 :type '(alist :key-type character :value-type string)
271 :group 'muse-latex)
273 (defcustom muse-latex-markup-specials-literal
274 '((?\n . "\\\n")
275 (?_ . "\\textunderscore{}")
276 (?\< . "\\textless{}")
277 (?\> . "\\textgreater{}")
278 (?^ . "\\^{}")
279 (?\~ . "\\~{}")
280 (?\$ . "\\$")
281 (?\% . "\\%")
282 (?\{ . "\\{")
283 (?\} . "\\}")
284 (?\& . "\\&")
285 (?\# . "\\#"))
286 "A table of characters which must be represented specially.
287 This applies to =monospaced text= and <code> regions."
288 :type '(alist :key-type character :value-type string)
289 :group 'muse-latex)
291 (defcustom muse-latex-markup-specials-url
292 '((?\\ . "\\\\")
293 (?\_ . "\\_")
294 (?\< . "\\<")
295 (?\> . "\\>")
296 (?\$ . "\\$")
297 (?\% . "\\%")
298 (?\{ . "\\{")
299 (?\} . "\\}")
300 (?\& . "\\&")
301 (?\# . "\\#"))
302 "A table of characters which must be represented specially.
303 These are applied to URLs."
304 :type '(alist :key-type character :value-type string)
305 :group 'muse-latex)
307 (defun muse-latex-decide-specials (context)
308 "Determine the specials to escape, depending on CONTEXT."
309 (cond ((memq context '(underline emphasis document url-desc verbatim))
310 muse-latex-markup-specials-document)
311 ((memq context '(email url))
312 muse-latex-markup-specials-url)
313 ((eq context 'literal)
314 muse-latex-markup-specials-literal)
315 ((eq context 'example)
316 muse-latex-markup-specials-example)
317 (t (error "Invalid context '%s' in muse-latex" context))))
319 (defun muse-latex-markup-table ()
320 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
321 (match-end 0)))
322 (row-len (car table-info))
323 (field-list (cdr table-info)))
324 (when table-info
325 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l) "}\n")
326 (dolist (fields field-list)
327 (let ((type (car fields)))
328 (setq fields (cdr fields))
329 (when (= type 3)
330 (muse-insert-markup "\\hline\n"))
331 (insert (car fields))
332 (setq fields (cdr fields))
333 (dolist (field fields)
334 (muse-insert-markup " & ")
335 (insert field))
336 (muse-insert-markup " \\\\\n")
337 (when (= type 2)
338 (muse-insert-markup "\\hline\n"))))
339 (muse-insert-markup "\\end{tabular}"))))
341 (defun muse-latex-fixup-dquotes ()
342 "Fixup double quotes."
343 (let ((open t))
344 (while (search-forward "\"" nil t)
345 (unless (get-text-property (match-beginning 0) 'read-only)
346 (when (or (bobp)
347 (eq (char-before) ?\n))
348 (setq open t))
349 (if open
350 (progn
351 (replace-match "``")
352 (setq open nil))
353 (replace-match "''")
354 (setq open t))))))
356 (defun muse-latex-finalize-buffer ()
357 (goto-char (point-min))
358 (muse-latex-fixup-dquotes))
360 (defun muse-latex-pdf-browse-file (file)
361 (shell-command (concat "open " file)))
363 (defun muse-latex-pdf-generate (file output-path final-target)
364 (muse-publish-transform-output
365 file output-path final-target "PDF"
366 (function
367 (lambda (file output-path)
368 (let ((command (format "cd \"%s\"; pdflatex \"%s\""
369 (file-name-directory output-path) file))
370 (times 0)
371 result)
372 ;; XEmacs can sometimes return a non-number result. We'll err
373 ;; on the side of caution by continuing to attempt to generate
374 ;; the PDF if this happens and treat the final result as
375 ;; successful.
376 (while (and (< times 2)
377 (or (not (numberp result))
378 (not (eq result 0))
379 ;; table of contents takes 2 passes
380 (file-readable-p
381 (muse-replace-regexp-in-string
382 "\\.tex\\'" ".toc" file t t))))
383 (setq result (shell-command command)
384 times (1+ times)))
385 (if (or (not (numberp result))
386 (eq result 0))
388 nil))))
389 ".aux" ".toc" ".out" ".log"))
391 (unless (assoc "latex" muse-publishing-styles)
392 (muse-define-style "latex"
393 :suffix 'muse-latex-extension
394 :regexps 'muse-latex-markup-regexps
395 :functions 'muse-latex-markup-functions
396 :strings 'muse-latex-markup-strings
397 :specials 'muse-latex-decide-specials
398 :after 'muse-latex-finalize-buffer
399 :header 'muse-latex-header
400 :footer 'muse-latex-footer
401 :browser 'find-file)
403 (muse-derive-style "pdf" "latex"
404 :final 'muse-latex-pdf-generate
405 :browser 'muse-latex-pdf-browse-file
406 :link-suffix 'muse-latex-pdf-extension
407 :osuffix 'muse-latex-pdf-extension)
409 (muse-derive-style "latexcjk" "latex"
410 :header 'muse-latexcjk-header
411 :footer 'muse-latexcjk-footer)
413 (muse-derive-style "pdfcjk" "latexcjk"
414 :final 'muse-latex-pdf-generate
415 :browser 'muse-latex-pdf-browse-file
416 :link-suffix 'muse-latex-pdf-extension
417 :osuffix 'muse-latex-pdf-extension))
419 (provide 'muse-latex)
421 ;;; muse-latex.el ends here