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 ;; be careful of closing quote pairs
122 (10100 "\"'" 0 "\"\\\\-'")
124 ;; join together the parts of a list or table
126 "\\\\end{\\(tabular\\|description\\|itemize\\|enumerate\\)}"
127 "\\([" muse-regexp-blank
"]*\n\\)\\{0,2\\}"
128 "[" muse-regexp-blank
"]*"
129 "\\\\begin{\\1}\\({[^\n}]+}\\)?\n+") 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"
135 (choice regexp symbol
)
137 (choice string function symbol
))
141 (defcustom muse-latex-markup-functions
142 '((anchor . muse-latex-markup-anchor
)
143 (table . muse-latex-markup-table
))
144 "An alist of style types to custom functions for that kind of text.
145 For more on the structure of this list, see
146 `muse-publish-markup-functions'."
147 :type
'(alist :key-type symbol
:value-type function
)
150 (defcustom muse-latex-markup-strings
151 '((image-with-desc .
"\\includegraphics[width=\\textwidth]{%s}")
152 (image-link .
"\\includegraphics[width=\\textwidth]{%s}")
153 (url-with-image .
"%% %s\n\\includegraphics[width=\\textwidth]{%s}")
154 (url-link .
"\\href{%s}{%s}")
155 (internal-link .
"\\hyperlink{%s}{%s}")
156 (email-addr .
"\\verb|%s|")
158 (comment-begin .
"\\comment{")
161 (enddots .
"\\ldots{}")
165 (chapter .
"\\chapter{")
167 (section .
"\\section{")
169 (subsection .
"\\subsection{")
170 (subsection-end .
"}")
171 (subsubsection .
"\\subsubsection{")
172 (subsubsection-end .
"}")
173 (section-other .
"\\paragraph{")
174 (section-other-end .
"}")
175 (footnote .
"\\footnote{")
177 (footnotemark .
"\\footnotemark[%d]")
178 (footnotetext .
"\\footnotetext[%d]{")
179 (footnotetext-end .
"}")
180 (begin-underline .
"\\underline{")
181 (end-underline .
"}")
182 (begin-literal .
"\\texttt{")
184 (begin-emph .
"\\emph{")
186 (begin-more-emph .
"\\textbf{")
187 (end-more-emph .
"}")
188 (begin-most-emph .
"\\textbf{\\emph{")
189 (end-most-emph .
"}}")
190 (begin-verse .
"\\begin{verse}\n")
191 (end-verse-line .
" \\\\")
192 (verse-space .
"~~~~")
193 (end-verse .
"\n\\end{verse}")
194 (begin-example .
"\\begin{quote}\n\\begin{verbatim}")
195 (end-example .
"\\end{verbatim}\n\\end{quote}")
196 (begin-center .
"\\begin{center}\n")
197 (end-center .
"\n\\end{center}")
198 (begin-quote .
"\\begin{quote}\n")
199 (end-quote .
"\n\\end{quote}")
200 (begin-uli .
"\\begin{itemize}\n\\item ")
201 (end-uli .
"\n\\end{itemize}")
202 (begin-oli .
"\\begin{enumerate}\n\\item ")
203 (end-oli .
"\n\\end{enumerate}")
204 (begin-ddt .
"\\begin{description}\n\\item[")
206 (end-ddt .
"\\end{description}"))
207 "Strings used for marking up text.
208 These cover the most basic kinds of markup, the handling of which
209 differs little between the various styles."
210 :type
'(alist :key-type symbol
:value-type string
)
213 (defcustom muse-latexcjk-encoding-map
214 '((utf-8 .
"{UTF8}{song}")
215 (japanese-iso-8bit .
"[dnp]{JIS}{min}")
216 (chinese-big5 .
"{Bg5}{bsmi}")
217 (mule-utf-8 .
"{UTF8}{song}")
218 (chinese-iso-8bit .
"{GB}{song}")
219 (chinese-gbk .
"{GBK}{song}"))
220 "An alist mapping emacs coding systems to appropriate CJK codings.
221 Use the base name of the coding system (ie, without the -unix)."
222 :type
'(alist :key-type coding-system
:value-type string
)
225 (defcustom muse-latexcjk-encoding-default
"{GB}{song}"
226 "The default Emacs buffer encoding to use in published files.
227 This will be used if no special characters are found."
231 (defun muse-latexcjk-encoding ()
232 (when (boundp 'buffer-file-coding-system
)
233 (muse-latexcjk-transform-content-type buffer-file-coding-system
)))
235 (defun muse-latexcjk-transform-content-type (content-type)
236 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
237 system to an associated CJK coding system."
238 (let ((match (and (fboundp 'coding-system-base
)
239 (assoc (coding-system-base content-type
)
240 muse-latexcjk-encoding-map
))))
243 muse-latexcjk-encoding-default
)))
245 (defcustom muse-latex-markup-specials
247 (?\_ .
"\\textunderscore{}")
248 (?\
< .
"\\textless{}")
249 (?\
> .
"\\textgreater{}")
256 "A table of characters which must be represented specially."
257 :type
'(alist :key-type character
:value-type string
)
260 (defun muse-latex-insert-anchor (anchor)
261 "Insert an anchor, either around the word at point, or within a tag.
262 If the anchor occurs at the end of a line, ignore it."
263 (unless (bolp) ; point is placed after newline if anchor at end
264 (skip-chars-forward muse-regexp-space
)
265 (if (looking-at "<\\([^ />]+\\)>")
266 (let ((tag (match-string 1)))
267 (goto-char (match-end 0))
268 (insert "\\hypertarget{" anchor
"}{")
269 (or (and (search-forward (format "</%s>" tag
)
270 (muse-line-end-position) t
)
271 (goto-char (match-beginning 0)))
274 (insert "\\hypertarget{" anchor
"}{")
278 (defun muse-latex-markup-anchor ()
280 (muse-latex-insert-anchor (match-string 1)))
283 (defun muse-latex-markup-table ()
286 (delete-region (match-beginning 0) (match-end 0))))
287 (fields (split-string str
"\\s-*|+\\s-*"))
288 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str
)
289 (length (match-string 1 str
)))))
290 (insert "\\begin{tabular}{" (make-string (length fields
) ?l
) "}\n")
292 (insert "\\hline\n"))
293 (insert (mapconcat 'identity fields
" & "))
296 (insert "\\hline\n"))
297 (insert "\\end{tabular}")))
299 (defun muse-latex-fixup-dquotes ()
300 "Fixup double quotes."
302 (while (search-forward "\"" nil t
)
303 (unless (get-text-property (match-beginning 0) 'read-only
)
304 (if (and (bolp) (eq (char-before) ?
\n))
313 (defun muse-latex-finalize-buffer ()
314 (goto-char (point-min))
315 (muse-latex-fixup-dquotes))
317 (defun muse-latex-pdf-browse-file (file)
318 (shell-command (concat "open " file
)))
320 (defun muse-latex-pdf-generate (file output-path final-target
)
321 (muse-publish-transform-output
322 file output-path final-target
"PDF"
324 (lambda (file output-path
)
325 (let ((command (format "cd \"%s\"; pdflatex \"%s\""
326 (file-name-directory output-path
) file
))
329 ;; XEmacs can sometimes return a non-number result. We'll err
330 ;; on the side of caution by continuing to attempt to generate
331 ;; the PDF if this happens and treat the final result as
333 (while (and (< times
3)
334 (or (not (numberp result
))
335 (not (eq result
0))))
336 (setq result
(shell-command command
)
338 (if (or (not (numberp result
))
342 ".aux" ".toc" ".out" ".log"))
344 (unless (assoc "latex" muse-publishing-styles
)
345 (muse-define-style "latex"
346 :suffix
'muse-latex-extension
347 :regexps
'muse-latex-markup-regexps
348 :functions
'muse-latex-markup-functions
349 :strings
'muse-latex-markup-strings
350 :specials
'muse-latex-markup-specials
351 :after
'muse-latex-finalize-buffer
352 :header
'muse-latex-header
353 :footer
'muse-latex-footer
356 (muse-derive-style "pdf" "latex"
357 :final
'muse-latex-pdf-generate
358 :browser
'muse-latex-pdf-browse-file
359 :link-suffix
'muse-latex-pdf-extension
360 :osuffix
'muse-latex-pdf-extension
)
362 (muse-derive-style "latexcjk" "latex"
363 :header
'muse-latexcjk-header
364 :footer
'muse-latexcjk-footer
)
366 (muse-derive-style "pdfcjk" "latexcjk"
367 :final
'muse-latex-pdf-generate
368 :browser
'muse-latex-pdf-browse-file
369 :link-suffix
'muse-latex-pdf-extension
370 :osuffix
'muse-latex-pdf-extension
))
372 (provide 'muse-latex
)
374 ;;; muse-latex.el ends here