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-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}
82 \\usepackage[utf8x]{inputenc}
83 \\usepackage[T1]{fontenc}
84 \\usepackage{hyperref}
85 \\usepackage[pdftex]{graphicx}
87 \\def\\museincludegraphics{%
92 \\includegraphics[width=0.75\\textwidth]
97 \\title{<lisp>(muse-publish-escape-specials-in-string
98 (muse-publishing-directive \"title\") 'document)</lisp>}
99 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
100 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
104 <lisp>(and muse-publish-generate-contents
105 (not muse-latex-permit-contents-tag)
106 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
107 "Header used for publishing LaTeX files. This may be text or a filename."
111 (defcustom muse-latex-footer
"<lisp>(muse-latex-bibliography)</lisp>
113 "Footer used for publishing LaTeX files. This may be text or a filename."
117 (defcustom muse-latexcjk-header
118 "\\documentclass{article}
121 \\usepackage{indentfirst}
122 \\usepackage[CJKbookmarks=true]{hyperref}
123 \\usepackage[pdftex]{graphicx}
126 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
128 \\title{<lisp>(muse-publish-escape-specials-in-string
129 (muse-publishing-directive \"title\") 'document)</lisp>}
130 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
131 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
135 <lisp>(and muse-publish-generate-contents
136 (not muse-latex-permit-contents-tag)
137 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
138 "Header used for publishing LaTeX files (CJK). This may be text or a
143 (defcustom muse-latexcjk-footer
146 "Footer used for publishing LaTeX files (CJK). This may be text or a
151 (defcustom muse-latex-slides-header
152 "\\documentclass[ignorenonframetext]{beamer}
154 \\usepackage[english]{babel}
156 \\usepackage[utf8x]{inputenc}
157 \\usepackage[T1]{fontenc}
158 \\usepackage{hyperref}
162 \\title{<lisp>(muse-publish-escape-specials-in-string
163 (muse-publishing-directive \"title\") 'document)</lisp>}
164 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
165 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
169 <lisp>(and muse-publish-generate-contents
170 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
171 "Header for publishing of slides using LaTeX.
172 This may be text or a filename.
174 You must have the Beamer extension for LaTeX installed for this to work."
178 (defcustom muse-latex-lecture-notes-header
179 "\\documentclass{article}
180 \\usepackage{beamerarticle}
182 \\usepackage[english]{babel}
184 \\usepackage[utf8x]{inputenc}
185 \\usepackage[T1]{fontenc}
186 \\usepackage{hyperref}
190 \\title{<lisp>(muse-publish-escape-specials-in-string
191 (muse-publishing-directive \"title\") 'document)</lisp>}
192 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
193 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
197 <lisp>(and muse-publish-generate-contents
198 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
199 "Header for publishing of lecture notes using LaTeX.
200 This may be text or a filename.
202 You must have the Beamer extension for LaTeX installed for this to work."
206 (defcustom muse-latex-markup-regexps
208 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
210 ;; be careful of closing quote pairs
211 (10100 "\"'" 0 "\"\\\\-'"))
212 "List of markup regexps for identifying regions in a Muse page.
213 For more on the structure of this list, see `muse-publish-markup-regexps'."
214 :type
'(repeat (choice
215 (list :tag
"Markup rule"
217 (choice regexp symbol
)
219 (choice string function symbol
))
223 (defcustom muse-latex-markup-functions
224 '((table . muse-latex-markup-table
))
225 "An alist of style types to custom functions for that kind of text.
226 For more on the structure of this list, see
227 `muse-publish-markup-functions'."
228 :type
'(alist :key-type symbol
:value-type function
)
231 (defcustom muse-latex-markup-strings
232 '((image-with-desc .
"\\begin{figure}[h]
233 \\centering\\museincludegraphics{%s.%s}|endgroup
236 (image .
"\\begin{figure}[h]
237 \\centering\\museincludegraphics{%s.%s}|endgroup
240 \\museincludegraphics{%s.%s}|endgroup")
241 (anchor-ref .
"\\ref{%s}")
243 (url-and-desc .
"\\href{%s}{%s}\\footnote{%1%}")
244 (link .
"\\href{%s}{%s}\\footnote{%1%}")
245 (link-and-anchor .
"\\href{%1%}{%3%}\\footnote{%1%}")
246 (email-addr .
"\\verb|%s|")
247 (anchor .
"\\label{%s}")
249 (comment-begin .
"% ")
250 (rule .
"\\vspace{.5cm}\\hrule\\vspace{.5cm}")
251 (no-break-space .
"~")
252 (line-break .
"\\\\")
253 (enddots .
"\\ldots{}")
257 (chapter .
"\\chapter{")
259 (section .
"\\section{")
261 (subsection .
"\\subsection{")
262 (subsection-end .
"}")
263 (subsubsection .
"\\subsubsection{")
264 (subsubsection-end .
"}")
265 (section-other .
"\\paragraph{")
266 (section-other-end .
"}")
267 (footnote .
"\\footnote{")
269 (footnotetext .
"\\footnotetext[%d]{")
270 (begin-underline .
"\\underline{")
271 (end-underline .
"}")
272 (begin-literal .
"\\texttt{")
274 (begin-emph .
"\\emph{")
276 (begin-more-emph .
"\\textbf{")
277 (end-more-emph .
"}")
278 (begin-most-emph .
"\\textbf{\\emph{")
279 (end-most-emph .
"}}")
280 (begin-verse .
"\\begin{verse}\n")
281 (end-verse-line .
" \\\\")
282 (verse-space .
"~~~~")
283 (end-verse .
"\n\\end{verse}")
284 (begin-example .
"\\begin{quote}\n\\begin{verbatim}")
285 (end-example .
"\\end{verbatim}\n\\end{quote}")
286 (begin-center .
"\\begin{center}\n")
287 (end-center .
"\n\\end{center}")
288 (begin-quote .
"\\begin{quote}\n")
289 (end-quote .
"\n\\end{quote}")
290 (begin-cite .
"\\cite{")
291 (begin-cite-author .
"\\citet{")
292 (begin-cite-year .
"\\citet{")
294 (begin-uli .
"\\begin{itemize}\n")
295 (end-uli .
"\n\\end{itemize}")
296 (begin-uli-item .
"\\item ")
297 (begin-oli .
"\\begin{enumerate}\n")
298 (end-oli .
"\n\\end{enumerate}")
299 (begin-oli-item .
"\\item ")
300 (begin-dl .
"\\begin{description}\n")
301 (end-dl .
"\n\\end{description}")
302 (begin-ddt .
"\\item[")
303 (end-ddt .
"] \\mbox{}\n"))
304 "Strings used for marking up text.
305 These cover the most basic kinds of markup, the handling of which
306 differs little between the various styles."
307 :type
'(alist :key-type symbol
:value-type string
)
310 (defcustom muse-latex-slides-markup-tags
311 '(("slide" t t nil muse-latex-slide-tag
))
312 "A list of tag specifications, for specially marking up LaTeX slides."
313 :type
'(repeat (list (string :tag
"Markup tag")
314 (boolean :tag
"Expect closing tag" :value t
)
315 (boolean :tag
"Parse attributes" :value nil
)
316 (boolean :tag
"Nestable" :value nil
)
320 (defcustom muse-latexcjk-encoding-map
321 '((utf-8 .
"{UTF8}{song}")
322 (japanese-iso-8bit .
"[dnp]{JIS}{min}")
323 (chinese-big5 .
"{Bg5}{bsmi}")
324 (mule-utf-8 .
"{UTF8}{song}")
325 (chinese-iso-8bit .
"{GB}{song}")
326 (chinese-gbk .
"{GBK}{song}"))
327 "An alist mapping emacs coding systems to appropriate CJK codings.
328 Use the base name of the coding system (ie, without the -unix)."
329 :type
'(alist :key-type coding-system
:value-type string
)
332 (defcustom muse-latexcjk-encoding-default
"{GB}{song}"
333 "The default Emacs buffer encoding to use in published files.
334 This will be used if no special characters are found."
338 (defun muse-latexcjk-encoding ()
339 (when (boundp 'buffer-file-coding-system
)
340 (muse-latexcjk-transform-content-type buffer-file-coding-system
)))
342 (defun muse-latexcjk-transform-content-type (content-type)
343 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
344 system to an associated CJK coding system."
345 (let ((match (and (fboundp 'coding-system-base
)
346 (assoc (coding-system-base content-type
)
347 muse-latexcjk-encoding-map
))))
350 muse-latexcjk-encoding-default
)))
352 (defcustom muse-latex-markup-specials-document
353 '((?
\\ .
"\\textbackslash{}")
354 (?\_ .
"\\textunderscore{}")
355 (?\
< .
"\\textless{}")
356 (?\
> .
"\\textgreater{}")
366 "A table of characters which must be represented specially.
367 These are applied to the entire document, sans already-escaped
369 :type
'(alist :key-type character
:value-type string
)
372 (defcustom muse-latex-markup-specials-example
374 "A table of characters which must be represented specially.
375 These are applied to <example> regions.
377 With the default interpretation of <example> regions, no specials
379 :type
'(alist :key-type character
:value-type string
)
382 (defcustom muse-latex-markup-specials-literal
384 (?
\\ .
"\\textbackslash{}")
385 (?_ .
"\\textunderscore{}")
386 (?\
< .
"\\textless{}")
387 (?\
> .
"\\textgreater{}")
396 "A table of characters which must be represented specially.
397 This applies to =monospaced text= and <code> regions."
398 :type
'(alist :key-type character
:value-type string
)
401 (defcustom muse-latex-markup-specials-url
402 '((?
\\ .
"\\textbackslash{}")
412 "A table of characters which must be represented specially.
413 These are applied to URLs."
414 :type
'(alist :key-type character
:value-type string
)
417 (defcustom muse-latex-markup-specials-image
428 "A table of characters which must be represented specially.
429 These are applied to image filenames."
430 :type
'(alist :key-type character
:value-type string
)
433 (defun muse-latex-decide-specials (context)
434 "Determine the specials to escape, depending on CONTEXT."
435 (cond ((memq context
'(underline emphasis document url-desc verbatim
437 muse-latex-markup-specials-document
)
439 muse-latex-markup-specials-image
)
440 ((memq context
'(email url
))
441 muse-latex-markup-specials-url
)
442 ((eq context
'literal
)
443 muse-latex-markup-specials-literal
)
444 ((eq context
'example
)
445 muse-latex-markup-specials-example
)
446 (t (error "Invalid context '%s' in muse-latex" context
))))
448 (defcustom muse-latex-permit-contents-tag nil
449 "If nil, ignore <contents> tags. Otherwise, insert table of contents.
451 Most of the time, it is best to have a table of contents on the
452 first page, with a new page immediately following. To make this
453 work with documents published in both HTML and LaTeX, we need to
454 ignore the <contents> tag.
456 If you don't agree with this, then set this option to non-nil,
457 and it will do what you expect."
461 (defun muse-latex-markup-table ()
462 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
464 (row-len (car table-info
))
465 (field-list (cdr table-info
)))
467 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l
) "}\n")
468 (dolist (fields field-list
)
469 (let ((type (car fields
)))
470 (setq fields
(cdr fields
))
472 (muse-insert-markup "\\hline\n")
474 (muse-insert-markup "\\hline\n"))
475 (insert (car fields
))
476 (setq fields
(cdr fields
))
477 (dolist (field fields
)
478 (muse-insert-markup " & ")
480 (muse-insert-markup " \\\\\n")
482 (muse-insert-markup "\\hline\n")))))
483 (muse-insert-markup "\\end{tabular}"))))
487 (defun muse-latex-slide-tag (beg end attrs
)
488 "Publish the <slide> tag in LaTeX.
489 This is used by the slides and lecture-notes publishing styles."
490 (let ((title (cdr (assoc "title" attrs
))))
492 (muse-insert-markup "\\begin{frame}\n")
494 (muse-insert-markup "\\frametitle{")
496 (muse-insert-markup "}\n"))
499 (muse-insert-markup "\n\\end{frame}"))))
501 ;;; Post-publishing functions
503 (defun muse-latex-fixup-dquotes ()
504 "Fixup double quotes."
505 (goto-char (point-min))
507 (while (search-forward "\"" nil t
)
508 (unless (get-text-property (match-beginning 0) 'read-only
)
510 (eq (char-before) ?
\n))
519 (defun muse-latex-fixup-citations ()
520 "Replace semicolons in multi-head citations with colons."
521 (goto-char (point-min))
522 (while (re-search-forward "\\\\cite.?{" nil t
)
523 (let ((start (point))
524 (end (re-search-forward "}")))
526 (narrow-to-region start end
)
527 (goto-char (point-min))
528 (while (re-search-forward ";" nil t
)
529 (replace-match ","))))))
531 (defun muse-latex-fixup-headings ()
532 "Remove footnotes in headings, since LaTeX does not permit them to exist.
534 This can happen if there is a link in a heading, because by
535 default Muse will add a footnote for each link."
536 (goto-char (point-min))
537 (while (re-search-forward "^\\\\section.?{" nil t
)
539 (narrow-to-region (match-beginning 0) (muse-line-end-position))
540 (goto-char (point-min))
541 (while (re-search-forward "\\\\footnote{[^}\n]+}" nil t
)
545 (defun muse-latex-munge-buffer ()
546 (muse-latex-fixup-dquotes)
547 (muse-latex-fixup-citations)
548 (muse-latex-fixup-headings)
549 (when (and muse-latex-permit-contents-tag
550 muse-publish-generate-contents
)
551 (goto-char (car muse-publish-generate-contents
))
552 (muse-insert-markup "\\tableofcontents")))
554 (defun muse-latex-bibliography ()
556 (goto-char (point-min))
557 (if (re-search-forward "\\\\cite.?{" nil t
)
560 (muse-publishing-directive "bibsource")
564 (defun muse-latex-pdf-browse-file (file)
565 (shell-command (concat "open " file
)))
567 (defun muse-latex-pdf-generate (file output-path final-target
)
569 #'muse-publish-transform-output
570 file output-path final-target
"PDF"
572 (lambda (file output-path
)
573 (let* ((fnd (file-name-directory output-path
))
574 (command (format "%s \"%s\""
575 muse-latex-pdf-program
576 (file-relative-name file fnd
)))
578 (default-directory fnd
)
580 ;; XEmacs can sometimes return a non-number result. We'll err
581 ;; on the side of caution by continuing to attempt to generate
582 ;; the PDF if this happens and treat the final result as
584 (while (and (< times
2)
585 (or (not (numberp result
))
587 ;; table of contents takes 2 passes
589 (muse-replace-regexp-in-string
590 "\\.tex\\'" ".toc" file t t
))))
591 (setq result
(shell-command command
)
593 (if (or (not (numberp result
))
597 muse-latex-pdf-cruft
))
599 ;;; Register the Muse LATEX Publishers
601 (muse-define-style "latex"
602 :suffix
'muse-latex-extension
603 :regexps
'muse-latex-markup-regexps
604 :functions
'muse-latex-markup-functions
605 :strings
'muse-latex-markup-strings
606 :specials
'muse-latex-decide-specials
607 :before-end
'muse-latex-munge-buffer
608 :header
'muse-latex-header
609 :footer
'muse-latex-footer
612 (muse-derive-style "pdf" "latex"
613 :final
'muse-latex-pdf-generate
614 :browser
'muse-latex-pdf-browse-file
615 :link-suffix
'muse-latex-pdf-extension
616 :osuffix
'muse-latex-pdf-extension
)
618 (muse-derive-style "latexcjk" "latex"
619 :header
'muse-latexcjk-header
620 :footer
'muse-latexcjk-footer
)
622 (muse-derive-style "pdfcjk" "latexcjk"
623 :final
'muse-latex-pdf-generate
624 :browser
'muse-latex-pdf-browse-file
625 :link-suffix
'muse-latex-pdf-extension
626 :osuffix
'muse-latex-pdf-extension
)
628 (muse-derive-style "slides" "latex"
629 :header
'muse-latex-slides-header
630 :tags
'muse-latex-slides-markup-tags
)
632 (muse-derive-style "slides-pdf" "pdf"
633 :header
'muse-latex-slides-header
634 :tags
'muse-latex-slides-markup-tags
)
636 (muse-derive-style "lecture-notes" "slides"
637 :header
'muse-latex-lecture-notes-header
)
639 (muse-derive-style "lecture-notes-pdf" "slides-pdf"
640 :header
'muse-latex-lecture-notes-header
)
642 (provide 'muse-latex
)
644 ;;; muse-latex.el ends here