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
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 ;; Muse LaTeX Publishing
46 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48 (require 'muse-publish
)
50 (defgroup muse-latex nil
51 "Rules for marking up a Muse file as a LaTeX article."
54 (defcustom muse-latex-extension
".tex"
55 "Default file extension for publishing LaTeX files."
59 (defcustom muse-latex-pdf-extension
".pdf"
60 "Default file extension for publishing LaTeX files to PDF."
64 (defcustom muse-latex-pdf-program
"pdflatex"
65 "The program that is called to generate PDF content from LaTeX content."
69 (defcustom muse-latex-pdf-cruft
'(".aux" ".toc" ".out" ".log")
70 "Extensions of files to remove after generating PDF output successfully."
74 (defcustom muse-latex-header
75 "\\documentclass{article}
77 \\usepackage[english]{babel}
78 \\usepackage[latin1]{inputenc}
79 \\usepackage[T1]{fontenc}
80 \\usepackage{hyperref}
81 \\usepackage[pdftex]{graphicx}
85 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
86 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
87 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
91 <lisp>(and muse-publish-generate-contents
92 (not muse-latex-permit-contents-tag)
93 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
94 "Header used for publishing LaTeX files. This may be text or a filename."
98 (defcustom muse-latex-footer
"<lisp>(muse-latex-bibliography)</lisp>
100 "Footer used for publishing LaTeX files. This may be text or a filename."
104 (defcustom muse-latexcjk-header
105 "\\documentclass{article}
108 \\usepackage{indentfirst}
109 \\usepackage[CJKbookmarks=true]{hyperref}
110 \\usepackage[pdftex]{graphicx}
113 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
115 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
116 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
117 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
121 <lisp>(and muse-publish-generate-contents
122 (not muse-latex-permit-contents-tag)
123 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
124 "Header used for publishing LaTeX files (CJK). This may be text or a
129 (defcustom muse-latexcjk-footer
132 "Footer used for publishing LaTeX files (CJK). This may be text or a
137 (defcustom muse-latex-slides-header
138 "\\documentclass[ignorenonframetext]{beamer}
140 \\usepackage[english]{babel}
141 \\usepackage[latin1]{inputenc}
142 \\usepackage[T1]{fontenc}
143 \\usepackage{hyperref}
147 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
148 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
149 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
153 <lisp>(and muse-publish-generate-contents
154 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
155 "Header for publishing of slides using LaTeX.
156 This may be text or a filename.
158 You must have the Beamer extension for LaTeX installed for this to work."
162 (defcustom muse-latex-lecture-notes-header
163 "\\documentclass{article}
164 \\usepackage{beamerarticle}
166 \\usepackage[english]{babel}
167 \\usepackage[latin1]{inputenc}
168 \\usepackage[T1]{fontenc}
169 \\usepackage{hyperref}
173 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
174 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
175 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
179 <lisp>(and muse-publish-generate-contents
180 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
181 "Header for publishing of lecture notes using LaTeX.
182 This may be text or a filename.
184 You must have the Beamer extension for LaTeX installed for this to work."
188 (defcustom muse-latex-markup-regexps
190 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
192 ;; be careful of closing quote pairs
193 (10100 "\"'" 0 "\"\\\\-'"))
194 "List of markup regexps for identifying regions in a Muse page.
195 For more on the structure of this list, see `muse-publish-markup-regexps'."
196 :type
'(repeat (choice
197 (list :tag
"Markup rule"
199 (choice regexp symbol
)
201 (choice string function symbol
))
205 (defcustom muse-latex-markup-functions
206 '((table . muse-latex-markup-table
))
207 "An alist of style types to custom functions for that kind of text.
208 For more on the structure of this list, see
209 `muse-publish-markup-functions'."
210 :type
'(alist :key-type symbol
:value-type function
)
213 (defcustom muse-latex-markup-strings
214 '((image-with-desc .
"\\begin{figure}[h]
215 \\centering\\includegraphics[width=0.75\\textwidth]{%s.%s}
218 (image .
"\\begin{figure}[h]
219 \\centering\\includegraphics[width=0.75\\textwidth]{%s.%s}
222 \\includegraphics[width=0.75\\textwidth]{%s.%s}")
223 (anchor-ref .
"\\ref{%s}")
225 (url-and-desc .
"\\href{%s}{%s}\\footnote{%1%}")
226 (link .
"\\href{%s}{%s}\\footnote{%1%}")
227 (link-and-anchor .
"\\href{%1%}{%3%}\\footnote{%1%}")
228 (email-addr .
"\\verb|%s|")
229 (anchor .
"\\label{%s}")
231 (comment-begin .
"% ")
232 (rule .
"\\vspace{.5cm}\\hrule\\vspace{.5cm}")
233 (no-break-space .
"~")
234 (enddots .
"\\ldots{}")
238 (chapter .
"\\chapter{")
240 (section .
"\\section{")
242 (subsection .
"\\subsection{")
243 (subsection-end .
"}")
244 (subsubsection .
"\\subsubsection{")
245 (subsubsection-end .
"}")
246 (section-other .
"\\paragraph{")
247 (section-other-end .
"}")
248 (footnote .
"\\footnote{")
250 (footnotetext .
"\\footnotetext[%d]{")
251 (begin-underline .
"\\underline{")
252 (end-underline .
"}")
253 (begin-literal .
"\\texttt{")
255 (begin-emph .
"\\emph{")
257 (begin-more-emph .
"\\textbf{")
258 (end-more-emph .
"}")
259 (begin-most-emph .
"\\textbf{\\emph{")
260 (end-most-emph .
"}}")
261 (begin-verse .
"\\begin{verse}\n")
262 (end-verse-line .
" \\\\")
263 (verse-space .
"~~~~")
264 (end-verse .
"\n\\end{verse}")
265 (begin-example .
"\\begin{quote}\n\\begin{verbatim}")
266 (end-example .
"\\end{verbatim}\n\\end{quote}")
267 (begin-center .
"\\begin{center}\n")
268 (end-center .
"\n\\end{center}")
269 (begin-quote .
"\\begin{quote}\n")
270 (end-quote .
"\n\\end{quote}")
271 (begin-cite .
"\\cite{")
272 (begin-cite-author .
"\\citet{")
273 (begin-cite-year .
"\\citet{")
275 (begin-uli .
"\\begin{itemize}\n")
276 (end-uli .
"\n\\end{itemize}")
277 (begin-uli-item .
"\\item ")
278 (begin-oli .
"\\begin{enumerate}\n")
279 (end-oli .
"\n\\end{enumerate}")
280 (begin-oli-item .
"\\item ")
281 (begin-dl .
"\\begin{description}\n")
282 (end-dl .
"\n\\end{description}")
283 (begin-ddt .
"\\item[")
284 (end-ddt .
"] \\mbox{}\n"))
285 "Strings used for marking up text.
286 These cover the most basic kinds of markup, the handling of which
287 differs little between the various styles."
288 :type
'(alist :key-type symbol
:value-type string
)
291 (defcustom muse-latex-slides-markup-tags
292 '(("slide" t t nil muse-latex-slide-tag
))
293 "A list of tag specifications, for specially marking up LaTeX slides."
294 :type
'(repeat (list (string :tag
"Markup tag")
295 (boolean :tag
"Expect closing tag" :value t
)
296 (boolean :tag
"Parse attributes" :value nil
)
297 (boolean :tag
"Nestable" :value nil
)
301 (defcustom muse-latexcjk-encoding-map
302 '((utf-8 .
"{UTF8}{song}")
303 (japanese-iso-8bit .
"[dnp]{JIS}{min}")
304 (chinese-big5 .
"{Bg5}{bsmi}")
305 (mule-utf-8 .
"{UTF8}{song}")
306 (chinese-iso-8bit .
"{GB}{song}")
307 (chinese-gbk .
"{GBK}{song}"))
308 "An alist mapping emacs coding systems to appropriate CJK codings.
309 Use the base name of the coding system (ie, without the -unix)."
310 :type
'(alist :key-type coding-system
:value-type string
)
313 (defcustom muse-latexcjk-encoding-default
"{GB}{song}"
314 "The default Emacs buffer encoding to use in published files.
315 This will be used if no special characters are found."
319 (defun muse-latexcjk-encoding ()
320 (when (boundp 'buffer-file-coding-system
)
321 (muse-latexcjk-transform-content-type buffer-file-coding-system
)))
323 (defun muse-latexcjk-transform-content-type (content-type)
324 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
325 system to an associated CJK coding system."
326 (let ((match (and (fboundp 'coding-system-base
)
327 (assoc (coding-system-base content-type
)
328 muse-latexcjk-encoding-map
))))
331 muse-latexcjk-encoding-default
)))
333 (defcustom muse-latex-markup-specials-document
334 '((?
\\ .
"\\textbackslash{}")
335 (?\_ .
"\\textunderscore{}")
336 (?\
< .
"\\textless{}")
337 (?\
> .
"\\textgreater{}")
347 "A table of characters which must be represented specially.
348 These are applied to the entire document, sans already-escaped
350 :type
'(alist :key-type character
:value-type string
)
353 (defcustom muse-latex-markup-specials-example
355 "A table of characters which must be represented specially.
356 These are applied to <example> regions.
358 With the default interpretation of <example> regions, no specials
360 :type
'(alist :key-type character
:value-type string
)
363 (defcustom muse-latex-markup-specials-literal
365 (?_ .
"\\textunderscore{}")
366 (?\
< .
"\\textless{}")
367 (?\
> .
"\\textgreater{}")
376 "A table of characters which must be represented specially.
377 This applies to =monospaced text= and <code> regions."
378 :type
'(alist :key-type character
:value-type string
)
381 (defcustom muse-latex-markup-specials-url
382 '((?
\\ .
"\\textbackslash{}")
392 "A table of characters which must be represented specially.
393 These are applied to URLs."
394 :type
'(alist :key-type character
:value-type string
)
397 (defcustom muse-latex-markup-specials-image
398 '((?
\\ .
"\\textbackslash{}") ; cannot find suitable replacement
406 (?\
# .
"\\#") ; cannot find suitable replacement
408 "A table of characters which must be represented specially.
409 These are applied to image filenames."
410 :type
'(alist :key-type character
:value-type string
)
413 (defun muse-latex-decide-specials (context)
414 "Determine the specials to escape, depending on CONTEXT."
415 (cond ((memq context
'(underline emphasis document url-desc verbatim
))
416 muse-latex-markup-specials-document
)
418 muse-latex-markup-specials-image
)
419 ((memq context
'(email url
))
420 muse-latex-markup-specials-url
)
421 ((eq context
'literal
)
422 muse-latex-markup-specials-literal
)
423 ((eq context
'example
)
424 muse-latex-markup-specials-example
)
425 (t (error "Invalid context '%s' in muse-latex" context
))))
427 (defcustom muse-latex-permit-contents-tag nil
428 "If nil, ignore <contents> tags. Otherwise, insert table of contents.
430 Most of the time, it is best to have a table of contents on the
431 first page, with a new page immediately following. To make this
432 work with documents published in both HTML and LaTeX, we need to
433 ignore the <contents> tag.
435 If you don't agree with this, then set this option to non-nil,
436 and it will do what you expect."
440 (defun muse-latex-markup-table ()
441 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
443 (row-len (car table-info
))
444 (field-list (cdr table-info
)))
446 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l
) "}\n")
447 (dolist (fields field-list
)
448 (let ((type (car fields
)))
449 (setq fields
(cdr fields
))
451 (muse-insert-markup "\\hline\n")
453 (muse-insert-markup "\\hline\n"))
454 (insert (car fields
))
455 (setq fields
(cdr fields
))
456 (dolist (field fields
)
457 (muse-insert-markup " & ")
459 (muse-insert-markup " \\\\\n")
461 (muse-insert-markup "\\hline\n")))))
462 (muse-insert-markup "\\end{tabular}"))))
466 (defun muse-latex-slide-tag (beg end attrs
)
467 "Publish the <slide> tag in LaTeX.
468 This is used by the slides and lecture-notes publishing styles."
469 (let ((title (cdr (assoc "title" attrs
))))
471 (muse-insert-markup "\\begin{frame}\n")
473 (muse-insert-markup "\\frametitle{")
475 (muse-insert-markup "}\n"))
477 (muse-insert-markup "\n\\end{frame}")))
479 ;;; Post-publishing functions
481 (defun muse-latex-fixup-dquotes ()
482 "Fixup double quotes."
483 (goto-char (point-min))
485 (while (search-forward "\"" nil t
)
486 (unless (get-text-property (match-beginning 0) 'read-only
)
488 (eq (char-before) ?
\n))
497 (defun muse-latex-fixup-citations ()
498 "Replace semicolons in multi-head citations with colons."
499 (goto-char (point-min))
500 (while (re-search-forward "\\\\cite.?{" nil t
)
501 (let ((start (point))
502 (end (re-search-forward "}")))
504 (narrow-to-region start end
)
505 (goto-char (point-min))
506 (while (re-search-forward ";" nil t
)
507 (replace-match ","))))))
509 (defun muse-latex-fixup-headings ()
510 "Remove footnotes in headings, since LaTeX does not permit them to exist.
512 This can happen if there is a link in a heading, because by
513 default Muse will add a footnote for each link."
514 (goto-char (point-min))
515 (while (re-search-forward "^\\\\section.?{" nil t
)
517 (narrow-to-region (match-beginning 0) (muse-line-end-position))
518 (goto-char (point-min))
519 (while (re-search-forward "\\\\footnote{[^}\n]+}" nil t
)
523 (defun muse-latex-munge-buffer ()
524 (muse-latex-fixup-dquotes)
525 (muse-latex-fixup-citations)
526 (muse-latex-fixup-headings)
527 (when (and muse-latex-permit-contents-tag
528 muse-publish-generate-contents
)
529 (goto-char (car muse-publish-generate-contents
))
530 (muse-insert-markup "\\tableofcontents")))
532 (defun muse-latex-bibliography ()
534 (goto-char (point-min))
535 (if (re-search-forward "\\\\cite.?{" nil t
)
538 (muse-publishing-directive "bibsource")
542 (defun muse-latex-pdf-browse-file (file)
543 (shell-command (concat "open " file
)))
545 (defun muse-latex-pdf-generate (file output-path final-target
)
547 #'muse-publish-transform-output
548 file output-path final-target
"PDF"
550 (lambda (file output-path
)
551 (let* ((fnd (file-name-directory output-path
))
552 (command (format "cd \"%s\"; %s \"%s\""
553 fnd muse-latex-pdf-program
554 (file-relative-name file fnd
)))
557 ;; XEmacs can sometimes return a non-number result. We'll err
558 ;; on the side of caution by continuing to attempt to generate
559 ;; the PDF if this happens and treat the final result as
561 (while (and (< times
2)
562 (or (not (numberp result
))
564 ;; table of contents takes 2 passes
566 (muse-replace-regexp-in-string
567 "\\.tex\\'" ".toc" file t t
))))
568 (setq result
(shell-command command
)
570 (if (or (not (numberp result
))
574 muse-latex-pdf-cruft
))
576 ;;; Register the Muse LATEX Publishers
578 (muse-define-style "latex"
579 :suffix
'muse-latex-extension
580 :regexps
'muse-latex-markup-regexps
581 :functions
'muse-latex-markup-functions
582 :strings
'muse-latex-markup-strings
583 :specials
'muse-latex-decide-specials
584 :before-end
'muse-latex-munge-buffer
585 :header
'muse-latex-header
586 :footer
'muse-latex-footer
589 (muse-derive-style "pdf" "latex"
590 :final
'muse-latex-pdf-generate
591 :browser
'muse-latex-pdf-browse-file
592 :link-suffix
'muse-latex-pdf-extension
593 :osuffix
'muse-latex-pdf-extension
)
595 (muse-derive-style "latexcjk" "latex"
596 :header
'muse-latexcjk-header
597 :footer
'muse-latexcjk-footer
)
599 (muse-derive-style "pdfcjk" "latexcjk"
600 :final
'muse-latex-pdf-generate
601 :browser
'muse-latex-pdf-browse-file
602 :link-suffix
'muse-latex-pdf-extension
603 :osuffix
'muse-latex-pdf-extension
)
605 (muse-derive-style "slides" "latex"
606 :header
'muse-latex-slides-header
607 :tags
'muse-latex-slides-markup-tags
)
609 (muse-derive-style "slides-pdf" "pdf"
610 :header
'muse-latex-slides-header
611 :tags
'muse-latex-slides-markup-tags
)
613 (muse-derive-style "lecture-notes" "slides"
614 :header
'muse-latex-lecture-notes-header
)
616 (muse-derive-style "lecture-notes-pdf" "slides-pdf"
617 :header
'muse-latex-lecture-notes-header
)
619 (provide 'muse-latex
)
621 ;;; muse-latex.el ends here