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.
30 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 ;; Muse LaTeX Publishing
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 (require 'muse-publish
)
38 (defgroup muse-latex nil
39 "Rules for marking up a Muse file as a LaTeX article."
42 (defcustom muse-latex-extension
".tex"
43 "Default file extension for publishing LaTeX files."
47 (defcustom muse-latex-pdf-extension
".pdf"
48 "Default file extension for publishing LaTeX files to PDF."
52 (defcustom muse-latex-header
53 "\\documentclass{article}
55 \\usepackage[english]{babel}
56 \\usepackage[latin1]{inputenc}
57 \\usepackage[T1]{fontenc}
58 \\usepackage{hyperref}
59 \\usepackage[pdftex]{graphicx}
63 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
64 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
65 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
69 <lisp>(and muse-publish-generate-contents
70 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
71 "Header used for publishing LaTeX files. This may be text or a filename."
75 (defcustom muse-latex-footer
"\n\\end{document}\n"
76 "Footer used for publishing LaTeX files. This may be text or a filename."
80 (defcustom muse-latexcjk-header
81 "\\documentclass{article}
84 \\usepackage{indentfirst}
85 \\usepackage[CJKbookmarks=true]{hyperref}
86 \\usepackage[pdftex]{graphicx}
89 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
91 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
92 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
93 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
97 <lisp>(and muse-publish-generate-contents
98 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
99 "Header used for publishing LaTeX files (CJK). This may be text or a
104 (defcustom muse-latexcjk-footer
107 "Footer used for publishing LaTeX files (CJK). This may be text or a
112 (defcustom muse-latex-markup-regexps
114 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
116 ;; characters which need quoting
117 (10100 "\\([%$#_]\\)" 0 "\\\\\\1")
119 ;; be careful of closing quote pairs
120 (10200 "\"'" 0 "\"\\\\-'")
122 ;; join together the parts of a list or table
124 "\\\\end{\\(tabular\\|description\\|itemize\\|enumerate\\)}\n+"
125 "\\\\begin{\\1}\\({[^\n}]+}\\)?\n+") 0 ""))
126 "List of markup regexps for identifying regions in a Muse page.
127 For more on the structure of this list, see `muse-publish-markup-regexps'."
128 :type
'(repeat (choice
129 (list :tag
"Markup rule"
131 (choice regexp symbol
)
133 (choice string function symbol
))
137 (defcustom muse-latex-markup-functions
138 '((anchor . muse-latex-markup-anchor
)
139 (table . muse-latex-markup-table
))
140 "An alist of style types to custom functions for that kind of text.
141 For more on the structure of this list, see
142 `muse-publish-markup-functions'."
143 :type
'(alist :key-type symbol
:value-type function
)
146 (defcustom muse-latex-markup-strings
147 '((image-with-desc .
"\\includegraphics[width=\\textwidth]{%s}")
148 (image-link .
"\\includegraphics[width=\\textwidth]{%s}")
149 (url-with-image .
"%% %s\n\\includegraphics[width=\\textwidth]{%s}")
150 (url-link .
"\\href{%s}{%s}")
151 (internal-link .
"\\hyperlink{%s}{%s}")
152 (email-addr .
"\\verb|%s|")
155 (enddots .
"\\ldots{}")
159 (chapter .
"\\chapter{")
161 (section .
"\\section{")
163 (subsection .
"\\subsection{")
164 (subsection-end .
"}")
165 (subsubsection .
"\\subsubsection{")
166 (subsubsection-end .
"}")
167 (section-other .
"\\paragraph{")
168 (section-other-end .
"}")
169 (footnote .
"\\footnote{")
171 (footnotemark .
"\\footnotemark[%d]")
172 (footnotetext .
"\\footnotetext[%d]{")
173 (footnotetext-end .
"}")
174 (begin-underline .
"\\underline{")
175 (end-underline .
"}")
176 (begin-literal .
"\\verb|")
178 (begin-emph .
"\\emph{")
180 (begin-more-emph .
"\\textbf{")
181 (end-more-emph .
"}")
182 (begin-most-emph .
"\\textbf{\\emph{")
183 (end-most-emph .
"}}")
184 (begin-verse .
"\\begin{verse}\n")
185 (end-verse-line .
" \\\\")
186 (verse-space .
"~~~~")
187 (end-verse .
"\n\\end{verse}")
188 (begin-example .
"\\begin{quote}\n\\begin{verbatim}")
189 (end-example .
"\\end{verbatim}\n\\end{quote}")
190 (begin-center .
"\\begin{center}\n")
191 (end-center .
"\n\\end{center}")
192 (begin-quote .
"\\begin{quote}\n")
193 (end-quote .
"\n\\end{quote}")
194 (begin-uli .
"\\begin{itemize}\n\\item ")
195 (end-uli .
"\n\\end{itemize}")
196 (begin-oli .
"\\begin{enumerate}\n\\item ")
197 (end-oli .
"\n\\end{enumerate}")
198 (begin-ddt .
"\\begin{description}\n\\item[")
200 (end-ddt .
"\\end{description}"))
201 "Strings used for marking up text.
202 These cover the most basic kinds of markup, the handling of which
203 differs little between the various styles."
204 :type
'(alist :key-type symbol
:value-type string
)
207 (defcustom muse-latexcjk-encoding-map
208 '((utf-8 .
"{UTF8}{song}")
209 (japanese-iso-8bit .
"[dnp]{JIS}{min}")
210 (chinese-big5 .
"{Bg5}{bsmi}")
211 (mule-utf-8 .
"{UTF8}{song}")
212 (chinese-iso-8bit .
"{GB}{song}")
213 (chinese-gbk .
"{GBK}{song}"))
214 "An alist mapping emacs coding systems to appropriate CJK codings.
215 Use the base name of the coding system (ie, without the -unix)."
216 :type
'(alist :key-type coding-system
:value-type string
)
219 (defcustom muse-latexcjk-encoding-default
"{GB}{song}"
220 "The default Emacs buffer encoding to use in published files.
221 This will be used if no special characters are found."
225 (defun muse-latexcjk-encoding ()
226 (when (boundp 'buffer-file-coding-system
)
227 (muse-latexcjk-transform-content-type buffer-file-coding-system
)))
229 (defun muse-latexcjk-transform-content-type (content-type)
230 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
231 system to an associated CJK coding system."
232 (let ((match (and (fboundp 'coding-system-base
)
233 (assoc (coding-system-base content-type
)
234 muse-latexcjk-encoding-map
))))
237 muse-latexcjk-encoding-default
)))
239 (defcustom muse-latex-markup-specials
241 "A table of characters which must be represented specially."
242 :type
'(alist :key-type character
:value-type string
)
245 (defun muse-latex-insert-anchor (anchor)
246 "Insert an anchor, either around the word at point, or within a tag."
247 (skip-chars-forward muse-regexp-space
)
248 (if (looking-at "<\\([^ />]+\\)>")
249 (let ((tag (match-string 1)))
250 (goto-char (match-end 0))
251 (insert "\\hypertarget{" anchor
"}{")
252 (or (and (search-forward (format "</%s>" tag
)
253 (muse-line-end-position) t
)
254 (goto-char (match-beginning 0)))
257 (insert "\\hypertarget{" anchor
"}{")
261 (defun muse-latex-markup-anchor ()
263 (muse-latex-insert-anchor (match-string 1))) "")
265 (defun muse-latex-markup-table ()
268 (delete-region (match-beginning 0) (match-end 0))))
269 (fields (split-string str
"\\s-*|+\\s-*"))
270 ;; FIXME: `type' isn't used
271 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str
)
272 (length (match-string 1 str
)))))
273 (insert "\\begin{tabular}{" (make-string (length fields
) ?l
) "}\n")
274 (insert (mapconcat 'identity fields
" & "))
275 (insert " \\\\\n\\end{tabular}")))
277 (defun muse-latex-fixup-dquotes ()
278 "Fixup double quotes."
280 (while (search-forward "\"" nil t
)
281 (unless (get-text-property (match-beginning 0) 'read-only
)
282 (if (and (bolp) (eq (char-before) ?
\n))
291 (defun muse-latex-finalize-buffer ()
292 (goto-char (point-min))
293 (muse-latex-fixup-dquotes))
295 (defun muse-latex-pdf-browse-file (file)
296 (shell-command (concat "open " file
)))
298 (defun muse-latex-pdf-generate (file output-path final-target
)
299 (muse-publish-transform-output
300 file output-path final-target
"PDF"
302 (lambda (file output-path
)
303 (let ((command (format "cd %s; pdflatex %s"
304 (file-name-directory output-path
) file
)))
305 (shell-command command
))))
306 ".aux" ".toc" ".out" ".log"))
308 (unless (assoc "latex" muse-publishing-styles
)
309 (muse-define-style "latex"
310 :suffix
'muse-latex-extension
311 :regexps
'muse-latex-markup-regexps
312 :functions
'muse-latex-markup-functions
313 :strings
'muse-latex-markup-strings
314 :specials
'muse-latex-markup-specials
315 :after
'muse-latex-finalize-buffer
316 :header
'muse-latex-header
317 :footer
'muse-latex-footer
320 (muse-derive-style "pdf" "latex"
321 :final
'muse-latex-pdf-generate
322 :browser
'muse-latex-pdf-browse-file
323 :link-suffix
'muse-latex-pdf-extension
324 :osuffix
'muse-latex-pdf-extension
)
326 (muse-derive-style "latexcjk" "latex"
327 :header
'muse-latexcjk-header
328 :footer
'muse-latexcjk-footer
)
330 (muse-derive-style "pdfcjk" "latexcjk"
331 :final
'muse-latex-pdf-generate
332 :browser
'muse-latex-pdf-browse-file
333 :link-suffix
'muse-latex-pdf-extension
334 :osuffix
'muse-latex-pdf-extension
))
336 (provide 'muse-latex
)
338 ;;; muse-latex.el ends here