1 ;;; muse-latex.el --- publish entries in LaTex or PDF format
3 ;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 ;; This file is not part of GNU Emacs.
7 ;; This is free software; you can redistribute it and/or modify it under
8 ;; the terms of the GNU General Public License as published by the Free
9 ;; Software Foundation; either version 2, or (at your option) any later
12 ;; This is distributed in the hope that it will be useful, but WITHOUT
13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; 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.
33 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 ;; Muse LaTeX Publishing
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 (require 'muse-publish
)
41 (defgroup muse-latex nil
42 "Rules for marking up a Muse file as a LaTeX article."
45 (defcustom muse-latex-extension
".tex"
46 "Default file extension for publishing LaTeX files."
50 (defcustom muse-latex-pdf-extension
".pdf"
51 "Default file extension for publishing LaTeX files to PDF."
55 (defcustom muse-latex-header
56 "\\documentclass{article}
58 \\usepackage[english]{babel}
59 \\usepackage[latin1]{inputenc}
60 \\usepackage[T1]{fontenc}
61 \\usepackage{hyperref}
62 \\usepackage[pdftex]{graphicx}
64 \\newcommand{\\comment}[1]{}
68 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
69 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
70 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
74 <lisp>(and muse-publish-generate-contents
75 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
76 "Header used for publishing LaTeX files. This may be text or a filename."
80 (defcustom muse-latex-footer
"\n\\end{document}\n"
81 "Footer used for publishing LaTeX files. This may be text or a filename."
85 (defcustom muse-latexcjk-header
86 "\\documentclass{article}
89 \\usepackage{indentfirst}
90 \\usepackage[CJKbookmarks=true]{hyperref}
91 \\usepackage[pdftex]{graphicx}
94 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
96 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
97 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
98 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
102 <lisp>(and muse-publish-generate-contents
103 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
104 "Header used for publishing LaTeX files (CJK). This may be text or a
109 (defcustom muse-latexcjk-footer
112 "Footer used for publishing LaTeX files (CJK). This may be text or a
117 (defcustom muse-latex-markup-regexps
119 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
121 ;; characters which need quoting
122 (10010 "\\([$#%]\\)" 0 "\\\\\\1")
123 (10020 "_" 0 "\\\\textunderscore{}")
124 (10030 "<" 0 "\\\\textless{}")
125 (10040 ">" 0 "\\\\textgreater{}")
126 (10050 "\\^" 0 "\\\\^{}")
128 ;; be careful of closing quote pairs
129 (10100 "\"'" 0 "\"\\\\-'")
131 ;; join together the parts of a list or table
133 "\\\\end{\\(tabular\\|description\\|itemize\\|enumerate\\)}"
134 "\\([" muse-regexp-blank
"]*\n\\)\\{0,2\\}"
135 "[" muse-regexp-blank
"]*"
136 "\\\\begin{\\1}\\({[^\n}]+}\\)?\n+") 0 ""))
137 "List of markup regexps for identifying regions in a Muse page.
138 For more on the structure of this list, see `muse-publish-markup-regexps'."
139 :type
'(repeat (choice
140 (list :tag
"Markup rule"
142 (choice regexp symbol
)
144 (choice string function symbol
))
148 (defcustom muse-latex-markup-functions
149 '((anchor . muse-latex-markup-anchor
)
150 (table . muse-latex-markup-table
))
151 "An alist of style types to custom functions for that kind of text.
152 For more on the structure of this list, see
153 `muse-publish-markup-functions'."
154 :type
'(alist :key-type symbol
:value-type function
)
157 (defcustom muse-latex-markup-strings
158 '((image-with-desc .
"\\includegraphics[width=\\textwidth]{%s}")
159 (image-link .
"\\includegraphics[width=\\textwidth]{%s}")
160 (url-with-image .
"%% %s\n\\includegraphics[width=\\textwidth]{%s}")
161 (url-link .
"\\href{%s}{%s}")
162 (internal-link .
"\\hyperlink{%s}{%s}")
163 (email-addr .
"\\verb|%s|")
165 (comment-begin .
"\\comment{")
168 (enddots .
"\\ldots{}")
172 (chapter .
"\\chapter{")
174 (section .
"\\section{")
176 (subsection .
"\\subsection{")
177 (subsection-end .
"}")
178 (subsubsection .
"\\subsubsection{")
179 (subsubsection-end .
"}")
180 (section-other .
"\\paragraph{")
181 (section-other-end .
"}")
182 (footnote .
"\\footnote{")
184 (footnotetext .
"\\footnotetext[%d]{")
185 (begin-underline .
"\\underline{")
186 (end-underline .
"}")
187 (begin-literal .
"\\texttt{")
189 (begin-emph .
"\\emph{")
191 (begin-more-emph .
"\\textbf{")
192 (end-more-emph .
"}")
193 (begin-most-emph .
"\\textbf{\\emph{")
194 (end-most-emph .
"}}")
195 (begin-verse .
"\\begin{verse}\n")
196 (end-verse-line .
" \\\\")
197 (verse-space .
"~~~~")
198 (end-verse .
"\n\\end{verse}")
199 (begin-example .
"\\begin{quote}\n\\begin{verbatim}")
200 (end-example .
"\\end{verbatim}\n\\end{quote}")
201 (begin-center .
"\\begin{center}\n")
202 (end-center .
"\n\\end{center}")
203 (begin-quote .
"\\begin{quote}\n")
204 (end-quote .
"\n\\end{quote}")
205 (begin-uli .
"\\begin{itemize}\n\\item ")
206 (end-uli .
"\n\\end{itemize}")
207 (begin-oli .
"\\begin{enumerate}\n\\item ")
208 (end-oli .
"\n\\end{enumerate}")
209 (begin-ddt .
"\\begin{description}\n\\item[")
211 (end-ddt .
"\\end{description}"))
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
)
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
)
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."
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
))))
248 muse-latexcjk-encoding-default
)))
250 (defcustom muse-latex-markup-specials
252 "A table of characters which must be represented specially."
253 :type
'(alist :key-type character
:value-type string
)
256 (defcustom muse-latex-markup-texttt-specials
258 (?_ .
"\\textunderscore{}")
259 (?\
< .
"\\textless{}")
260 (?\
> .
"\\textgreater{}")
268 "A table of characters which must be represented specially.
269 This applies to text in \\texttt{} regions."
270 :type
'(alist :key-type character
:value-type string
)
273 (defun muse-latex-insert-anchor (anchor)
274 "Insert an anchor, either around the word at point, or within a tag.
275 If the anchor occurs at the end of a line, ignore it."
276 (unless (bolp) ; point is placed after newline if anchor at end
277 (skip-chars-forward muse-regexp-space
)
278 (if (looking-at "<\\([^ />]+\\)>")
279 (let ((tag (match-string 1)))
280 (goto-char (match-end 0))
281 (insert "\\hypertarget{" anchor
"}{")
282 (or (and (search-forward (format "</%s>" tag
)
283 (muse-line-end-position) t
)
284 (goto-char (match-beginning 0)))
287 (insert "\\hypertarget{" anchor
"}{")
291 (defun muse-latex-markup-anchor ()
293 (muse-latex-insert-anchor (match-string 1)))
296 (defun muse-latex-markup-table ()
299 (delete-region (match-beginning 0) (match-end 0))))
300 (fields (split-string str
"\\s-*|+\\s-*"))
301 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str
)
302 (length (match-string 1 str
)))))
303 (insert "\\begin{tabular}{" (make-string (length fields
) ?l
) "}\n")
305 (insert "\\hline\n"))
306 (insert (mapconcat 'identity fields
" & "))
309 (insert "\\hline\n"))
310 (insert "\\end{tabular}")))
312 (defun muse-latex-fixup-dquotes ()
313 "Fixup double quotes."
315 (while (search-forward "\"" nil t
)
316 (unless (get-text-property (match-beginning 0) 'read-only
)
317 (if (and (bolp) (eq (char-before) ?
\n))
326 (defun muse-latex-fixup-texttt ()
327 "Escape extra characters in texttt sections."
328 (let ((inhibit-read-only t
)
330 (while (and (< (point) (point-max))
331 (setq beg
(search-forward "\\texttt{" nil t
)))
332 (goto-char (next-single-property-change (point) 'read-only
))
334 (setq end
(point-marker))
336 (while (< (point) end
)
337 (let ((repl (assoc (char-after) muse-latex-markup-texttt-specials
)))
341 (insert-before-markers (cdr repl
))))))))
343 (defun muse-latex-finalize-buffer ()
344 (goto-char (point-min))
345 (muse-latex-fixup-dquotes)
346 (goto-char (point-min))
347 (muse-latex-fixup-texttt))
349 (defun muse-latex-pdf-browse-file (file)
350 (shell-command (concat "open " file
)))
352 (defun muse-latex-pdf-generate (file output-path final-target
)
353 (muse-publish-transform-output
354 file output-path final-target
"PDF"
356 (lambda (file output-path
)
357 (let ((command (format "cd \"%s\"; pdflatex \"%s\""
358 (file-name-directory output-path
) file
))
361 ;; XEmacs can sometimes return a non-number result. We'll err
362 ;; on the side of caution by continuing to attempt to generate
363 ;; the PDF if this happens and treat the final result as
365 (while (and (< times
3)
366 (or (not (numberp result
))
367 (not (eq result
0))))
368 (setq result
(shell-command command
)
370 (if (or (not (numberp result
))
374 ".aux" ".toc" ".out" ".log"))
376 (unless (assoc "latex" muse-publishing-styles
)
377 (muse-define-style "latex"
378 :suffix
'muse-latex-extension
379 :regexps
'muse-latex-markup-regexps
380 :functions
'muse-latex-markup-functions
381 :strings
'muse-latex-markup-strings
382 :specials
'muse-latex-markup-specials
383 :after
'muse-latex-finalize-buffer
384 :header
'muse-latex-header
385 :footer
'muse-latex-footer
388 (muse-derive-style "pdf" "latex"
389 :final
'muse-latex-pdf-generate
390 :browser
'muse-latex-pdf-browse-file
391 :link-suffix
'muse-latex-pdf-extension
392 :osuffix
'muse-latex-pdf-extension
)
394 (muse-derive-style "latexcjk" "latex"
395 :header
'muse-latexcjk-header
396 :footer
'muse-latexcjk-footer
)
398 (muse-derive-style "pdfcjk" "latexcjk"
399 :final
'muse-latex-pdf-generate
400 :browser
'muse-latex-pdf-browse-file
401 :link-suffix
'muse-latex-pdf-extension
402 :osuffix
'muse-latex-pdf-extension
))
404 (provide 'muse-latex
)
406 ;;; muse-latex.el ends here