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 (?
\\ .
"\\textbackslash{}")
381 (?_ .
"\\textunderscore{}")
382 (?\
< .
"\\textless{}")
383 (?\
> .
"\\textgreater{}")
392 "A table of characters which must be represented specially.
393 This applies to =monospaced text= and <code> regions."
394 :type
'(alist :key-type character
:value-type string
)
397 (defcustom muse-latex-markup-specials-url
398 '((?
\\ .
"\\textbackslash{}")
408 "A table of characters which must be represented specially.
409 These are applied to URLs."
410 :type
'(alist :key-type character
:value-type string
)
413 (defcustom muse-latex-markup-specials-image
424 "A table of characters which must be represented specially.
425 These are applied to image filenames."
426 :type
'(alist :key-type character
:value-type string
)
429 (defun muse-latex-decide-specials (context)
430 "Determine the specials to escape, depending on CONTEXT."
431 (cond ((memq context
'(underline emphasis document url-desc verbatim
433 muse-latex-markup-specials-document
)
435 muse-latex-markup-specials-image
)
436 ((memq context
'(email url
))
437 muse-latex-markup-specials-url
)
438 ((eq context
'literal
)
439 muse-latex-markup-specials-literal
)
440 ((eq context
'example
)
441 muse-latex-markup-specials-example
)
442 (t (error "Invalid context '%s' in muse-latex" context
))))
444 (defcustom muse-latex-permit-contents-tag nil
445 "If nil, ignore <contents> tags. Otherwise, insert table of contents.
447 Most of the time, it is best to have a table of contents on the
448 first page, with a new page immediately following. To make this
449 work with documents published in both HTML and LaTeX, we need to
450 ignore the <contents> tag.
452 If you don't agree with this, then set this option to non-nil,
453 and it will do what you expect."
457 (defun muse-latex-markup-table ()
458 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
460 (row-len (car table-info
))
461 (field-list (cdr table-info
)))
463 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l
) "}\n")
464 (dolist (fields field-list
)
465 (let ((type (car fields
)))
466 (setq fields
(cdr fields
))
468 (muse-insert-markup "\\hline\n")
470 (muse-insert-markup "\\hline\n"))
471 (insert (car fields
))
472 (setq fields
(cdr fields
))
473 (dolist (field fields
)
474 (muse-insert-markup " & ")
476 (muse-insert-markup " \\\\\n")
478 (muse-insert-markup "\\hline\n")))))
479 (muse-insert-markup "\\end{tabular}"))))
483 (defun muse-latex-slide-tag (beg end attrs
)
484 "Publish the <slide> tag in LaTeX.
485 This is used by the slides and lecture-notes publishing styles."
486 (let ((title (cdr (assoc "title" attrs
))))
488 (muse-insert-markup "\\begin{frame}\n")
490 (muse-insert-markup "\\frametitle{")
492 (muse-insert-markup "}\n"))
494 (muse-insert-markup "\n\\end{frame}")))
496 ;;; Post-publishing functions
498 (defun muse-latex-fixup-dquotes ()
499 "Fixup double quotes."
500 (goto-char (point-min))
502 (while (search-forward "\"" nil t
)
503 (unless (get-text-property (match-beginning 0) 'read-only
)
505 (eq (char-before) ?
\n))
514 (defun muse-latex-fixup-citations ()
515 "Replace semicolons in multi-head citations with colons."
516 (goto-char (point-min))
517 (while (re-search-forward "\\\\cite.?{" nil t
)
518 (let ((start (point))
519 (end (re-search-forward "}")))
521 (narrow-to-region start end
)
522 (goto-char (point-min))
523 (while (re-search-forward ";" nil t
)
524 (replace-match ","))))))
526 (defun muse-latex-fixup-headings ()
527 "Remove footnotes in headings, since LaTeX does not permit them to exist.
529 This can happen if there is a link in a heading, because by
530 default Muse will add a footnote for each link."
531 (goto-char (point-min))
532 (while (re-search-forward "^\\\\section.?{" nil t
)
534 (narrow-to-region (match-beginning 0) (muse-line-end-position))
535 (goto-char (point-min))
536 (while (re-search-forward "\\\\footnote{[^}\n]+}" nil t
)
540 (defun muse-latex-munge-buffer ()
541 (muse-latex-fixup-dquotes)
542 (muse-latex-fixup-citations)
543 (muse-latex-fixup-headings)
544 (when (and muse-latex-permit-contents-tag
545 muse-publish-generate-contents
)
546 (goto-char (car muse-publish-generate-contents
))
547 (muse-insert-markup "\\tableofcontents")))
549 (defun muse-latex-bibliography ()
551 (goto-char (point-min))
552 (if (re-search-forward "\\\\cite.?{" nil t
)
555 (muse-publishing-directive "bibsource")
559 (defun muse-latex-pdf-browse-file (file)
560 (shell-command (concat "open " file
)))
562 (defun muse-latex-pdf-generate (file output-path final-target
)
564 #'muse-publish-transform-output
565 file output-path final-target
"PDF"
567 (lambda (file output-path
)
568 (let* ((fnd (file-name-directory output-path
))
569 (command (format "%s \"%s\""
570 muse-latex-pdf-program
571 (file-relative-name file fnd
)))
573 (default-directory fnd
)
575 ;; XEmacs can sometimes return a non-number result. We'll err
576 ;; on the side of caution by continuing to attempt to generate
577 ;; the PDF if this happens and treat the final result as
579 (while (and (< times
2)
580 (or (not (numberp result
))
582 ;; table of contents takes 2 passes
584 (muse-replace-regexp-in-string
585 "\\.tex\\'" ".toc" file t t
))))
586 (setq result
(shell-command command
)
588 (if (or (not (numberp result
))
592 muse-latex-pdf-cruft
))
594 ;;; Register the Muse LATEX Publishers
596 (muse-define-style "latex"
597 :suffix
'muse-latex-extension
598 :regexps
'muse-latex-markup-regexps
599 :functions
'muse-latex-markup-functions
600 :strings
'muse-latex-markup-strings
601 :specials
'muse-latex-decide-specials
602 :before-end
'muse-latex-munge-buffer
603 :header
'muse-latex-header
604 :footer
'muse-latex-footer
607 (muse-derive-style "pdf" "latex"
608 :final
'muse-latex-pdf-generate
609 :browser
'muse-latex-pdf-browse-file
610 :link-suffix
'muse-latex-pdf-extension
611 :osuffix
'muse-latex-pdf-extension
)
613 (muse-derive-style "latexcjk" "latex"
614 :header
'muse-latexcjk-header
615 :footer
'muse-latexcjk-footer
)
617 (muse-derive-style "pdfcjk" "latexcjk"
618 :final
'muse-latex-pdf-generate
619 :browser
'muse-latex-pdf-browse-file
620 :link-suffix
'muse-latex-pdf-extension
621 :osuffix
'muse-latex-pdf-extension
)
623 (muse-derive-style "slides" "latex"
624 :header
'muse-latex-slides-header
625 :tags
'muse-latex-slides-markup-tags
)
627 (muse-derive-style "slides-pdf" "pdf"
628 :header
'muse-latex-slides-header
629 :tags
'muse-latex-slides-markup-tags
)
631 (muse-derive-style "lecture-notes" "slides"
632 :header
'muse-latex-lecture-notes-header
)
634 (muse-derive-style "lecture-notes-pdf" "slides-pdf"
635 :header
'muse-latex-lecture-notes-header
)
637 (provide 'muse-latex
)
639 ;;; muse-latex.el ends here