org-export-latex.el 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. ;;; org-export-latex.el --- LaTeX exporter for org-mode
  2. ;;
  3. ;; Copyright (c) 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-export-latex.el
  7. ;; Version: 5.23
  8. ;; Author: Bastien Guerry <bzg AT altern DOT org>
  9. ;; Maintainer: Bastien Guerry <bzg AT altern DOT org>
  10. ;; Keywords: org, wp, tex
  11. ;; Description: Converts an org-mode buffer into LaTeX
  12. ;; URL: http://www.cognition.ens.fr/~guerry/u/org-export-latex.el
  13. ;;
  14. ;; This file is part of GNU Emacs.
  15. ;;
  16. ;; GNU Emacs is free software; you can redistribute it and/or modify it
  17. ;; under the terms of the GNU General Public License as published by the
  18. ;; Free Software Foundation; either version 3, or (at your option) any
  19. ;; later version.
  20. ;;
  21. ;; GNU Emacs is distributed in the hope that it will be useful, but
  22. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  23. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. ;; General Public License for more details.
  25. ;;
  26. ;; You should have received a copy of the GNU General Public License
  27. ;; along with GNU Emacs; see the file COPYING. If not, write to the Free
  28. ;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  29. ;; MA 02110-1301, USA.
  30. ;;
  31. ;;; Commentary:
  32. ;;
  33. ;; This library implements a LaTeX exporter for org-mode.
  34. ;;
  35. ;; Put this file into your load-path and the following into your ~/.emacs:
  36. ;; (require 'org-export-latex)
  37. ;;
  38. ;; The interactive functions are similar to those of the HTML exporter:
  39. ;;
  40. ;; M-x `org-export-as-latex'
  41. ;; M-x `org-export-as-latex-batch'
  42. ;; M-x `org-export-as-latex-to-buffer'
  43. ;; M-x `org-export-region-as-latex'
  44. ;; M-x `org-replace-region-by-latex'
  45. ;;
  46. ;;; Code:
  47. (eval-when-compile
  48. (require 'cl))
  49. (require 'footnote)
  50. (require 'org)
  51. ;;; Variables:
  52. (defvar org-export-latex-class nil)
  53. (defvar org-export-latex-header nil)
  54. (defvar org-export-latex-append-header nil)
  55. (defvar org-export-latex-options-plist nil)
  56. (defvar org-export-latex-todo-keywords-1 nil)
  57. (defvar org-export-latex-all-targets-re nil)
  58. (defvar org-export-latex-add-level 0)
  59. (defvar org-export-latex-sectioning "")
  60. (defvar org-export-latex-sectioning-depth 0)
  61. (defvar org-export-latex-list-beginning-re
  62. "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) +?")
  63. (defvar org-export-latex-special-string-regexps
  64. '(org-ts-regexp
  65. org-scheduled-string
  66. org-deadline-string
  67. org-clock-string)
  68. "A list of regexps to convert as special keywords.")
  69. (defvar latexp) ; dynamically scoped from org.el
  70. (defvar re-quote) ; dynamically scoped from org.el
  71. (defvar commentsp) ; dynamically scoped from org.el
  72. ;;; User variables:
  73. (defcustom org-export-latex-default-class "article"
  74. "The default LaTeX class."
  75. :group 'org-export-latex
  76. :type '(string :tag "LaTeX class"))
  77. (defcustom org-export-latex-classes
  78. '(("article"
  79. "\\documentclass[11pt,a4paper]{article}
  80. \\usepackage[utf8]{inputenc}
  81. \\usepackage[T1]{fontenc}
  82. \\usepackage{hyperref}"
  83. ("\\section{%s}" . "\\section*{%s}")
  84. ("\\subsection{%s}" . "\\subsection*{%s}")
  85. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
  86. ("\\paragraph{%s}" . "\\paragraph*{%s}")
  87. ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
  88. ("report"
  89. "\\documentclass[11pt,a4paper]{report}
  90. \\usepackage[utf8]{inputenc}
  91. \\usepackage[T1]{fontenc}
  92. \\usepackage{hyperref}"
  93. ("\\part{%s}" . "\\part*{%s}")
  94. ("\\chapter{%s}" . "\\chapter*{%s}")
  95. ("\\section{%s}" . "\\section*{%s}")
  96. ("\\subsection{%s}" . "\\subsection*{%s}")
  97. ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
  98. ("book"
  99. "\\documentclass[11pt,a4paper]{book}
  100. \\usepackage[utf8]{inputenc}
  101. \\usepackage[T1]{fontenc}
  102. \\usepackage{hyperref}"
  103. ("\\part{%s}" . "\\part*{%s}")
  104. ("\\chapter{%s}" . "\\chapter*{%s}")
  105. ("\\section{%s}" . "\\section*{%s}")
  106. ("\\subsection{%s}" . "\\subsection*{%s}")
  107. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
  108. "Alist of LaTeX classes and associated header and structure.
  109. If #+LaTeX_CLASS is set in the buffer, use its value and the
  110. associated information. Here is the structure of each cell:
  111. \(class-name
  112. header-string
  113. (unnumbered-section numbered-section\)
  114. ...\)
  115. A %s formatter is mandatory in each section string and will be
  116. replaced by the title of the section."
  117. :group 'org-export-latex
  118. :type '(repeat
  119. (list (string :tag "LaTeX class")
  120. (string :tag "LaTeX header")
  121. (cons :tag "Level 1"
  122. (string :tag "Numbered")
  123. (string :tag "Unnumbered"))
  124. (cons :tag "Level 2"
  125. (string :tag "Numbered")
  126. (string :tag "Unnumbered"))
  127. (cons :tag "Level 3"
  128. (string :tag "Numbered")
  129. (string :tag "Unnumbered"))
  130. (cons :tag "Level 4"
  131. (string :tag "Numbered")
  132. (string :tag "Unnumbered"))
  133. (cons :tag "Level 5"
  134. (string :tag "Numbered")
  135. (string :tag "Unnumbered")))))
  136. (defcustom org-export-latex-emphasis-alist
  137. '(("*" "\\textbf{%s}" nil)
  138. ("/" "\\emph{%s}" nil)
  139. ("_" "\\underline{%s}" nil)
  140. ("+" "\\texttt{%s}" nil)
  141. ("=" "\\texttt{%s}" nil)
  142. ("~" "\\texttt{%s}" t))
  143. "Alist of LaTeX expressions to convert emphasis fontifiers.
  144. Each element of the list is a list of three elements.
  145. The first element is the character used as a marker for fontification.
  146. The second element is a formatting string to wrap fontified text with.
  147. The third element decides whether to protect converted text from other
  148. conversions."
  149. :group 'org-export-latex
  150. :type 'alist)
  151. (defcustom org-export-latex-title-command "\\maketitle"
  152. "The command used to insert the title just after \\begin{document}.
  153. If this string contains the formatting specification \"%s\" then
  154. it will be used as a formatting string, passing the title as an
  155. argument."
  156. :group 'org-export-latex
  157. :type 'string)
  158. (defcustom org-export-latex-date-format
  159. "%d %B %Y"
  160. "Format string for \\date{...}."
  161. :group 'org-export-latex
  162. :type 'string)
  163. (defcustom org-export-latex-tables-verbatim nil
  164. "When non-nil, export tables as verbatim."
  165. :group 'org-export-latex
  166. :type 'boolean)
  167. (defcustom org-export-latex-tables-column-borders nil
  168. "When non-nil, group of columns are surrounded with borders,
  169. XSeven if these borders are the outside borders of the table."
  170. :group 'org-export-latex
  171. :type 'boolean)
  172. (defcustom org-export-latex-packages-alist nil
  173. "Alist of packages to be inserted in the header.
  174. Each cell is of the forma \( \"option\" . \"package\" \)."
  175. :group 'org-export-latex
  176. :type 'alist)
  177. (defcustom org-export-latex-low-levels 'description
  178. "How to convert sections below the current level of sectioning,
  179. as specified by `org-export-headline-levels' or the value of \"H:\"
  180. in Org's #+OPTION line.
  181. This can be either nil (skip the sections), 'description (convert
  182. the sections as descriptive lists) or a string to be used instead
  183. of \\section{%s}. In this latter case, the %s stands here for the
  184. inserted headline and is mandatory."
  185. :group 'org-export-latex
  186. :type '(choice (const :tag "Ignore" nil)
  187. (symbol :tag "Convert as descriptive list" description)
  188. (string :tag "Use a section string" :value "\\subparagraph{%s}")))
  189. (defcustom org-export-latex-remove-from-headlines
  190. '(:todo t :priority t :tags t)
  191. "A plist of keywords to remove from headlines.
  192. Non-nil means remove this keyword type from the headline.
  193. Don't remove the keys, just change their values."
  194. :type 'plist
  195. :group 'org-export-latex)
  196. (defcustom org-export-latex-image-default-option "width=10em"
  197. "Default option for images."
  198. :group 'org-export-latex
  199. :type 'string)
  200. (defcustom org-export-latex-coding-system nil
  201. "Coding system for the exported LaTex file."
  202. :group 'org-export-latex
  203. :type 'coding-system)
  204. (defcustom org-list-radio-list-templates
  205. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  206. % END RECEIVE ORGLST %n
  207. \\begin{comment}
  208. #+ORGLST: SEND %n org-list-to-latex
  209. | | |
  210. \\end{comment}\n")
  211. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  212. @c END RECEIVE ORGLST %n
  213. @ignore
  214. #+ORGLST: SEND %n org-list-to-texinfo
  215. | | |
  216. @end ignore\n")
  217. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  218. <!-- END RECEIVE ORGLST %n -->
  219. <!--
  220. #+ORGLST: SEND %n org-list-to-html
  221. | | |
  222. -->\n"))
  223. "Templates for radio lists in different major modes.
  224. All occurrences of %n in a template will be replaced with the name of the
  225. list, obtained by prompting the user."
  226. :group 'org-plain-lists
  227. :type '(repeat
  228. (list (symbol :tag "Major mode")
  229. (string :tag "Format"))))
  230. ;;; Autoload functions:
  231. ;;;###autoload
  232. (defun org-export-as-latex-batch ()
  233. "Call `org-export-as-latex', may be used in batch processing as
  234. emacs --batch
  235. --load=$HOME/lib/emacs/org.el
  236. --eval \"(setq org-export-headline-levels 2)\"
  237. --visit=MyFile --funcall org-export-as-latex-batch"
  238. (org-export-as-latex org-export-headline-levels 'hidden))
  239. ;;;###autoload
  240. (defun org-export-as-latex-to-buffer (arg)
  241. "Call `org-exort-as-latex` with output to a temporary buffer.
  242. No file is created. The prefix ARG is passed through to `org-export-as-latex'."
  243. (interactive "P")
  244. (org-export-as-latex arg nil nil "*Org LaTeX Export*")
  245. (switch-to-buffer-other-window "*Org LaTeX Export*"))
  246. ;;;###autoload
  247. (defun org-replace-region-by-latex (beg end)
  248. "Replace the region from BEG to END with its LaTeX export.
  249. It assumes the region has `org-mode' syntax, and then convert it to
  250. LaTeX. This can be used in any buffer. For example, you could
  251. write an itemized list in `org-mode' syntax in an LaTeX buffer and
  252. then use this command to convert it."
  253. (interactive "r")
  254. (let (reg latex buf)
  255. (save-window-excursion
  256. (if (org-mode-p)
  257. (setq latex (org-export-region-as-latex
  258. beg end t 'string))
  259. (setq reg (buffer-substring beg end)
  260. buf (get-buffer-create "*Org tmp*"))
  261. (save-excursion
  262. (set-buffer buf)
  263. (erase-buffer)
  264. (insert reg)
  265. (org-mode)
  266. (setq latex (org-export-region-as-latex
  267. (point-min) (point-max) t 'string)))
  268. (kill-buffer buf)))
  269. (delete-region beg end)
  270. (insert latex)))
  271. ;;;###autoload
  272. (defun org-export-region-as-latex (beg end &optional body-only buffer)
  273. "Convert region from BEG to END in `org-mode' buffer to LaTeX.
  274. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  275. contents, and only produce the region of converted text, useful for
  276. cut-and-paste operations.
  277. If BUFFER is a buffer or a string, use/create that buffer as a target
  278. of the converted LaTeX. If BUFFER is the symbol `string', return the
  279. produced LaTeX as a string and leave not buffer behind. For example,
  280. a Lisp program could call this function in the following way:
  281. (setq latex (org-export-region-as-latex beg end t 'string))
  282. When called interactively, the output buffer is selected, and shown
  283. in a window. A non-interactive call will only retunr the buffer."
  284. (interactive "r\nP")
  285. (when (interactive-p)
  286. (setq buffer "*Org LaTeX Export*"))
  287. (let ((transient-mark-mode t) (zmacs-regions t)
  288. rtn)
  289. (goto-char end)
  290. (set-mark (point)) ;; to activate the region
  291. (goto-char beg)
  292. (setq rtn (org-export-as-latex
  293. nil nil nil
  294. buffer body-only))
  295. (if (fboundp 'deactivate-mark) (deactivate-mark))
  296. (if (and (interactive-p) (bufferp rtn))
  297. (switch-to-buffer-other-window rtn)
  298. rtn)))
  299. ;;;###autoload
  300. (defun org-export-as-latex (arg &optional hidden ext-plist
  301. to-buffer body-only pub-dir)
  302. "Export current buffer to a LaTeX file.
  303. If there is an active region, export only the region. The prefix
  304. ARG specifies how many levels of the outline should become
  305. headlines. The default is 3. Lower levels will be exported
  306. depending on `org-export-latex-low-levels'. The default is to
  307. convert them as description lists. When HIDDEN is non-nil, don't
  308. display the LaTeX buffer. EXT-PLIST is a property list with
  309. external parameters overriding org-mode's default settings, but
  310. still inferior to file-local settings. When TO-BUFFER is
  311. non-nil, create a buffer with that name and export to that
  312. buffer. If TO-BUFFER is the symbol `string', don't leave any
  313. buffer behind but just return the resulting LaTeX as a string.
  314. When BODY-ONLY is set, don't produce the file header and footer,
  315. simply return the content of \begin{document}...\end{document},
  316. without even the \begin{document} and \end{document} commands.
  317. when PUB-DIR is set, use this as the publishing directory."
  318. (interactive "P")
  319. ;; Make sure we have a file name when we need it.
  320. (when (and (not (or to-buffer body-only))
  321. (not buffer-file-name))
  322. (if (buffer-base-buffer)
  323. (org-set-local 'buffer-file-name
  324. (with-current-buffer (buffer-base-buffer)
  325. buffer-file-name))
  326. (error "Need a file name to be able to export")))
  327. (message "Exporting to LaTeX...")
  328. (org-update-radio-target-regexp)
  329. (org-export-latex-set-initial-vars ext-plist arg)
  330. (let* ((wcf (current-window-configuration))
  331. (opt-plist org-export-latex-options-plist)
  332. (region-p (org-region-active-p))
  333. (subtree-p
  334. (when region-p
  335. (save-excursion
  336. (goto-char (region-beginning))
  337. (and (org-at-heading-p)
  338. (>= (org-end-of-subtree t t) (region-end))))))
  339. (title (or (and subtree-p (org-export-get-title-from-subtree))
  340. (plist-get opt-plist :title)
  341. (and (not
  342. (plist-get opt-plist :skip-before-1st-heading))
  343. (org-export-grab-title-from-buffer))
  344. (file-name-sans-extension
  345. (file-name-nondirectory buffer-file-name))))
  346. (filename (concat (file-name-as-directory
  347. (or pub-dir
  348. (org-export-directory :LaTeX ext-plist)))
  349. (file-name-sans-extension
  350. (file-name-nondirectory ;sans-extension
  351. buffer-file-name)) ".tex"))
  352. (filename (if (equal (file-truename filename)
  353. (file-truename buffer-file-name))
  354. (concat filename ".tex")
  355. filename))
  356. (buffer (if to-buffer
  357. (cond
  358. ((eq to-buffer 'string) (get-buffer-create
  359. "*Org LaTeX Export*"))
  360. (t (get-buffer-create to-buffer)))
  361. (find-file-noselect filename)))
  362. (odd org-odd-levels-only)
  363. (header (org-export-latex-make-header title opt-plist))
  364. (skip (cond (subtree-p nil)
  365. (region-p t)
  366. ;; never skip first lines when exporting a subtree
  367. (t (plist-get opt-plist :skip-before-1st-heading))))
  368. (text (plist-get opt-plist :text))
  369. (first-lines (if skip "" (org-export-latex-first-lines)))
  370. (coding-system (and (boundp 'buffer-file-coding-system)
  371. buffer-file-coding-system))
  372. (coding-system-for-write (or org-export-latex-coding-system
  373. coding-system))
  374. (save-buffer-coding-system (or org-export-latex-coding-system
  375. coding-system))
  376. (region (buffer-substring
  377. (if region-p (region-beginning) (point-min))
  378. (if region-p (region-end) (point-max))))
  379. (string-for-export
  380. (org-cleaned-string-for-export
  381. region :emph-multiline t
  382. :for-LaTeX t
  383. :comments nil
  384. :add-text (if (eq to-buffer 'string) nil text)
  385. :skip-before-1st-heading skip
  386. :LaTeX-fragments nil)))
  387. (set-buffer buffer)
  388. (erase-buffer)
  389. (and (fboundp 'set-buffer-file-coding-system)
  390. (set-buffer-file-coding-system coding-system-for-write))
  391. ;; insert the header and initial document commands
  392. (unless (or (eq to-buffer 'string) body-only)
  393. (insert header))
  394. ;; insert text found in #+TEXT
  395. (when (and text (not (eq to-buffer 'string)))
  396. (insert (org-export-latex-content
  397. text '(lists tables fixed-width keywords))
  398. "\n\n"))
  399. ;; insert lines before the first headline
  400. (unless (or skip (eq to-buffer 'string))
  401. (insert first-lines))
  402. ;; handle the case where the region does not begin with a section
  403. (when region-p
  404. (insert (with-temp-buffer
  405. (insert string-for-export)
  406. (org-export-latex-first-lines))))
  407. ;; export the content of headlines
  408. (org-export-latex-global
  409. (with-temp-buffer
  410. (insert string-for-export)
  411. (goto-char (point-min))
  412. (when (re-search-forward "^\\(\\*+\\) " nil t)
  413. (let* ((asters (length (match-string 1)))
  414. (level (if odd (- asters 2) (- asters 1))))
  415. (setq org-export-latex-add-level
  416. (if odd (1- (/ (1+ asters) 2)) (1- asters)))
  417. (org-export-latex-parse-global level odd)))))
  418. ;; finalization
  419. (unless body-only (insert "\n\\end{document}"))
  420. (or to-buffer (save-buffer))
  421. (goto-char (point-min))
  422. (message "Exporting to LaTeX...done")
  423. (prog1
  424. (if (eq to-buffer 'string)
  425. (prog1 (buffer-substring (point-min) (point-max))
  426. (kill-buffer (current-buffer)))
  427. (current-buffer))
  428. (set-window-configuration wcf))))
  429. ;;; Parsing functions:
  430. (defun org-export-latex-parse-global (level odd)
  431. "Parse the current buffer recursively, starting at LEVEL.
  432. If ODD is non-nil, assume the buffer only contains odd sections.
  433. Return a list reflecting the document structure."
  434. (save-excursion
  435. (goto-char (point-min))
  436. (let* ((cnt 0) output
  437. (depth org-export-latex-sectioning-depth))
  438. (while (re-search-forward
  439. (concat "^\\(\\(?:\\*\\)\\{"
  440. (number-to-string (+ (if odd 2 1) level))
  441. "\\}\\) \\(.*\\)$")
  442. ;; make sure that there is no upper heading
  443. (when (> level 0)
  444. (save-excursion
  445. (save-match-data
  446. (re-search-forward
  447. (concat "^\\(\\(?:\\*\\)\\{"
  448. (number-to-string level)
  449. "\\}\\) \\(.*\\)$") nil t)))) t)
  450. (setq cnt (1+ cnt))
  451. (let* ((pos (match-beginning 0))
  452. (heading (match-string 2))
  453. (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
  454. (save-excursion
  455. (narrow-to-region
  456. (point)
  457. (save-match-data
  458. (if (re-search-forward
  459. (concat "^\\(\\(?:\\*\\)\\{"
  460. (number-to-string (+ (if odd 2 1) level))
  461. "\\}\\) \\(.*\\)$") nil t)
  462. (match-beginning 0)
  463. (point-max))))
  464. (goto-char (point-min))
  465. (setq output
  466. (append output
  467. (list
  468. (list
  469. `(pos . ,pos)
  470. `(level . ,nlevel)
  471. `(occur . ,cnt)
  472. `(heading . ,heading)
  473. `(content . ,(org-export-latex-parse-content))
  474. `(subcontent . ,(org-export-latex-parse-subcontent
  475. level odd)))))))
  476. (widen)))
  477. (list output))))
  478. (defun org-export-latex-parse-content ()
  479. "Extract the content of a section."
  480. (let ((beg (point))
  481. (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t)
  482. (progn (beginning-of-line) (point))
  483. (point-max))))
  484. (buffer-substring beg end)))
  485. (defun org-export-latex-parse-subcontent (level odd)
  486. "Extract the subcontent of a section at LEVEL.
  487. If ODD Is non-nil, assume subcontent only contains odd sections."
  488. (if (not (re-search-forward
  489. (concat "^\\(\\(?:\\*\\)\\{"
  490. (number-to-string (+ (if odd 4 2) level))
  491. "\\}\\) \\(.*\\)$")
  492. nil t))
  493. nil ; subcontent is nil
  494. (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
  495. ;;; Rendering functions:
  496. (defun org-export-latex-global (content)
  497. "Export CONTENT to LaTeX.
  498. CONTENT is an element of the list produced by
  499. `org-export-latex-parse-global'."
  500. (if (eq (car content) 'subcontent)
  501. (mapc 'org-export-latex-sub (cdr content))
  502. (org-export-latex-sub (car content))))
  503. (defun org-export-latex-sub (subcontent)
  504. "Export the list SUBCONTENT to LaTeX.
  505. SUBCONTENT is an alist containing information about the headline
  506. and its content."
  507. (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
  508. (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
  509. (defun org-export-latex-subcontent (subcontent num)
  510. "Export each cell of SUBCONTENT to LaTeX."
  511. (let ((heading (org-export-latex-fontify-headline
  512. (cdr (assoc 'heading subcontent))))
  513. (level (- (cdr (assoc 'level subcontent))
  514. org-export-latex-add-level))
  515. (occur (number-to-string (cdr (assoc 'occur subcontent))))
  516. (content (cdr (assoc 'content subcontent)))
  517. (subcontent (cadr (assoc 'subcontent subcontent))))
  518. (cond
  519. ;; Normal conversion
  520. ((<= level org-export-latex-sectioning-depth)
  521. (let ((sec (nth (1- level) org-export-latex-sectioning)))
  522. (insert (format (if num (car sec) (cdr sec)) heading) "\n"))
  523. (insert (org-export-latex-content content))
  524. (cond ((stringp subcontent) (insert subcontent))
  525. ((listp subcontent) (org-export-latex-sub subcontent))))
  526. ;; At a level under the hl option: we can drop this subsection
  527. ((> level org-export-latex-sectioning-depth)
  528. (cond ((eq org-export-latex-low-levels 'description)
  529. (insert (format "\\begin{description}\n\n\\item[%s]\n\n" heading))
  530. (insert (org-export-latex-content content))
  531. (cond ((stringp subcontent) (insert subcontent))
  532. ((listp subcontent) (org-export-latex-sub subcontent)))
  533. (insert "\\end{description}\n"))
  534. ((stringp org-export-latex-low-levels)
  535. (insert (format org-export-latex-low-levels heading) "\n")
  536. (insert (org-export-latex-content content))
  537. (cond ((stringp subcontent) (insert subcontent))
  538. ((listp subcontent) (org-export-latex-sub subcontent)))))))))
  539. ;;; Exporting internals:
  540. (defun org-export-latex-set-initial-vars (ext-plist level)
  541. "Store org local variables required for LaTeX export.
  542. EXT-PLIST is an optional additional plist.
  543. LEVEL indicates the default depth for export."
  544. (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
  545. org-export-latex-all-targets-re
  546. (org-make-target-link-regexp (org-all-targets))
  547. org-export-latex-options-plist
  548. (org-combine-plists (org-default-export-plist) ext-plist
  549. (org-infile-export-plist))
  550. org-export-latex-class
  551. (save-excursion
  552. (goto-char (point-min))
  553. (if (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
  554. (assoc (match-string 1) org-export-latex-classes))
  555. (match-string 1)
  556. org-export-latex-default-class))
  557. org-export-latex-header
  558. (cadr (assoc org-export-latex-class org-export-latex-classes))
  559. org-export-latex-sectioning
  560. (cddr (assoc org-export-latex-class org-export-latex-classes))
  561. org-export-latex-sectioning-depth
  562. (or level
  563. (let ((hl-levels
  564. (plist-get org-export-latex-options-plist :headline-levels))
  565. (sec-depth (length org-export-latex-sectioning)))
  566. (if (> hl-levels sec-depth) sec-depth hl-levels)))))
  567. (defun org-export-latex-make-header (title opt-plist)
  568. "Make the LaTeX header and return it as a string.
  569. TITLE is the current title from the buffer or region.
  570. OPT-PLIST is the options plist for current buffer."
  571. (let ((toc (plist-get opt-plist :table-of-contents))
  572. (author (plist-get opt-plist :author)))
  573. (concat
  574. (if (plist-get opt-plist :time-stamp-file)
  575. (format-time-string "% Created %Y-%m-%d %a %H:%M\n"))
  576. ;; insert LaTeX custom header
  577. org-export-latex-header
  578. "\n"
  579. ;; insert information on LaTeX packages
  580. (when org-export-latex-packages-alist
  581. (mapconcat (lambda(p)
  582. (if (equal "" (car p))
  583. (format "\\usepackage{%s}" (cadr p))
  584. (format "\\usepackage[%s]{%s}"
  585. (car p) (cadr p))))
  586. org-export-latex-packages-alist "\n"))
  587. ;; insert additional commands in the header
  588. org-export-latex-append-header
  589. ;; insert the title
  590. (format
  591. "\n\n\\title{%s}\n"
  592. ;; convert the title
  593. (org-export-latex-content
  594. title '(lists tables fixed-width keywords)))
  595. ;; insert author info
  596. (if (plist-get opt-plist :author-info)
  597. (format "\\author{%s}\n"
  598. (or author user-full-name))
  599. (format "%%\\author{%s}\n"
  600. (or author user-full-name)))
  601. ;; insert the date
  602. (format "\\date{%s}\n"
  603. (format-time-string
  604. (or (plist-get opt-plist :date)
  605. org-export-latex-date-format)))
  606. ;; beginning of the document
  607. "\n\\begin{document}\n\n"
  608. ;; insert the title command
  609. (if (string-match "%s" org-export-latex-title-command)
  610. (format org-export-latex-title-command title)
  611. org-export-latex-title-command)
  612. "\n\n"
  613. ;; table of contents
  614. (when (and org-export-with-toc
  615. (plist-get opt-plist :section-numbers))
  616. (cond ((numberp toc)
  617. (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
  618. (min toc (plist-get opt-plist :headline-levels))))
  619. (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
  620. (plist-get opt-plist :headline-levels))))))))
  621. (defun org-export-latex-first-lines (&optional comments)
  622. "Export the first lines before first headline.
  623. COMMENTS is either nil to replace them with the empty string or a
  624. formatting string like %%%%s if we want to comment them out."
  625. (save-excursion
  626. (goto-char (point-min))
  627. (if (org-at-heading-p) (beginning-of-line 2))
  628. (let* ((pt (point))
  629. (end (if (and (re-search-forward "^\\* " nil t)
  630. (not (eq pt (match-beginning 0))))
  631. (goto-char (match-beginning 0))
  632. (goto-char (point-max)))))
  633. (org-export-latex-content
  634. (org-cleaned-string-for-export
  635. (buffer-substring (point-min) end)
  636. :for-LaTeX t
  637. :emph-multiline t
  638. :add-text nil
  639. :comments nil
  640. :skip-before-1st-heading nil
  641. :LaTeX-fragments nil)))))
  642. (defun org-export-latex-content (content &optional exclude-list)
  643. "Convert CONTENT string to LaTeX.
  644. Don't perform conversions that are in EXCLUDE-LIST. Recognized
  645. conversion types are: quotation-marks, emphasis, sub-superscript,
  646. links, keywords, lists, tables, fixed-width"
  647. (with-temp-buffer
  648. (insert content)
  649. (unless (memq 'quotation-marks exclude-list)
  650. (org-export-latex-quotation-marks))
  651. (unless (memq 'emphasis exclude-list)
  652. (when (plist-get org-export-latex-options-plist :emphasize)
  653. (org-export-latex-fontify)))
  654. (unless (memq 'sub-superscript exclude-list)
  655. (org-export-latex-special-chars
  656. (plist-get org-export-latex-options-plist :sub-superscript)))
  657. (unless (memq 'links exclude-list)
  658. (org-export-latex-links))
  659. (unless (memq 'keywords exclude-list)
  660. (org-export-latex-keywords
  661. (plist-get org-export-latex-options-plist :timestamps)))
  662. (unless (memq 'lists exclude-list)
  663. (org-export-latex-lists))
  664. (unless (memq 'tables exclude-list)
  665. (org-export-latex-tables
  666. (plist-get org-export-latex-options-plist :tables)))
  667. (unless (memq 'fixed-width exclude-list)
  668. (org-export-latex-fixed-width
  669. (plist-get org-export-latex-options-plist :fixed-width)))
  670. ;; return string
  671. (buffer-substring (point-min) (point-max))))
  672. (defun org-export-latex-protect-string (s)
  673. "Prevent further conversion for string S by adding the
  674. org-protect property."
  675. (add-text-properties 0 (length s) '(org-protected t) s) s)
  676. (defun org-export-latex-protect-char-in-string (char-list string)
  677. "Add org-protected text-property to char from CHAR-LIST in STRING."
  678. (with-temp-buffer
  679. (save-match-data
  680. (insert string)
  681. (goto-char (point-min))
  682. (while (re-search-forward (regexp-opt char-list) nil t)
  683. (add-text-properties (match-beginning 0)
  684. (match-end 0) '(org-protected t)))
  685. (buffer-string))))
  686. (defun org-export-latex-keywords-maybe (remove-list)
  687. "Maybe remove keywords depending on rules in REMOVE-LIST."
  688. (goto-char (point-min))
  689. (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
  690. (case-fold-search nil))
  691. ;; convert TODO keywords
  692. (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
  693. (if (plist-get remove-list :todo)
  694. (replace-match "")
  695. (replace-match (format "\\texttt{%s}" (match-string 1)) t t)))
  696. ;; convert priority string
  697. (when (re-search-forward "\\[\\\\#.\\]" nil t)
  698. (if (plist-get remove-list :priority)
  699. (replace-match "")
  700. (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))
  701. ;; convert tags
  702. (when (re-search-forward "\\(:[a-zA-Z0-9]+\\)+:" nil t)
  703. (if (or (not org-export-with-tags)
  704. (plist-get remove-list :tags))
  705. (replace-match "")
  706. (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))))
  707. (defun org-export-latex-fontify-headline (string)
  708. "Fontify special words in string."
  709. (with-temp-buffer
  710. ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
  711. ;; the beginning of the buffer - inserting "\n" is safe here though.
  712. (insert "\n" string)
  713. (goto-char (point-min))
  714. (when (plist-get org-export-latex-options-plist :emphasize)
  715. (org-export-latex-fontify))
  716. (org-export-latex-special-chars
  717. (plist-get org-export-latex-options-plist :sub-superscript))
  718. (org-export-latex-keywords-maybe
  719. org-export-latex-remove-from-headlines)
  720. (org-export-latex-links)
  721. (org-trim (buffer-substring-no-properties (point-min) (point-max)))))
  722. (defun org-export-latex-quotation-marks ()
  723. "Export question marks depending on language conventions."
  724. (let* ((lang (plist-get org-export-latex-options-plist :language))
  725. (quote-rpl (if (equal lang "fr")
  726. '(("\\(\\s-\\)\"" "«~")
  727. ("\\(\\S-\\)\"" "~»")
  728. ("\\(\\s-\\)'" "`"))
  729. '(("\\(\\s-\\)\"" "``")
  730. ("\\(\\S-\\)\"" "''")
  731. ("\\(\\s-\\)'" "`")))))
  732. (mapc (lambda(l) (goto-char (point-min))
  733. (while (re-search-forward (car l) nil t)
  734. (let ((rpl (concat (match-string 1) (cadr l))))
  735. (org-export-latex-protect-string rpl)
  736. (org-if-unprotected
  737. (replace-match rpl t t))))) quote-rpl)))
  738. (defun org-export-latex-special-chars (sub-superscript)
  739. "Export special characters to LaTeX.
  740. If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
  741. See the `org-export-latex.el' code for a complete conversion table."
  742. (goto-char (point-min))
  743. (mapc (lambda(c)
  744. (goto-char (point-min))
  745. (while (re-search-forward c nil t)
  746. ;; Put the point where to check for org-protected
  747. (unless (or (get-text-property (match-beginning 2) 'org-protected)
  748. (org-at-table-p))
  749. (cond ((member (match-string 2) '("\\$" "$"))
  750. (if (equal (match-string 2) "\\$")
  751. (replace-match (concat (match-string 1) "$"
  752. (match-string 3)) t t)
  753. (replace-match (concat (match-string 1) "\\$"
  754. (match-string 3)) t t)))
  755. ((member (match-string 2) '("&" "%" "#"))
  756. (if (equal (match-string 1) "\\")
  757. (replace-match (match-string 2) t t)
  758. (replace-match (concat (match-string 1) "\\"
  759. (match-string 2)) t t)))
  760. ((equal (match-string 2) "...")
  761. (replace-match
  762. (concat (match-string 1)
  763. (org-export-latex-protect-string "\\ldots{}")) t t))
  764. ((equal (match-string 2) "~")
  765. (cond ((equal (match-string 1) "\\") nil)
  766. ((eq 'org-link (get-text-property 0 'face (match-string 2)))
  767. (replace-match (concat (match-string 1) "\\~") t t))
  768. (t (replace-match
  769. (org-export-latex-protect-string
  770. (concat (match-string 1) "\\~{}")) t t))))
  771. ((member (match-string 2) '("{" "}"))
  772. (unless (save-match-data (org-inside-LaTeX-fragment-p))
  773. (if (equal (match-string 1) "\\")
  774. (replace-match (match-string 2) t t)
  775. (replace-match (concat (match-string 1) "\\"
  776. (match-string 2)) t t)))))
  777. (unless (save-match-data (org-inside-LaTeX-fragment-p))
  778. (cond ((equal (match-string 2) "\\")
  779. (replace-match (or (save-match-data
  780. (org-export-latex-treat-backslash-char
  781. (match-string 1)
  782. (match-string 3))) "") t t))
  783. ((member (match-string 2) '("_" "^"))
  784. (replace-match (or (save-match-data
  785. (org-export-latex-treat-sub-super-char
  786. sub-superscript
  787. (match-string 1)
  788. (match-string 2)
  789. (match-string 3))) "") t t)))))))
  790. '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
  791. "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
  792. "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
  793. "\\(.\\|^\\)\\(&\\)"
  794. "\\(.\\|^\\)\\(#\\)"
  795. "\\(.\\|^\\)\\(%\\)"
  796. "\\(.\\|^\\)\\({\\)"
  797. "\\(.\\|^\\)\\(}\\)"
  798. "\\(.\\|^\\)\\(~\\)"
  799. "\\(.\\|^\\)\\(\\.\\.\\.\\)"
  800. ;; (?\< . "\\textless{}")
  801. ;; (?\> . "\\textgreater{}")
  802. )))
  803. (defun org-export-latex-treat-sub-super-char
  804. (subsup string-before char string-after)
  805. "Convert the \"_\" and \"^\" characters to LaTeX.
  806. SUBSUP corresponds to the ^: option in the #+OPTIONS line.
  807. Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
  808. (cond ((equal string-before "\\")
  809. (concat string-before char string-after))
  810. ;; this is part of a math formula
  811. ((and (string-match "\\S-+" string-before)
  812. (string-match "\\S-+" string-after))
  813. (cond ((eq 'org-link (get-text-property 0 'face char))
  814. (concat string-before "\\" char string-after))
  815. ((save-match-data (org-inside-LaTeX-fragment-p))
  816. (if subsup
  817. (cond ((eq 1 (length string-after))
  818. (concat string-before char string-after))
  819. ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
  820. (format "%s%s{%s}" string-before char
  821. (match-string 1 string-after))))))
  822. ((and subsup
  823. (> (length string-after) 1)
  824. (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
  825. (format "$%s%s{%s}$" string-before char
  826. (match-string 1 string-after)))
  827. (subsup (concat "$" string-before char string-after "$"))
  828. (t (org-export-latex-protect-string
  829. (concat string-before "\\" char "{}" string-after)))))
  830. (t (org-export-latex-protect-string
  831. (concat string-before "\\" char "{}" string-after)))))
  832. (defun org-export-latex-treat-backslash-char (string-before string-after)
  833. "Convert the \"$\" special character to LaTeX.
  834. The conversion is made depending of STRING-BEFORE and STRING-AFTER."
  835. (cond ((member (list string-after) org-html-entities)
  836. ;; backslash is part of a special entity (like "\alpha")
  837. (concat string-before "$\\"
  838. (or (cdar (member (list string-after) org-html-entities))
  839. string-after) "$"))
  840. ((and (not (string-match "^[ \n\t]" string-after))
  841. (not (string-match "[ \t]\\'\\|^" string-before)))
  842. ;; backslash is inside a word
  843. (org-export-latex-protect-string
  844. (concat string-before "\\textbackslash{}" string-after)))
  845. ((not (or (equal string-after "")
  846. (string-match "^[ \t\n]" string-after)))
  847. ;; backslash might escape a character (like \#) or a user TeX
  848. ;; macro (like \setcounter)
  849. (org-export-latex-protect-string
  850. (concat string-before "\\" string-after)))
  851. ((and (string-match "^[ \t\n]" string-after)
  852. (string-match "[ \t\n]\\'" string-before))
  853. ;; backslash is alone, convert it to $\backslash$
  854. (org-export-latex-protect-string
  855. (concat string-before "\\textbackslash{}" string-after)))
  856. (t (org-export-latex-protect-string
  857. (concat string-before "\\textbackslash{}" string-after)))))
  858. (defun org-export-latex-keywords (timestamps)
  859. "Convert special keywords to LaTeX.
  860. Regexps are those from `org-export-latex-special-string-regexps'."
  861. (let ((rg org-export-latex-special-string-regexps) r)
  862. (while (setq r (pop rg))
  863. (goto-char (point-min))
  864. (while (re-search-forward (eval r) nil t)
  865. (if (not timestamps)
  866. (replace-match (format "\\\\texttt{%s}" (match-string 0)) t)
  867. (replace-match ""))))))
  868. (defun org-export-latex-fixed-width (opt)
  869. "When OPT is non-nil convert fixed-width sections to LaTeX."
  870. (goto-char (point-min))
  871. (while (re-search-forward "^[ \t]*:" nil t)
  872. (if opt
  873. (progn (goto-char (match-beginning 0))
  874. (insert "\\begin{verbatim}\n")
  875. (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
  876. (replace-match (concat (match-string 1)
  877. (match-string 2)) t t)
  878. (forward-line))
  879. (insert "\\end{verbatim}\n\n"))
  880. (progn (goto-char (match-beginning 0))
  881. (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
  882. (replace-match (concat "%" (match-string 1)
  883. (match-string 2)) t t)
  884. (forward-line))))))
  885. (defun org-export-latex-tables (insert)
  886. "Convert tables to LaTeX and INSERT it."
  887. (goto-char (point-min))
  888. (while (re-search-forward "^\\([ \t]*\\)|" nil t)
  889. ;; FIXME really need to save-excursion?
  890. (save-excursion (org-table-align))
  891. (let* ((beg (org-table-begin))
  892. (end (org-table-end))
  893. (raw-table (buffer-substring-no-properties beg end))
  894. fnum fields line lines olines gr colgropen line-fmt align)
  895. (if org-export-latex-tables-verbatim
  896. (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
  897. "\\end{verbatim}\n")))
  898. (apply 'delete-region (list beg end))
  899. (insert tbl))
  900. (progn
  901. (setq lines (split-string raw-table "\n" t))
  902. (apply 'delete-region (list beg end))
  903. (when org-export-table-remove-special-lines
  904. (setq lines (org-table-clean-before-export lines)))
  905. ;; make a formatting string to reflect aligment
  906. (setq olines lines)
  907. (while (and (not line-fmt) (setq line (pop olines)))
  908. (unless (string-match "^[ \t]*|-" line)
  909. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  910. (setq fnum (make-vector (length fields) 0))
  911. (setq line-fmt
  912. (mapconcat
  913. (lambda (x)
  914. (setq gr (pop org-table-colgroup-info))
  915. (format "%s%%s%s"
  916. (cond ((eq gr ':start)
  917. (prog1 (if colgropen "|" "")
  918. (setq colgropen t)))
  919. ((eq gr ':startend)
  920. (prog1 (if colgropen "|" "|")
  921. (setq colgropen nil)))
  922. (t ""))
  923. (if (memq gr '(:end :startend))
  924. (progn (setq colgropen nil) "|")
  925. "")))
  926. fnum ""))))
  927. ;; fix double || in line-fmt
  928. (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
  929. ;; maybe remove the first and last "|"
  930. (when (and (not org-export-latex-tables-column-borders)
  931. (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
  932. (setq line-fmt (match-string 2 line-fmt)))
  933. ;; format alignment
  934. (setq align (apply 'format
  935. (cons line-fmt
  936. (mapcar (lambda (x) (if x "r" "l"))
  937. org-table-last-alignment))))
  938. ;; prepare the table to send to orgtbl-to-latex
  939. (setq lines
  940. (mapcar
  941. (lambda(elem)
  942. (or (and (string-match "[ \t]*|-+" elem) 'hline)
  943. (split-string (org-trim elem) "|" t)))
  944. lines))
  945. (when insert
  946. (insert (orgtbl-to-latex
  947. lines `(:tstart ,(concat "\\begin{tabular}{" align "}")))
  948. "\n\n")))))))
  949. (defun org-export-latex-fontify ()
  950. "Convert fontification to LaTeX."
  951. (goto-char (point-min))
  952. (while (re-search-forward org-emph-re nil t)
  953. ;; The match goes one char after the *string*
  954. (let ((emph (assoc (match-string 3)
  955. org-export-latex-emphasis-alist))
  956. rpl)
  957. (unless (get-text-property (1- (point)) 'org-protected)
  958. (setq rpl (concat (match-string 1)
  959. (format (org-export-latex-protect-char-in-string
  960. '("\\" "{" "}") (cadr emph))
  961. (match-string 4))
  962. (match-string 5)))
  963. (if (caddr emph)
  964. (setq rpl (org-export-latex-protect-string rpl)))
  965. (replace-match rpl t t)))
  966. (backward-char)))
  967. (defun org-export-latex-links ()
  968. ;; Make sure to use the LaTeX hyperref and graphicx package
  969. ;; or send some warnings.
  970. "Convert links to LaTeX."
  971. (goto-char (point-min))
  972. (while (re-search-forward org-bracket-link-analytic-regexp nil t)
  973. (org-if-unprotected
  974. (goto-char (match-beginning 0))
  975. (let* ((re-radio org-export-latex-all-targets-re)
  976. (remove (list (match-beginning 0) (match-end 0)))
  977. (type (match-string 2))
  978. (raw-path (match-string 3))
  979. (full-raw-path (concat (match-string 1) raw-path))
  980. (desc (match-string 5))
  981. imgp radiop
  982. ;; define the path of the link
  983. (path (cond
  984. ((member type '("http" "https" "ftp"))
  985. (concat type ":" raw-path))
  986. ((and re-radio (string-match re-radio raw-path))
  987. (setq radiop t))
  988. ((equal type "mailto")
  989. (concat type ":" raw-path))
  990. ((equal type "file")
  991. (if (and (or (org-file-image-p (expand-file-name raw-path))
  992. (string-match "\\.eps$" raw-path))
  993. (equal desc full-raw-path))
  994. (setq imgp t)
  995. (progn (when (string-match "\\(.+\\)::.+" raw-path)
  996. (setq raw-path (match-string 1 raw-path)))
  997. (if (file-exists-p raw-path)
  998. (concat type "://" (expand-file-name raw-path))
  999. (concat type "://" (org-export-directory
  1000. :LaTeX org-export-latex-options-plist)
  1001. raw-path))))))))
  1002. ;; process with link inserting
  1003. (apply 'delete-region remove)
  1004. (cond ((and imgp (plist-get org-export-latex-options-plist :inline-images))
  1005. (insert (format "\\includegraphics[%s]{%s}"
  1006. ;; image option should be set be a comment line
  1007. org-export-latex-image-default-option
  1008. (expand-file-name raw-path))))
  1009. (radiop (insert (format "\\hyperref[%s]{%s}" raw-path desc)))
  1010. (path (insert (format "\\href{%s}{%s}" path desc)))
  1011. (t (insert "\\texttt{" desc "}")))))))
  1012. (defvar org-latex-entities) ; defined below
  1013. (defun org-export-latex-cleaned-string ()
  1014. "Clean stuff in the LaTeX export."
  1015. ;; Preserve line breaks
  1016. (goto-char (point-min))
  1017. (while (re-search-forward "\\\\\\\\" nil t)
  1018. (add-text-properties (match-beginning 0) (match-end 0)
  1019. '(org-protected t)))
  1020. ;; Convert LaTeX to \LaTeX{}
  1021. (goto-char (point-min))
  1022. (let ((case-fold-search nil) rpl)
  1023. (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
  1024. (replace-match (org-export-latex-protect-string
  1025. (concat (match-string 1) "\\LaTeX{}")) t t)))
  1026. ;; Convert horizontal rules
  1027. (goto-char (point-min))
  1028. (while (re-search-forward "^----+.$" nil t)
  1029. (replace-match (org-export-latex-protect-string "\\hrule") t t))
  1030. ;; Protect LaTeX commands like \commad[...]{...} or \command{...}
  1031. (goto-char (point-min))
  1032. (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t)
  1033. (add-text-properties (match-beginning 0) (match-end 0)
  1034. '(org-protected t)))
  1035. ;; Protect LaTeX entities
  1036. (goto-char (point-min))
  1037. (while (re-search-forward (regexp-opt org-latex-entities) nil t)
  1038. (add-text-properties (match-beginning 0) (match-end 0)
  1039. '(org-protected t)))
  1040. ;; Replace radio links
  1041. (goto-char (point-min))
  1042. (while (re-search-forward
  1043. (concat "<<<?" org-export-latex-all-targets-re
  1044. ">>>?\\((INVISIBLE)\\)?") nil t)
  1045. (replace-match
  1046. (org-export-latex-protect-string
  1047. (format "\\label{%s}%s"(match-string 1)
  1048. (if (match-string 2) "" (match-string 1)))) t t))
  1049. ;; Delete @<...> constructs
  1050. ;; Thanks to Daniel Clemente for this regexp
  1051. (goto-char (point-min))
  1052. (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
  1053. (replace-match ""))
  1054. ;; When converting to LaTeX, replace footnotes
  1055. ;; FIXME: don't protect footnotes from conversion
  1056. (when (plist-get org-export-latex-options-plist :footnotes)
  1057. (goto-char (point-min))
  1058. (while (re-search-forward "\\[[0-9]+\\]" nil t)
  1059. (when (save-match-data
  1060. (save-excursion (beginning-of-line)
  1061. (looking-at "[^:|#]")))
  1062. (let ((foot-beg (match-beginning 0))
  1063. (foot-end (match-end 0))
  1064. (foot-prefix (match-string 0))
  1065. footnote footnote-rpl)
  1066. (save-excursion
  1067. (when (search-forward foot-prefix nil t)
  1068. (replace-match "")
  1069. (let ((end (save-excursion
  1070. (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
  1071. (match-beginning 0) (point-max)))))
  1072. (setq footnote (concat (org-trim (buffer-substring (point) end))
  1073. " ")) ; prevent last } being part of a link
  1074. (delete-region (point) end))
  1075. (goto-char foot-beg)
  1076. (delete-region foot-beg foot-end)
  1077. (unless (null footnote)
  1078. (setq footnote-rpl (format "\\footnote{%s}" footnote))
  1079. (add-text-properties 0 10 '(org-protected t) footnote-rpl)
  1080. (add-text-properties (1- (length footnote-rpl))
  1081. (length footnote-rpl)
  1082. '(org-protected t) footnote-rpl)
  1083. (insert footnote-rpl)))))))
  1084. ;; Replace footnote section tag for LaTeX
  1085. (goto-char (point-min))
  1086. (while (re-search-forward
  1087. (concat "^" footnote-section-tag-regexp) nil t)
  1088. (replace-match ""))))
  1089. ;;; List handling:
  1090. (defun org-export-latex-lists ()
  1091. "Replace plain text lists in current buffer into LaTeX lists."
  1092. "Convert lists to LaTeX."
  1093. (goto-char (point-min))
  1094. (while (re-search-forward org-export-latex-list-beginning-re nil t)
  1095. (beginning-of-line)
  1096. (insert (org-list-to-latex (org-list-parse-list t)) "\n")))
  1097. (defun org-list-parse-list (&optional delete)
  1098. "Parse the list at point.
  1099. Return a list containing first level items as strings and
  1100. sublevels as a list of strings."
  1101. (let ((start (org-list-item-begin))
  1102. (end (org-list-end))
  1103. output itemsep)
  1104. (while (re-search-forward org-export-latex-list-beginning-re end t)
  1105. (setq itemsep (if (save-match-data
  1106. (string-match "^[0-9]" (match-string 2)))
  1107. "[0-9]+\\(?:\\.\\|)\\)" "[-+]"))
  1108. (let* ((indent1 (match-string 1))
  1109. (nextitem (save-excursion
  1110. (save-match-data
  1111. (or (and (re-search-forward
  1112. (concat "^" indent1 itemsep " *?") end t)
  1113. (match-beginning 0)) end))))
  1114. (item (buffer-substring
  1115. (point)
  1116. (or (and (re-search-forward
  1117. org-export-latex-list-beginning-re end t)
  1118. (goto-char (match-beginning 0)))
  1119. (goto-char end))))
  1120. (nextindent (match-string 1))
  1121. (item (org-trim item))
  1122. (item (if (string-match "^\\[.+\\]" item)
  1123. (replace-match "\\\\texttt{\\&}"
  1124. t nil item) item)))
  1125. (push item output)
  1126. (when (> (length nextindent)
  1127. (length indent1))
  1128. (narrow-to-region (point) nextitem)
  1129. (push (org-list-parse-list) output)
  1130. (widen))))
  1131. (when delete (delete-region start end))
  1132. (setq output (nreverse output))
  1133. (push (if (string-match "^\\[0" itemsep)
  1134. 'ordered 'unordered) output)))
  1135. (defun org-list-item-begin ()
  1136. "Find the beginning of the list item and return its position."
  1137. (save-excursion
  1138. (if (not (or (looking-at org-export-latex-list-beginning-re)
  1139. (re-search-backward
  1140. org-export-latex-list-beginning-re nil t)))
  1141. (progn (goto-char (point-min)) (point))
  1142. (match-beginning 0))))
  1143. (defun org-list-end ()
  1144. "Find the end of the list and return its position."
  1145. (save-excursion
  1146. (catch 'exit
  1147. (while (or (looking-at org-export-latex-list-beginning-re)
  1148. (looking-at "^[ \t]+\\|^$"))
  1149. (if (eq (point) (point-max))
  1150. (throw 'exit (point-max)))
  1151. (forward-line 1))) (point)))
  1152. (defun org-list-insert-radio-list ()
  1153. "Insert a radio list template appropriate for this major mode."
  1154. (interactive)
  1155. (let* ((e (assq major-mode org-list-radio-list-templates))
  1156. (txt (nth 1 e))
  1157. name pos)
  1158. (unless e (error "No radio list setup defined for %s" major-mode))
  1159. (setq name (read-string "List name: "))
  1160. (while (string-match "%n" txt)
  1161. (setq txt (replace-match name t t txt)))
  1162. (or (bolp) (insert "\n"))
  1163. (setq pos (point))
  1164. (insert txt)
  1165. (goto-char pos)))
  1166. (defun org-list-send-list (&optional maybe)
  1167. "Send a tranformed version of this list to the receiver position.
  1168. With argument MAYBE, fail quietly if no transformation is defined for
  1169. this list."
  1170. (interactive)
  1171. (catch 'exit
  1172. (unless (org-at-item-p) (error "Not at a list"))
  1173. (save-excursion
  1174. (goto-char (org-list-item-begin))
  1175. (beginning-of-line 0)
  1176. (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  1177. (if maybe
  1178. (throw 'exit nil)
  1179. (error "Don't know how to transform this list"))))
  1180. (let* ((name (match-string 1))
  1181. beg
  1182. (transform (intern (match-string 2)))
  1183. (txt (buffer-substring-no-properties
  1184. (org-list-item-begin)
  1185. (org-list-end)))
  1186. (list (org-list-parse-list)))
  1187. (unless (fboundp transform)
  1188. (error "No such transformation function %s" transform))
  1189. (setq txt (funcall transform list))
  1190. ;; Find the insertion place
  1191. (save-excursion
  1192. (goto-char (point-min))
  1193. (unless (re-search-forward
  1194. (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
  1195. (error "Don't know where to insert translated list"))
  1196. (goto-char (match-beginning 0))
  1197. (beginning-of-line 2)
  1198. (setq beg (point))
  1199. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  1200. (error "Cannot find end of insertion region"))
  1201. (beginning-of-line 1)
  1202. (delete-region beg (point))
  1203. (goto-char beg)
  1204. (insert txt "\n"))
  1205. (message "List converted and installed at receiver location"))))
  1206. (defun org-list-to-generic (list params)
  1207. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  1208. Valid parameters are
  1209. :ustart String to start an unordered list
  1210. :uend String to end an unordered list
  1211. :ostart String to start an ordered list
  1212. :oend String to end an ordered list
  1213. :splice When set to t, return only list body lines, don't wrap
  1214. them into :[u/o]start and :[u/o]end. Default is nil.
  1215. :istart String to start a list item
  1216. :iend String to end a list item
  1217. :isep String to separate items
  1218. :lsep String to separate sublists"
  1219. (interactive)
  1220. (let* ((p params) sublist
  1221. (splicep (plist-get p :splice))
  1222. (ostart (plist-get p :ostart))
  1223. (oend (plist-get p :oend))
  1224. (ustart (plist-get p :ustart))
  1225. (uend (plist-get p :uend))
  1226. (istart (plist-get p :istart))
  1227. (iend (plist-get p :iend))
  1228. (isep (plist-get p :isep))
  1229. (lsep (plist-get p :lsep)))
  1230. (let ((wrapper
  1231. (cond ((eq (car list) 'ordered)
  1232. (concat ostart "\n%s" oend "\n"))
  1233. ((eq (car list) 'unordered)
  1234. (concat ustart "\n%s" uend "\n"))))
  1235. rtn)
  1236. (while (setq sublist (pop list))
  1237. (cond ((symbolp sublist) nil)
  1238. ((stringp sublist)
  1239. (setq rtn (concat rtn istart sublist iend isep)))
  1240. (t
  1241. (setq rtn (concat rtn ;; previous list
  1242. lsep ;; list separator
  1243. (org-list-to-generic sublist p)
  1244. lsep ;; list separator
  1245. )))))
  1246. (format wrapper rtn))))
  1247. (defun org-list-to-latex (list)
  1248. "Convert LIST into a LaTeX list."
  1249. (org-list-to-generic
  1250. list '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  1251. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  1252. :istart "\\item " :iend ""
  1253. :isep "\n" :lsep "\n")))
  1254. (defun org-list-to-html (list)
  1255. "Convert LIST into a HTML list."
  1256. (org-list-to-generic
  1257. list '(:splicep nil :ostart "<ol>" :oend "</ol>"
  1258. :ustart "<ul>" :uend "</ul>"
  1259. :istart "<li>" :iend "</li>"
  1260. :isep "\n" :lsep "\n")))
  1261. (defun org-list-to-texinfo (list)
  1262. "Convert LIST into a Texinfo list."
  1263. (org-list-to-generic
  1264. list '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  1265. :ustart "@enumerate" :uend "@end enumerate"
  1266. :istart "@item\n" :iend ""
  1267. :isep "\n" :lsep "\n")))
  1268. (defconst org-latex-entities
  1269. '("\\!"
  1270. "\\'"
  1271. "\\+"
  1272. "\\,"
  1273. "\\-"
  1274. "\\:"
  1275. "\\;"
  1276. "\\<"
  1277. "\\="
  1278. "\\>"
  1279. "\\Huge"
  1280. "\\LARGE"
  1281. "\\Large"
  1282. "\\Styles"
  1283. "\\\\"
  1284. "\\`"
  1285. "\\addcontentsline"
  1286. "\\address"
  1287. "\\addtocontents"
  1288. "\\addtocounter"
  1289. "\\addtolength"
  1290. "\\addvspace"
  1291. "\\alph"
  1292. "\\appendix"
  1293. "\\arabic"
  1294. "\\author"
  1295. "\\begin{array}"
  1296. "\\begin{center}"
  1297. "\\begin{description}"
  1298. "\\begin{enumerate}"
  1299. "\\begin{eqnarray}"
  1300. "\\begin{equation}"
  1301. "\\begin{figure}"
  1302. "\\begin{flushleft}"
  1303. "\\begin{flushright}"
  1304. "\\begin{itemize}"
  1305. "\\begin{list}"
  1306. "\\begin{minipage}"
  1307. "\\begin{picture}"
  1308. "\\begin{quotation}"
  1309. "\\begin{quote}"
  1310. "\\begin{tabbing}"
  1311. "\\begin{table}"
  1312. "\\begin{tabular}"
  1313. "\\begin{thebibliography}"
  1314. "\\begin{theorem}"
  1315. "\\begin{titlepage}"
  1316. "\\begin{verbatim}"
  1317. "\\begin{verse}"
  1318. "\\bf"
  1319. "\\bf"
  1320. "\\bibitem"
  1321. "\\bigskip"
  1322. "\\cdots"
  1323. "\\centering"
  1324. "\\circle"
  1325. "\\cite"
  1326. "\\cleardoublepage"
  1327. "\\clearpage"
  1328. "\\cline"
  1329. "\\closing"
  1330. "\\dashbox"
  1331. "\\date"
  1332. "\\ddots"
  1333. "\\dotfill"
  1334. "\\em"
  1335. "\\fbox"
  1336. "\\flushbottom"
  1337. "\\fnsymbol"
  1338. "\\footnote"
  1339. "\\footnotemark"
  1340. "\\footnotesize"
  1341. "\\footnotetext"
  1342. "\\frac"
  1343. "\\frame"
  1344. "\\framebox"
  1345. "\\hfill"
  1346. "\\hline"
  1347. "\\hrulespace"
  1348. "\\hspace"
  1349. "\\huge"
  1350. "\\hyphenation"
  1351. "\\include"
  1352. "\\includeonly"
  1353. "\\indent"
  1354. "\\input"
  1355. "\\it"
  1356. "\\kill"
  1357. "\\label"
  1358. "\\large"
  1359. "\\ldots"
  1360. "\\line"
  1361. "\\linebreak"
  1362. "\\linethickness"
  1363. "\\listoffigures"
  1364. "\\listoftables"
  1365. "\\location"
  1366. "\\makebox"
  1367. "\\maketitle"
  1368. "\\mark"
  1369. "\\mbox"
  1370. "\\medskip"
  1371. "\\multicolumn"
  1372. "\\multiput"
  1373. "\\newcommand"
  1374. "\\newcounter"
  1375. "\\newenvironment"
  1376. "\\newfont"
  1377. "\\newlength"
  1378. "\\newline"
  1379. "\\newpage"
  1380. "\\newsavebox"
  1381. "\\newtheorem"
  1382. "\\nocite"
  1383. "\\nofiles"
  1384. "\\noindent"
  1385. "\\nolinebreak"
  1386. "\\nopagebreak"
  1387. "\\normalsize"
  1388. "\\onecolumn"
  1389. "\\opening"
  1390. "\\oval"
  1391. "\\overbrace"
  1392. "\\overline"
  1393. "\\pagebreak"
  1394. "\\pagenumbering"
  1395. "\\pageref"
  1396. "\\pagestyle"
  1397. "\\par"
  1398. "\\parbox"
  1399. "\\put"
  1400. "\\raggedbottom"
  1401. "\\raggedleft"
  1402. "\\raggedright"
  1403. "\\raisebox"
  1404. "\\ref"
  1405. "\\rm"
  1406. "\\roman"
  1407. "\\rule"
  1408. "\\savebox"
  1409. "\\sc"
  1410. "\\scriptsize"
  1411. "\\setcounter"
  1412. "\\setlength"
  1413. "\\settowidth"
  1414. "\\sf"
  1415. "\\shortstack"
  1416. "\\signature"
  1417. "\\sl"
  1418. "\\small"
  1419. "\\smallskip"
  1420. "\\sqrt"
  1421. "\\tableofcontents"
  1422. "\\telephone"
  1423. "\\thanks"
  1424. "\\thispagestyle"
  1425. "\\tiny"
  1426. "\\title"
  1427. "\\tt"
  1428. "\\twocolumn"
  1429. "\\typein"
  1430. "\\typeout"
  1431. "\\underbrace"
  1432. "\\underline"
  1433. "\\usebox"
  1434. "\\usecounter"
  1435. "\\value"
  1436. "\\vdots"
  1437. "\\vector"
  1438. "\\verb"
  1439. "\\vfill"
  1440. "\\vline"
  1441. "\\vspace")
  1442. "A list of LaTeX commands to be protected when performing conversion.")
  1443. (provide 'org-export-latex)
  1444. ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
  1445. ;;; org-export-latex.el ends here