latex: Remove footnote references in headers
[muse-el.git] / lisp / muse-latex.el
blob04a42c33a8e3f7dd43ae3d905a889567e21da850
1 ;;; muse-latex.el --- publish entries in LaTex or PDF format
3 ;; Copyright (C) 2004, 2005, 2006, 2007 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 3, 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 ;; Jean Magnan de Bornier (jean AT bornier DOT net) provided the
35 ;; markup string for link-and-anchor.
37 ;; Jim Ottaway (j DOT ottaway AT lse DOT ac DOT uk) implemented slides
38 ;; and lecture notes.
40 ;;; Code:
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 ;; Muse LaTeX Publishing
46 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48 (require 'muse-publish)
50 (defgroup muse-latex nil
51 "Rules for marking up a Muse file as a LaTeX article."
52 :group 'muse-publish)
54 (defcustom muse-latex-extension ".tex"
55 "Default file extension for publishing LaTeX files."
56 :type 'string
57 :group 'muse-latex)
59 (defcustom muse-latex-pdf-extension ".pdf"
60 "Default file extension for publishing LaTeX files to PDF."
61 :type 'string
62 :group 'muse-latex)
64 (defcustom muse-latex-pdf-program "pdflatex"
65 "The program that is called to generate PDF content from LaTeX content."
66 :type 'string
67 :group 'muse-latex)
69 (defcustom muse-latex-pdf-cruft '(".aux" ".toc" ".out" ".log")
70 "Extensions of files to remove after generating PDF output successfully."
71 :type 'string
72 :group 'muse-latex)
74 (defcustom muse-latex-header
75 "\\documentclass{article}
77 \\usepackage[english]{babel}
78 \\usepackage[latin1]{inputenc}
79 \\usepackage[T1]{fontenc}
80 \\usepackage{hyperref}
81 \\usepackage[pdftex]{graphicx}
83 \\begin{document}
85 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
86 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
87 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
89 \\maketitle
91 <lisp>(and muse-publish-generate-contents
92 (not muse-latex-permit-contents-tag)
93 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
94 "Header used for publishing LaTeX files. This may be text or a filename."
95 :type 'string
96 :group 'muse-latex)
98 (defcustom muse-latex-footer "<lisp>(muse-latex-bibliography)</lisp>
99 \\end{document}\n"
100 "Footer used for publishing LaTeX files. This may be text or a filename."
101 :type 'string
102 :group 'muse-latex)
104 (defcustom muse-latexcjk-header
105 "\\documentclass{article}
107 \\usepackage{CJK}
108 \\usepackage{indentfirst}
109 \\usepackage[CJKbookmarks=true]{hyperref}
110 \\usepackage[pdftex]{graphicx}
112 \\begin{document}
113 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
115 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
116 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
117 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
119 \\maketitle
121 <lisp>(and muse-publish-generate-contents
122 (not muse-latex-permit-contents-tag)
123 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
124 "Header used for publishing LaTeX files (CJK). This may be text or a
125 filename."
126 :type 'string
127 :group 'muse-latex)
129 (defcustom muse-latexcjk-footer
130 "\n\\end{CJK*}
131 \\end{document}\n"
132 "Footer used for publishing LaTeX files (CJK). This may be text or a
133 filename."
134 :type 'string
135 :group 'muse-latex)
137 (defcustom muse-latex-slides-header
138 "\\documentclass[ignorenonframetext]{beamer}
140 \\usepackage[english]{babel}
141 \\usepackage[latin1]{inputenc}
142 \\usepackage[T1]{fontenc}
143 \\usepackage{hyperref}
145 \\begin{document}
147 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
148 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
149 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
151 \\maketitle
153 <lisp>(and muse-publish-generate-contents
154 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
155 "Header for publishing of slides using LaTeX.
156 This may be text or a filename.
158 You must have the Beamer extension for LaTeX installed for this to work."
159 :type 'string
160 :group 'muse-latex)
162 (defcustom muse-latex-lecture-notes-header
163 "\\documentclass{article}
164 \\usepackage{beamerarticle}
166 \\usepackage[english]{babel}
167 \\usepackage[latin1]{inputenc}
168 \\usepackage[T1]{fontenc}
169 \\usepackage{hyperref}
171 \\begin{document}
173 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
174 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
175 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
177 \\maketitle
179 <lisp>(and muse-publish-generate-contents
180 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
181 "Header for publishing of lecture notes using LaTeX.
182 This may be text or a filename.
184 You must have the Beamer extension for LaTeX installed for this to work."
185 :type 'string
186 :group 'muse-latex)
188 (defcustom muse-latex-markup-regexps
189 `(;; numeric ranges
190 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
192 ;; be careful of closing quote pairs
193 (10100 "\"'" 0 "\"\\\\-'"))
194 "List of markup regexps for identifying regions in a Muse page.
195 For more on the structure of this list, see `muse-publish-markup-regexps'."
196 :type '(repeat (choice
197 (list :tag "Markup rule"
198 integer
199 (choice regexp symbol)
200 integer
201 (choice string function symbol))
202 function))
203 :group 'muse-latex)
205 (defcustom muse-latex-markup-functions
206 '((table . muse-latex-markup-table))
207 "An alist of style types to custom functions for that kind of text.
208 For more on the structure of this list, see
209 `muse-publish-markup-functions'."
210 :type '(alist :key-type symbol :value-type function)
211 :group 'muse-latex)
213 (defcustom muse-latex-markup-strings
214 '((image-with-desc . "\\begin{figure}[h]
215 \\centering\\includegraphics[width=0.75\\textwidth]{%s.%s}
216 \\caption{%s}
217 \\end{figure}")
218 (image . "\\begin{figure}[h]
219 \\centering\\includegraphics[width=0.75\\textwidth]{%s.%s}
220 \\end{figure}")
221 (image-link . "%% %s
222 \\includegraphics[width=0.75\\textwidth]{%s.%s}")
223 (anchor-ref . "\\ref{%s}")
224 (url . "\\url{%s}")
225 (url-and-desc . "\\href{%s}{%s}\\footnote{%1%}")
226 (link . "\\href{%s}{%s}\\footnote{%1%}")
227 (link-and-anchor . "\\href{%1%}{%3%}\\footnote{%1%}")
228 (email-addr . "\\verb|%s|")
229 (anchor . "\\label{%s}")
230 (emdash . "---")
231 (comment-begin . "% ")
232 (rule . "\\vspace{.5cm}\\hrule\\vspace{.5cm}")
233 (no-break-space . "~")
234 (enddots . "\\ldots{}")
235 (dots . "\\dots{}")
236 (part . "\\part{")
237 (part-end . "}")
238 (chapter . "\\chapter{")
239 (chapter-end . "}")
240 (section . "\\section{")
241 (section-end . "}")
242 (subsection . "\\subsection{")
243 (subsection-end . "}")
244 (subsubsection . "\\subsubsection{")
245 (subsubsection-end . "}")
246 (section-other . "\\paragraph{")
247 (section-other-end . "}")
248 (footnote . "\\footnote{")
249 (footnote-end . "}")
250 (footnotetext . "\\footnotetext[%d]{")
251 (begin-underline . "\\underline{")
252 (end-underline . "}")
253 (begin-literal . "\\texttt{")
254 (end-literal . "}")
255 (begin-emph . "\\emph{")
256 (end-emph . "}")
257 (begin-more-emph . "\\textbf{")
258 (end-more-emph . "}")
259 (begin-most-emph . "\\textbf{\\emph{")
260 (end-most-emph . "}}")
261 (begin-verse . "\\begin{verse}\n")
262 (end-verse-line . " \\\\")
263 (verse-space . "~~~~")
264 (end-verse . "\n\\end{verse}")
265 (begin-example . "\\begin{quote}\n\\begin{verbatim}")
266 (end-example . "\\end{verbatim}\n\\end{quote}")
267 (begin-center . "\\begin{center}\n")
268 (end-center . "\n\\end{center}")
269 (begin-quote . "\\begin{quote}\n")
270 (end-quote . "\n\\end{quote}")
271 (begin-cite . "\\cite{")
272 (begin-cite-author . "\\citet{")
273 (begin-cite-year . "\\citet{")
274 (end-cite . "}")
275 (begin-uli . "\\begin{itemize}\n")
276 (end-uli . "\n\\end{itemize}")
277 (begin-uli-item . "\\item ")
278 (begin-oli . "\\begin{enumerate}\n")
279 (end-oli . "\n\\end{enumerate}")
280 (begin-oli-item . "\\item ")
281 (begin-dl . "\\begin{description}\n")
282 (end-dl . "\n\\end{description}")
283 (begin-ddt . "\\item[")
284 (end-ddt . "] \\mbox{}\n"))
285 "Strings used for marking up text.
286 These cover the most basic kinds of markup, the handling of which
287 differs little between the various styles."
288 :type '(alist :key-type symbol :value-type string)
289 :group 'muse-latex)
291 (defcustom muse-latex-slides-markup-tags
292 '(("slide" t t nil muse-latex-slide-tag))
293 "A list of tag specifications, for specially marking up LaTeX slides."
294 :type '(repeat (list (string :tag "Markup tag")
295 (boolean :tag "Expect closing tag" :value t)
296 (boolean :tag "Parse attributes" :value nil)
297 (boolean :tag "Nestable" :value nil)
298 function))
299 :group 'muse-latex)
301 (defcustom muse-latexcjk-encoding-map
302 '((utf-8 . "{UTF8}{song}")
303 (japanese-iso-8bit . "[dnp]{JIS}{min}")
304 (chinese-big5 . "{Bg5}{bsmi}")
305 (mule-utf-8 . "{UTF8}{song}")
306 (chinese-iso-8bit . "{GB}{song}")
307 (chinese-gbk . "{GBK}{song}"))
308 "An alist mapping emacs coding systems to appropriate CJK codings.
309 Use the base name of the coding system (ie, without the -unix)."
310 :type '(alist :key-type coding-system :value-type string)
311 :group 'muse-latex)
313 (defcustom muse-latexcjk-encoding-default "{GB}{song}"
314 "The default Emacs buffer encoding to use in published files.
315 This will be used if no special characters are found."
316 :type 'string
317 :group 'muse-latex)
319 (defun muse-latexcjk-encoding ()
320 (when (boundp 'buffer-file-coding-system)
321 (muse-latexcjk-transform-content-type buffer-file-coding-system)))
323 (defun muse-latexcjk-transform-content-type (content-type)
324 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
325 system to an associated CJK coding system."
326 (let ((match (and (fboundp 'coding-system-base)
327 (assoc (coding-system-base content-type)
328 muse-latexcjk-encoding-map))))
329 (if match
330 (cdr match)
331 muse-latexcjk-encoding-default)))
333 (defcustom muse-latex-markup-specials-document
334 '((?\\ . "\\textbackslash{}")
335 (?\_ . "\\textunderscore{}")
336 (?\< . "\\textless{}")
337 (?\> . "\\textgreater{}")
338 (?^ . "\\^{}")
339 (?\~ . "\\~{}")
340 (?\@ . "\\@")
341 (?\$ . "\\$")
342 (?\% . "\\%")
343 (?\{ . "\\{")
344 (?\} . "\\}")
345 (?\& . "\\&")
346 (?\# . "\\#"))
347 "A table of characters which must be represented specially.
348 These are applied to the entire document, sans already-escaped
349 regions."
350 :type '(alist :key-type character :value-type string)
351 :group 'muse-latex)
353 (defcustom muse-latex-markup-specials-example
355 "A table of characters which must be represented specially.
356 These are applied to <example> regions.
358 With the default interpretation of <example> regions, no specials
359 need to be escaped."
360 :type '(alist :key-type character :value-type string)
361 :group 'muse-latex)
363 (defcustom muse-latex-markup-specials-literal
364 '((?\n . "\\\n")
365 (?_ . "\\textunderscore{}")
366 (?\< . "\\textless{}")
367 (?\> . "\\textgreater{}")
368 (?^ . "\\^{}")
369 (?\~ . "\\~{}")
370 (?\$ . "\\$")
371 (?\% . "\\%")
372 (?\{ . "\\{")
373 (?\} . "\\}")
374 (?\& . "\\&")
375 (?\# . "\\#"))
376 "A table of characters which must be represented specially.
377 This applies to =monospaced text= and <code> regions."
378 :type '(alist :key-type character :value-type string)
379 :group 'muse-latex)
381 (defcustom muse-latex-markup-specials-url
382 '((?\\ . "\\textbackslash{}")
383 (?\_ . "\\_")
384 (?\< . "\\<")
385 (?\> . "\\>")
386 (?\$ . "\\$")
387 (?\% . "\\%")
388 (?\{ . "\\{")
389 (?\} . "\\}")
390 (?\& . "\\&")
391 (?\# . "\\#"))
392 "A table of characters which must be represented specially.
393 These are applied to URLs."
394 :type '(alist :key-type character :value-type string)
395 :group 'muse-latex)
397 (defcustom muse-latex-markup-specials-image
398 '((?\\ . "\\textbackslash{}") ; cannot find suitable replacement
399 (?\< . "\\<")
400 (?\> . "\\>")
401 (?\$ . "\\$")
402 (?\% . "\\%")
403 (?\{ . "\\{")
404 (?\} . "\\}")
405 (?\& . "\\&")
406 (?\# . "\\#") ; cannot find suitable replacement
408 "A table of characters which must be represented specially.
409 These are applied to image filenames."
410 :type '(alist :key-type character :value-type string)
411 :group 'muse-latex)
413 (defun muse-latex-decide-specials (context)
414 "Determine the specials to escape, depending on CONTEXT."
415 (cond ((memq context '(underline emphasis document url-desc verbatim))
416 muse-latex-markup-specials-document)
417 ((eq context 'image)
418 muse-latex-markup-specials-image)
419 ((memq context '(email url))
420 muse-latex-markup-specials-url)
421 ((eq context 'literal)
422 muse-latex-markup-specials-literal)
423 ((eq context 'example)
424 muse-latex-markup-specials-example)
425 (t (error "Invalid context '%s' in muse-latex" context))))
427 (defcustom muse-latex-permit-contents-tag nil
428 "If nil, ignore <contents> tags. Otherwise, insert table of contents.
430 Most of the time, it is best to have a table of contents on the
431 first page, with a new page immediately following. To make this
432 work with documents published in both HTML and LaTeX, we need to
433 ignore the <contents> tag.
435 If you don't agree with this, then set this option to non-nil,
436 and it will do what you expect."
437 :type 'boolean
438 :group 'muse-latex)
440 (defun muse-latex-markup-table ()
441 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
442 (match-end 0)))
443 (row-len (car table-info))
444 (field-list (cdr table-info)))
445 (when table-info
446 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l) "}\n")
447 (dolist (fields field-list)
448 (let ((type (car fields)))
449 (setq fields (cdr fields))
450 (if (eq type 'hline)
451 (muse-insert-markup "\\hline\n")
452 (when (= type 3)
453 (muse-insert-markup "\\hline\n"))
454 (insert (car fields))
455 (setq fields (cdr fields))
456 (dolist (field fields)
457 (muse-insert-markup " & ")
458 (insert field))
459 (muse-insert-markup " \\\\\n")
460 (when (= type 2)
461 (muse-insert-markup "\\hline\n")))))
462 (muse-insert-markup "\\end{tabular}"))))
464 ;;; Tags for LaTeX
466 (defun muse-latex-slide-tag (beg end attrs)
467 "Publish the <slide> tag in LaTeX.
468 This is used by the slides and lecture-notes publishing styles."
469 (let ((title (cdr (assoc "title" attrs))))
470 (goto-char beg)
471 (muse-insert-markup "\\begin{frame}\n")
472 (when title
473 (muse-insert-markup "\\frametitle{")
474 (insert title)
475 (muse-insert-markup "}\n"))
476 (goto-char end)
477 (muse-insert-markup "\n\\end{frame}")))
479 ;;; Post-publishing functions
481 (defun muse-latex-fixup-dquotes ()
482 "Fixup double quotes."
483 (goto-char (point-min))
484 (let ((open t))
485 (while (search-forward "\"" nil t)
486 (unless (get-text-property (match-beginning 0) 'read-only)
487 (when (or (bobp)
488 (eq (char-before) ?\n))
489 (setq open t))
490 (if open
491 (progn
492 (replace-match "``")
493 (setq open nil))
494 (replace-match "''")
495 (setq open t))))))
497 (defun muse-latex-fixup-citations ()
498 "Replace semicolons in multi-head citations with colons."
499 (goto-char (point-min))
500 (while (re-search-forward "\\\\cite.?{" nil t)
501 (let ((start (point))
502 (end (re-search-forward "}")))
503 (save-restriction
504 (narrow-to-region start end)
505 (goto-char (point-min))
506 (while (re-search-forward ";" nil t)
507 (replace-match ","))))))
509 (defun muse-latex-fixup-headings ()
510 "Remove footnotes in headings, since LaTeX does not permit them to exist.
512 This can happen if there is a link in a heading, because by
513 default Muse will add a footnote for each link."
514 (goto-char (point-min))
515 (while (re-search-forward "^\\\\section.?{" nil t)
516 (save-restriction
517 (narrow-to-region (match-beginning 0) (muse-line-end-position))
518 (goto-char (point-min))
519 (while (re-search-forward "\\\\footnote{[^}\n]+}" nil t)
520 (replace-match ""))
521 (forward-line 1))))
523 (defun muse-latex-munge-buffer ()
524 (muse-latex-fixup-dquotes)
525 (muse-latex-fixup-citations)
526 (muse-latex-fixup-headings)
527 (when (and muse-latex-permit-contents-tag
528 muse-publish-generate-contents)
529 (goto-char (car muse-publish-generate-contents))
530 (muse-insert-markup "\\tableofcontents")))
532 (defun muse-latex-bibliography ()
533 (save-excursion
534 (goto-char (point-min))
535 (if (re-search-forward "\\\\cite.?{" nil t)
536 (concat
537 "\\bibliography{"
538 (muse-publishing-directive "bibsource")
539 "}\n")
540 "")))
542 (defun muse-latex-pdf-browse-file (file)
543 (shell-command (concat "open " file)))
545 (defun muse-latex-pdf-generate (file output-path final-target)
546 (apply
547 #'muse-publish-transform-output
548 file output-path final-target "PDF"
549 (function
550 (lambda (file output-path)
551 (let* ((fnd (file-name-directory output-path))
552 (command (format "cd \"%s\"; %s \"%s\""
553 fnd muse-latex-pdf-program
554 (file-relative-name file fnd)))
555 (times 0)
556 result)
557 ;; XEmacs can sometimes return a non-number result. We'll err
558 ;; on the side of caution by continuing to attempt to generate
559 ;; the PDF if this happens and treat the final result as
560 ;; successful.
561 (while (and (< times 2)
562 (or (not (numberp result))
563 (not (eq result 0))
564 ;; table of contents takes 2 passes
565 (file-readable-p
566 (muse-replace-regexp-in-string
567 "\\.tex\\'" ".toc" file t t))))
568 (setq result (shell-command command)
569 times (1+ times)))
570 (if (or (not (numberp result))
571 (eq result 0))
573 nil))))
574 muse-latex-pdf-cruft))
576 ;;; Register the Muse LATEX Publishers
578 (muse-define-style "latex"
579 :suffix 'muse-latex-extension
580 :regexps 'muse-latex-markup-regexps
581 :functions 'muse-latex-markup-functions
582 :strings 'muse-latex-markup-strings
583 :specials 'muse-latex-decide-specials
584 :before-end 'muse-latex-munge-buffer
585 :header 'muse-latex-header
586 :footer 'muse-latex-footer
587 :browser 'find-file)
589 (muse-derive-style "pdf" "latex"
590 :final 'muse-latex-pdf-generate
591 :browser 'muse-latex-pdf-browse-file
592 :link-suffix 'muse-latex-pdf-extension
593 :osuffix 'muse-latex-pdf-extension)
595 (muse-derive-style "latexcjk" "latex"
596 :header 'muse-latexcjk-header
597 :footer 'muse-latexcjk-footer)
599 (muse-derive-style "pdfcjk" "latexcjk"
600 :final 'muse-latex-pdf-generate
601 :browser 'muse-latex-pdf-browse-file
602 :link-suffix 'muse-latex-pdf-extension
603 :osuffix 'muse-latex-pdf-extension)
605 (muse-derive-style "slides" "latex"
606 :header 'muse-latex-slides-header
607 :tags 'muse-latex-slides-markup-tags)
609 (muse-derive-style "slides-pdf" "pdf"
610 :header 'muse-latex-slides-header
611 :tags 'muse-latex-slides-markup-tags)
613 (muse-derive-style "lecture-notes" "slides"
614 :header 'muse-latex-lecture-notes-header)
616 (muse-derive-style "lecture-notes-pdf" "slides-pdf"
617 :header 'muse-latex-lecture-notes-header)
619 (provide 'muse-latex)
621 ;;; muse-latex.el ends here