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}
66 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
67 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
68 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
72 <lisp>(and muse-publish-generate-contents
73 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
74 "Header used for publishing LaTeX files. This may be text or a filename."
78 (defcustom muse-latex-footer
"\n\\end{document}\n"
79 "Footer used for publishing LaTeX files. This may be text or a filename."
83 (defcustom muse-latexcjk-header
84 "\\documentclass{article}
87 \\usepackage{indentfirst}
88 \\usepackage[CJKbookmarks=true]{hyperref}
89 \\usepackage[pdftex]{graphicx}
92 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
94 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
95 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
96 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
100 <lisp>(and muse-publish-generate-contents
101 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
102 "Header used for publishing LaTeX files (CJK). This may be text or a
107 (defcustom muse-latexcjk-footer
110 "Footer used for publishing LaTeX files (CJK). This may be text or a
115 (defcustom muse-latex-markup-regexps
117 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
119 ;; be careful of closing quote pairs
120 (10100 "\"'" 0 "\"\\\\-'")
122 ;; join together the parts of a list or table
124 "\\\\end{\\(tabular\\|description\\|itemize\\|enumerate\\)}"
125 "\\([" muse-regexp-blank
"]*\n\\)\\{0,2\\}"
126 "[" muse-regexp-blank
"]*"
127 "\\\\begin{\\1}\\({[^\n}]+}\\)?\n+") 0 ""))
128 "List of markup regexps for identifying regions in a Muse page.
129 For more on the structure of this list, see `muse-publish-markup-regexps'."
130 :type
'(repeat (choice
131 (list :tag
"Markup rule"
133 (choice regexp symbol
)
135 (choice string function symbol
))
139 (defcustom muse-latex-markup-functions
140 '((anchor . muse-latex-markup-anchor
)
141 (table . muse-latex-markup-table
))
142 "An alist of style types to custom functions for that kind of text.
143 For more on the structure of this list, see
144 `muse-publish-markup-functions'."
145 :type
'(alist :key-type symbol
:value-type function
)
148 (defcustom muse-latex-markup-strings
149 '((image-with-desc .
"\\includegraphics[width=\\textwidth]{%s}")
150 (image-link .
"\\includegraphics[width=\\textwidth]{%s}")
151 (url-with-image .
"%% %s\n\\includegraphics[width=\\textwidth]{%s}")
152 (url-link .
"\\href{%s}{%s}")
153 (internal-link .
"\\hyperlink{%s}{%s}")
154 (email-addr .
"\\verb|%s|")
157 (enddots .
"\\ldots{}")
161 (chapter .
"\\chapter{")
163 (section .
"\\section{")
165 (subsection .
"\\subsection{")
166 (subsection-end .
"}")
167 (subsubsection .
"\\subsubsection{")
168 (subsubsection-end .
"}")
169 (section-other .
"\\paragraph{")
170 (section-other-end .
"}")
171 (footnote .
"\\footnote{")
173 (footnotemark .
"\\footnotemark[%d]")
174 (footnotetext .
"\\footnotetext[%d]{")
175 (footnotetext-end .
"}")
176 (begin-underline .
"\\underline{")
177 (end-underline .
"}")
178 (begin-literal .
"\\texttt{")
180 (begin-emph .
"\\emph{")
182 (begin-more-emph .
"\\textbf{")
183 (end-more-emph .
"}")
184 (begin-most-emph .
"\\textbf{\\emph{")
185 (end-most-emph .
"}}")
186 (begin-verse .
"\\begin{verse}\n")
187 (end-verse-line .
" \\\\")
188 (verse-space .
"~~~~")
189 (end-verse .
"\n\\end{verse}")
190 (begin-example .
"\\begin{quote}\n\\begin{verbatim}")
191 (end-example .
"\\end{verbatim}\n\\end{quote}")
192 (begin-center .
"\\begin{center}\n")
193 (end-center .
"\n\\end{center}")
194 (begin-quote .
"\\begin{quote}\n")
195 (end-quote .
"\n\\end{quote}")
196 (begin-uli .
"\\begin{itemize}\n\\item ")
197 (end-uli .
"\n\\end{itemize}")
198 (begin-oli .
"\\begin{enumerate}\n\\item ")
199 (end-oli .
"\n\\end{enumerate}")
200 (begin-ddt .
"\\begin{description}\n\\item[")
202 (end-ddt .
"\\end{description}"))
203 "Strings used for marking up text.
204 These cover the most basic kinds of markup, the handling of which
205 differs little between the various styles."
206 :type
'(alist :key-type symbol
:value-type string
)
209 (defcustom muse-latexcjk-encoding-map
210 '((utf-8 .
"{UTF8}{song}")
211 (japanese-iso-8bit .
"[dnp]{JIS}{min}")
212 (chinese-big5 .
"{Bg5}{bsmi}")
213 (mule-utf-8 .
"{UTF8}{song}")
214 (chinese-iso-8bit .
"{GB}{song}")
215 (chinese-gbk .
"{GBK}{song}"))
216 "An alist mapping emacs coding systems to appropriate CJK codings.
217 Use the base name of the coding system (ie, without the -unix)."
218 :type
'(alist :key-type coding-system
:value-type string
)
221 (defcustom muse-latexcjk-encoding-default
"{GB}{song}"
222 "The default Emacs buffer encoding to use in published files.
223 This will be used if no special characters are found."
227 (defun muse-latexcjk-encoding ()
228 (when (boundp 'buffer-file-coding-system
)
229 (muse-latexcjk-transform-content-type buffer-file-coding-system
)))
231 (defun muse-latexcjk-transform-content-type (content-type)
232 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
233 system to an associated CJK coding system."
234 (let ((match (and (fboundp 'coding-system-base
)
235 (assoc (coding-system-base content-type
)
236 muse-latexcjk-encoding-map
))))
239 muse-latexcjk-encoding-default
)))
241 (defcustom muse-latex-markup-specials
243 (?\_ .
"\\textunderscore{}")
244 (?\
< .
"\\textless{}")
245 (?\
> .
"\\textgreater{}")
252 "A table of characters which must be represented specially."
253 :type
'(alist :key-type character
:value-type string
)
256 (defun muse-latex-insert-anchor (anchor)
257 "Insert an anchor, either around the word at point, or within a tag.
258 If the anchor occurs at the end of a line, ignore it."
259 (unless (bolp) ; point is placed after newline if anchor at end
260 (skip-chars-forward muse-regexp-space
)
261 (if (looking-at "<\\([^ />]+\\)>")
262 (let ((tag (match-string 1)))
263 (goto-char (match-end 0))
264 (insert "\\hypertarget{" anchor
"}{")
265 (or (and (search-forward (format "</%s>" tag
)
266 (muse-line-end-position) t
)
267 (goto-char (match-beginning 0)))
270 (insert "\\hypertarget{" anchor
"}{")
274 (defun muse-latex-markup-anchor ()
276 (muse-latex-insert-anchor (match-string 1)))
279 (defun muse-latex-markup-table ()
282 (delete-region (match-beginning 0) (match-end 0))))
283 (fields (split-string str
"\\s-*|+\\s-*"))
284 ;; FIXME: `type' isn't used
285 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str
)
286 (length (match-string 1 str
)))))
287 (insert "\\begin{tabular}{" (make-string (length fields
) ?l
) "}\n")
288 (insert (mapconcat 'identity fields
" & "))
289 (insert " \\\\\n\\end{tabular}")))
291 (defun muse-latex-fixup-dquotes ()
292 "Fixup double quotes."
294 (while (search-forward "\"" nil t
)
295 (unless (get-text-property (match-beginning 0) 'read-only
)
296 (if (and (bolp) (eq (char-before) ?
\n))
305 (defun muse-latex-finalize-buffer ()
306 (goto-char (point-min))
307 (muse-latex-fixup-dquotes))
309 (defun muse-latex-pdf-browse-file (file)
310 (shell-command (concat "open " file
)))
312 (defun muse-latex-pdf-generate (file output-path final-target
)
313 (muse-publish-transform-output
314 file output-path final-target
"PDF"
316 (lambda (file output-path
)
317 (let ((command (format "cd \"%s\"; pdflatex \"%s\"; pdflatex \"%s\""
318 (file-name-directory output-path
) file file
)))
319 (shell-command command
))))
320 ".aux" ".toc" ".out" ".log"))
322 (unless (assoc "latex" muse-publishing-styles
)
323 (muse-define-style "latex"
324 :suffix
'muse-latex-extension
325 :regexps
'muse-latex-markup-regexps
326 :functions
'muse-latex-markup-functions
327 :strings
'muse-latex-markup-strings
328 :specials
'muse-latex-markup-specials
329 :after
'muse-latex-finalize-buffer
330 :header
'muse-latex-header
331 :footer
'muse-latex-footer
334 (muse-derive-style "pdf" "latex"
335 :final
'muse-latex-pdf-generate
336 :browser
'muse-latex-pdf-browse-file
337 :link-suffix
'muse-latex-pdf-extension
338 :osuffix
'muse-latex-pdf-extension
)
340 (muse-derive-style "latexcjk" "latex"
341 :header
'muse-latexcjk-header
342 :footer
'muse-latexcjk-footer
)
344 (muse-derive-style "pdfcjk" "latexcjk"
345 :final
'muse-latex-pdf-generate
346 :browser
'muse-latex-pdf-browse-file
347 :link-suffix
'muse-latex-pdf-extension
348 :osuffix
'muse-latex-pdf-extension
))
350 (provide 'muse-latex
)
352 ;;; muse-latex.el ends here