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.
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-program
"pdflatex"
68 "The program that is called to generate PDF content from LaTeX content."
72 (defcustom muse-latex-pdf-cruft
'(".aux" ".toc" ".out" ".log")
73 "Extensions of files to remove after generating PDF output successfully."
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{%
91 \\includegraphics[width=0.75\\textwidth]
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>}
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."
110 (defcustom muse-latex-footer
"<lisp>(muse-latex-bibliography)</lisp>
112 "Footer used for publishing LaTeX files. This may be text or a filename."
116 (defcustom muse-latexcjk-header
117 "\\documentclass{article}
120 \\usepackage{indentfirst}
121 \\usepackage[CJKbookmarks=true]{hyperref}
122 \\usepackage[pdftex]{graphicx}
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>}
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
142 (defcustom muse-latexcjk-footer
145 "Footer used for publishing LaTeX files (CJK). This may be text or a
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}
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>}
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."
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}
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>}
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."
203 (defcustom muse-latex-markup-regexps
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"
214 (choice regexp symbol
)
216 (choice string function symbol
))
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
)
228 (defcustom muse-latex-markup-strings
229 '((image-with-desc .
"\\begin{figure}[h]
230 \\centering\\museincludegraphics{%s.%s}|endgroup
233 (image .
"\\begin{figure}[h]
234 \\centering\\museincludegraphics{%s.%s}|endgroup
237 \\museincludegraphics{%s.%s}|endgroup")
238 (anchor-ref .
"\\ref{%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}")
246 (comment-begin .
"% ")
247 (rule .
"\\vspace{.5cm}\\hrule\\vspace{.5cm}")
248 (no-break-space .
"~")
249 (enddots .
"\\ldots{}")
253 (chapter .
"\\chapter{")
255 (section .
"\\section{")
257 (subsection .
"\\subsection{")
258 (subsection-end .
"}")
259 (subsubsection .
"\\subsubsection{")
260 (subsubsection-end .
"}")
261 (section-other .
"\\paragraph{")
262 (section-other-end .
"}")
263 (footnote .
"\\footnote{")
265 (footnotetext .
"\\footnotetext[%d]{")
266 (begin-underline .
"\\underline{")
267 (end-underline .
"}")
268 (begin-literal .
"\\texttt{")
270 (begin-emph .
"\\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{")
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
)
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
)
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
)
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."
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
))))
346 muse-latexcjk-encoding-default
)))
348 (defcustom muse-latex-markup-specials-document
349 '((?
\\ .
"\\textbackslash{}")
350 (?\_ .
"\\textunderscore{}")
351 (?\
< .
"\\textless{}")
352 (?\
> .
"\\textgreater{}")
362 "A table of characters which must be represented specially.
363 These are applied to the entire document, sans already-escaped
365 :type
'(alist :key-type character
:value-type string
)
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
375 :type
'(alist :key-type character
:value-type string
)
378 (defcustom muse-latex-markup-specials-literal
380 (?_ .
"\\textunderscore{}")
381 (?\
< .
"\\textless{}")
382 (?\
> .
"\\textgreater{}")
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
)
396 (defcustom muse-latex-markup-specials-url
397 '((?
\\ .
"\\textbackslash{}")
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
)
412 (defcustom muse-latex-markup-specials-image
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
)
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
432 muse-latex-markup-specials-document
)
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."
456 (defun muse-latex-markup-table ()
457 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
459 (row-len (car table-info
))
460 (field-list (cdr 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
))
467 (muse-insert-markup "\\hline\n")
469 (muse-insert-markup "\\hline\n"))
470 (insert (car fields
))
471 (setq fields
(cdr fields
))
472 (dolist (field fields
)
473 (muse-insert-markup " & ")
475 (muse-insert-markup " \\\\\n")
477 (muse-insert-markup "\\hline\n")))))
478 (muse-insert-markup "\\end{tabular}"))))
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
))))
487 (muse-insert-markup "\\begin{frame}\n")
489 (muse-insert-markup "\\frametitle{")
491 (muse-insert-markup "}\n"))
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))
501 (while (search-forward "\"" nil t
)
502 (unless (get-text-property (match-beginning 0) 'read-only
)
504 (eq (char-before) ?
\n))
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 "}")))
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
)
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
)
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 ()
550 (goto-char (point-min))
551 (if (re-search-forward "\\\\cite.?{" nil t
)
554 (muse-publishing-directive "bibsource")
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
)
563 #'muse-publish-transform-output
564 file output-path final-target
"PDF"
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
)))
572 (default-directory fnd
)
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
578 (while (and (< times
2)
579 (or (not (numberp result
))
581 ;; table of contents takes 2 passes
583 (muse-replace-regexp-in-string
584 "\\.tex\\'" ".toc" file t t
))))
585 (setq result
(shell-command command
)
587 (if (or (not (numberp result
))
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
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