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