Implement <src> tag
[muse-el.git] / lisp / muse-latex.el
blobef87b18b293dda144a912661a115d66a4831b92c
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 ;; Matthias Kegelmann (mathias DOT kegelmann AT sdm DOT de) provided a
32 ;; scenario where we would need to respect the <contents> tag.
34 ;;; Code:
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 ;; Muse LaTeX Publishing
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42 (require 'muse-publish)
44 (defgroup muse-latex nil
45 "Rules for marking up a Muse file as a LaTeX article."
46 :group 'muse-publish)
48 (defcustom muse-latex-extension ".tex"
49 "Default file extension for publishing LaTeX files."
50 :type 'string
51 :group 'muse-latex)
53 (defcustom muse-latex-pdf-extension ".pdf"
54 "Default file extension for publishing LaTeX files to PDF."
55 :type 'string
56 :group 'muse-latex)
58 (defcustom muse-latex-header
59 "\\documentclass{article}
61 \\usepackage[english]{babel}
62 \\usepackage[latin1]{inputenc}
63 \\usepackage[T1]{fontenc}
64 \\usepackage{hyperref}
65 \\usepackage[pdftex]{graphicx}
67 \\begin{document}
69 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
70 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
71 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
73 \\maketitle
75 <lisp>(and muse-publish-generate-contents
76 (not muse-latex-permit-contents-tag)
77 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
78 "Header used for publishing LaTeX files. This may be text or a filename."
79 :type 'string
80 :group 'muse-latex)
82 (defcustom muse-latex-footer "\n\\end{document}\n"
83 "Footer used for publishing LaTeX files. This may be text or a filename."
84 :type 'string
85 :group 'muse-latex)
87 (defcustom muse-latexcjk-header
88 "\\documentclass{article}
90 \\usepackage{CJK}
91 \\usepackage{indentfirst}
92 \\usepackage[CJKbookmarks=true]{hyperref}
93 \\usepackage[pdftex]{graphicx}
95 \\begin{document}
96 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
98 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
99 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
100 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
102 \\maketitle
104 <lisp>(and muse-publish-generate-contents
105 (not muse-latex-permit-contents-tag)
106 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
107 "Header used for publishing LaTeX files (CJK). This may be text or a
108 filename."
109 :type 'string
110 :group 'muse-latex)
112 (defcustom muse-latexcjk-footer
113 "\n\\end{CJK*}
114 \\end{document}\n"
115 "Footer used for publishing LaTeX files (CJK). This may be text or a
116 filename."
117 :type 'string
118 :group 'muse-latex)
120 (defcustom muse-latex-markup-regexps
121 `(;; numeric ranges
122 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
124 ;; be careful of closing quote pairs
125 (10100 "\"'" 0 "\"\\\\-'"))
126 "List of markup regexps for identifying regions in a Muse page.
127 For more on the structure of this list, see `muse-publish-markup-regexps'."
128 :type '(repeat (choice
129 (list :tag "Markup rule"
130 integer
131 (choice regexp symbol)
132 integer
133 (choice string function symbol))
134 function))
135 :group 'muse-latex)
137 (defcustom muse-latex-markup-functions
138 '((table . muse-latex-markup-table))
139 "An alist of style types to custom functions for that kind of text.
140 For more on the structure of this list, see
141 `muse-publish-markup-functions'."
142 :type '(alist :key-type symbol :value-type function)
143 :group 'muse-latex)
145 (defcustom muse-latex-markup-strings
146 '((image-with-desc . "\\begin{figure}[h]
147 \\centering\\includegraphics[width=0.75\\textwidth]{%s.%s}
148 \\caption{%s}
149 \\end{figure}")
150 (image . "\\begin{figure}[h]
151 \\centering\\includegraphics[width=0.75\\textwidth]{%s.%s}
152 \\end{figure}")
153 (image-link . "%% %s
154 \\includegraphics[width=0.75\\textwidth]{%s.%s}")
155 (anchor-ref . "\\ref{%s}")
156 (url . "\\url{%s}")
157 (url-and-desc . "\\href{%s}{%s}\\footnote{%1%}")
158 (link . "\\href{%s}{%s}\\footnote{%1%}")
159 (link-and-anchor . "\\href{%1%}{%3%}\\footnote{%1%}")
160 (email-addr . "\\verb|%s|")
161 (anchor . "\\label{%s}")
162 (emdash . "---")
163 (comment-begin . "% ")
164 (rule . "\\bigskip")
165 (no-break-space . "~")
166 (enddots . "\\ldots{}")
167 (dots . "\\dots{}")
168 (part . "\\part{")
169 (part-end . "}")
170 (chapter . "\\chapter{")
171 (chapter-end . "}")
172 (section . "\\section{")
173 (section-end . "}")
174 (subsection . "\\subsection{")
175 (subsection-end . "}")
176 (subsubsection . "\\subsubsection{")
177 (subsubsection-end . "}")
178 (section-other . "\\paragraph{")
179 (section-other-end . "}")
180 (footnote . "\\footnote{")
181 (footnote-end . "}")
182 (footnotetext . "\\footnotetext[%d]{")
183 (begin-underline . "\\underline{")
184 (end-underline . "}")
185 (begin-literal . "\\texttt{")
186 (end-literal . "}")
187 (begin-emph . "\\emph{")
188 (end-emph . "}")
189 (begin-more-emph . "\\textbf{")
190 (end-more-emph . "}")
191 (begin-most-emph . "\\textbf{\\emph{")
192 (end-most-emph . "}}")
193 (begin-verse . "\\begin{verse}\n")
194 (end-verse-line . " \\\\")
195 (verse-space . "~~~~")
196 (end-verse . "\n\\end{verse}")
197 (begin-example . "\\begin{quote}\n\\begin{verbatim}")
198 (end-example . "\\end{verbatim}\n\\end{quote}")
199 (begin-center . "\\begin{center}\n")
200 (end-center . "\n\\end{center}")
201 (begin-quote . "\\begin{quote}\n")
202 (end-quote . "\n\\end{quote}")
203 (begin-uli . "\\begin{itemize}\n")
204 (end-uli . "\n\\end{itemize}")
205 (begin-uli-item . "\\item ")
206 (begin-oli . "\\begin{enumerate}\n")
207 (end-oli . "\n\\end{enumerate}")
208 (begin-oli-item . "\\item ")
209 (begin-dl . "\\begin{description}\n")
210 (end-dl . "\n\\end{description}")
211 (begin-ddt . "\\item[")
212 (end-ddt . "] \\mbox{}\n"))
213 "Strings used for marking up text.
214 These cover the most basic kinds of markup, the handling of which
215 differs little between the various styles."
216 :type '(alist :key-type symbol :value-type string)
217 :group 'muse-latex)
219 (defcustom muse-latexcjk-encoding-map
220 '((utf-8 . "{UTF8}{song}")
221 (japanese-iso-8bit . "[dnp]{JIS}{min}")
222 (chinese-big5 . "{Bg5}{bsmi}")
223 (mule-utf-8 . "{UTF8}{song}")
224 (chinese-iso-8bit . "{GB}{song}")
225 (chinese-gbk . "{GBK}{song}"))
226 "An alist mapping emacs coding systems to appropriate CJK codings.
227 Use the base name of the coding system (ie, without the -unix)."
228 :type '(alist :key-type coding-system :value-type string)
229 :group 'muse-latex)
231 (defcustom muse-latexcjk-encoding-default "{GB}{song}"
232 "The default Emacs buffer encoding to use in published files.
233 This will be used if no special characters are found."
234 :type 'string
235 :group 'muse-latex)
237 (defun muse-latexcjk-encoding ()
238 (when (boundp 'buffer-file-coding-system)
239 (muse-latexcjk-transform-content-type buffer-file-coding-system)))
241 (defun muse-latexcjk-transform-content-type (content-type)
242 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
243 system to an associated CJK coding system."
244 (let ((match (and (fboundp 'coding-system-base)
245 (assoc (coding-system-base content-type)
246 muse-latexcjk-encoding-map))))
247 (if match
248 (cdr match)
249 muse-latexcjk-encoding-default)))
251 (defcustom muse-latex-markup-specials-document
252 '((?\\ . "\\textbackslash{}")
253 (?\_ . "\\textunderscore{}")
254 (?\< . "\\textless{}")
255 (?\> . "\\textgreater{}")
256 (?^ . "\\^{}")
257 (?\~ . "\\~{}")
258 (?\@ . "\\@")
259 (?\$ . "\\$")
260 (?\% . "\\%")
261 (?\{ . "\\{")
262 (?\} . "\\}")
263 (?\& . "\\&")
264 (?\# . "\\#"))
265 "A table of characters which must be represented specially.
266 These are applied to the entire document, sans already-escaped
267 regions."
268 :type '(alist :key-type character :value-type string)
269 :group 'muse-latex)
271 (defcustom muse-latex-markup-specials-example
273 "A table of characters which must be represented specially.
274 These are applied to <example> regions.
276 With the default interpretation of <example> regions, no specials
277 need to be escaped."
278 :type '(alist :key-type character :value-type string)
279 :group 'muse-latex)
281 (defcustom muse-latex-markup-specials-literal
282 '((?\n . "\\\n")
283 (?_ . "\\textunderscore{}")
284 (?\< . "\\textless{}")
285 (?\> . "\\textgreater{}")
286 (?^ . "\\^{}")
287 (?\~ . "\\~{}")
288 (?\$ . "\\$")
289 (?\% . "\\%")
290 (?\{ . "\\{")
291 (?\} . "\\}")
292 (?\& . "\\&")
293 (?\# . "\\#"))
294 "A table of characters which must be represented specially.
295 This applies to =monospaced text= and <code> regions."
296 :type '(alist :key-type character :value-type string)
297 :group 'muse-latex)
299 (defcustom muse-latex-markup-specials-url
300 '((?\\ . "\\textbackslash{}")
301 (?\_ . "\\_")
302 (?\< . "\\<")
303 (?\> . "\\>")
304 (?\$ . "\\$")
305 (?\% . "\\%")
306 (?\{ . "\\{")
307 (?\} . "\\}")
308 (?\& . "\\&")
309 (?\# . "\\#"))
310 "A table of characters which must be represented specially.
311 These are applied to URLs."
312 :type '(alist :key-type character :value-type string)
313 :group 'muse-latex)
315 (defcustom muse-latex-markup-specials-image
316 '((?\\ . "\\textbackslash{}") ; cannot find suitable replacement
317 (?\< . "\\<")
318 (?\> . "\\>")
319 (?\$ . "\\$")
320 (?\% . "\\%")
321 (?\{ . "\\{")
322 (?\} . "\\}")
323 (?\& . "\\&")
324 (?\# . "\\#") ; cannot find suitable replacement
326 "A table of characters which must be represented specially.
327 These are applied to image filenames."
328 :type '(alist :key-type character :value-type string)
329 :group 'muse-latex)
331 (defun muse-latex-decide-specials (context)
332 "Determine the specials to escape, depending on CONTEXT."
333 (cond ((memq context '(underline emphasis document url-desc verbatim))
334 muse-latex-markup-specials-document)
335 ((eq context 'image)
336 muse-latex-markup-specials-image)
337 ((memq context '(email url))
338 muse-latex-markup-specials-url)
339 ((eq context 'literal)
340 muse-latex-markup-specials-literal)
341 ((eq context 'example)
342 muse-latex-markup-specials-example)
343 (t (error "Invalid context '%s' in muse-latex" context))))
345 (defun muse-latex-markup-table ()
346 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
347 (match-end 0)))
348 (row-len (car table-info))
349 (field-list (cdr table-info)))
350 (when table-info
351 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l) "}\n")
352 (dolist (fields field-list)
353 (let ((type (car fields)))
354 (setq fields (cdr fields))
355 (when (= type 3)
356 (muse-insert-markup "\\hline\n"))
357 (insert (car fields))
358 (setq fields (cdr fields))
359 (dolist (field fields)
360 (muse-insert-markup " & ")
361 (insert field))
362 (muse-insert-markup " \\\\\n")
363 (when (= type 2)
364 (muse-insert-markup "\\hline\n"))))
365 (muse-insert-markup "\\end{tabular}"))))
367 (defun muse-latex-fixup-dquotes ()
368 "Fixup double quotes."
369 (goto-char (point-min))
370 (let ((open t))
371 (while (search-forward "\"" nil t)
372 (unless (get-text-property (match-beginning 0) 'read-only)
373 (when (or (bobp)
374 (eq (char-before) ?\n))
375 (setq open t))
376 (if open
377 (progn
378 (replace-match "``")
379 (setq open nil))
380 (replace-match "''")
381 (setq open t))))))
383 (defcustom muse-latex-permit-contents-tag nil
384 "If nil, ignore <contents> tags. Otherwise, insert table of contents.
386 Most of the time, it is best to have a table of contents on the
387 first page, with a new page immediately following. To make this
388 work with documents published in both HTML and LaTeX, we need to
389 ignore the <contents> tag.
391 If you don't agree with this, then set this option to non-nil,
392 and it will do what you expect."
393 :type 'boolean
394 :group 'muse-latex)
396 (defun muse-latex-munge-buffer ()
397 (muse-latex-fixup-dquotes)
398 (when (and muse-latex-permit-contents-tag
399 muse-publish-generate-contents)
400 (goto-char (car muse-publish-generate-contents))
401 (muse-insert-markup "\\tableofcontents")))
403 (defun muse-latex-pdf-browse-file (file)
404 (shell-command (concat "open " file)))
406 (defun muse-latex-pdf-generate (file output-path final-target)
407 (muse-publish-transform-output
408 file output-path final-target "PDF"
409 (function
410 (lambda (file output-path)
411 (let ((command (format "cd \"%s\"; pdflatex \"%s\""
412 (file-name-directory output-path) file))
413 (times 0)
414 result)
415 ;; XEmacs can sometimes return a non-number result. We'll err
416 ;; on the side of caution by continuing to attempt to generate
417 ;; the PDF if this happens and treat the final result as
418 ;; successful.
419 (while (and (< times 2)
420 (or (not (numberp result))
421 (not (eq result 0))
422 ;; table of contents takes 2 passes
423 (file-readable-p
424 (muse-replace-regexp-in-string
425 "\\.tex\\'" ".toc" file t t))))
426 (setq result (shell-command command)
427 times (1+ times)))
428 (if (or (not (numberp result))
429 (eq result 0))
431 nil))))
432 ".aux" ".toc" ".out" ".log"))
434 ;;; Register the Muse LATEX Publishers
436 (muse-define-style "latex"
437 :suffix 'muse-latex-extension
438 :regexps 'muse-latex-markup-regexps
439 :functions 'muse-latex-markup-functions
440 :strings 'muse-latex-markup-strings
441 :specials 'muse-latex-decide-specials
442 :before-end 'muse-latex-munge-buffer
443 :header 'muse-latex-header
444 :footer 'muse-latex-footer
445 :browser 'find-file)
447 (muse-derive-style "pdf" "latex"
448 :final 'muse-latex-pdf-generate
449 :browser 'muse-latex-pdf-browse-file
450 :link-suffix 'muse-latex-pdf-extension
451 :osuffix 'muse-latex-pdf-extension)
453 (muse-derive-style "latexcjk" "latex"
454 :header 'muse-latexcjk-header
455 :footer 'muse-latexcjk-footer)
457 (muse-derive-style "pdfcjk" "latexcjk"
458 :final 'muse-latex-pdf-generate
459 :browser 'muse-latex-pdf-browse-file
460 :link-suffix 'muse-latex-pdf-extension
461 :osuffix 'muse-latex-pdf-extension)
463 (provide 'muse-latex)
465 ;;; muse-latex.el ends here