1 ;;; muse-latex.el --- publish entries in LaTex or PDF format
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
8 ;; Emacs Muse is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published
10 ;; by the Free Software Foundation; either version 3, or (at your
11 ;; option) any later version.
13 ;; Emacs Muse is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;; General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with Emacs Muse; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
27 ;; Li Daobing (lidaobing AT gmail DOT com) provided CJK support.
29 ;; Trent Buck (trentbuck AT gmail DOT com) gave valuable advice for
30 ;; how to treat LaTeX specials and the like.
32 ;; Matthias Kegelmann (mathias DOT kegelmann AT sdm DOT de) provided a
33 ;; scenario where we would need to respect the <contents> tag.
35 ;; Jean Magnan de Bornier (jean AT bornier DOT net) provided the
36 ;; markup string for link-and-anchor.
38 ;; Jim Ottaway (j DOT ottaway AT lse DOT ac DOT uk) implemented slides
41 ;; Karl Berry (karl AT freefriends DOT org) suggested how to escape
42 ;; additional special characters in image filenames.
46 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48 ;; Muse LaTeX Publishing
50 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
52 (require 'muse-publish
)
54 (defgroup muse-latex nil
55 "Rules for marking up a Muse file as a LaTeX article."
58 (defcustom muse-latex-extension
".tex"
59 "Default file extension for publishing LaTeX files."
63 (defcustom muse-latex-pdf-extension
".pdf"
64 "Default file extension for publishing LaTeX files to PDF."
68 (defcustom muse-latex-pdf-browser
"open %s"
69 "The program to use when browsing a published PDF file.
70 This should be a format string."
74 (defcustom muse-latex-pdf-program
"pdflatex"
75 "The program that is called to generate PDF content from LaTeX content."
79 (defcustom muse-latex-pdf-cruft
80 '(".aux" ".log" ".nav" ".out" ".snm" ".toc" ".vrb")
81 "Extensions of files to remove after generating PDF output successfully."
85 (defcustom muse-latex-header
86 "\\documentclass{article}
88 \\usepackage[english]{babel}
90 \\usepackage[utf8x]{inputenc}
91 \\usepackage[T1]{fontenc}
92 \\usepackage{hyperref}
93 \\usepackage[pdftex]{graphicx}
95 \\def\\museincludegraphics{%
100 \\includegraphics[width=0.75\\textwidth]
105 \\title{<lisp>(muse-publish-escape-specials-in-string
106 (muse-publishing-directive \"title\") 'document)</lisp>}
107 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
108 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
112 <lisp>(and muse-publish-generate-contents
113 (not muse-latex-permit-contents-tag)
114 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
115 "Header used for publishing LaTeX files. This may be text or a filename."
119 (defcustom muse-latex-footer
"<lisp>(muse-latex-bibliography)</lisp>
121 "Footer used for publishing LaTeX files. This may be text or a filename."
125 (defcustom muse-latexcjk-header
126 "\\documentclass{article}
129 \\usepackage{indentfirst}
130 \\usepackage[CJKbookmarks=true]{hyperref}
131 \\usepackage[pdftex]{graphicx}
134 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
136 \\title{<lisp>(muse-publish-escape-specials-in-string
137 (muse-publishing-directive \"title\") 'document)</lisp>}
138 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
139 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
143 <lisp>(and muse-publish-generate-contents
144 (not muse-latex-permit-contents-tag)
145 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
146 "Header used for publishing LaTeX files (CJK). This may be text or a
151 (defcustom muse-latexcjk-footer
154 "Footer used for publishing LaTeX files (CJK). This may be text or a
159 (defcustom muse-latex-slides-header
160 "\\documentclass[ignorenonframetext]{beamer}
162 \\usepackage[english]{babel}
164 \\usepackage[utf8x]{inputenc}
165 \\usepackage[T1]{fontenc}
166 \\usepackage{hyperref}
170 \\title{<lisp>(muse-publish-escape-specials-in-string
171 (muse-publishing-directive \"title\") 'document)</lisp>}
172 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
173 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
177 <lisp>(and muse-publish-generate-contents
178 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
179 "Header for publishing of slides using LaTeX.
180 This may be text or a filename.
182 You must have the Beamer extension for LaTeX installed for this to work."
186 (defcustom muse-latex-lecture-notes-header
187 "\\documentclass{article}
188 \\usepackage{beamerarticle}
190 \\usepackage[english]{babel}
192 \\usepackage[utf8x]{inputenc}
193 \\usepackage[T1]{fontenc}
194 \\usepackage{hyperref}
198 \\title{<lisp>(muse-publish-escape-specials-in-string
199 (muse-publishing-directive \"title\") 'document)</lisp>}
200 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
201 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
205 <lisp>(and muse-publish-generate-contents
206 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
207 "Header for publishing of lecture notes using LaTeX.
208 This may be text or a filename.
210 You must have the Beamer extension for LaTeX installed for this to work."
214 (defcustom muse-latex-markup-regexps
216 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
218 ;; be careful of closing quote pairs
219 (10100 "\"'" 0 "\"\\\\-'"))
220 "List of markup regexps for identifying regions in a Muse page.
221 For more on the structure of this list, see `muse-publish-markup-regexps'."
222 :type
'(repeat (choice
223 (list :tag
"Markup rule"
225 (choice regexp symbol
)
227 (choice string function symbol
))
231 (defcustom muse-latex-markup-functions
232 '((table . muse-latex-markup-table
))
233 "An alist of style types to custom functions for that kind of text.
234 For more on the structure of this list, see
235 `muse-publish-markup-functions'."
236 :type
'(alist :key-type symbol
:value-type function
)
239 (defcustom muse-latex-markup-strings
240 '((image-with-desc .
"\\begin{figure}[h]
241 \\centering\\museincludegraphics{%s.%s}|endgroup
244 (image .
"\\begin{figure}[h]
245 \\centering\\museincludegraphics{%s.%s}|endgroup
248 \\museincludegraphics{%s.%s}|endgroup")
249 (anchor-ref .
"\\ref{%s}")
251 (url-and-desc .
"\\href{%s}{%s}\\footnote{%1%}")
252 (link .
"\\href{%s}{%s}\\footnote{%1%}")
253 (link-and-anchor .
"\\href{%1%}{%3%}\\footnote{%1%}")
254 (email-addr .
"\\verb|%s|")
255 (anchor .
"\\label{%s}")
257 (comment-begin .
"% ")
258 (rule .
"\\vspace{.5cm}\\hrule\\vspace{.5cm}")
259 (no-break-space .
"~")
260 (line-break .
"\\\\")
261 (enddots .
"\\ldots{}")
265 (chapter .
"\\chapter{")
267 (section .
"\\section{")
269 (subsection .
"\\subsection{")
270 (subsection-end .
"}")
271 (subsubsection .
"\\subsubsection{")
272 (subsubsection-end .
"}")
273 (section-other .
"\\paragraph{")
274 (section-other-end .
"}")
275 (footnote .
"\\footnote{")
277 (footnotetext .
"\\footnotetext[%d]{")
278 (begin-underline .
"\\underline{")
279 (end-underline .
"}")
280 (begin-literal .
"\\texttt{")
282 (begin-emph .
"\\emph{")
284 (begin-more-emph .
"\\textbf{")
285 (end-more-emph .
"}")
286 (begin-most-emph .
"\\textbf{\\emph{")
287 (end-most-emph .
"}}")
288 (begin-verse .
"\\begin{verse}\n")
289 (end-verse-line .
" \\\\")
290 (verse-space .
"~~~~")
291 (end-verse .
"\n\\end{verse}")
292 (begin-example .
"\\begin{quote}\n\\begin{verbatim}")
293 (end-example .
"\\end{verbatim}\n\\end{quote}")
294 (begin-center .
"\\begin{center}\n")
295 (end-center .
"\n\\end{center}")
296 (begin-quote .
"\\begin{quote}\n")
297 (end-quote .
"\n\\end{quote}")
298 (begin-cite .
"\\cite{")
299 (begin-cite-author .
"\\citet{")
300 (begin-cite-year .
"\\citet{")
302 (begin-uli .
"\\begin{itemize}\n")
303 (end-uli .
"\n\\end{itemize}")
304 (begin-uli-item .
"\\item ")
305 (begin-oli .
"\\begin{enumerate}\n")
306 (end-oli .
"\n\\end{enumerate}")
307 (begin-oli-item .
"\\item ")
308 (begin-dl .
"\\begin{description}\n")
309 (end-dl .
"\n\\end{description}")
310 (begin-ddt .
"\\item[")
311 (end-ddt .
"] \\mbox{}\n"))
312 "Strings used for marking up text.
313 These cover the most basic kinds of markup, the handling of which
314 differs little between the various styles."
315 :type
'(alist :key-type symbol
:value-type string
)
318 (defcustom muse-latex-slides-markup-tags
319 '(("slide" t t nil muse-latex-slide-tag
))
320 "A list of tag specifications, for specially marking up LaTeX slides."
321 :type
'(repeat (list (string :tag
"Markup tag")
322 (boolean :tag
"Expect closing tag" :value t
)
323 (boolean :tag
"Parse attributes" :value nil
)
324 (boolean :tag
"Nestable" :value nil
)
328 (defcustom muse-latexcjk-encoding-map
329 '((utf-8 .
"{UTF8}{song}")
330 (japanese-iso-8bit .
"[dnp]{JIS}{min}")
331 (chinese-big5 .
"{Bg5}{bsmi}")
332 (mule-utf-8 .
"{UTF8}{song}")
333 (chinese-iso-8bit .
"{GB}{song}")
334 (chinese-gbk .
"{GBK}{song}"))
335 "An alist mapping emacs coding systems to appropriate CJK codings.
336 Use the base name of the coding system (ie, without the -unix)."
337 :type
'(alist :key-type coding-system
:value-type string
)
340 (defcustom muse-latexcjk-encoding-default
"{GB}{song}"
341 "The default Emacs buffer encoding to use in published files.
342 This will be used if no special characters are found."
346 (defun muse-latexcjk-encoding ()
347 (when (boundp 'buffer-file-coding-system
)
348 (muse-latexcjk-transform-content-type buffer-file-coding-system
)))
350 (defun muse-latexcjk-transform-content-type (content-type)
351 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
352 system to an associated CJK coding system."
353 (let ((match (and (fboundp 'coding-system-base
)
354 (assoc (coding-system-base content-type
)
355 muse-latexcjk-encoding-map
))))
358 muse-latexcjk-encoding-default
)))
360 (defcustom muse-latex-markup-specials-document
361 '((?
\\ .
"\\textbackslash{}")
362 (?\_ .
"\\textunderscore{}")
363 (?\
< .
"\\textless{}")
364 (?\
> .
"\\textgreater{}")
374 "A table of characters which must be represented specially.
375 These are applied to the entire document, sans already-escaped
377 :type
'(alist :key-type character
:value-type string
)
380 (defcustom muse-latex-markup-specials-example
382 "A table of characters which must be represented specially.
383 These are applied to <example> regions.
385 With the default interpretation of <example> regions, no specials
387 :type
'(alist :key-type character
:value-type string
)
390 (defcustom muse-latex-markup-specials-literal
392 (?
\\ .
"\\textbackslash{}")
393 (?_ .
"\\textunderscore{}")
394 (?\
< .
"\\textless{}")
395 (?\
> .
"\\textgreater{}")
404 "A table of characters which must be represented specially.
405 This applies to =monospaced text= and <code> regions."
406 :type
'(alist :key-type character
:value-type string
)
409 (defcustom muse-latex-markup-specials-url
410 '((?
\\ .
"\\textbackslash{}")
420 "A table of characters which must be represented specially.
421 These are applied to URLs."
422 :type
'(alist :key-type character
:value-type string
)
425 (defcustom muse-latex-markup-specials-image
436 "A table of characters which must be represented specially.
437 These are applied to image filenames."
438 :type
'(alist :key-type character
:value-type string
)
441 (defun muse-latex-decide-specials (context)
442 "Determine the specials to escape, depending on CONTEXT."
443 (cond ((memq context
'(underline emphasis document url-desc verbatim
445 muse-latex-markup-specials-document
)
447 muse-latex-markup-specials-image
)
448 ((memq context
'(email url
))
449 muse-latex-markup-specials-url
)
450 ((eq context
'literal
)
451 muse-latex-markup-specials-literal
)
452 ((eq context
'example
)
453 muse-latex-markup-specials-example
)
454 (t (error "Invalid context '%s' in muse-latex" context
))))
456 (defcustom muse-latex-permit-contents-tag nil
457 "If nil, ignore <contents> tags. Otherwise, insert table of contents.
459 Most of the time, it is best to have a table of contents on the
460 first page, with a new page immediately following. To make this
461 work with documents published in both HTML and LaTeX, we need to
462 ignore the <contents> tag.
464 If you don't agree with this, then set this option to non-nil,
465 and it will do what you expect."
469 (defun muse-latex-markup-table ()
470 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
472 (row-len (car table-info
))
473 (field-list (cdr table-info
)))
475 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l
) "}\n")
476 (dolist (fields field-list
)
477 (let ((type (car fields
)))
478 (setq fields
(cdr fields
))
480 (muse-insert-markup "\\hline\n")
482 (muse-insert-markup "\\hline\n"))
483 (insert (car fields
))
484 (setq fields
(cdr fields
))
485 (dolist (field fields
)
486 (muse-insert-markup " & ")
488 (muse-insert-markup " \\\\\n")
490 (muse-insert-markup "\\hline\n")))))
491 (muse-insert-markup "\\end{tabular}"))))
495 (defun muse-latex-slide-tag (beg end attrs
)
496 "Publish the <slide> tag in LaTeX.
497 This is used by the slides and lecture-notes publishing styles."
498 (let ((title (cdr (assoc "title" attrs
))))
500 (muse-insert-markup "\\begin{frame}[fragile]\n")
502 (muse-insert-markup "\\frametitle{")
504 (muse-insert-markup "}\n"))
507 (muse-insert-markup "\n\\end{frame}"))))
509 ;;; Post-publishing functions
511 (defun muse-latex-fixup-dquotes ()
512 "Fixup double quotes."
513 (goto-char (point-min))
515 (while (search-forward "\"" nil t
)
516 (unless (get-text-property (match-beginning 0) 'read-only
)
518 (eq (char-before) ?
\n))
527 (defun muse-latex-fixup-citations ()
528 "Replace semicolons in multi-head citations with colons."
529 (goto-char (point-min))
530 (while (re-search-forward "\\\\cite.?{" nil t
)
531 (let ((start (point))
532 (end (re-search-forward "}")))
534 (narrow-to-region start end
)
535 (goto-char (point-min))
536 (while (re-search-forward ";" nil t
)
537 (replace-match ","))))))
539 (defun muse-latex-fixup-headings ()
540 "Remove footnotes in headings, since LaTeX does not permit them to exist.
542 This can happen if there is a link in a heading, because by
543 default Muse will add a footnote for each link."
544 (goto-char (point-min))
545 (while (re-search-forward "^\\\\section.?{" nil t
)
547 (narrow-to-region (match-beginning 0) (muse-line-end-position))
548 (goto-char (point-min))
549 (while (re-search-forward "\\\\footnote{[^}\n]+}" nil t
)
553 (defun muse-latex-munge-buffer ()
554 (muse-latex-fixup-dquotes)
555 (muse-latex-fixup-citations)
556 (muse-latex-fixup-headings)
557 (when (and muse-latex-permit-contents-tag
558 muse-publish-generate-contents
)
559 (goto-char (car muse-publish-generate-contents
))
560 (muse-insert-markup "\\tableofcontents")))
562 (defun muse-latex-bibliography ()
564 (goto-char (point-min))
565 (if (re-search-forward "\\\\cite.?{" nil t
)
568 (muse-publishing-directive "bibsource")
572 (defun muse-latex-pdf-browse-file (file)
573 (shell-command (format muse-latex-pdf-browser file
)))
575 (defun muse-latex-pdf-generate (file output-path final-target
)
577 #'muse-publish-transform-output
578 file output-path final-target
"PDF"
580 (lambda (file output-path
)
581 (let* ((fnd (file-name-directory output-path
))
582 (command (format "%s \"%s\""
583 muse-latex-pdf-program
584 (file-relative-name file fnd
)))
586 (default-directory fnd
)
588 ;; XEmacs can sometimes return a non-number result. We'll err
589 ;; on the side of caution by continuing to attempt to generate
590 ;; the PDF if this happens and treat the final result as
592 (while (and (< times
2)
593 (or (not (numberp result
))
595 ;; table of contents takes 2 passes
597 (muse-replace-regexp-in-string
598 "\\.tex\\'" ".toc" file t t
))))
599 (setq result
(shell-command command
)
601 (if (or (not (numberp result
))
605 muse-latex-pdf-cruft
))
607 ;;; Register the Muse LATEX Publishers
609 (muse-define-style "latex"
610 :suffix
'muse-latex-extension
611 :regexps
'muse-latex-markup-regexps
612 :functions
'muse-latex-markup-functions
613 :strings
'muse-latex-markup-strings
614 :specials
'muse-latex-decide-specials
615 :before-end
'muse-latex-munge-buffer
616 :header
'muse-latex-header
617 :footer
'muse-latex-footer
620 (muse-derive-style "pdf" "latex"
621 :final
'muse-latex-pdf-generate
622 :browser
'muse-latex-pdf-browse-file
623 :link-suffix
'muse-latex-pdf-extension
624 :osuffix
'muse-latex-pdf-extension
)
626 (muse-derive-style "latexcjk" "latex"
627 :header
'muse-latexcjk-header
628 :footer
'muse-latexcjk-footer
)
630 (muse-derive-style "pdfcjk" "latexcjk"
631 :final
'muse-latex-pdf-generate
632 :browser
'muse-latex-pdf-browse-file
633 :link-suffix
'muse-latex-pdf-extension
634 :osuffix
'muse-latex-pdf-extension
)
636 (muse-derive-style "slides" "latex"
637 :header
'muse-latex-slides-header
638 :tags
'muse-latex-slides-markup-tags
)
640 (muse-derive-style "slides-pdf" "pdf"
641 :header
'muse-latex-slides-header
642 :tags
'muse-latex-slides-markup-tags
)
644 (muse-derive-style "lecture-notes" "slides"
645 :header
'muse-latex-lecture-notes-header
)
647 (muse-derive-style "lecture-notes-pdf" "slides-pdf"
648 :header
'muse-latex-lecture-notes-header
)
650 (provide 'muse-latex
)
652 ;;; muse-latex.el ends here