Permit tag attributes to span more than one line.
[muse-el.git] / lisp / muse-latex.el
blob8500510a1710f4935bf40cd8c449c1ee78dbeca6
1 ;;; muse-latex.el --- publish entries in LaTex or PDF format
3 ;; Copyright (C) 2004, 2005, 2006, 2007 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 3, 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 ;; Jean Magnan de Bornier (jean AT bornier DOT net) provided the
35 ;; markup string for link-and-anchor.
37 ;; Jim Ottaway (j DOT ottaway AT lse DOT ac DOT uk) implemented slides
38 ;; and lecture notes.
40 ;; Karl Berry (karl AT freefriends DOT org) suggested how to escape
41 ;; additional special characters in image filenames.
43 ;;; Code:
45 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
47 ;; Muse LaTeX Publishing
49 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
51 (require 'muse-publish)
53 (defgroup muse-latex nil
54 "Rules for marking up a Muse file as a LaTeX article."
55 :group 'muse-publish)
57 (defcustom muse-latex-extension ".tex"
58 "Default file extension for publishing LaTeX files."
59 :type 'string
60 :group 'muse-latex)
62 (defcustom muse-latex-pdf-extension ".pdf"
63 "Default file extension for publishing LaTeX files to PDF."
64 :type 'string
65 :group 'muse-latex)
67 (defcustom muse-latex-pdf-program "pdflatex"
68 "The program that is called to generate PDF content from LaTeX content."
69 :type 'string
70 :group 'muse-latex)
72 (defcustom muse-latex-pdf-cruft '(".aux" ".toc" ".out" ".log")
73 "Extensions of files to remove after generating PDF output successfully."
74 :type 'string
75 :group 'muse-latex)
77 (defcustom muse-latex-header
78 "\\documentclass{article}
80 \\usepackage[english]{babel}
81 \\usepackage[latin1]{inputenc}
82 \\usepackage[T1]{fontenc}
83 \\usepackage{hyperref}
84 \\usepackage[pdftex]{graphicx}
86 \\def\\museincludegraphics{%
87 \\begingroup
88 \\catcode`\\|=0
89 \\catcode`\\\\=12
90 \\catcode`\\#=12
91 \\includegraphics[width=0.75\\textwidth]
94 \\begin{document}
96 \\title{<lisp>(muse-publish-escape-specials-in-string
97 (muse-publishing-directive \"title\") 'document)</lisp>}
98 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
99 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
101 \\maketitle
103 <lisp>(and muse-publish-generate-contents
104 (not muse-latex-permit-contents-tag)
105 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
106 "Header used for publishing LaTeX files. This may be text or a filename."
107 :type 'string
108 :group 'muse-latex)
110 (defcustom muse-latex-footer "<lisp>(muse-latex-bibliography)</lisp>
111 \\end{document}\n"
112 "Footer used for publishing LaTeX files. This may be text or a filename."
113 :type 'string
114 :group 'muse-latex)
116 (defcustom muse-latexcjk-header
117 "\\documentclass{article}
119 \\usepackage{CJK}
120 \\usepackage{indentfirst}
121 \\usepackage[CJKbookmarks=true]{hyperref}
122 \\usepackage[pdftex]{graphicx}
124 \\begin{document}
125 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
127 \\title{<lisp>(muse-publish-escape-specials-in-string
128 (muse-publishing-directive \"title\") 'document)</lisp>}
129 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
130 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
132 \\maketitle
134 <lisp>(and muse-publish-generate-contents
135 (not muse-latex-permit-contents-tag)
136 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
137 "Header used for publishing LaTeX files (CJK). This may be text or a
138 filename."
139 :type 'string
140 :group 'muse-latex)
142 (defcustom muse-latexcjk-footer
143 "\n\\end{CJK*}
144 \\end{document}\n"
145 "Footer used for publishing LaTeX files (CJK). This may be text or a
146 filename."
147 :type 'string
148 :group 'muse-latex)
150 (defcustom muse-latex-slides-header
151 "\\documentclass[ignorenonframetext]{beamer}
153 \\usepackage[english]{babel}
154 \\usepackage[latin1]{inputenc}
155 \\usepackage[T1]{fontenc}
156 \\usepackage{hyperref}
158 \\begin{document}
160 \\title{<lisp>(muse-publish-escape-specials-in-string
161 (muse-publishing-directive \"title\") 'document)</lisp>}
162 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
163 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
165 \\maketitle
167 <lisp>(and muse-publish-generate-contents
168 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
169 "Header for publishing of slides using LaTeX.
170 This may be text or a filename.
172 You must have the Beamer extension for LaTeX installed for this to work."
173 :type 'string
174 :group 'muse-latex)
176 (defcustom muse-latex-lecture-notes-header
177 "\\documentclass{article}
178 \\usepackage{beamerarticle}
180 \\usepackage[english]{babel}
181 \\usepackage[latin1]{inputenc}
182 \\usepackage[T1]{fontenc}
183 \\usepackage{hyperref}
185 \\begin{document}
187 \\title{<lisp>(muse-publish-escape-specials-in-string
188 (muse-publishing-directive \"title\") 'document)</lisp>}
189 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
190 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
192 \\maketitle
194 <lisp>(and muse-publish-generate-contents
195 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
196 "Header for publishing of lecture notes using LaTeX.
197 This may be text or a filename.
199 You must have the Beamer extension for LaTeX installed for this to work."
200 :type 'string
201 :group 'muse-latex)
203 (defcustom muse-latex-markup-regexps
204 `(;; numeric ranges
205 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
207 ;; be careful of closing quote pairs
208 (10100 "\"'" 0 "\"\\\\-'"))
209 "List of markup regexps for identifying regions in a Muse page.
210 For more on the structure of this list, see `muse-publish-markup-regexps'."
211 :type '(repeat (choice
212 (list :tag "Markup rule"
213 integer
214 (choice regexp symbol)
215 integer
216 (choice string function symbol))
217 function))
218 :group 'muse-latex)
220 (defcustom muse-latex-markup-functions
221 '((table . muse-latex-markup-table))
222 "An alist of style types to custom functions for that kind of text.
223 For more on the structure of this list, see
224 `muse-publish-markup-functions'."
225 :type '(alist :key-type symbol :value-type function)
226 :group 'muse-latex)
228 (defcustom muse-latex-markup-strings
229 '((image-with-desc . "\\begin{figure}[h]
230 \\centering\\museincludegraphics{%s.%s}|endgroup
231 \\caption{%s}
232 \\end{figure}")
233 (image . "\\begin{figure}[h]
234 \\centering\\museincludegraphics{%s.%s}|endgroup
235 \\end{figure}")
236 (image-link . "%% %s
237 \\museincludegraphics{%s.%s}|endgroup")
238 (anchor-ref . "\\ref{%s}")
239 (url . "\\url{%s}")
240 (url-and-desc . "\\href{%s}{%s}\\footnote{%1%}")
241 (link . "\\href{%s}{%s}\\footnote{%1%}")
242 (link-and-anchor . "\\href{%1%}{%3%}\\footnote{%1%}")
243 (email-addr . "\\verb|%s|")
244 (anchor . "\\label{%s}")
245 (emdash . "---")
246 (comment-begin . "% ")
247 (rule . "\\vspace{.5cm}\\hrule\\vspace{.5cm}")
248 (no-break-space . "~")
249 (enddots . "\\ldots{}")
250 (dots . "\\dots{}")
251 (part . "\\part{")
252 (part-end . "}")
253 (chapter . "\\chapter{")
254 (chapter-end . "}")
255 (section . "\\section{")
256 (section-end . "}")
257 (subsection . "\\subsection{")
258 (subsection-end . "}")
259 (subsubsection . "\\subsubsection{")
260 (subsubsection-end . "}")
261 (section-other . "\\paragraph{")
262 (section-other-end . "}")
263 (footnote . "\\footnote{")
264 (footnote-end . "}")
265 (footnotetext . "\\footnotetext[%d]{")
266 (begin-underline . "\\underline{")
267 (end-underline . "}")
268 (begin-literal . "\\texttt{")
269 (end-literal . "}")
270 (begin-emph . "\\emph{")
271 (end-emph . "}")
272 (begin-more-emph . "\\textbf{")
273 (end-more-emph . "}")
274 (begin-most-emph . "\\textbf{\\emph{")
275 (end-most-emph . "}}")
276 (begin-verse . "\\begin{verse}\n")
277 (end-verse-line . " \\\\")
278 (verse-space . "~~~~")
279 (end-verse . "\n\\end{verse}")
280 (begin-example . "\\begin{quote}\n\\begin{verbatim}")
281 (end-example . "\\end{verbatim}\n\\end{quote}")
282 (begin-center . "\\begin{center}\n")
283 (end-center . "\n\\end{center}")
284 (begin-quote . "\\begin{quote}\n")
285 (end-quote . "\n\\end{quote}")
286 (begin-cite . "\\cite{")
287 (begin-cite-author . "\\citet{")
288 (begin-cite-year . "\\citet{")
289 (end-cite . "}")
290 (begin-uli . "\\begin{itemize}\n")
291 (end-uli . "\n\\end{itemize}")
292 (begin-uli-item . "\\item ")
293 (begin-oli . "\\begin{enumerate}\n")
294 (end-oli . "\n\\end{enumerate}")
295 (begin-oli-item . "\\item ")
296 (begin-dl . "\\begin{description}\n")
297 (end-dl . "\n\\end{description}")
298 (begin-ddt . "\\item[")
299 (end-ddt . "] \\mbox{}\n"))
300 "Strings used for marking up text.
301 These cover the most basic kinds of markup, the handling of which
302 differs little between the various styles."
303 :type '(alist :key-type symbol :value-type string)
304 :group 'muse-latex)
306 (defcustom muse-latex-slides-markup-tags
307 '(("slide" t t nil muse-latex-slide-tag))
308 "A list of tag specifications, for specially marking up LaTeX slides."
309 :type '(repeat (list (string :tag "Markup tag")
310 (boolean :tag "Expect closing tag" :value t)
311 (boolean :tag "Parse attributes" :value nil)
312 (boolean :tag "Nestable" :value nil)
313 function))
314 :group 'muse-latex)
316 (defcustom muse-latexcjk-encoding-map
317 '((utf-8 . "{UTF8}{song}")
318 (japanese-iso-8bit . "[dnp]{JIS}{min}")
319 (chinese-big5 . "{Bg5}{bsmi}")
320 (mule-utf-8 . "{UTF8}{song}")
321 (chinese-iso-8bit . "{GB}{song}")
322 (chinese-gbk . "{GBK}{song}"))
323 "An alist mapping emacs coding systems to appropriate CJK codings.
324 Use the base name of the coding system (ie, without the -unix)."
325 :type '(alist :key-type coding-system :value-type string)
326 :group 'muse-latex)
328 (defcustom muse-latexcjk-encoding-default "{GB}{song}"
329 "The default Emacs buffer encoding to use in published files.
330 This will be used if no special characters are found."
331 :type 'string
332 :group 'muse-latex)
334 (defun muse-latexcjk-encoding ()
335 (when (boundp 'buffer-file-coding-system)
336 (muse-latexcjk-transform-content-type buffer-file-coding-system)))
338 (defun muse-latexcjk-transform-content-type (content-type)
339 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
340 system to an associated CJK coding system."
341 (let ((match (and (fboundp 'coding-system-base)
342 (assoc (coding-system-base content-type)
343 muse-latexcjk-encoding-map))))
344 (if match
345 (cdr match)
346 muse-latexcjk-encoding-default)))
348 (defcustom muse-latex-markup-specials-document
349 '((?\\ . "\\textbackslash{}")
350 (?\_ . "\\textunderscore{}")
351 (?\< . "\\textless{}")
352 (?\> . "\\textgreater{}")
353 (?^ . "\\^{}")
354 (?\~ . "\\~{}")
355 (?\@ . "\\@")
356 (?\$ . "\\$")
357 (?\% . "\\%")
358 (?\{ . "\\{")
359 (?\} . "\\}")
360 (?\& . "\\&")
361 (?\# . "\\#"))
362 "A table of characters which must be represented specially.
363 These are applied to the entire document, sans already-escaped
364 regions."
365 :type '(alist :key-type character :value-type string)
366 :group 'muse-latex)
368 (defcustom muse-latex-markup-specials-example
370 "A table of characters which must be represented specially.
371 These are applied to <example> regions.
373 With the default interpretation of <example> regions, no specials
374 need to be escaped."
375 :type '(alist :key-type character :value-type string)
376 :group 'muse-latex)
378 (defcustom muse-latex-markup-specials-literal
379 '((?\n . "\\\n")
380 (?\\ . "\\textbackslash{}")
381 (?_ . "\\textunderscore{}")
382 (?\< . "\\textless{}")
383 (?\> . "\\textgreater{}")
384 (?^ . "\\^{}")
385 (?\~ . "\\~{}")
386 (?\$ . "\\$")
387 (?\% . "\\%")
388 (?\{ . "\\{")
389 (?\} . "\\}")
390 (?\& . "\\&")
391 (?\# . "\\#"))
392 "A table of characters which must be represented specially.
393 This applies to =monospaced text= and <code> regions."
394 :type '(alist :key-type character :value-type string)
395 :group 'muse-latex)
397 (defcustom muse-latex-markup-specials-url
398 '((?\\ . "\\textbackslash{}")
399 (?\_ . "\\_")
400 (?\< . "\\<")
401 (?\> . "\\>")
402 (?\$ . "\\$")
403 (?\% . "\\%")
404 (?\{ . "\\{")
405 (?\} . "\\}")
406 (?\& . "\\&")
407 (?\# . "\\#"))
408 "A table of characters which must be represented specially.
409 These are applied to URLs."
410 :type '(alist :key-type character :value-type string)
411 :group 'muse-latex)
413 (defcustom muse-latex-markup-specials-image
414 '((?\\ . "\\\\")
415 (?\< . "\\<")
416 (?\> . "\\>")
417 (?\$ . "\\$")
418 (?\% . "\\%")
419 (?\{ . "\\{")
420 (?\} . "\\}")
421 (?\& . "\\&")
422 (?\# . "\\#")
423 (?\| . "\\|"))
424 "A table of characters which must be represented specially.
425 These are applied to image filenames."
426 :type '(alist :key-type character :value-type string)
427 :group 'muse-latex)
429 (defun muse-latex-decide-specials (context)
430 "Determine the specials to escape, depending on CONTEXT."
431 (cond ((memq context '(underline emphasis document url-desc verbatim
432 footnote))
433 muse-latex-markup-specials-document)
434 ((eq context 'image)
435 muse-latex-markup-specials-image)
436 ((memq context '(email url))
437 muse-latex-markup-specials-url)
438 ((eq context 'literal)
439 muse-latex-markup-specials-literal)
440 ((eq context 'example)
441 muse-latex-markup-specials-example)
442 (t (error "Invalid context '%s' in muse-latex" context))))
444 (defcustom muse-latex-permit-contents-tag nil
445 "If nil, ignore <contents> tags. Otherwise, insert table of contents.
447 Most of the time, it is best to have a table of contents on the
448 first page, with a new page immediately following. To make this
449 work with documents published in both HTML and LaTeX, we need to
450 ignore the <contents> tag.
452 If you don't agree with this, then set this option to non-nil,
453 and it will do what you expect."
454 :type 'boolean
455 :group 'muse-latex)
457 (defun muse-latex-markup-table ()
458 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
459 (match-end 0)))
460 (row-len (car table-info))
461 (field-list (cdr table-info)))
462 (when table-info
463 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l) "}\n")
464 (dolist (fields field-list)
465 (let ((type (car fields)))
466 (setq fields (cdr fields))
467 (if (eq type 'hline)
468 (muse-insert-markup "\\hline\n")
469 (when (= type 3)
470 (muse-insert-markup "\\hline\n"))
471 (insert (car fields))
472 (setq fields (cdr fields))
473 (dolist (field fields)
474 (muse-insert-markup " & ")
475 (insert field))
476 (muse-insert-markup " \\\\\n")
477 (when (= type 2)
478 (muse-insert-markup "\\hline\n")))))
479 (muse-insert-markup "\\end{tabular}"))))
481 ;;; Tags for LaTeX
483 (defun muse-latex-slide-tag (beg end attrs)
484 "Publish the <slide> tag in LaTeX.
485 This is used by the slides and lecture-notes publishing styles."
486 (let ((title (cdr (assoc "title" attrs))))
487 (goto-char beg)
488 (muse-insert-markup "\\begin{frame}\n")
489 (when title
490 (muse-insert-markup "\\frametitle{")
491 (insert title)
492 (muse-insert-markup "}\n"))
493 (goto-char end)
494 (muse-insert-markup "\n\\end{frame}")))
496 ;;; Post-publishing functions
498 (defun muse-latex-fixup-dquotes ()
499 "Fixup double quotes."
500 (goto-char (point-min))
501 (let ((open t))
502 (while (search-forward "\"" nil t)
503 (unless (get-text-property (match-beginning 0) 'read-only)
504 (when (or (bobp)
505 (eq (char-before) ?\n))
506 (setq open t))
507 (if open
508 (progn
509 (replace-match "``")
510 (setq open nil))
511 (replace-match "''")
512 (setq open t))))))
514 (defun muse-latex-fixup-citations ()
515 "Replace semicolons in multi-head citations with colons."
516 (goto-char (point-min))
517 (while (re-search-forward "\\\\cite.?{" nil t)
518 (let ((start (point))
519 (end (re-search-forward "}")))
520 (save-restriction
521 (narrow-to-region start end)
522 (goto-char (point-min))
523 (while (re-search-forward ";" nil t)
524 (replace-match ","))))))
526 (defun muse-latex-fixup-headings ()
527 "Remove footnotes in headings, since LaTeX does not permit them to exist.
529 This can happen if there is a link in a heading, because by
530 default Muse will add a footnote for each link."
531 (goto-char (point-min))
532 (while (re-search-forward "^\\\\section.?{" nil t)
533 (save-restriction
534 (narrow-to-region (match-beginning 0) (muse-line-end-position))
535 (goto-char (point-min))
536 (while (re-search-forward "\\\\footnote{[^}\n]+}" nil t)
537 (replace-match ""))
538 (forward-line 1))))
540 (defun muse-latex-munge-buffer ()
541 (muse-latex-fixup-dquotes)
542 (muse-latex-fixup-citations)
543 (muse-latex-fixup-headings)
544 (when (and muse-latex-permit-contents-tag
545 muse-publish-generate-contents)
546 (goto-char (car muse-publish-generate-contents))
547 (muse-insert-markup "\\tableofcontents")))
549 (defun muse-latex-bibliography ()
550 (save-excursion
551 (goto-char (point-min))
552 (if (re-search-forward "\\\\cite.?{" nil t)
553 (concat
554 "\\bibliography{"
555 (muse-publishing-directive "bibsource")
556 "}\n")
557 "")))
559 (defun muse-latex-pdf-browse-file (file)
560 (shell-command (concat "open " file)))
562 (defun muse-latex-pdf-generate (file output-path final-target)
563 (apply
564 #'muse-publish-transform-output
565 file output-path final-target "PDF"
566 (function
567 (lambda (file output-path)
568 (let* ((fnd (file-name-directory output-path))
569 (command (format "%s \"%s\""
570 muse-latex-pdf-program
571 (file-relative-name file fnd)))
572 (times 0)
573 (default-directory fnd)
574 result)
575 ;; XEmacs can sometimes return a non-number result. We'll err
576 ;; on the side of caution by continuing to attempt to generate
577 ;; the PDF if this happens and treat the final result as
578 ;; successful.
579 (while (and (< times 2)
580 (or (not (numberp result))
581 (not (eq result 0))
582 ;; table of contents takes 2 passes
583 (file-readable-p
584 (muse-replace-regexp-in-string
585 "\\.tex\\'" ".toc" file t t))))
586 (setq result (shell-command command)
587 times (1+ times)))
588 (if (or (not (numberp result))
589 (eq result 0))
591 nil))))
592 muse-latex-pdf-cruft))
594 ;;; Register the Muse LATEX Publishers
596 (muse-define-style "latex"
597 :suffix 'muse-latex-extension
598 :regexps 'muse-latex-markup-regexps
599 :functions 'muse-latex-markup-functions
600 :strings 'muse-latex-markup-strings
601 :specials 'muse-latex-decide-specials
602 :before-end 'muse-latex-munge-buffer
603 :header 'muse-latex-header
604 :footer 'muse-latex-footer
605 :browser 'find-file)
607 (muse-derive-style "pdf" "latex"
608 :final 'muse-latex-pdf-generate
609 :browser 'muse-latex-pdf-browse-file
610 :link-suffix 'muse-latex-pdf-extension
611 :osuffix 'muse-latex-pdf-extension)
613 (muse-derive-style "latexcjk" "latex"
614 :header 'muse-latexcjk-header
615 :footer 'muse-latexcjk-footer)
617 (muse-derive-style "pdfcjk" "latexcjk"
618 :final 'muse-latex-pdf-generate
619 :browser 'muse-latex-pdf-browse-file
620 :link-suffix 'muse-latex-pdf-extension
621 :osuffix 'muse-latex-pdf-extension)
623 (muse-derive-style "slides" "latex"
624 :header 'muse-latex-slides-header
625 :tags 'muse-latex-slides-markup-tags)
627 (muse-derive-style "slides-pdf" "pdf"
628 :header 'muse-latex-slides-header
629 :tags 'muse-latex-slides-markup-tags)
631 (muse-derive-style "lecture-notes" "slides"
632 :header 'muse-latex-lecture-notes-header)
634 (muse-derive-style "lecture-notes-pdf" "slides-pdf"
635 :header 'muse-latex-lecture-notes-header)
637 (provide 'muse-latex)
639 ;;; muse-latex.el ends here