Use separate escaping for embedded images.
[muse-el.git] / lisp / muse-latex.el
blobab3d1787ded07b452f6e16d0fedf78a65ee47ec3
1 ;;; muse-latex.el --- publish entries in LaTex or PDF format
3 ;; Copyright (C) 2004, 2005, 2006 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.
22 ;;; Commentary:
24 ;;; Contributors:
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 ;;; Code:
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 ;; Muse LaTeX Publishing
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42 (require 'muse-publish)
44 (defgroup muse-latex nil
45 "Rules for marking up a Muse file as a LaTeX article."
46 :group 'muse-publish)
48 (defcustom muse-latex-extension ".tex"
49 "Default file extension for publishing LaTeX files."
50 :type 'string
51 :group 'muse-latex)
53 (defcustom muse-latex-pdf-extension ".pdf"
54 "Default file extension for publishing LaTeX files to PDF."
55 :type 'string
56 :group 'muse-latex)
58 (defcustom muse-latex-header
59 "\\documentclass{article}
61 \\usepackage[english]{babel}
62 \\usepackage[latin1]{inputenc}
63 \\usepackage[T1]{fontenc}
64 \\usepackage{hyperref}
65 \\usepackage[pdftex]{graphicx}
67 \\newcommand{\\comment}[1]{}
69 \\begin{document}
71 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
72 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
73 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
75 \\maketitle
77 <lisp>(and muse-publish-generate-contents
78 (not muse-latex-permit-contents-tag)
79 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
80 "Header used for publishing LaTeX files. This may be text or a filename."
81 :type 'string
82 :group 'muse-latex)
84 (defcustom muse-latex-footer "\n\\end{document}\n"
85 "Footer used for publishing LaTeX files. This may be text or a filename."
86 :type 'string
87 :group 'muse-latex)
89 (defcustom muse-latexcjk-header
90 "\\documentclass{article}
92 \\usepackage{CJK}
93 \\usepackage{indentfirst}
94 \\usepackage[CJKbookmarks=true]{hyperref}
95 \\usepackage[pdftex]{graphicx}
97 \\newcommand{\\comment}[1]{}
99 \\begin{document}
100 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
102 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
103 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
104 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
106 \\maketitle
108 <lisp>(and muse-publish-generate-contents
109 (not muse-latex-permit-contents-tag)
110 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
111 "Header used for publishing LaTeX files (CJK). This may be text or a
112 filename."
113 :type 'string
114 :group 'muse-latex)
116 (defcustom muse-latexcjk-footer
117 "\n\\end{CJK*}
118 \\end{document}\n"
119 "Footer used for publishing LaTeX files (CJK). This may be text or a
120 filename."
121 :type 'string
122 :group 'muse-latex)
124 (defcustom muse-latex-markup-regexps
125 `(;; numeric ranges
126 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
128 ;; be careful of closing quote pairs
129 (10100 "\"'" 0 "\"\\\\-'"))
130 "List of markup regexps for identifying regions in a Muse page.
131 For more on the structure of this list, see `muse-publish-markup-regexps'."
132 :type '(repeat (choice
133 (list :tag "Markup rule"
134 integer
135 (choice regexp symbol)
136 integer
137 (choice string function symbol))
138 function))
139 :group 'muse-latex)
141 (defcustom muse-latex-markup-functions
142 '((table . muse-latex-markup-table))
143 "An alist of style types to custom functions for that kind of text.
144 For more on the structure of this list, see
145 `muse-publish-markup-functions'."
146 :type '(alist :key-type symbol :value-type function)
147 :group 'muse-latex)
149 (defcustom muse-latex-markup-strings
150 '((image-with-desc . "\\begin{figure}[h]\\centering\\includegraphics[width=0.75\\textwidth]{%s}\\caption{%s}\\end{figure}")
151 (image-link . "\\begin{figure}[h]\\centering\\includegraphics[width=0.75\\textwidth]{%s}\\end{figure}")
152 (url-with-image . "%% %s\n\\includegraphics[width=0.75\\textwidth]{%s}")
153 (anchor-ref . "\\ref{%s}")
154 (url . "\\url{%s}")
155 (url-and-desc . "\\href{%s}{%s}\\footnote{%1%}")
156 (link . "\\href{%s}{%s}\\footnote{%1%}")
157 (link-and-anchor . "\\href{%1%}{%3%}\\footnote{%1%}")
158 (email-addr . "\\verb|%s|")
159 (anchor . "\\label{%s}")
160 (emdash . "---")
161 (comment-begin . "\\comment{")
162 (comment-end . "}")
163 (rule . "\\bigskip")
164 (no-break-space . "~")
165 (enddots . "\\ldots{}")
166 (dots . "\\dots{}")
167 (part . "\\part{")
168 (part-end . "}")
169 (chapter . "\\chapter{")
170 (chapter-end . "}")
171 (section . "\\section{")
172 (section-end . "}")
173 (subsection . "\\subsection{")
174 (subsection-end . "}")
175 (subsubsection . "\\subsubsection{")
176 (subsubsection-end . "}")
177 (section-other . "\\paragraph{")
178 (section-other-end . "}")
179 (footnote . "\\footnote{")
180 (footnote-end . "}")
181 (footnotetext . "\\footnotetext[%d]{")
182 (begin-underline . "\\underline{")
183 (end-underline . "}")
184 (begin-literal . "\\texttt{")
185 (end-literal . "}")
186 (begin-emph . "\\emph{")
187 (end-emph . "}")
188 (begin-more-emph . "\\textbf{")
189 (end-more-emph . "}")
190 (begin-most-emph . "\\textbf{\\emph{")
191 (end-most-emph . "}}")
192 (begin-verse . "\\begin{verse}\n")
193 (end-verse-line . " \\\\")
194 (verse-space . "~~~~")
195 (end-verse . "\n\\end{verse}")
196 (begin-example . "\\begin{quote}\n\\begin{verbatim}")
197 (end-example . "\\end{verbatim}\n\\end{quote}")
198 (begin-center . "\\begin{center}\n")
199 (end-center . "\n\\end{center}")
200 (begin-quote . "\\begin{quote}\n")
201 (end-quote . "\n\\end{quote}")
202 (begin-uli . "\\begin{itemize}\n")
203 (end-uli . "\n\\end{itemize}")
204 (begin-uli-item . "\\item ")
205 (begin-oli . "\\begin{enumerate}\n")
206 (end-oli . "\n\\end{enumerate}")
207 (begin-oli-item . "\\item ")
208 (begin-dl . "\\begin{description}\n")
209 (end-dl . "\n\\end{description}")
210 (begin-ddt . "\\item[")
211 (end-ddt . "] "))
212 "Strings used for marking up text.
213 These cover the most basic kinds of markup, the handling of which
214 differs little between the various styles."
215 :type '(alist :key-type symbol :value-type string)
216 :group 'muse-latex)
218 (defcustom muse-latexcjk-encoding-map
219 '((utf-8 . "{UTF8}{song}")
220 (japanese-iso-8bit . "[dnp]{JIS}{min}")
221 (chinese-big5 . "{Bg5}{bsmi}")
222 (mule-utf-8 . "{UTF8}{song}")
223 (chinese-iso-8bit . "{GB}{song}")
224 (chinese-gbk . "{GBK}{song}"))
225 "An alist mapping emacs coding systems to appropriate CJK codings.
226 Use the base name of the coding system (ie, without the -unix)."
227 :type '(alist :key-type coding-system :value-type string)
228 :group 'muse-latex)
230 (defcustom muse-latexcjk-encoding-default "{GB}{song}"
231 "The default Emacs buffer encoding to use in published files.
232 This will be used if no special characters are found."
233 :type 'string
234 :group 'muse-latex)
236 (defun muse-latexcjk-encoding ()
237 (when (boundp 'buffer-file-coding-system)
238 (muse-latexcjk-transform-content-type buffer-file-coding-system)))
240 (defun muse-latexcjk-transform-content-type (content-type)
241 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
242 system to an associated CJK coding system."
243 (let ((match (and (fboundp 'coding-system-base)
244 (assoc (coding-system-base content-type)
245 muse-latexcjk-encoding-map))))
246 (if match
247 (cdr match)
248 muse-latexcjk-encoding-default)))
250 (defcustom muse-latex-markup-specials-document
251 '((?\\ . "\\textbackslash{}")
252 (?\_ . "\\textunderscore{}")
253 (?\< . "\\textless{}")
254 (?\> . "\\textgreater{}")
255 (?^ . "\\^{}")
256 (?\~ . "\\~{}")
257 (?\@ . "\\@")
258 (?\$ . "\\$")
259 (?\% . "\\%")
260 (?\{ . "\\{")
261 (?\} . "\\}")
262 (?\& . "\\&")
263 (?\# . "\\#"))
264 "A table of characters which must be represented specially.
265 These are applied to the entire document, sans already-escaped
266 regions."
267 :type '(alist :key-type character :value-type string)
268 :group 'muse-latex)
270 (defcustom muse-latex-markup-specials-example
272 "A table of characters which must be represented specially.
273 These are applied to <example> regions.
275 With the default interpretation of <example> regions, no specials
276 need to be escaped."
277 :type '(alist :key-type character :value-type string)
278 :group 'muse-latex)
280 (defcustom muse-latex-markup-specials-literal
281 '((?\n . "\\\n")
282 (?_ . "\\textunderscore{}")
283 (?\< . "\\textless{}")
284 (?\> . "\\textgreater{}")
285 (?^ . "\\^{}")
286 (?\~ . "\\~{}")
287 (?\$ . "\\$")
288 (?\% . "\\%")
289 (?\{ . "\\{")
290 (?\} . "\\}")
291 (?\& . "\\&")
292 (?\# . "\\#"))
293 "A table of characters which must be represented specially.
294 This applies to =monospaced text= and <code> regions."
295 :type '(alist :key-type character :value-type string)
296 :group 'muse-latex)
298 (defcustom muse-latex-markup-specials-url
299 '((?\\ . "\\textbackslash{}")
300 (?\_ . "\\_")
301 (?\< . "\\<")
302 (?\> . "\\>")
303 (?\$ . "\\$")
304 (?\% . "\\%")
305 (?\{ . "\\{")
306 (?\} . "\\}")
307 (?\& . "\\&")
308 (?\# . "\\#"))
309 "A table of characters which must be represented specially.
310 These are applied to URLs."
311 :type '(alist :key-type character :value-type string)
312 :group 'muse-latex)
314 (defcustom muse-latex-markup-specials-image
315 '((?\\ . "\\textbackslash{}") ; cannot find suitable replacement
316 (?\< . "\\<")
317 (?\> . "\\>")
318 (?\$ . "\\$")
319 (?\% . "\\%")
320 (?\{ . "\\{")
321 (?\} . "\\}")
322 (?\& . "\\&")
323 (?\# . "\\#") ; cannot find suitable replacement
325 "A table of characters which must be represented specially.
326 These are applied to image filenames."
327 :type '(alist :key-type character :value-type string)
328 :group 'muse-latex)
330 (defun muse-latex-decide-specials (context)
331 "Determine the specials to escape, depending on CONTEXT."
332 (cond ((memq context '(underline emphasis document url-desc verbatim))
333 muse-latex-markup-specials-document)
334 ((eq context 'image)
335 muse-latex-markup-specials-image)
336 ((memq context '(email url))
337 muse-latex-markup-specials-url)
338 ((eq context 'literal)
339 muse-latex-markup-specials-literal)
340 ((eq context 'example)
341 muse-latex-markup-specials-example)
342 (t (error "Invalid context '%s' in muse-latex" context))))
344 (defun muse-latex-markup-table ()
345 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
346 (match-end 0)))
347 (row-len (car table-info))
348 (field-list (cdr table-info)))
349 (when table-info
350 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l) "}\n")
351 (dolist (fields field-list)
352 (let ((type (car fields)))
353 (setq fields (cdr fields))
354 (when (= type 3)
355 (muse-insert-markup "\\hline\n"))
356 (insert (car fields))
357 (setq fields (cdr fields))
358 (dolist (field fields)
359 (muse-insert-markup " & ")
360 (insert field))
361 (muse-insert-markup " \\\\\n")
362 (when (= type 2)
363 (muse-insert-markup "\\hline\n"))))
364 (muse-insert-markup "\\end{tabular}"))))
366 (defun muse-latex-fixup-dquotes ()
367 "Fixup double quotes."
368 (let ((open t))
369 (while (search-forward "\"" nil t)
370 (unless (get-text-property (match-beginning 0) 'read-only)
371 (when (or (bobp)
372 (eq (char-before) ?\n))
373 (setq open t))
374 (if open
375 (progn
376 (replace-match "``")
377 (setq open nil))
378 (replace-match "''")
379 (setq open t))))))
381 (defcustom muse-latex-permit-contents-tag nil
382 "If nil, ignore <contents> tags. Otherwise, insert table of contents.
384 Most of the time, it is best to have a table of contents on the
385 first page, with a new page immediately following. To make this
386 work with documents published in both HTML and LaTeX, we need to
387 ignore the <contents> tag.
389 If you don't agree with this, then set this option to non-nil,
390 and it will do what you expect."
391 :type 'boolean
392 :group 'muse-latex)
394 (defun muse-latex-finalize-buffer ()
395 (goto-char (point-min))
396 (muse-latex-fixup-dquotes)
397 (when (and muse-latex-permit-contents-tag
398 muse-publish-generate-contents)
399 (goto-char (car muse-publish-generate-contents))
400 (muse-insert-markup "\\tableofcontents")))
402 (defun muse-latex-pdf-browse-file (file)
403 (shell-command (concat "open " file)))
405 (defun muse-latex-pdf-generate (file output-path final-target)
406 (muse-publish-transform-output
407 file output-path final-target "PDF"
408 (function
409 (lambda (file output-path)
410 (let ((command (format "cd \"%s\"; pdflatex \"%s\""
411 (file-name-directory output-path) file))
412 (times 0)
413 result)
414 ;; XEmacs can sometimes return a non-number result. We'll err
415 ;; on the side of caution by continuing to attempt to generate
416 ;; the PDF if this happens and treat the final result as
417 ;; successful.
418 (while (and (< times 2)
419 (or (not (numberp result))
420 (not (eq result 0))
421 ;; table of contents takes 2 passes
422 (file-readable-p
423 (muse-replace-regexp-in-string
424 "\\.tex\\'" ".toc" file t t))))
425 (setq result (shell-command command)
426 times (1+ times)))
427 (if (or (not (numberp result))
428 (eq result 0))
430 nil))))
431 ".aux" ".toc" ".out" ".log"))
433 (unless (assoc "latex" muse-publishing-styles)
434 (muse-define-style "latex"
435 :suffix 'muse-latex-extension
436 :regexps 'muse-latex-markup-regexps
437 :functions 'muse-latex-markup-functions
438 :strings 'muse-latex-markup-strings
439 :specials 'muse-latex-decide-specials
440 :after 'muse-latex-finalize-buffer
441 :header 'muse-latex-header
442 :footer 'muse-latex-footer
443 :browser 'find-file)
445 (muse-derive-style "pdf" "latex"
446 :final 'muse-latex-pdf-generate
447 :browser 'muse-latex-pdf-browse-file
448 :link-suffix 'muse-latex-pdf-extension
449 :osuffix 'muse-latex-pdf-extension)
451 (muse-derive-style "latexcjk" "latex"
452 :header 'muse-latexcjk-header
453 :footer 'muse-latexcjk-footer)
455 (muse-derive-style "pdfcjk" "latexcjk"
456 :final 'muse-latex-pdf-generate
457 :browser 'muse-latex-pdf-browse-file
458 :link-suffix 'muse-latex-pdf-extension
459 :osuffix 'muse-latex-pdf-extension))
461 (provide 'muse-latex)
463 ;;; muse-latex.el ends here