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"))
498 (muse-insert-markup "\n\\end{frame}")))
500 ;;; Post-publishing functions
502 (defun muse-latex-fixup-dquotes ()
503 "Fixup double quotes."
504 (goto-char (point-min))
506 (while (search-forward "\"" nil t
)
507 (unless (get-text-property (match-beginning 0) 'read-only
)
509 (eq (char-before) ?
\n))
518 (defun muse-latex-fixup-citations ()
519 "Replace semicolons in multi-head citations with colons."
520 (goto-char (point-min))
521 (while (re-search-forward "\\\\cite.?{" nil t
)
522 (let ((start (point))
523 (end (re-search-forward "}")))
525 (narrow-to-region start end
)
526 (goto-char (point-min))
527 (while (re-search-forward ";" nil t
)
528 (replace-match ","))))))
530 (defun muse-latex-fixup-headings ()
531 "Remove footnotes in headings, since LaTeX does not permit them to exist.
533 This can happen if there is a link in a heading, because by
534 default Muse will add a footnote for each link."
535 (goto-char (point-min))
536 (while (re-search-forward "^\\\\section.?{" nil t
)
538 (narrow-to-region (match-beginning 0) (muse-line-end-position))
539 (goto-char (point-min))
540 (while (re-search-forward "\\\\footnote{[^}\n]+}" nil t
)
544 (defun muse-latex-munge-buffer ()
545 (muse-latex-fixup-dquotes)
546 (muse-latex-fixup-citations)
547 (muse-latex-fixup-headings)
548 (when (and muse-latex-permit-contents-tag
549 muse-publish-generate-contents
)
550 (goto-char (car muse-publish-generate-contents
))
551 (muse-insert-markup "\\tableofcontents")))
553 (defun muse-latex-bibliography ()
555 (goto-char (point-min))
556 (if (re-search-forward "\\\\cite.?{" nil t
)
559 (muse-publishing-directive "bibsource")
563 (defun muse-latex-pdf-browse-file (file)
564 (shell-command (concat "open " file
)))
566 (defun muse-latex-pdf-generate (file output-path final-target
)
568 #'muse-publish-transform-output
569 file output-path final-target
"PDF"
571 (lambda (file output-path
)
572 (let* ((fnd (file-name-directory output-path
))
573 (command (format "%s \"%s\""
574 muse-latex-pdf-program
575 (file-relative-name file fnd
)))
577 (default-directory fnd
)
579 ;; XEmacs can sometimes return a non-number result. We'll err
580 ;; on the side of caution by continuing to attempt to generate
581 ;; the PDF if this happens and treat the final result as
583 (while (and (< times
2)
584 (or (not (numberp result
))
586 ;; table of contents takes 2 passes
588 (muse-replace-regexp-in-string
589 "\\.tex\\'" ".toc" file t t
))))
590 (setq result
(shell-command command
)
592 (if (or (not (numberp result
))
596 muse-latex-pdf-cruft
))
598 ;;; Register the Muse LATEX Publishers
600 (muse-define-style "latex"
601 :suffix
'muse-latex-extension
602 :regexps
'muse-latex-markup-regexps
603 :functions
'muse-latex-markup-functions
604 :strings
'muse-latex-markup-strings
605 :specials
'muse-latex-decide-specials
606 :before-end
'muse-latex-munge-buffer
607 :header
'muse-latex-header
608 :footer
'muse-latex-footer
611 (muse-derive-style "pdf" "latex"
612 :final
'muse-latex-pdf-generate
613 :browser
'muse-latex-pdf-browse-file
614 :link-suffix
'muse-latex-pdf-extension
615 :osuffix
'muse-latex-pdf-extension
)
617 (muse-derive-style "latexcjk" "latex"
618 :header
'muse-latexcjk-header
619 :footer
'muse-latexcjk-footer
)
621 (muse-derive-style "pdfcjk" "latexcjk"
622 :final
'muse-latex-pdf-generate
623 :browser
'muse-latex-pdf-browse-file
624 :link-suffix
'muse-latex-pdf-extension
625 :osuffix
'muse-latex-pdf-extension
)
627 (muse-derive-style "slides" "latex"
628 :header
'muse-latex-slides-header
629 :tags
'muse-latex-slides-markup-tags
)
631 (muse-derive-style "slides-pdf" "pdf"
632 :header
'muse-latex-slides-header
633 :tags
'muse-latex-slides-markup-tags
)
635 (muse-derive-style "lecture-notes" "slides"
636 :header
'muse-latex-lecture-notes-header
)
638 (muse-derive-style "lecture-notes-pdf" "slides-pdf"
639 :header
'muse-latex-lecture-notes-header
)
641 (provide 'muse-latex
)
643 ;;; muse-latex.el ends here