1 ;;; muse-latex.el --- publish entries in LaTex or PDF format
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
8 ;; Emacs Muse is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published
10 ;; by the Free Software Foundation; either version 3, or (at your
11 ;; option) any later version.
13 ;; Emacs Muse is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;; General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with Emacs Muse; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
27 ;; Li Daobing (lidaobing AT gmail DOT com) provided CJK support.
29 ;; Trent Buck (trentbuck AT gmail DOT com) gave valuable advice for
30 ;; how to treat LaTeX specials and the like.
32 ;; Matthias Kegelmann (mathias DOT kegelmann AT sdm DOT de) provided a
33 ;; scenario where we would need to respect the <contents> tag.
35 ;; Jean Magnan de Bornier (jean AT bornier DOT net) provided the
36 ;; markup string for link-and-anchor.
38 ;; Jim Ottaway (j DOT ottaway AT lse DOT ac DOT uk) implemented slides
41 ;; Karl Berry (karl AT freefriends DOT org) suggested how to escape
42 ;; additional special characters in image filenames.
46 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48 ;; Muse LaTeX Publishing
50 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
52 (require 'muse-publish
)
54 (defgroup muse-latex nil
55 "Rules for marking up a Muse file as a LaTeX article."
58 (defcustom muse-latex-extension
".tex"
59 "Default file extension for publishing LaTeX files."
63 (defcustom muse-latex-pdf-extension
".pdf"
64 "Default file extension for publishing LaTeX files to PDF."
68 (defcustom muse-latex-pdf-browser
"open %s"
69 "The program to use when browsing a published PDF file.
70 This should be a format string."
74 (defcustom muse-latex-pdf-program
"pdflatex"
75 "The program that is called to generate PDF content from LaTeX content."
79 (defcustom muse-latex-pdf-cruft
80 '(".aux" ".log" ".nav" ".out" ".snm" ".toc" ".vrb")
81 "Extensions of files to remove after generating PDF output successfully."
85 (defcustom muse-latex-header
86 "\\documentclass{article}
88 \\usepackage[english]{babel}
90 \\usepackage[utf8x]{inputenc}
91 \\usepackage[T1]{fontenc}
92 \\usepackage{hyperref}
93 \\usepackage[pdftex]{graphicx}
95 \\def\\museincludegraphics{%
100 \\includegraphics[width=0.75\\textwidth]
105 \\title{<lisp>(muse-publish-escape-specials-in-string
106 (muse-publishing-directive \"title\") 'document)</lisp>}
107 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
108 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
112 <lisp>(and muse-publish-generate-contents
113 (not muse-latex-permit-contents-tag)
114 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
115 "Header used for publishing LaTeX files. This may be text or a filename."
119 (defcustom muse-latex-footer
"<lisp>(muse-latex-bibliography)</lisp>
121 "Footer used for publishing LaTeX files. This may be text or a filename."
125 (defcustom muse-latexcjk-header
126 "\\documentclass{article}
129 \\usepackage{indentfirst}
130 \\usepackage[CJKbookmarks=true]{hyperref}
131 \\usepackage[pdftex]{graphicx}
134 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
136 \\title{<lisp>(muse-publish-escape-specials-in-string
137 (muse-publishing-directive \"title\") 'document)</lisp>}
138 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
139 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
143 <lisp>(and muse-publish-generate-contents
144 (not muse-latex-permit-contents-tag)
145 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
146 "Header used for publishing LaTeX files (CJK). This may be text or a
151 (defcustom muse-latexcjk-footer
154 "Footer used for publishing LaTeX files (CJK). This may be text or a
159 (defcustom muse-latex-slides-header
160 "\\documentclass[ignorenonframetext]{beamer}
162 \\usepackage[english]{babel}
164 \\usepackage[utf8x]{inputenc}
165 \\usepackage[T1]{fontenc}
166 \\usepackage{hyperref}
168 \\def\\museincludegraphics{%
173 \\includegraphics[width=0.50\\textwidth]
176 \\title{<lisp>(muse-publish-escape-specials-in-string
177 (muse-publishing-directive \"title\") 'document)</lisp>}
178 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
179 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
185 <lisp>(and muse-publish-generate-contents
186 \"\\\\frame{\\\\tableofcontents}\")</lisp>\n\n"
187 "Header for publishing of slides using LaTeX.
188 This may be text or a filename.
190 You must have the Beamer extension for LaTeX installed for this to work."
194 (defcustom muse-latex-lecture-notes-header
195 "\\documentclass{article}
196 \\usepackage{beamerarticle}
198 \\usepackage[english]{babel}
200 \\usepackage[utf8x]{inputenc}
201 \\usepackage[T1]{fontenc}
202 \\usepackage{hyperref}
203 \\usepackage[pdftex]{graphicx}
205 \\def\\museincludegraphics{%
210 \\includegraphics[width=0.50\\textwidth]
213 \\title{<lisp>(muse-publish-escape-specials-in-string
214 (muse-publishing-directive \"title\") 'document)</lisp>}
215 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
216 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
222 <lisp>(and muse-publish-generate-contents
223 \"\\\\frame{\\\\tableofcontents}\")</lisp>\n\n"
224 "Header for publishing of lecture notes using LaTeX.
225 This may be text or a filename.
227 You must have the Beamer extension for LaTeX installed for this to work."
231 (defcustom muse-latex-markup-regexps
233 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
235 ;; be careful of closing quote pairs
236 (10100 "\"'" 0 "\"\\\\-'"))
237 "List of markup regexps for identifying regions in a Muse page.
238 For more on the structure of this list, see `muse-publish-markup-regexps'."
239 :type
'(repeat (choice
240 (list :tag
"Markup rule"
242 (choice regexp symbol
)
244 (choice string function symbol
))
248 (defcustom muse-latex-markup-functions
249 '((table . muse-latex-markup-table
))
250 "An alist of style types to custom functions for that kind of text.
251 For more on the structure of this list, see
252 `muse-publish-markup-functions'."
253 :type
'(alist :key-type symbol
:value-type function
)
256 (defcustom muse-latex-markup-strings
257 '((image-with-desc .
"\\begin{figure}[h]
258 \\centering\\museincludegraphics{%s.%s}|endgroup
261 (image .
"\\begin{figure}[h]
262 \\centering\\museincludegraphics{%s.%s}|endgroup
265 \\museincludegraphics{%s.%s}|endgroup")
266 (anchor-ref .
"\\ref{%s}")
268 (url-and-desc .
"\\href{%s}{%s}\\footnote{%1%}")
269 (link .
"\\href{%s}{%s}\\footnote{%1%}")
270 (link-and-anchor .
"\\href{%1%}{%3%}\\footnote{%1%}")
271 (email-addr .
"\\verb|%s|")
272 (anchor .
"\\label{%s}")
274 (comment-begin .
"% ")
275 (rule .
"\\vspace{.5cm}\\hrule\\vspace{.5cm}")
276 (no-break-space .
"~")
277 (line-break .
"\\\\")
278 (enddots .
"\\ldots{}")
282 (chapter .
"\\chapter{")
284 (section .
"\\section{")
286 (subsection .
"\\subsection{")
287 (subsection-end .
"}")
288 (subsubsection .
"\\subsubsection{")
289 (subsubsection-end .
"}")
290 (section-other .
"\\paragraph{")
291 (section-other-end .
"}")
292 (footnote .
"\\footnote{")
294 (footnotetext .
"\\footnotetext[%d]{")
295 (begin-underline .
"\\underline{")
296 (end-underline .
"}")
297 (begin-literal .
"\\texttt{")
299 (begin-emph .
"\\emph{")
301 (begin-more-emph .
"\\textbf{")
302 (end-more-emph .
"}")
303 (begin-most-emph .
"\\textbf{\\emph{")
304 (end-most-emph .
"}}")
305 (begin-verse .
"\\begin{verse}\n")
306 (end-verse-line .
" \\\\")
307 (verse-space .
"~~~~")
308 (end-verse .
"\n\\end{verse}")
309 (begin-example .
"\\begin{quote}\n\\begin{verbatim}")
310 (end-example .
"\\end{verbatim}\n\\end{quote}")
311 (begin-center .
"\\begin{center}\n")
312 (end-center .
"\n\\end{center}")
313 (begin-quote .
"\\begin{quote}\n")
314 (end-quote .
"\n\\end{quote}")
315 (begin-cite .
"\\cite{")
316 (begin-cite-author .
"\\citet{")
317 (begin-cite-year .
"\\citet{")
319 (begin-uli .
"\\begin{itemize}\n")
320 (end-uli .
"\n\\end{itemize}")
321 (begin-uli-item .
"\\item ")
322 (begin-oli .
"\\begin{enumerate}\n")
323 (end-oli .
"\n\\end{enumerate}")
324 (begin-oli-item .
"\\item ")
325 (begin-dl .
"\\begin{description}\n")
326 (end-dl .
"\n\\end{description}")
327 (begin-ddt .
"\\item[")
328 (end-ddt .
"] \\mbox{}\n"))
329 "Strings used for marking up text.
330 These cover the most basic kinds of markup, the handling of which
331 differs little between the various styles."
332 :type
'(alist :key-type symbol
:value-type string
)
335 (defcustom muse-latex-slides-markup-tags
336 '(("slide" t t nil muse-latex-slide-tag
))
337 "A list of tag specifications, for specially marking up LaTeX slides."
338 :type
'(repeat (list (string :tag
"Markup tag")
339 (boolean :tag
"Expect closing tag" :value t
)
340 (boolean :tag
"Parse attributes" :value nil
)
341 (boolean :tag
"Nestable" :value nil
)
345 (defcustom muse-latexcjk-encoding-map
346 '((utf-8 .
"{UTF8}{song}")
347 (japanese-iso-8bit .
"[dnp]{JIS}{min}")
348 (chinese-big5 .
"{Bg5}{bsmi}")
349 (mule-utf-8 .
"{UTF8}{song}")
350 (chinese-iso-8bit .
"{GB}{song}")
351 (chinese-gbk .
"{GBK}{song}"))
352 "An alist mapping emacs coding systems to appropriate CJK codings.
353 Use the base name of the coding system (ie, without the -unix)."
354 :type
'(alist :key-type coding-system
:value-type string
)
357 (defcustom muse-latexcjk-encoding-default
"{GB}{song}"
358 "The default Emacs buffer encoding to use in published files.
359 This will be used if no special characters are found."
363 (defun muse-latexcjk-encoding ()
364 (when (boundp 'buffer-file-coding-system
)
365 (muse-latexcjk-transform-content-type buffer-file-coding-system
)))
367 (defun muse-latexcjk-transform-content-type (content-type)
368 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
369 system to an associated CJK coding system."
370 (let ((match (and (fboundp 'coding-system-base
)
371 (assoc (coding-system-base content-type
)
372 muse-latexcjk-encoding-map
))))
375 muse-latexcjk-encoding-default
)))
377 (defcustom muse-latex-markup-specials-document
378 '((?
\\ .
"\\textbackslash{}")
379 (?\_ .
"\\textunderscore{}")
380 (?\
< .
"\\textless{}")
381 (?\
> .
"\\textgreater{}")
391 "A table of characters which must be represented specially.
392 These are applied to the entire document, sans already-escaped
394 :type
'(alist :key-type character
:value-type string
)
397 (defcustom muse-latex-markup-specials-example
399 "A table of characters which must be represented specially.
400 These are applied to <example> regions.
402 With the default interpretation of <example> regions, no specials
404 :type
'(alist :key-type character
:value-type string
)
407 (defcustom muse-latex-markup-specials-literal
409 (?
\\ .
"\\textbackslash{}")
410 (?_ .
"\\textunderscore{}")
411 (?\
< .
"\\textless{}")
412 (?\
> .
"\\textgreater{}")
421 "A table of characters which must be represented specially.
422 This applies to =monospaced text= and <code> regions."
423 :type
'(alist :key-type character
:value-type string
)
426 (defcustom muse-latex-markup-specials-url
427 '((?
\\ .
"\\textbackslash{}")
437 "A table of characters which must be represented specially.
438 These are applied to URLs."
439 :type
'(alist :key-type character
:value-type string
)
442 (defcustom muse-latex-markup-specials-image
453 "A table of characters which must be represented specially.
454 These are applied to image filenames."
455 :type
'(alist :key-type character
:value-type string
)
458 (defun muse-latex-decide-specials (context)
459 "Determine the specials to escape, depending on CONTEXT."
460 (cond ((memq context
'(underline emphasis document url-desc verbatim
462 muse-latex-markup-specials-document
)
464 muse-latex-markup-specials-image
)
465 ((memq context
'(email url
))
466 muse-latex-markup-specials-url
)
467 ((eq context
'literal
)
468 muse-latex-markup-specials-literal
)
469 ((eq context
'example
)
470 muse-latex-markup-specials-example
)
471 (t (error "Invalid context '%s' in muse-latex" context
))))
473 (defcustom muse-latex-permit-contents-tag nil
474 "If nil, ignore <contents> tags. Otherwise, insert table of contents.
476 Most of the time, it is best to have a table of contents on the
477 first page, with a new page immediately following. To make this
478 work with documents published in both HTML and LaTeX, we need to
479 ignore the <contents> tag.
481 If you don't agree with this, then set this option to non-nil,
482 and it will do what you expect."
486 (defun muse-latex-markup-table ()
487 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
489 (row-len (car table-info
))
490 (field-list (cdr table-info
)))
492 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l
) "}\n")
493 (dolist (fields field-list
)
494 (let ((type (car fields
)))
495 (setq fields
(cdr fields
))
497 (muse-insert-markup "\\hline\n")
499 (muse-insert-markup "\\hline\n"))
500 (insert (car fields
))
501 (setq fields
(cdr fields
))
502 (dolist (field fields
)
503 (muse-insert-markup " & ")
505 (muse-insert-markup " \\\\\n")
507 (muse-insert-markup "\\hline\n")))))
508 (muse-insert-markup "\\end{tabular}"))))
512 (defun muse-latex-slide-tag (beg end attrs
)
513 "Publish the <slide> tag in LaTeX.
514 This is used by the slides and lecture-notes publishing styles."
515 (let ((title (cdr (assoc "title" attrs
))))
517 (muse-insert-markup "\\begin{frame}[fragile]\n")
519 (muse-insert-markup "\\frametitle{")
521 (muse-insert-markup "}\n"))
524 (muse-insert-markup "\n\\end{frame}"))))
526 ;;; Post-publishing functions
528 (defun muse-latex-fixup-dquotes ()
529 "Fixup double quotes."
530 (goto-char (point-min))
532 (while (search-forward "\"" nil t
)
533 (unless (get-text-property (match-beginning 0) 'read-only
)
535 (eq (char-before) ?
\n))
544 (defun muse-latex-fixup-citations ()
545 "Replace semicolons in multi-head citations with colons."
546 (goto-char (point-min))
547 (while (re-search-forward "\\\\cite.?{" nil t
)
548 (let ((start (point))
549 (end (re-search-forward "}")))
551 (narrow-to-region start end
)
552 (goto-char (point-min))
553 (while (re-search-forward ";" nil t
)
554 (replace-match ","))))))
556 (defun muse-latex-fixup-headings ()
557 "Remove footnotes in headings, since LaTeX does not permit them to exist.
559 This can happen if there is a link in a heading, because by
560 default Muse will add a footnote for each link."
561 (goto-char (point-min))
562 (while (re-search-forward "^\\\\section.?{" nil t
)
564 (narrow-to-region (match-beginning 0) (muse-line-end-position))
565 (goto-char (point-min))
566 (while (re-search-forward "\\\\footnote{[^}\n]+}" nil t
)
570 (defun muse-latex-munge-buffer ()
571 (muse-latex-fixup-dquotes)
572 (muse-latex-fixup-citations)
573 (muse-latex-fixup-headings)
574 (when (and muse-latex-permit-contents-tag
575 muse-publish-generate-contents
)
576 (goto-char (car muse-publish-generate-contents
))
577 (muse-insert-markup "\\tableofcontents")))
579 (defun muse-latex-bibliography ()
581 (goto-char (point-min))
582 (if (re-search-forward "\\\\cite.?{" nil t
)
585 (muse-publishing-directive "bibsource")
589 (defun muse-latex-pdf-browse-file (file)
590 (shell-command (format muse-latex-pdf-browser file
)))
592 (defun muse-latex-pdf-generate (file output-path final-target
)
594 #'muse-publish-transform-output
595 file output-path final-target
"PDF"
597 (lambda (file output-path
)
598 (let* ((fnd (file-name-directory output-path
))
599 (command (format "%s \"%s\""
600 muse-latex-pdf-program
601 (file-relative-name file fnd
)))
603 (default-directory fnd
)
605 ;; XEmacs can sometimes return a non-number result. We'll err
606 ;; on the side of caution by continuing to attempt to generate
607 ;; the PDF if this happens and treat the final result as
609 (while (and (< times
2)
610 (or (not (numberp result
))
612 ;; table of contents takes 2 passes
614 (muse-replace-regexp-in-string
615 "\\.tex\\'" ".toc" file t t
))))
616 (setq result
(shell-command command
)
618 (if (or (not (numberp result
))
622 muse-latex-pdf-cruft
))
624 ;;; Register the Muse LATEX Publishers
626 (muse-define-style "latex"
627 :suffix
'muse-latex-extension
628 :regexps
'muse-latex-markup-regexps
629 :functions
'muse-latex-markup-functions
630 :strings
'muse-latex-markup-strings
631 :specials
'muse-latex-decide-specials
632 :before-end
'muse-latex-munge-buffer
633 :header
'muse-latex-header
634 :footer
'muse-latex-footer
637 (muse-derive-style "pdf" "latex"
638 :final
'muse-latex-pdf-generate
639 :browser
'muse-latex-pdf-browse-file
640 :link-suffix
'muse-latex-pdf-extension
641 :osuffix
'muse-latex-pdf-extension
)
643 (muse-derive-style "latexcjk" "latex"
644 :header
'muse-latexcjk-header
645 :footer
'muse-latexcjk-footer
)
647 (muse-derive-style "pdfcjk" "latexcjk"
648 :final
'muse-latex-pdf-generate
649 :browser
'muse-latex-pdf-browse-file
650 :link-suffix
'muse-latex-pdf-extension
651 :osuffix
'muse-latex-pdf-extension
)
653 (muse-derive-style "slides" "latex"
654 :header
'muse-latex-slides-header
655 :tags
'muse-latex-slides-markup-tags
)
657 (muse-derive-style "slides-pdf" "pdf"
658 :header
'muse-latex-slides-header
659 :tags
'muse-latex-slides-markup-tags
)
661 (muse-derive-style "lecture-notes" "slides"
662 :header
'muse-latex-lecture-notes-header
)
664 (muse-derive-style "lecture-notes-pdf" "slides-pdf"
665 :header
'muse-latex-lecture-notes-header
)
667 (provide 'muse-latex
)
669 ;;; muse-latex.el ends here