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.
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
40 ;; Karl Berry (karl AT freefriends DOT org) suggested how to escape
41 ;; additional special characters in image filenames.
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."
57 (defcustom muse-latex-extension
".tex"
58 "Default file extension for publishing LaTeX files."
62 (defcustom muse-latex-pdf-extension
".pdf"
63 "Default file extension for publishing LaTeX files to PDF."
67 (defcustom muse-latex-pdf-browser
"open %s"
68 "The program to use when browsing a published PDF file.
69 This should be a format string."
73 (defcustom muse-latex-pdf-program
"pdflatex"
74 "The program that is called to generate PDF content from LaTeX content."
78 (defcustom muse-latex-pdf-cruft
'(".aux" ".toc" ".out" ".log")
79 "Extensions of files to remove after generating PDF output successfully."
83 (defcustom muse-latex-header
84 "\\documentclass{article}
86 \\usepackage[english]{babel}
88 \\usepackage[utf8x]{inputenc}
89 \\usepackage[T1]{fontenc}
90 \\usepackage{hyperref}
91 \\usepackage[pdftex]{graphicx}
93 \\def\\museincludegraphics{%
98 \\includegraphics[width=0.75\\textwidth]
103 \\title{<lisp>(muse-publish-escape-specials-in-string
104 (muse-publishing-directive \"title\") 'document)</lisp>}
105 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
106 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
110 <lisp>(and muse-publish-generate-contents
111 (not muse-latex-permit-contents-tag)
112 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
113 "Header used for publishing LaTeX files. This may be text or a filename."
117 (defcustom muse-latex-footer
"<lisp>(muse-latex-bibliography)</lisp>
119 "Footer used for publishing LaTeX files. This may be text or a filename."
123 (defcustom muse-latexcjk-header
124 "\\documentclass{article}
127 \\usepackage{indentfirst}
128 \\usepackage[CJKbookmarks=true]{hyperref}
129 \\usepackage[pdftex]{graphicx}
132 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
134 \\title{<lisp>(muse-publish-escape-specials-in-string
135 (muse-publishing-directive \"title\") 'document)</lisp>}
136 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
137 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
141 <lisp>(and muse-publish-generate-contents
142 (not muse-latex-permit-contents-tag)
143 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
144 "Header used for publishing LaTeX files (CJK). This may be text or a
149 (defcustom muse-latexcjk-footer
152 "Footer used for publishing LaTeX files (CJK). This may be text or a
157 (defcustom muse-latex-slides-header
158 "\\documentclass[ignorenonframetext]{beamer}
160 \\usepackage[english]{babel}
162 \\usepackage[utf8x]{inputenc}
163 \\usepackage[T1]{fontenc}
164 \\usepackage{hyperref}
168 \\title{<lisp>(muse-publish-escape-specials-in-string
169 (muse-publishing-directive \"title\") 'document)</lisp>}
170 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
171 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
175 <lisp>(and muse-publish-generate-contents
176 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
177 "Header for publishing of slides using LaTeX.
178 This may be text or a filename.
180 You must have the Beamer extension for LaTeX installed for this to work."
184 (defcustom muse-latex-lecture-notes-header
185 "\\documentclass{article}
186 \\usepackage{beamerarticle}
188 \\usepackage[english]{babel}
190 \\usepackage[utf8x]{inputenc}
191 \\usepackage[T1]{fontenc}
192 \\usepackage{hyperref}
196 \\title{<lisp>(muse-publish-escape-specials-in-string
197 (muse-publishing-directive \"title\") 'document)</lisp>}
198 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
199 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
203 <lisp>(and muse-publish-generate-contents
204 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
205 "Header for publishing of lecture notes using LaTeX.
206 This may be text or a filename.
208 You must have the Beamer extension for LaTeX installed for this to work."
212 (defcustom muse-latex-markup-regexps
214 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
216 ;; be careful of closing quote pairs
217 (10100 "\"'" 0 "\"\\\\-'"))
218 "List of markup regexps for identifying regions in a Muse page.
219 For more on the structure of this list, see `muse-publish-markup-regexps'."
220 :type
'(repeat (choice
221 (list :tag
"Markup rule"
223 (choice regexp symbol
)
225 (choice string function symbol
))
229 (defcustom muse-latex-markup-functions
230 '((table . muse-latex-markup-table
))
231 "An alist of style types to custom functions for that kind of text.
232 For more on the structure of this list, see
233 `muse-publish-markup-functions'."
234 :type
'(alist :key-type symbol
:value-type function
)
237 (defcustom muse-latex-markup-strings
238 '((image-with-desc .
"\\begin{figure}[h]
239 \\centering\\museincludegraphics{%s.%s}|endgroup
242 (image .
"\\begin{figure}[h]
243 \\centering\\museincludegraphics{%s.%s}|endgroup
246 \\museincludegraphics{%s.%s}|endgroup")
247 (anchor-ref .
"\\ref{%s}")
249 (url-and-desc .
"\\href{%s}{%s}\\footnote{%1%}")
250 (link .
"\\href{%s}{%s}\\footnote{%1%}")
251 (link-and-anchor .
"\\href{%1%}{%3%}\\footnote{%1%}")
252 (email-addr .
"\\verb|%s|")
253 (anchor .
"\\label{%s}")
255 (comment-begin .
"% ")
256 (rule .
"\\vspace{.5cm}\\hrule\\vspace{.5cm}")
257 (no-break-space .
"~")
258 (line-break .
"\\\\")
259 (enddots .
"\\ldots{}")
263 (chapter .
"\\chapter{")
265 (section .
"\\section{")
267 (subsection .
"\\subsection{")
268 (subsection-end .
"}")
269 (subsubsection .
"\\subsubsection{")
270 (subsubsection-end .
"}")
271 (section-other .
"\\paragraph{")
272 (section-other-end .
"}")
273 (footnote .
"\\footnote{")
275 (footnotetext .
"\\footnotetext[%d]{")
276 (begin-underline .
"\\underline{")
277 (end-underline .
"}")
278 (begin-literal .
"\\texttt{")
280 (begin-emph .
"\\emph{")
282 (begin-more-emph .
"\\textbf{")
283 (end-more-emph .
"}")
284 (begin-most-emph .
"\\textbf{\\emph{")
285 (end-most-emph .
"}}")
286 (begin-verse .
"\\begin{verse}\n")
287 (end-verse-line .
" \\\\")
288 (verse-space .
"~~~~")
289 (end-verse .
"\n\\end{verse}")
290 (begin-example .
"\\begin{quote}\n\\begin{verbatim}")
291 (end-example .
"\\end{verbatim}\n\\end{quote}")
292 (begin-center .
"\\begin{center}\n")
293 (end-center .
"\n\\end{center}")
294 (begin-quote .
"\\begin{quote}\n")
295 (end-quote .
"\n\\end{quote}")
296 (begin-cite .
"\\cite{")
297 (begin-cite-author .
"\\citet{")
298 (begin-cite-year .
"\\citet{")
300 (begin-uli .
"\\begin{itemize}\n")
301 (end-uli .
"\n\\end{itemize}")
302 (begin-uli-item .
"\\item ")
303 (begin-oli .
"\\begin{enumerate}\n")
304 (end-oli .
"\n\\end{enumerate}")
305 (begin-oli-item .
"\\item ")
306 (begin-dl .
"\\begin{description}\n")
307 (end-dl .
"\n\\end{description}")
308 (begin-ddt .
"\\item[")
309 (end-ddt .
"] \\mbox{}\n"))
310 "Strings used for marking up text.
311 These cover the most basic kinds of markup, the handling of which
312 differs little between the various styles."
313 :type
'(alist :key-type symbol
:value-type string
)
316 (defcustom muse-latex-slides-markup-tags
317 '(("slide" t t nil muse-latex-slide-tag
))
318 "A list of tag specifications, for specially marking up LaTeX slides."
319 :type
'(repeat (list (string :tag
"Markup tag")
320 (boolean :tag
"Expect closing tag" :value t
)
321 (boolean :tag
"Parse attributes" :value nil
)
322 (boolean :tag
"Nestable" :value nil
)
326 (defcustom muse-latexcjk-encoding-map
327 '((utf-8 .
"{UTF8}{song}")
328 (japanese-iso-8bit .
"[dnp]{JIS}{min}")
329 (chinese-big5 .
"{Bg5}{bsmi}")
330 (mule-utf-8 .
"{UTF8}{song}")
331 (chinese-iso-8bit .
"{GB}{song}")
332 (chinese-gbk .
"{GBK}{song}"))
333 "An alist mapping emacs coding systems to appropriate CJK codings.
334 Use the base name of the coding system (ie, without the -unix)."
335 :type
'(alist :key-type coding-system
:value-type string
)
338 (defcustom muse-latexcjk-encoding-default
"{GB}{song}"
339 "The default Emacs buffer encoding to use in published files.
340 This will be used if no special characters are found."
344 (defun muse-latexcjk-encoding ()
345 (when (boundp 'buffer-file-coding-system
)
346 (muse-latexcjk-transform-content-type buffer-file-coding-system
)))
348 (defun muse-latexcjk-transform-content-type (content-type)
349 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
350 system to an associated CJK coding system."
351 (let ((match (and (fboundp 'coding-system-base
)
352 (assoc (coding-system-base content-type
)
353 muse-latexcjk-encoding-map
))))
356 muse-latexcjk-encoding-default
)))
358 (defcustom muse-latex-markup-specials-document
359 '((?
\\ .
"\\textbackslash{}")
360 (?\_ .
"\\textunderscore{}")
361 (?\
< .
"\\textless{}")
362 (?\
> .
"\\textgreater{}")
372 "A table of characters which must be represented specially.
373 These are applied to the entire document, sans already-escaped
375 :type
'(alist :key-type character
:value-type string
)
378 (defcustom muse-latex-markup-specials-example
380 "A table of characters which must be represented specially.
381 These are applied to <example> regions.
383 With the default interpretation of <example> regions, no specials
385 :type
'(alist :key-type character
:value-type string
)
388 (defcustom muse-latex-markup-specials-literal
390 (?
\\ .
"\\textbackslash{}")
391 (?_ .
"\\textunderscore{}")
392 (?\
< .
"\\textless{}")
393 (?\
> .
"\\textgreater{}")
402 "A table of characters which must be represented specially.
403 This applies to =monospaced text= and <code> regions."
404 :type
'(alist :key-type character
:value-type string
)
407 (defcustom muse-latex-markup-specials-url
408 '((?
\\ .
"\\textbackslash{}")
418 "A table of characters which must be represented specially.
419 These are applied to URLs."
420 :type
'(alist :key-type character
:value-type string
)
423 (defcustom muse-latex-markup-specials-image
434 "A table of characters which must be represented specially.
435 These are applied to image filenames."
436 :type
'(alist :key-type character
:value-type string
)
439 (defun muse-latex-decide-specials (context)
440 "Determine the specials to escape, depending on CONTEXT."
441 (cond ((memq context
'(underline emphasis document url-desc verbatim
443 muse-latex-markup-specials-document
)
445 muse-latex-markup-specials-image
)
446 ((memq context
'(email url
))
447 muse-latex-markup-specials-url
)
448 ((eq context
'literal
)
449 muse-latex-markup-specials-literal
)
450 ((eq context
'example
)
451 muse-latex-markup-specials-example
)
452 (t (error "Invalid context '%s' in muse-latex" context
))))
454 (defcustom muse-latex-permit-contents-tag nil
455 "If nil, ignore <contents> tags. Otherwise, insert table of contents.
457 Most of the time, it is best to have a table of contents on the
458 first page, with a new page immediately following. To make this
459 work with documents published in both HTML and LaTeX, we need to
460 ignore the <contents> tag.
462 If you don't agree with this, then set this option to non-nil,
463 and it will do what you expect."
467 (defun muse-latex-markup-table ()
468 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
470 (row-len (car table-info
))
471 (field-list (cdr table-info
)))
473 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l
) "}\n")
474 (dolist (fields field-list
)
475 (let ((type (car fields
)))
476 (setq fields
(cdr fields
))
478 (muse-insert-markup "\\hline\n")
480 (muse-insert-markup "\\hline\n"))
481 (insert (car fields
))
482 (setq fields
(cdr fields
))
483 (dolist (field fields
)
484 (muse-insert-markup " & ")
486 (muse-insert-markup " \\\\\n")
488 (muse-insert-markup "\\hline\n")))))
489 (muse-insert-markup "\\end{tabular}"))))
493 (defun muse-latex-slide-tag (beg end attrs
)
494 "Publish the <slide> tag in LaTeX.
495 This is used by the slides and lecture-notes publishing styles."
496 (let ((title (cdr (assoc "title" attrs
))))
498 (muse-insert-markup "\\begin{frame}[fragile]\n")
500 (muse-insert-markup "\\frametitle{")
502 (muse-insert-markup "}\n"))
505 (muse-insert-markup "\n\\end{frame}"))))
507 ;;; Post-publishing functions
509 (defun muse-latex-fixup-dquotes ()
510 "Fixup double quotes."
511 (goto-char (point-min))
513 (while (search-forward "\"" nil t
)
514 (unless (get-text-property (match-beginning 0) 'read-only
)
516 (eq (char-before) ?
\n))
525 (defun muse-latex-fixup-citations ()
526 "Replace semicolons in multi-head citations with colons."
527 (goto-char (point-min))
528 (while (re-search-forward "\\\\cite.?{" nil t
)
529 (let ((start (point))
530 (end (re-search-forward "}")))
532 (narrow-to-region start end
)
533 (goto-char (point-min))
534 (while (re-search-forward ";" nil t
)
535 (replace-match ","))))))
537 (defun muse-latex-fixup-headings ()
538 "Remove footnotes in headings, since LaTeX does not permit them to exist.
540 This can happen if there is a link in a heading, because by
541 default Muse will add a footnote for each link."
542 (goto-char (point-min))
543 (while (re-search-forward "^\\\\section.?{" nil t
)
545 (narrow-to-region (match-beginning 0) (muse-line-end-position))
546 (goto-char (point-min))
547 (while (re-search-forward "\\\\footnote{[^}\n]+}" nil t
)
551 (defun muse-latex-munge-buffer ()
552 (muse-latex-fixup-dquotes)
553 (muse-latex-fixup-citations)
554 (muse-latex-fixup-headings)
555 (when (and muse-latex-permit-contents-tag
556 muse-publish-generate-contents
)
557 (goto-char (car muse-publish-generate-contents
))
558 (muse-insert-markup "\\tableofcontents")))
560 (defun muse-latex-bibliography ()
562 (goto-char (point-min))
563 (if (re-search-forward "\\\\cite.?{" nil t
)
566 (muse-publishing-directive "bibsource")
570 (defun muse-latex-pdf-browse-file (file)
571 (shell-command (format muse-latex-pdf-browser file
)))
573 (defun muse-latex-pdf-generate (file output-path final-target
)
575 #'muse-publish-transform-output
576 file output-path final-target
"PDF"
578 (lambda (file output-path
)
579 (let* ((fnd (file-name-directory output-path
))
580 (command (format "%s \"%s\""
581 muse-latex-pdf-program
582 (file-relative-name file fnd
)))
584 (default-directory fnd
)
586 ;; XEmacs can sometimes return a non-number result. We'll err
587 ;; on the side of caution by continuing to attempt to generate
588 ;; the PDF if this happens and treat the final result as
590 (while (and (< times
2)
591 (or (not (numberp result
))
593 ;; table of contents takes 2 passes
595 (muse-replace-regexp-in-string
596 "\\.tex\\'" ".toc" file t t
))))
597 (setq result
(shell-command command
)
599 (if (or (not (numberp result
))
603 muse-latex-pdf-cruft
))
605 ;;; Register the Muse LATEX Publishers
607 (muse-define-style "latex"
608 :suffix
'muse-latex-extension
609 :regexps
'muse-latex-markup-regexps
610 :functions
'muse-latex-markup-functions
611 :strings
'muse-latex-markup-strings
612 :specials
'muse-latex-decide-specials
613 :before-end
'muse-latex-munge-buffer
614 :header
'muse-latex-header
615 :footer
'muse-latex-footer
618 (muse-derive-style "pdf" "latex"
619 :final
'muse-latex-pdf-generate
620 :browser
'muse-latex-pdf-browse-file
621 :link-suffix
'muse-latex-pdf-extension
622 :osuffix
'muse-latex-pdf-extension
)
624 (muse-derive-style "latexcjk" "latex"
625 :header
'muse-latexcjk-header
626 :footer
'muse-latexcjk-footer
)
628 (muse-derive-style "pdfcjk" "latexcjk"
629 :final
'muse-latex-pdf-generate
630 :browser
'muse-latex-pdf-browse-file
631 :link-suffix
'muse-latex-pdf-extension
632 :osuffix
'muse-latex-pdf-extension
)
634 (muse-derive-style "slides" "latex"
635 :header
'muse-latex-slides-header
636 :tags
'muse-latex-slides-markup-tags
)
638 (muse-derive-style "slides-pdf" "pdf"
639 :header
'muse-latex-slides-header
640 :tags
'muse-latex-slides-markup-tags
)
642 (muse-derive-style "lecture-notes" "slides"
643 :header
'muse-latex-lecture-notes-header
)
645 (muse-derive-style "lecture-notes-pdf" "slides-pdf"
646 :header
'muse-latex-lecture-notes-header
)
648 (provide 'muse-latex
)
650 ;;; muse-latex.el ends here