Try to fix issue with changing directory in LaTeX and ConTeXt styles
[muse-el.git] / lisp / muse-latex.el
blobf6f21f70dd949bd0d67d8a5af830665fb2b8bbfa
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 (?_ . "\\textunderscore{}")
381 (?\< . "\\textless{}")
382 (?\> . "\\textgreater{}")
383 (?^ . "\\^{}")
384 (?\~ . "\\~{}")
385 (?\$ . "\\$")
386 (?\% . "\\%")
387 (?\{ . "\\{")
388 (?\} . "\\}")
389 (?\& . "\\&")
390 (?\# . "\\#"))
391 "A table of characters which must be represented specially.
392 This applies to =monospaced text= and <code> regions."
393 :type '(alist :key-type character :value-type string)
394 :group 'muse-latex)
396 (defcustom muse-latex-markup-specials-url
397 '((?\\ . "\\textbackslash{}")
398 (?\_ . "\\_")
399 (?\< . "\\<")
400 (?\> . "\\>")
401 (?\$ . "\\$")
402 (?\% . "\\%")
403 (?\{ . "\\{")
404 (?\} . "\\}")
405 (?\& . "\\&")
406 (?\# . "\\#"))
407 "A table of characters which must be represented specially.
408 These are applied to URLs."
409 :type '(alist :key-type character :value-type string)
410 :group 'muse-latex)
412 (defcustom muse-latex-markup-specials-image
413 '((?\\ . "\\\\")
414 (?\< . "\\<")
415 (?\> . "\\>")
416 (?\$ . "\\$")
417 (?\% . "\\%")
418 (?\{ . "\\{")
419 (?\} . "\\}")
420 (?\& . "\\&")
421 (?\# . "\\#")
422 (?\| . "\\|"))
423 "A table of characters which must be represented specially.
424 These are applied to image filenames."
425 :type '(alist :key-type character :value-type string)
426 :group 'muse-latex)
428 (defun muse-latex-decide-specials (context)
429 "Determine the specials to escape, depending on CONTEXT."
430 (cond ((memq context '(underline emphasis document url-desc verbatim
431 footnote))
432 muse-latex-markup-specials-document)
433 ((eq context 'image)
434 muse-latex-markup-specials-image)
435 ((memq context '(email url))
436 muse-latex-markup-specials-url)
437 ((eq context 'literal)
438 muse-latex-markup-specials-literal)
439 ((eq context 'example)
440 muse-latex-markup-specials-example)
441 (t (error "Invalid context '%s' in muse-latex" context))))
443 (defcustom muse-latex-permit-contents-tag nil
444 "If nil, ignore <contents> tags. Otherwise, insert table of contents.
446 Most of the time, it is best to have a table of contents on the
447 first page, with a new page immediately following. To make this
448 work with documents published in both HTML and LaTeX, we need to
449 ignore the <contents> tag.
451 If you don't agree with this, then set this option to non-nil,
452 and it will do what you expect."
453 :type 'boolean
454 :group 'muse-latex)
456 (defun muse-latex-markup-table ()
457 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
458 (match-end 0)))
459 (row-len (car table-info))
460 (field-list (cdr table-info)))
461 (when table-info
462 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l) "}\n")
463 (dolist (fields field-list)
464 (let ((type (car fields)))
465 (setq fields (cdr fields))
466 (if (eq type 'hline)
467 (muse-insert-markup "\\hline\n")
468 (when (= type 3)
469 (muse-insert-markup "\\hline\n"))
470 (insert (car fields))
471 (setq fields (cdr fields))
472 (dolist (field fields)
473 (muse-insert-markup " & ")
474 (insert field))
475 (muse-insert-markup " \\\\\n")
476 (when (= type 2)
477 (muse-insert-markup "\\hline\n")))))
478 (muse-insert-markup "\\end{tabular}"))))
480 ;;; Tags for LaTeX
482 (defun muse-latex-slide-tag (beg end attrs)
483 "Publish the <slide> tag in LaTeX.
484 This is used by the slides and lecture-notes publishing styles."
485 (let ((title (cdr (assoc "title" attrs))))
486 (goto-char beg)
487 (muse-insert-markup "\\begin{frame}\n")
488 (when title
489 (muse-insert-markup "\\frametitle{")
490 (insert title)
491 (muse-insert-markup "}\n"))
492 (goto-char end)
493 (muse-insert-markup "\n\\end{frame}")))
495 ;;; Post-publishing functions
497 (defun muse-latex-fixup-dquotes ()
498 "Fixup double quotes."
499 (goto-char (point-min))
500 (let ((open t))
501 (while (search-forward "\"" nil t)
502 (unless (get-text-property (match-beginning 0) 'read-only)
503 (when (or (bobp)
504 (eq (char-before) ?\n))
505 (setq open t))
506 (if open
507 (progn
508 (replace-match "``")
509 (setq open nil))
510 (replace-match "''")
511 (setq open t))))))
513 (defun muse-latex-fixup-citations ()
514 "Replace semicolons in multi-head citations with colons."
515 (goto-char (point-min))
516 (while (re-search-forward "\\\\cite.?{" nil t)
517 (let ((start (point))
518 (end (re-search-forward "}")))
519 (save-restriction
520 (narrow-to-region start end)
521 (goto-char (point-min))
522 (while (re-search-forward ";" nil t)
523 (replace-match ","))))))
525 (defun muse-latex-fixup-headings ()
526 "Remove footnotes in headings, since LaTeX does not permit them to exist.
528 This can happen if there is a link in a heading, because by
529 default Muse will add a footnote for each link."
530 (goto-char (point-min))
531 (while (re-search-forward "^\\\\section.?{" nil t)
532 (save-restriction
533 (narrow-to-region (match-beginning 0) (muse-line-end-position))
534 (goto-char (point-min))
535 (while (re-search-forward "\\\\footnote{[^}\n]+}" nil t)
536 (replace-match ""))
537 (forward-line 1))))
539 (defun muse-latex-munge-buffer ()
540 (muse-latex-fixup-dquotes)
541 (muse-latex-fixup-citations)
542 (muse-latex-fixup-headings)
543 (when (and muse-latex-permit-contents-tag
544 muse-publish-generate-contents)
545 (goto-char (car muse-publish-generate-contents))
546 (muse-insert-markup "\\tableofcontents")))
548 (defun muse-latex-bibliography ()
549 (save-excursion
550 (goto-char (point-min))
551 (if (re-search-forward "\\\\cite.?{" nil t)
552 (concat
553 "\\bibliography{"
554 (muse-publishing-directive "bibsource")
555 "}\n")
556 "")))
558 (defun muse-latex-pdf-browse-file (file)
559 (shell-command (concat "open " file)))
561 (defun muse-latex-pdf-generate (file output-path final-target)
562 (apply
563 #'muse-publish-transform-output
564 file output-path final-target "PDF"
565 (function
566 (lambda (file output-path)
567 (let* ((fnd (file-name-directory output-path))
568 (command (format "%s \"%s\""
569 muse-latex-pdf-program
570 (file-relative-name file fnd)))
571 (times 0)
572 (default-directory fnd)
573 result)
574 ;; XEmacs can sometimes return a non-number result. We'll err
575 ;; on the side of caution by continuing to attempt to generate
576 ;; the PDF if this happens and treat the final result as
577 ;; successful.
578 (while (and (< times 2)
579 (or (not (numberp result))
580 (not (eq result 0))
581 ;; table of contents takes 2 passes
582 (file-readable-p
583 (muse-replace-regexp-in-string
584 "\\.tex\\'" ".toc" file t t))))
585 (setq result (shell-command command)
586 times (1+ times)))
587 (if (or (not (numberp result))
588 (eq result 0))
590 nil))))
591 muse-latex-pdf-cruft))
593 ;;; Register the Muse LATEX Publishers
595 (muse-define-style "latex"
596 :suffix 'muse-latex-extension
597 :regexps 'muse-latex-markup-regexps
598 :functions 'muse-latex-markup-functions
599 :strings 'muse-latex-markup-strings
600 :specials 'muse-latex-decide-specials
601 :before-end 'muse-latex-munge-buffer
602 :header 'muse-latex-header
603 :footer 'muse-latex-footer
604 :browser 'find-file)
606 (muse-derive-style "pdf" "latex"
607 :final 'muse-latex-pdf-generate
608 :browser 'muse-latex-pdf-browse-file
609 :link-suffix 'muse-latex-pdf-extension
610 :osuffix 'muse-latex-pdf-extension)
612 (muse-derive-style "latexcjk" "latex"
613 :header 'muse-latexcjk-header
614 :footer 'muse-latexcjk-footer)
616 (muse-derive-style "pdfcjk" "latexcjk"
617 :final 'muse-latex-pdf-generate
618 :browser 'muse-latex-pdf-browse-file
619 :link-suffix 'muse-latex-pdf-extension
620 :osuffix 'muse-latex-pdf-extension)
622 (muse-derive-style "slides" "latex"
623 :header 'muse-latex-slides-header
624 :tags 'muse-latex-slides-markup-tags)
626 (muse-derive-style "slides-pdf" "pdf"
627 :header 'muse-latex-slides-header
628 :tags 'muse-latex-slides-markup-tags)
630 (muse-derive-style "lecture-notes" "slides"
631 :header 'muse-latex-lecture-notes-header)
633 (muse-derive-style "lecture-notes-pdf" "slides-pdf"
634 :header 'muse-latex-lecture-notes-header)
636 (provide 'muse-latex)
638 ;;; muse-latex.el ends here