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 2, 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.
39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 ;; Muse LaTeX Publishing
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 (require 'muse-publish
)
47 (defgroup muse-latex nil
48 "Rules for marking up a Muse file as a LaTeX article."
51 (defcustom muse-latex-extension
".tex"
52 "Default file extension for publishing LaTeX files."
56 (defcustom muse-latex-pdf-extension
".pdf"
57 "Default file extension for publishing LaTeX files to PDF."
61 (defcustom muse-latex-pdf-program
"pdflatex"
62 "The program that is called to generate PDF content from LaTeX content."
66 (defcustom muse-latex-pdf-cruft
'(".aux" ".toc" ".out" ".log")
67 "Extensions of files to remove after generating PDF output successfully."
71 (defcustom muse-latex-header
72 "\\documentclass{article}
74 \\usepackage[english]{babel}
75 \\usepackage[latin1]{inputenc}
76 \\usepackage[T1]{fontenc}
77 \\usepackage{hyperref}
78 \\usepackage[pdftex]{graphicx}
82 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
83 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
84 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
88 <lisp>(and muse-publish-generate-contents
89 (not muse-latex-permit-contents-tag)
90 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
91 "Header used for publishing LaTeX files. This may be text or a filename."
95 (defcustom muse-latex-footer
"\n\\end{document}\n"
96 "Footer used for publishing LaTeX files. This may be text or a filename."
100 (defcustom muse-latexcjk-header
101 "\\documentclass{article}
104 \\usepackage{indentfirst}
105 \\usepackage[CJKbookmarks=true]{hyperref}
106 \\usepackage[pdftex]{graphicx}
109 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
111 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
112 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
113 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
117 <lisp>(and muse-publish-generate-contents
118 (not muse-latex-permit-contents-tag)
119 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
120 "Header used for publishing LaTeX files (CJK). This may be text or a
125 (defcustom muse-latexcjk-footer
128 "Footer used for publishing LaTeX files (CJK). This may be text or a
133 (defcustom muse-latex-markup-regexps
135 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
137 ;; be careful of closing quote pairs
138 (10100 "\"'" 0 "\"\\\\-'"))
139 "List of markup regexps for identifying regions in a Muse page.
140 For more on the structure of this list, see `muse-publish-markup-regexps'."
141 :type
'(repeat (choice
142 (list :tag
"Markup rule"
144 (choice regexp symbol
)
146 (choice string function symbol
))
150 (defcustom muse-latex-markup-functions
151 '((table . muse-latex-markup-table
))
152 "An alist of style types to custom functions for that kind of text.
153 For more on the structure of this list, see
154 `muse-publish-markup-functions'."
155 :type
'(alist :key-type symbol
:value-type function
)
158 (defcustom muse-latex-markup-strings
159 '((image-with-desc .
"\\begin{figure}[h]
160 \\centering\\includegraphics[width=0.75\\textwidth]{%s.%s}
163 (image .
"\\begin{figure}[h]
164 \\centering\\includegraphics[width=0.75\\textwidth]{%s.%s}
167 \\includegraphics[width=0.75\\textwidth]{%s.%s}")
168 (anchor-ref .
"\\ref{%s}")
170 (url-and-desc .
"\\href{%s}{%s}\\footnote{%1%}")
171 (link .
"\\href{%s}{%s}\\footnote{%1%}")
172 (link-and-anchor .
"\\href{%1%}{%3%}\\footnote{%1%}")
173 (email-addr .
"\\verb|%s|")
174 (anchor .
"\\label{%s}")
176 (comment-begin .
"% ")
178 (no-break-space .
"~")
179 (enddots .
"\\ldots{}")
183 (chapter .
"\\chapter{")
185 (section .
"\\section{")
187 (subsection .
"\\subsection{")
188 (subsection-end .
"}")
189 (subsubsection .
"\\subsubsection{")
190 (subsubsection-end .
"}")
191 (section-other .
"\\paragraph{")
192 (section-other-end .
"}")
193 (footnote .
"\\footnote{")
195 (footnotetext .
"\\footnotetext[%d]{")
196 (begin-underline .
"\\underline{")
197 (end-underline .
"}")
198 (begin-literal .
"\\texttt{")
200 (begin-emph .
"\\emph{")
202 (begin-more-emph .
"\\textbf{")
203 (end-more-emph .
"}")
204 (begin-most-emph .
"\\textbf{\\emph{")
205 (end-most-emph .
"}}")
206 (begin-verse .
"\\begin{verse}\n")
207 (end-verse-line .
" \\\\")
208 (verse-space .
"~~~~")
209 (end-verse .
"\n\\end{verse}")
210 (begin-example .
"\\begin{quote}\n\\begin{verbatim}")
211 (end-example .
"\\end{verbatim}\n\\end{quote}")
212 (begin-center .
"\\begin{center}\n")
213 (end-center .
"\n\\end{center}")
214 (begin-quote .
"\\begin{quote}\n")
215 (end-quote .
"\n\\end{quote}")
216 (begin-uli .
"\\begin{itemize}\n")
217 (end-uli .
"\n\\end{itemize}")
218 (begin-uli-item .
"\\item ")
219 (begin-oli .
"\\begin{enumerate}\n")
220 (end-oli .
"\n\\end{enumerate}")
221 (begin-oli-item .
"\\item ")
222 (begin-dl .
"\\begin{description}\n")
223 (end-dl .
"\n\\end{description}")
224 (begin-ddt .
"\\item[")
225 (end-ddt .
"] \\mbox{}\n"))
226 "Strings used for marking up text.
227 These cover the most basic kinds of markup, the handling of which
228 differs little between the various styles."
229 :type
'(alist :key-type symbol
:value-type string
)
232 (defcustom muse-latexcjk-encoding-map
233 '((utf-8 .
"{UTF8}{song}")
234 (japanese-iso-8bit .
"[dnp]{JIS}{min}")
235 (chinese-big5 .
"{Bg5}{bsmi}")
236 (mule-utf-8 .
"{UTF8}{song}")
237 (chinese-iso-8bit .
"{GB}{song}")
238 (chinese-gbk .
"{GBK}{song}"))
239 "An alist mapping emacs coding systems to appropriate CJK codings.
240 Use the base name of the coding system (ie, without the -unix)."
241 :type
'(alist :key-type coding-system
:value-type string
)
244 (defcustom muse-latexcjk-encoding-default
"{GB}{song}"
245 "The default Emacs buffer encoding to use in published files.
246 This will be used if no special characters are found."
250 (defun muse-latexcjk-encoding ()
251 (when (boundp 'buffer-file-coding-system
)
252 (muse-latexcjk-transform-content-type buffer-file-coding-system
)))
254 (defun muse-latexcjk-transform-content-type (content-type)
255 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
256 system to an associated CJK coding system."
257 (let ((match (and (fboundp 'coding-system-base
)
258 (assoc (coding-system-base content-type
)
259 muse-latexcjk-encoding-map
))))
262 muse-latexcjk-encoding-default
)))
264 (defcustom muse-latex-markup-specials-document
265 '((?
\\ .
"\\textbackslash{}")
266 (?\_ .
"\\textunderscore{}")
267 (?\
< .
"\\textless{}")
268 (?\
> .
"\\textgreater{}")
278 "A table of characters which must be represented specially.
279 These are applied to the entire document, sans already-escaped
281 :type
'(alist :key-type character
:value-type string
)
284 (defcustom muse-latex-markup-specials-example
286 "A table of characters which must be represented specially.
287 These are applied to <example> regions.
289 With the default interpretation of <example> regions, no specials
291 :type
'(alist :key-type character
:value-type string
)
294 (defcustom muse-latex-markup-specials-literal
296 (?_ .
"\\textunderscore{}")
297 (?\
< .
"\\textless{}")
298 (?\
> .
"\\textgreater{}")
307 "A table of characters which must be represented specially.
308 This applies to =monospaced text= and <code> regions."
309 :type
'(alist :key-type character
:value-type string
)
312 (defcustom muse-latex-markup-specials-url
313 '((?
\\ .
"\\textbackslash{}")
323 "A table of characters which must be represented specially.
324 These are applied to URLs."
325 :type
'(alist :key-type character
:value-type string
)
328 (defcustom muse-latex-markup-specials-image
329 '((?
\\ .
"\\textbackslash{}") ; cannot find suitable replacement
337 (?\
# .
"\\#") ; cannot find suitable replacement
339 "A table of characters which must be represented specially.
340 These are applied to image filenames."
341 :type
'(alist :key-type character
:value-type string
)
344 (defun muse-latex-decide-specials (context)
345 "Determine the specials to escape, depending on CONTEXT."
346 (cond ((memq context
'(underline emphasis document url-desc verbatim
))
347 muse-latex-markup-specials-document
)
349 muse-latex-markup-specials-image
)
350 ((memq context
'(email url
))
351 muse-latex-markup-specials-url
)
352 ((eq context
'literal
)
353 muse-latex-markup-specials-literal
)
354 ((eq context
'example
)
355 muse-latex-markup-specials-example
)
356 (t (error "Invalid context '%s' in muse-latex" context
))))
358 (defun muse-latex-markup-table ()
359 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
361 (row-len (car table-info
))
362 (field-list (cdr table-info
)))
364 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l
) "}\n")
365 (dolist (fields field-list
)
366 (let ((type (car fields
)))
367 (setq fields
(cdr fields
))
369 (muse-insert-markup "\\hline\n")
371 (muse-insert-markup "\\hline\n"))
372 (insert (car fields
))
373 (setq fields
(cdr fields
))
374 (dolist (field fields
)
375 (muse-insert-markup " & ")
377 (muse-insert-markup " \\\\\n")
379 (muse-insert-markup "\\hline\n")))))
380 (muse-insert-markup "\\end{tabular}"))))
382 (defun muse-latex-fixup-dquotes ()
383 "Fixup double quotes."
384 (goto-char (point-min))
386 (while (search-forward "\"" nil t
)
387 (unless (get-text-property (match-beginning 0) 'read-only
)
389 (eq (char-before) ?
\n))
398 (defcustom muse-latex-permit-contents-tag nil
399 "If nil, ignore <contents> tags. Otherwise, insert table of contents.
401 Most of the time, it is best to have a table of contents on the
402 first page, with a new page immediately following. To make this
403 work with documents published in both HTML and LaTeX, we need to
404 ignore the <contents> tag.
406 If you don't agree with this, then set this option to non-nil,
407 and it will do what you expect."
411 (defun muse-latex-munge-buffer ()
412 (muse-latex-fixup-dquotes)
413 (when (and muse-latex-permit-contents-tag
414 muse-publish-generate-contents
)
415 (goto-char (car muse-publish-generate-contents
))
416 (muse-insert-markup "\\tableofcontents")))
418 (defun muse-latex-pdf-browse-file (file)
419 (shell-command (concat "open " file
)))
421 (defun muse-latex-pdf-generate (file output-path final-target
)
423 #'muse-publish-transform-output
424 file output-path final-target
"PDF"
426 (lambda (file output-path
)
427 (let* ((fnd (file-name-directory output-path
))
428 (command (format "cd \"%s\"; %s \"%s\""
429 fnd muse-latex-pdf-program
430 (file-relative-name file fnd
)))
433 ;; XEmacs can sometimes return a non-number result. We'll err
434 ;; on the side of caution by continuing to attempt to generate
435 ;; the PDF if this happens and treat the final result as
437 (while (and (< times
2)
438 (or (not (numberp result
))
440 ;; table of contents takes 2 passes
442 (muse-replace-regexp-in-string
443 "\\.tex\\'" ".toc" file t t
))))
444 (setq result
(shell-command command
)
446 (if (or (not (numberp result
))
450 muse-latex-pdf-cruft
))
452 ;;; Register the Muse LATEX Publishers
454 (muse-define-style "latex"
455 :suffix
'muse-latex-extension
456 :regexps
'muse-latex-markup-regexps
457 :functions
'muse-latex-markup-functions
458 :strings
'muse-latex-markup-strings
459 :specials
'muse-latex-decide-specials
460 :before-end
'muse-latex-munge-buffer
461 :header
'muse-latex-header
462 :footer
'muse-latex-footer
465 (muse-derive-style "pdf" "latex"
466 :final
'muse-latex-pdf-generate
467 :browser
'muse-latex-pdf-browse-file
468 :link-suffix
'muse-latex-pdf-extension
469 :osuffix
'muse-latex-pdf-extension
)
471 (muse-derive-style "latexcjk" "latex"
472 :header
'muse-latexcjk-header
473 :footer
'muse-latexcjk-footer
)
475 (muse-derive-style "pdfcjk" "latexcjk"
476 :final
'muse-latex-pdf-generate
477 :browser
'muse-latex-pdf-browse-file
478 :link-suffix
'muse-latex-pdf-extension
479 :osuffix
'muse-latex-pdf-extension
)
481 (provide 'muse-latex
)
483 ;;; muse-latex.el ends here