org-export-latex.el 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. ;;; org-export-latex.el --- LaTeX exporter for org-mode
  2. ;;
  3. ;; Copyright (c) 2007 free software foundation, inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-export-latex.el
  7. ;; Version: 5.14
  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)
  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. (interactive "P")
  318. ;; Make sure we have a file name when we need it.
  319. (when (and (not (or to-buffer body-only))
  320. (not buffer-file-name))
  321. (if (buffer-base-buffer)
  322. (org-set-local 'buffer-file-name
  323. (with-current-buffer (buffer-base-buffer)
  324. buffer-file-name))
  325. (error "Need a file name to be able to export")))
  326. (message "Exporting to LaTeX...")
  327. (org-update-radio-target-regexp)
  328. (org-export-latex-set-initial-vars ext-plist arg)
  329. (let* ((wcf (current-window-configuration))
  330. (opt-plist org-export-latex-options-plist)
  331. (region-p (org-region-active-p))
  332. (subtree-p
  333. (when region-p
  334. (save-excursion
  335. (goto-char (region-beginning))
  336. (and (org-at-heading-p)
  337. (>= (org-end-of-subtree t t) (region-end))))))
  338. (title (or (and subtree-p (org-export-get-title-from-subtree))
  339. (plist-get opt-plist :title)
  340. (and (not
  341. (plist-get opt-plist :skip-before-1st-heading))
  342. (org-export-grab-title-from-buffer))
  343. (file-name-sans-extension
  344. (file-name-nondirectory buffer-file-name))))
  345. (filename (concat (file-name-as-directory
  346. (org-export-directory :LaTeX ext-plist))
  347. (file-name-sans-extension
  348. (file-name-nondirectory ;sans-extension
  349. buffer-file-name)) ".tex"))
  350. (filename (if (equal (file-truename filename)
  351. (file-truename buffer-file-name))
  352. (concat filename ".tex")
  353. filename))
  354. (buffer (if to-buffer
  355. (cond
  356. ((eq to-buffer 'string) (get-buffer-create
  357. "*Org LaTeX Export*"))
  358. (t (get-buffer-create to-buffer)))
  359. (find-file-noselect filename)))
  360. (odd org-odd-levels-only)
  361. (header (org-export-latex-make-header title opt-plist))
  362. (skip (if subtree-p nil
  363. ;; never skip first lines when exporting a subtree
  364. (plist-get opt-plist :skip-before-1st-heading)))
  365. (text (plist-get opt-plist :text))
  366. (first-lines (if skip "" (org-export-latex-first-lines)))
  367. (coding-system (and (boundp 'buffer-file-coding-system)
  368. buffer-file-coding-system))
  369. (coding-system-for-write (or org-export-latex-coding-system
  370. coding-system))
  371. (save-buffer-coding-system (or org-export-latex-coding-system
  372. coding-system))
  373. (region (buffer-substring
  374. (if region-p (region-beginning) (point-min))
  375. (if region-p (region-end) (point-max))))
  376. (string-for-export
  377. (org-cleaned-string-for-export
  378. region :emph-multiline t
  379. :for-LaTeX t
  380. :comments nil
  381. :add-text (if (eq to-buffer 'string) nil text)
  382. :skip-before-1st-heading skip
  383. :LaTeX-fragments nil)))
  384. (set-buffer buffer)
  385. (erase-buffer)
  386. (and (fboundp 'set-buffer-file-coding-system)
  387. (set-buffer-file-coding-system coding-system-for-write))
  388. ;; insert the header and initial document commands
  389. (unless (or (eq to-buffer 'string) body-only)
  390. (insert header))
  391. ;; insert text found in #+TEXT
  392. (when (and text (not (eq to-buffer 'string)))
  393. (insert (org-export-latex-content
  394. text '(lists tables fixed-width keywords))
  395. "\n\n"))
  396. ;; insert lines before the first headline
  397. (unless (or skip (eq to-buffer 'string))
  398. (insert first-lines))
  399. ;; handle the case where the region does not begin with a section
  400. (when region-p
  401. (insert (with-temp-buffer
  402. (insert string-for-export)
  403. (org-export-latex-first-lines))))
  404. ;; export the content of headlines
  405. (org-export-latex-global
  406. (with-temp-buffer
  407. (insert string-for-export)
  408. (goto-char (point-min))
  409. (when (re-search-forward "^\\(\\*+\\) " nil t)
  410. (let* ((asters (length (match-string 1)))
  411. (level (if odd (- asters 2) (- asters 1))))
  412. (setq org-export-latex-add-level
  413. (if odd (1- (/ (1+ asters) 2)) (1- asters)))
  414. (org-export-latex-parse-global level odd)))))
  415. ;; finalization
  416. (unless body-only (insert "\n\\end{document}"))
  417. (or to-buffer (save-buffer))
  418. (goto-char (point-min))
  419. (message "Exporting to LaTeX...done")
  420. (prog1
  421. (if (eq to-buffer 'string)
  422. (prog1 (buffer-substring (point-min) (point-max))
  423. (kill-buffer (current-buffer)))
  424. (current-buffer))
  425. (set-window-configuration wcf))))
  426. ;;; Parsing functions:
  427. (defun org-export-latex-parse-global (level odd)
  428. "Parse the current buffer recursively, starting at LEVEL.
  429. If ODD is non-nil, assume the buffer only contains odd sections.
  430. Return a list reflecting the document structure."
  431. (save-excursion
  432. (goto-char (point-min))
  433. (let* ((cnt 0) output
  434. (depth org-export-latex-sectioning-depth))
  435. (while (re-search-forward
  436. (concat "^\\(\\(?:\\*\\)\\{"
  437. (number-to-string (+ (if odd 2 1) level))
  438. "\\}\\) \\(.*\\)$")
  439. ;; make sure that there is no upper heading
  440. (when (> level 0)
  441. (save-excursion
  442. (save-match-data
  443. (re-search-forward
  444. (concat "^\\(\\(?:\\*\\)\\{"
  445. (number-to-string level)
  446. "\\}\\) \\(.*\\)$") nil t)))) t)
  447. (setq cnt (1+ cnt))
  448. (let* ((pos (match-beginning 0))
  449. (heading (match-string 2))
  450. (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
  451. (save-excursion
  452. (narrow-to-region
  453. (point)
  454. (save-match-data
  455. (if (re-search-forward
  456. (concat "^\\(\\(?:\\*\\)\\{"
  457. (number-to-string (+ (if odd 2 1) level))
  458. "\\}\\) \\(.*\\)$") nil t)
  459. (match-beginning 0)
  460. (point-max))))
  461. (goto-char (point-min))
  462. (setq output
  463. (append output
  464. (list
  465. (list
  466. `(pos . ,pos)
  467. `(level . ,nlevel)
  468. `(occur . ,cnt)
  469. `(heading . ,heading)
  470. `(content . ,(org-export-latex-parse-content))
  471. `(subcontent . ,(org-export-latex-parse-subcontent
  472. level odd)))))))
  473. (widen)))
  474. (list output))))
  475. (defun org-export-latex-parse-content ()
  476. "Extract the content of a section."
  477. (let ((beg (point))
  478. (end (if (re-search-forward "^\\(\\*\\)+ .*$" nil t)
  479. (progn (beginning-of-line) (point))
  480. (point-max))))
  481. (buffer-substring beg end)))
  482. (defun org-export-latex-parse-subcontent (level odd)
  483. "Extract the subcontent of a section at LEVEL.
  484. If ODD Is non-nil, assume subcontent only contains odd sections."
  485. (if (not (re-search-forward
  486. (concat "^\\(\\(?:\\*\\)\\{"
  487. (number-to-string (+ (if odd 4 2) level))
  488. "\\}\\) \\(.*\\)$")
  489. nil t))
  490. nil ; subcontent is nil
  491. (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
  492. ;;; Rendering functions:
  493. (defun org-export-latex-global (content)
  494. "Export CONTENT to LaTeX.
  495. CONTENT is an element of the list produced by
  496. `org-export-latex-parse-global'."
  497. (if (eq (car content) 'subcontent)
  498. (mapc 'org-export-latex-sub (cdr content))
  499. (org-export-latex-sub (car content))))
  500. (defun org-export-latex-sub (subcontent)
  501. "Export the list SUBCONTENT to LaTeX.
  502. SUBCONTENT is an alist containing information about the headline
  503. and its content."
  504. (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
  505. (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
  506. (defun org-export-latex-subcontent (subcontent num)
  507. "Export each cell of SUBCONTENT to LaTeX."
  508. (let ((heading (org-export-latex-fontify-headline
  509. (cdr (assoc 'heading subcontent))))
  510. (level (- (cdr (assoc 'level subcontent))
  511. org-export-latex-add-level))
  512. (occur (number-to-string (cdr (assoc 'occur subcontent))))
  513. (content (cdr (assoc 'content subcontent)))
  514. (subcontent (cadr (assoc 'subcontent subcontent))))
  515. (cond
  516. ;; Normal conversion
  517. ((<= level org-export-latex-sectioning-depth)
  518. (let ((sec (nth (1- level) org-export-latex-sectioning)))
  519. (insert (format (if num (car sec) (cdr sec)) heading) "\n"))
  520. (insert (org-export-latex-content content))
  521. (cond ((stringp subcontent) (insert subcontent))
  522. ((listp subcontent) (org-export-latex-sub subcontent))))
  523. ;; At a level under the hl option: we can drop this subsection
  524. ((> level org-export-latex-sectioning-depth)
  525. (cond ((eq org-export-latex-low-levels 'description)
  526. (insert (format "\\begin{description}\n\n\\item[%s]\n\n" heading))
  527. (insert (org-export-latex-content content))
  528. (cond ((stringp subcontent) (insert subcontent))
  529. ((listp subcontent) (org-export-latex-sub subcontent)))
  530. (insert "\\end{description}\n"))
  531. ((stringp org-export-latex-low-levels)
  532. (insert (format org-export-latex-low-levels heading) "\n")
  533. (insert (org-export-latex-content content))
  534. (cond ((stringp subcontent) (insert subcontent))
  535. ((listp subcontent) (org-export-latex-sub subcontent)))))))))
  536. ;;; Exporting internals:
  537. (defun org-export-latex-set-initial-vars (ext-plist level)
  538. "Store org local variables required for LaTeX export.
  539. EXT-PLIST is an optional additional plist.
  540. LEVEL indicates the default depth for export."
  541. (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
  542. org-export-latex-all-targets-re
  543. (org-make-target-link-regexp (org-all-targets))
  544. org-export-latex-options-plist
  545. (org-combine-plists (org-default-export-plist) ext-plist
  546. (org-infile-export-plist))
  547. org-export-latex-class
  548. (save-excursion
  549. (goto-char (point-min))
  550. (if (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
  551. (assoc (match-string 1) org-export-latex-classes))
  552. (match-string 1)
  553. org-export-latex-default-class))
  554. org-export-latex-header
  555. (cadr (assoc org-export-latex-class org-export-latex-classes))
  556. org-export-latex-sectioning
  557. (cddr (assoc org-export-latex-class org-export-latex-classes))
  558. org-export-latex-sectioning-depth
  559. (or level
  560. (let ((hl-levels
  561. (plist-get org-export-latex-options-plist :headline-levels))
  562. (sec-depth (length org-export-latex-sectioning)))
  563. (if (> hl-levels sec-depth) sec-depth hl-levels)))))
  564. (defun org-export-latex-make-header (title opt-plist)
  565. "Make the LaTeX header and return it as a string.
  566. TITLE is the current title from the buffer or region.
  567. OPT-PLIST is the options plist for current buffer."
  568. (let ((toc (plist-get opt-plist :table-of-contents))
  569. (author (plist-get opt-plist :author)))
  570. (concat
  571. (if (plist-get opt-plist :time-stamp-file)
  572. (format-time-string "% Created %Y-%m-%d %a %H:%M\n"))
  573. ;; insert LaTeX custom header
  574. org-export-latex-header
  575. "\n"
  576. ;; insert information on LaTeX packages
  577. (when org-export-latex-packages-alist
  578. (mapconcat (lambda(p)
  579. (if (equal "" (car p))
  580. (format "\\usepackage{%s}" (cadr p))
  581. (format "\\usepackage[%s]{%s}"
  582. (car p) (cadr p))))
  583. org-export-latex-packages-alist "\n"))
  584. ;; insert additional commands in the header
  585. org-export-latex-append-header
  586. ;; insert the title
  587. (format
  588. "\n\n\\title{%s}\n"
  589. ;; convert the title
  590. (org-export-latex-content
  591. title '(lists tables fixed-width keywords)))
  592. ;; insert author info
  593. (if (plist-get opt-plist :author-info)
  594. (format "\\author{%s}\n"
  595. (or author user-full-name))
  596. (format "%%\\author{%s}\n"
  597. (or author user-full-name)))
  598. ;; insert the date
  599. (format "\\date{%s}\n"
  600. (format-time-string
  601. (or (plist-get opt-plist :date)
  602. org-export-latex-date-format)))
  603. ;; beginning of the document
  604. "\n\\begin{document}\n\n"
  605. ;; insert the title command
  606. (if (string-match "%s" org-export-latex-title-command)
  607. (format org-export-latex-title-command title)
  608. org-export-latex-title-command)
  609. "\n\n"
  610. ;; table of contents
  611. (when (and org-export-with-toc
  612. (plist-get opt-plist :section-numbers))
  613. (cond ((numberp toc)
  614. (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
  615. (min toc (plist-get opt-plist :headline-levels))))
  616. (toc (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\n"
  617. (plist-get opt-plist :headline-levels))))))))
  618. (defun org-export-latex-first-lines (&optional comments)
  619. "Export the first lines before first headline.
  620. COMMENTS is either nil to replace them with the empty string or a
  621. formatting string like %%%%s if we want to comment them out."
  622. (save-excursion
  623. (goto-char (point-min))
  624. (if (org-at-heading-p) (beginning-of-line 2))
  625. (let* ((pt (point))
  626. (end (if (and (re-search-forward "^\\* " nil t)
  627. (not (eq pt (match-beginning 0))))
  628. (goto-char (match-beginning 0))
  629. (goto-char (point-max)))))
  630. (org-export-latex-content
  631. (org-cleaned-string-for-export
  632. (buffer-substring (point-min) end)
  633. :for-LaTeX t
  634. :emph-multiline t
  635. :add-text nil
  636. :comments nil
  637. :skip-before-1st-heading nil
  638. :LaTeX-fragments nil)))))
  639. (defun org-export-latex-content (content &optional exclude-list)
  640. "Convert CONTENT string to LaTeX.
  641. Don't perform conversions that are in EXCLUDE-LIST. Recognized
  642. conversion types are: quotation-marks, emphasis, sub-superscript,
  643. links, keywords, lists, tables, fixed-width"
  644. (with-temp-buffer
  645. (insert content)
  646. (unless (memq 'quotation-marks exclude-list)
  647. (org-export-latex-quotation-marks))
  648. (unless (memq 'emphasis exclude-list)
  649. (when (plist-get org-export-latex-options-plist :emphasize)
  650. (org-export-latex-fontify)))
  651. (unless (memq 'sub-superscript exclude-list)
  652. (org-export-latex-special-chars
  653. (plist-get org-export-latex-options-plist :sub-superscript)))
  654. (unless (memq 'links exclude-list)
  655. (org-export-latex-links))
  656. (unless (memq 'keywords exclude-list)
  657. (org-export-latex-keywords
  658. (plist-get org-export-latex-options-plist :timestamps)))
  659. (unless (memq 'lists exclude-list)
  660. (org-export-latex-lists))
  661. (unless (memq 'tables exclude-list)
  662. (org-export-latex-tables
  663. (plist-get org-export-latex-options-plist :tables)))
  664. (unless (memq 'fixed-width exclude-list)
  665. (org-export-latex-fixed-width
  666. (plist-get org-export-latex-options-plist :fixed-width)))
  667. ;; return string
  668. (buffer-substring (point-min) (point-max))))
  669. (defun org-export-latex-protect-string (s)
  670. "Prevent further conversion for string S by adding the
  671. org-protect property."
  672. (add-text-properties 0 (length s) '(org-protected t) s) s)
  673. (defun org-export-latex-protect-char-in-string (char-list string)
  674. "Add org-protected text-property to char from CHAR-LIST in STRING."
  675. (with-temp-buffer
  676. (save-match-data
  677. (insert string)
  678. (goto-char (point-min))
  679. (while (re-search-forward (regexp-opt char-list) nil t)
  680. (add-text-properties (match-beginning 0)
  681. (match-end 0) '(org-protected t)))
  682. (buffer-string))))
  683. (defun org-export-latex-keywords-maybe (remove-list)
  684. "Maybe remove keywords depending on rules in REMOVE-LIST."
  685. (goto-char (point-min))
  686. (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
  687. (case-fold-search nil))
  688. ;; convert TODO keywords
  689. (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
  690. (if (plist-get remove-list :todo)
  691. (replace-match "")
  692. (replace-match (format "\\texttt{%s}" (match-string 1)) t t)))
  693. ;; convert priority string
  694. (when (re-search-forward "\\[\\\\#.\\]" nil t)
  695. (if (plist-get remove-list :priority)
  696. (replace-match "")
  697. (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))
  698. ;; convert tags
  699. (when (re-search-forward "\\(:[a-zA-Z0-9]+\\)+:" nil t)
  700. (if (or (not org-export-with-tags)
  701. (plist-get remove-list :tags))
  702. (replace-match "")
  703. (replace-match (format "\\texttt{%s}" (match-string 0)) t t)))))
  704. (defun org-export-latex-fontify-headline (string)
  705. "Fontify special words in string."
  706. (with-temp-buffer
  707. ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
  708. ;; the beginning of the buffer - inserting "\n" is safe here though.
  709. (insert "\n" string)
  710. (goto-char (point-min))
  711. (when (plist-get org-export-latex-options-plist :emphasize)
  712. (org-export-latex-fontify))
  713. (org-export-latex-special-chars
  714. (plist-get org-export-latex-options-plist :sub-superscript))
  715. (org-export-latex-keywords-maybe
  716. org-export-latex-remove-from-headlines)
  717. (org-export-latex-links)
  718. (org-trim (buffer-substring-no-properties (point-min) (point-max)))))
  719. (defun org-export-latex-quotation-marks ()
  720. "Export question marks depending on language conventions."
  721. (let* ((lang (plist-get org-export-latex-options-plist :language))
  722. (quote-rpl (if (equal lang "fr")
  723. '(("\\(\\s-\\)\"" "«~")
  724. ("\\(\\S-\\)\"" "~»")
  725. ("\\(\\s-\\)'" "`"))
  726. '(("\\(\\s-\\)\"" "``")
  727. ("\\(\\S-\\)\"" "''")
  728. ("\\(\\s-\\)'" "`")))))
  729. (mapc (lambda(l) (goto-char (point-min))
  730. (while (re-search-forward (car l) nil t)
  731. (let ((rpl (concat (match-string 1) (cadr l))))
  732. (org-export-latex-protect-string rpl)
  733. (org-if-unprotected
  734. (replace-match rpl t t))))) quote-rpl)))
  735. (defun org-export-latex-special-chars (sub-superscript)
  736. "Export special characters to LaTeX.
  737. If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
  738. See the `org-export-latex.el' code for a complete conversion table."
  739. (goto-char (point-min))
  740. (mapc (lambda(c)
  741. (goto-char (point-min))
  742. (while (re-search-forward c nil t)
  743. ;; Put the point where to check for org-protected
  744. (unless (or (get-text-property (match-beginning 2) 'org-protected)
  745. (org-at-table-p))
  746. (cond ((member (match-string 2) '("\\$" "$"))
  747. (if (equal (match-string 2) "\\$")
  748. (replace-match (concat (match-string 1) "$"
  749. (match-string 3)) t t)
  750. (replace-match (concat (match-string 1) "\\$"
  751. (match-string 3)) t t)))
  752. ((member (match-string 2) '("&" "%" "#"))
  753. (if (equal (match-string 1) "\\")
  754. (replace-match (match-string 2) t t)
  755. (replace-match (concat (match-string 1) "\\"
  756. (match-string 2)) t t)))
  757. ((equal (match-string 2) "...")
  758. (replace-match
  759. (concat (match-string 1)
  760. (org-export-latex-protect-string "\\ldots{}")) t t))
  761. ((equal (match-string 2) "~")
  762. (cond ((equal (match-string 1) "\\") nil)
  763. ((eq 'org-link (get-text-property 0 'face (match-string 2)))
  764. (replace-match (concat (match-string 1) "\\~") t t))
  765. (t (replace-match
  766. (org-export-latex-protect-string
  767. (concat (match-string 1) "\\~{}")) t t))))
  768. ((member (match-string 2) '("{" "}"))
  769. (unless (save-match-data (org-inside-LaTeX-fragment-p))
  770. (if (equal (match-string 1) "\\")
  771. (replace-match (match-string 2) t t)
  772. (replace-match (concat (match-string 1) "\\"
  773. (match-string 2)) t t)))))
  774. (unless (save-match-data (org-inside-LaTeX-fragment-p))
  775. (cond ((equal (match-string 2) "\\")
  776. (replace-match (or (save-match-data
  777. (org-export-latex-treat-backslash-char
  778. (match-string 1)
  779. (match-string 3))) "") t t))
  780. ((member (match-string 2) '("_" "^"))
  781. (replace-match (or (save-match-data
  782. (org-export-latex-treat-sub-super-char
  783. sub-superscript
  784. (match-string 1)
  785. (match-string 2)
  786. (match-string 3))) "") t t)))))))
  787. '("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
  788. "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
  789. "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
  790. "\\(.\\|^\\)\\(&\\)"
  791. "\\(.\\|^\\)\\(#\\)"
  792. "\\(.\\|^\\)\\(%\\)"
  793. "\\(.\\|^\\)\\({\\)"
  794. "\\(.\\|^\\)\\(}\\)"
  795. "\\(.\\|^\\)\\(~\\)"
  796. "\\(.\\|^\\)\\(\\.\\.\\.\\)"
  797. ;; (?\< . "\\textless{}")
  798. ;; (?\> . "\\textgreater{}")
  799. )))
  800. (defun org-export-latex-treat-sub-super-char
  801. (subsup string-before char string-after)
  802. "Convert the \"_\" and \"^\" characters to LaTeX.
  803. SUBSUP corresponds to the ^: option in the #+OPTIONS line.
  804. Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
  805. (cond ((equal string-before "\\")
  806. (concat string-before char string-after))
  807. ;; this is part of a math formula
  808. ((and (string-match "\\S-+" string-before)
  809. (string-match "\\S-+" string-after))
  810. (cond ((eq 'org-link (get-text-property 0 'face char))
  811. (concat string-before "\\" char string-after))
  812. ((save-match-data (org-inside-LaTeX-fragment-p))
  813. (if subsup
  814. (cond ((eq 1 (length string-after))
  815. (concat string-before char string-after))
  816. ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
  817. (format "%s%s{%s}" string-before char
  818. (match-string 1 string-after))))))
  819. ((and subsup
  820. (> (length string-after) 1)
  821. (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
  822. (format "$%s%s{%s}$" string-before char
  823. (match-string 1 string-after)))
  824. (subsup (concat "$" string-before char string-after "$"))
  825. (t (org-export-latex-protect-string
  826. (concat string-before "\\" char "{}" string-after)))))
  827. (t (org-export-latex-protect-string
  828. (concat string-before "\\" char "{}" string-after)))))
  829. (defun org-export-latex-treat-backslash-char (string-before string-after)
  830. "Convert the \"$\" special character to LaTeX.
  831. The conversion is made depending of STRING-BEFORE and STRING-AFTER."
  832. (cond ((member (list string-after) org-html-entities)
  833. ;; backslash is part of a special entity (like "\alpha")
  834. (concat string-before "$\\"
  835. (or (cdar (member (list string-after) org-html-entities))
  836. string-after) "$"))
  837. ((and (not (string-match "^[ \n\t]" string-after))
  838. (not (string-match "[ \t]\\'\\|^" string-before)))
  839. ;; backslash is inside a word
  840. (org-export-latex-protect-string
  841. (concat string-before "\\textbackslash{}" string-after)))
  842. ((not (or (equal string-after "")
  843. (string-match "^[ \t\n]" string-after)))
  844. ;; backslash might escape a character (like \#) or a user TeX
  845. ;; macro (like \setcounter)
  846. (org-export-latex-protect-string
  847. (concat string-before "\\" string-after)))
  848. ((and (string-match "^[ \t\n]" string-after)
  849. (string-match "[ \t\n]\\'" string-before))
  850. ;; backslash is alone, convert it to $\backslash$
  851. (org-export-latex-protect-string
  852. (concat string-before "\\textbackslash{}" string-after)))
  853. (t (org-export-latex-protect-string
  854. (concat string-before "\\textbackslash{}" string-after)))))
  855. (defun org-export-latex-keywords (timestamps)
  856. "Convert special keywords to LaTeX.
  857. Regexps are those from `org-export-latex-special-string-regexps'."
  858. (let ((rg org-export-latex-special-string-regexps) r)
  859. (while (setq r (pop rg))
  860. (goto-char (point-min))
  861. (while (re-search-forward (eval r) nil t)
  862. (if (not timestamps)
  863. (replace-match (format "\\\\texttt{%s}" (match-string 0)) t)
  864. (replace-match ""))))))
  865. (defun org-export-latex-fixed-width (opt)
  866. "When OPT is non-nil convert fixed-width sections to LaTeX."
  867. (goto-char (point-min))
  868. (while (re-search-forward "^[ \t]*:" nil t)
  869. (if opt
  870. (progn (goto-char (match-beginning 0))
  871. (insert "\\begin{verbatim}\n")
  872. (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
  873. (replace-match (concat (match-string 1)
  874. (match-string 2)) t t)
  875. (forward-line))
  876. (insert "\\end{verbatim}\n\n"))
  877. (progn (goto-char (match-beginning 0))
  878. (while (looking-at "^\\([ \t]*\\):\\(.*\\)$")
  879. (replace-match (concat "%" (match-string 1)
  880. (match-string 2)) t t)
  881. (forward-line))))))
  882. (defun org-export-latex-tables (insert)
  883. "Convert tables to LaTeX and INSERT it."
  884. (goto-char (point-min))
  885. (while (re-search-forward "^\\([ \t]*\\)|" nil t)
  886. ;; FIXME really need to save-excursion?
  887. (save-excursion (org-table-align))
  888. (let* ((beg (org-table-begin))
  889. (end (org-table-end))
  890. (raw-table (buffer-substring-no-properties beg end))
  891. fnum fields line lines olines gr colgropen line-fmt align)
  892. (if org-export-latex-tables-verbatim
  893. (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
  894. "\\end{verbatim}\n")))
  895. (apply 'delete-region (list beg end))
  896. (insert tbl))
  897. (progn
  898. (setq lines (split-string raw-table "\n" t))
  899. (apply 'delete-region (list beg end))
  900. (when org-export-table-remove-special-lines
  901. (setq lines (org-table-clean-before-export lines)))
  902. ;; make a formatting string to reflect aligment
  903. (setq olines lines)
  904. (while (and (not line-fmt) (setq line (pop olines)))
  905. (unless (string-match "^[ \t]*|-" line)
  906. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  907. (setq fnum (make-vector (length fields) 0))
  908. (setq line-fmt
  909. (mapconcat
  910. (lambda (x)
  911. (setq gr (pop org-table-colgroup-info))
  912. (format "%s%%s%s"
  913. (cond ((eq gr ':start)
  914. (prog1 (if colgropen "|" "")
  915. (setq colgropen t)))
  916. ((eq gr ':startend)
  917. (prog1 (if colgropen "|" "|")
  918. (setq colgropen nil)))
  919. (t ""))
  920. (if (memq gr '(:end :startend))
  921. (progn (setq colgropen nil) "|")
  922. "")))
  923. fnum ""))))
  924. ;; fix double || in line-fmt
  925. (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
  926. ;; maybe remove the first and last "|"
  927. (when (and (not org-export-latex-tables-column-borders)
  928. (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
  929. (setq line-fmt (match-string 2 line-fmt)))
  930. ;; format alignment
  931. (setq align (apply 'format
  932. (cons line-fmt
  933. (mapcar (lambda (x) (if x "r" "l"))
  934. org-table-last-alignment))))
  935. ;; prepare the table to send to orgtbl-to-latex
  936. (setq lines
  937. (mapcar
  938. (lambda(elem)
  939. (or (and (string-match "[ \t]*|-+" elem) 'hline)
  940. (split-string (org-trim elem) "|" t)))
  941. lines))
  942. (when insert
  943. (insert (orgtbl-to-latex
  944. lines `(:tstart ,(concat "\\begin{tabular}{" align "}")))
  945. "\n\n")))))))
  946. (defun org-export-latex-fontify ()
  947. "Convert fontification to LaTeX."
  948. (goto-char (point-min))
  949. (while (re-search-forward org-emph-re nil t)
  950. ;; The match goes one char after the *string*
  951. (let ((emph (assoc (match-string 3)
  952. org-export-latex-emphasis-alist))
  953. rpl)
  954. (unless (get-text-property (1- (point)) 'org-protected)
  955. (setq rpl (concat (match-string 1)
  956. (format (org-export-latex-protect-char-in-string
  957. '("\\" "{" "}") (cadr emph))
  958. (match-string 4))
  959. (match-string 5)))
  960. (if (caddr emph)
  961. (setq rpl (org-export-latex-protect-string rpl)))
  962. (replace-match rpl t t)))
  963. (backward-char)))
  964. (defun org-export-latex-links ()
  965. ;; Make sure to use the LaTeX hyperref and graphicx package
  966. ;; or send some warnings.
  967. "Convert links to LaTeX."
  968. (goto-char (point-min))
  969. (while (re-search-forward org-bracket-link-analytic-regexp nil t)
  970. (org-if-unprotected
  971. (goto-char (match-beginning 0))
  972. (let* ((re-radio org-export-latex-all-targets-re)
  973. (remove (list (match-beginning 0) (match-end 0)))
  974. (type (match-string 2))
  975. (raw-path (match-string 3))
  976. (full-raw-path (concat (match-string 1) raw-path))
  977. (desc (match-string 5))
  978. imgp radiop
  979. ;; define the path of the link
  980. (path (cond
  981. ((member type '("http" "https" "ftp"))
  982. (concat type ":" raw-path))
  983. ((and re-radio (string-match re-radio raw-path))
  984. (setq radiop t))
  985. ((equal type "mailto")
  986. (concat type ":" raw-path))
  987. ((equal type "file")
  988. (if (and (or (org-file-image-p (expand-file-name raw-path))
  989. (string-match "\\.eps$" raw-path))
  990. (equal desc full-raw-path))
  991. (setq imgp t)
  992. (progn (when (string-match "\\(.+\\)::.+" raw-path)
  993. (setq raw-path (match-string 1 raw-path)))
  994. (if (file-exists-p raw-path)
  995. (concat type "://" (expand-file-name raw-path))
  996. (concat type "://" (org-export-directory
  997. :LaTeX org-export-latex-options-plist)
  998. raw-path))))))))
  999. ;; process with link inserting
  1000. (apply 'delete-region remove)
  1001. (cond ((and imgp (plist-get org-export-latex-options-plist :inline-images))
  1002. (insert (format "\\includegraphics[%s]{%s}"
  1003. ;; image option should be set be a comment line
  1004. org-export-latex-image-default-option
  1005. (expand-file-name raw-path))))
  1006. (radiop (insert (format "\\hyperref[%s]{%s}" raw-path desc)))
  1007. (path (insert (format "\\href{%s}{%s}" path desc)))
  1008. (t (insert "\\texttt{" desc "}")))))))
  1009. (defvar org-latex-entities) ; defined below
  1010. (defun org-export-latex-cleaned-string ()
  1011. "Clean stuff in the LaTeX export."
  1012. ;; Preserve line breaks
  1013. (goto-char (point-min))
  1014. (while (re-search-forward "\\\\\\\\" nil t)
  1015. (add-text-properties (match-beginning 0) (match-end 0)
  1016. '(org-protected t)))
  1017. ;; Convert LaTeX to \LaTeX{}
  1018. (goto-char (point-min))
  1019. (let ((case-fold-search nil) rpl)
  1020. (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
  1021. (replace-match (org-export-latex-protect-string
  1022. (concat (match-string 1) "\\LaTeX{}")) t t)))
  1023. ;; Convert horizontal rules
  1024. (goto-char (point-min))
  1025. (while (re-search-forward "^----+.$" nil t)
  1026. (replace-match (org-export-latex-protect-string "\\hrule") t t))
  1027. ;; Protect LaTeX commands like \commad[...]{...} or \command{...}
  1028. (goto-char (point-min))
  1029. (while (re-search-forward "\\\\[a-zA-Z]+\\(?:\\[.*\\]\\)?{.*}" nil t)
  1030. (add-text-properties (match-beginning 0) (match-end 0)
  1031. '(org-protected t)))
  1032. ;; Protect LaTeX entities
  1033. (goto-char (point-min))
  1034. (while (re-search-forward (regexp-opt org-latex-entities) nil t)
  1035. (add-text-properties (match-beginning 0) (match-end 0)
  1036. '(org-protected t)))
  1037. ;; Replace radio links
  1038. (goto-char (point-min))
  1039. (while (re-search-forward
  1040. (concat "<<<?" org-export-latex-all-targets-re
  1041. ">>>?\\((INVISIBLE)\\)?") nil t)
  1042. (replace-match
  1043. (org-export-latex-protect-string
  1044. (format "\\label{%s}%s"(match-string 1)
  1045. (if (match-string 2) "" (match-string 1)))) t t))
  1046. ;; Delete @<...> constructs
  1047. (goto-char (point-min))
  1048. ;; Thanks to Daniel Clemente for this regexp
  1049. (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
  1050. (replace-match ""))
  1051. ;; When converting to LaTeX, replace footnotes
  1052. ;; FIXME: don't protect footnotes from conversion
  1053. (when (plist-get org-export-latex-options-plist :footnotes)
  1054. (goto-char (point-min))
  1055. (while (re-search-forward "\\[[0-9]+\\]" nil t)
  1056. (when (save-match-data
  1057. (save-excursion (beginning-of-line)
  1058. (looking-at "[^:|#]")))
  1059. (let ((foot-beg (match-beginning 0))
  1060. (foot-end (match-end 0))
  1061. (foot-prefix (match-string 0))
  1062. footnote footnote-rpl)
  1063. (save-excursion
  1064. (when (search-forward foot-prefix nil t)
  1065. (replace-match "")
  1066. (let ((end (save-excursion
  1067. (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
  1068. (match-beginning 0) (point-max)))))
  1069. (setq footnote (concat (org-trim (buffer-substring (point) end))
  1070. " ")) ; prevent last } being part of a link
  1071. (delete-region (point) end))
  1072. (goto-char foot-beg)
  1073. (delete-region foot-beg foot-end)
  1074. (unless (null footnote)
  1075. (setq footnote-rpl (format "\\footnote{%s}" footnote))
  1076. (add-text-properties 0 10 '(org-protected t) footnote-rpl)
  1077. (add-text-properties (1- (length footnote-rpl))
  1078. (length footnote-rpl)
  1079. '(org-protected t) footnote-rpl)
  1080. (insert footnote-rpl)))))))
  1081. ;; Replace footnote section tag for LaTeX
  1082. (goto-char (point-min))
  1083. (while (re-search-forward
  1084. (concat "^" footnote-section-tag-regexp) nil t)
  1085. (replace-match ""))))
  1086. ;;; List handling:
  1087. (defun org-export-latex-lists ()
  1088. "Replace plain text lists in current buffer into LaTeX lists."
  1089. "Convert lists to LaTeX."
  1090. (goto-char (point-min))
  1091. (while (re-search-forward org-export-latex-list-beginning-re nil t)
  1092. (beginning-of-line)
  1093. (insert (org-list-to-latex (org-list-parse-list t)) "\n")))
  1094. (defun org-list-parse-list (&optional delete)
  1095. "Parse the list at point.
  1096. Return a list containing first level items as strings and
  1097. sublevels as a list of strings."
  1098. (let ((start (org-list-item-begin))
  1099. (end (org-list-end))
  1100. output itemsep)
  1101. (while (re-search-forward org-export-latex-list-beginning-re end t)
  1102. (setq itemsep (if (save-match-data
  1103. (string-match "^[0-9]" (match-string 2)))
  1104. "[0-9]+\\(?:\\.\\|)\\)" "[-+]"))
  1105. (let* ((indent1 (match-string 1))
  1106. (nextitem (save-excursion
  1107. (save-match-data
  1108. (or (and (re-search-forward
  1109. (concat "^" indent1 itemsep " *?") end t)
  1110. (match-beginning 0)) end))))
  1111. (item (buffer-substring
  1112. (point)
  1113. (or (and (re-search-forward
  1114. org-export-latex-list-beginning-re end t)
  1115. (goto-char (match-beginning 0)))
  1116. (goto-char end))))
  1117. (nextindent (match-string 1))
  1118. (item (org-trim item))
  1119. (item (if (string-match "^\\[.+\\]" item)
  1120. (replace-match "\\\\texttt{\\&}"
  1121. t nil item) item)))
  1122. (push item output)
  1123. (when (> (length nextindent)
  1124. (length indent1))
  1125. (narrow-to-region (point) nextitem)
  1126. (push (org-list-parse-list) output)
  1127. (widen))))
  1128. (when delete (delete-region start end))
  1129. (setq output (nreverse output))
  1130. (push (if (string-match "^\\[0" itemsep)
  1131. 'ordered 'unordered) output)))
  1132. (defun org-list-item-begin ()
  1133. "Find the beginning of the list item and return its position."
  1134. (save-excursion
  1135. (if (not (or (looking-at org-export-latex-list-beginning-re)
  1136. (re-search-backward
  1137. org-export-latex-list-beginning-re nil t)))
  1138. (progn (goto-char (point-min)) (point))
  1139. (match-beginning 0))))
  1140. (defun org-list-end ()
  1141. "Find the end of the list and return its position."
  1142. (save-excursion
  1143. (catch 'exit
  1144. (while (or (looking-at org-export-latex-list-beginning-re)
  1145. (looking-at "^[ \t]+\\|^$"))
  1146. (if (eq (point) (point-max))
  1147. (throw 'exit (point-max)))
  1148. (forward-line 1))) (point)))
  1149. (defun org-list-insert-radio-list ()
  1150. "Insert a radio list template appropriate for this major mode."
  1151. (interactive)
  1152. (let* ((e (assq major-mode org-list-radio-list-templates))
  1153. (txt (nth 1 e))
  1154. name pos)
  1155. (unless e (error "No radio list setup defined for %s" major-mode))
  1156. (setq name (read-string "List name: "))
  1157. (while (string-match "%n" txt)
  1158. (setq txt (replace-match name t t txt)))
  1159. (or (bolp) (insert "\n"))
  1160. (setq pos (point))
  1161. (insert txt)
  1162. (goto-char pos)))
  1163. (defun org-list-send-list (&optional maybe)
  1164. "Send a tranformed version of this list to the receiver position.
  1165. With argument MAYBE, fail quietly if no transformation is defined for
  1166. this list."
  1167. (interactive)
  1168. (catch 'exit
  1169. (unless (org-at-item-p) (error "Not at a list"))
  1170. (save-excursion
  1171. (goto-char (org-list-item-begin))
  1172. (beginning-of-line 0)
  1173. (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  1174. (if maybe
  1175. (throw 'exit nil)
  1176. (error "Don't know how to transform this list"))))
  1177. (let* ((name (match-string 1))
  1178. beg
  1179. (transform (intern (match-string 2)))
  1180. (txt (buffer-substring-no-properties
  1181. (org-list-item-begin)
  1182. (org-list-end)))
  1183. (list (org-list-parse-list)))
  1184. (unless (fboundp transform)
  1185. (error "No such transformation function %s" transform))
  1186. (setq txt (funcall transform list))
  1187. ;; Find the insertion place
  1188. (save-excursion
  1189. (goto-char (point-min))
  1190. (unless (re-search-forward
  1191. (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
  1192. (error "Don't know where to insert translated list"))
  1193. (goto-char (match-beginning 0))
  1194. (beginning-of-line 2)
  1195. (setq beg (point))
  1196. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  1197. (error "Cannot find end of insertion region"))
  1198. (beginning-of-line 1)
  1199. (delete-region beg (point))
  1200. (goto-char beg)
  1201. (insert txt "\n"))
  1202. (message "List converted and installed at receiver location"))))
  1203. (defun org-list-to-generic (list params)
  1204. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  1205. Valid parameters are
  1206. :ustart String to start an unordered list
  1207. :uend String to end an unordered list
  1208. :ostart String to start an ordered list
  1209. :oend String to end an ordered list
  1210. :splice When set to t, return only list body lines, don't wrap
  1211. them into :[u/o]start and :[u/o]end. Default is nil.
  1212. :istart String to start a list item
  1213. :iend String to end a list item
  1214. :isep String to separate items
  1215. :lsep String to separate sublists"
  1216. (interactive)
  1217. (let* ((p params) sublist
  1218. (splicep (plist-get p :splice))
  1219. (ostart (plist-get p :ostart))
  1220. (oend (plist-get p :oend))
  1221. (ustart (plist-get p :ustart))
  1222. (uend (plist-get p :uend))
  1223. (istart (plist-get p :istart))
  1224. (iend (plist-get p :iend))
  1225. (isep (plist-get p :isep))
  1226. (lsep (plist-get p :lsep)))
  1227. (let ((wrapper
  1228. (cond ((eq (car list) 'ordered)
  1229. (concat ostart "\n%s" oend "\n"))
  1230. ((eq (car list) 'unordered)
  1231. (concat ustart "\n%s" uend "\n"))))
  1232. rtn)
  1233. (while (setq sublist (pop list))
  1234. (cond ((symbolp sublist) nil)
  1235. ((stringp sublist)
  1236. (setq rtn (concat rtn istart sublist iend isep)))
  1237. (t
  1238. (setq rtn (concat rtn ;; previous list
  1239. lsep ;; list separator
  1240. (org-list-to-generic sublist p)
  1241. lsep ;; list separator
  1242. )))))
  1243. (format wrapper rtn))))
  1244. (defun org-list-to-latex (list)
  1245. "Convert LIST into a LaTeX list."
  1246. (org-list-to-generic
  1247. list '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  1248. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  1249. :istart "\\item " :iend ""
  1250. :isep "\n" :lsep "\n")))
  1251. (defun org-list-to-html (list)
  1252. "Convert LIST into a HTML list."
  1253. (org-list-to-generic
  1254. list '(:splicep nil :ostart "<ol>" :oend "</ol>"
  1255. :ustart "<ul>" :uend "</ul>"
  1256. :istart "<li>" :iend "</li>"
  1257. :isep "\n" :lsep "\n")))
  1258. (defun org-list-to-texinfo (list)
  1259. "Convert LIST into a Texinfo list."
  1260. (org-list-to-generic
  1261. list '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  1262. :ustart "@enumerate" :uend "@end enumerate"
  1263. :istart "@item\n" :iend ""
  1264. :isep "\n" :lsep "\n")))
  1265. (defconst org-latex-entities
  1266. '("\\!"
  1267. "\\'"
  1268. "\\+"
  1269. "\\,"
  1270. "\\-"
  1271. "\\:"
  1272. "\\;"
  1273. "\\<"
  1274. "\\="
  1275. "\\>"
  1276. "\\Huge"
  1277. "\\LARGE"
  1278. "\\Large"
  1279. "\\Styles"
  1280. "\\\\"
  1281. "\\`"
  1282. "\\addcontentsline"
  1283. "\\address"
  1284. "\\addtocontents"
  1285. "\\addtocounter"
  1286. "\\addtolength"
  1287. "\\addvspace"
  1288. "\\alph"
  1289. "\\appendix"
  1290. "\\arabic"
  1291. "\\author"
  1292. "\\begin{array}"
  1293. "\\begin{center}"
  1294. "\\begin{description}"
  1295. "\\begin{enumerate}"
  1296. "\\begin{eqnarray}"
  1297. "\\begin{equation}"
  1298. "\\begin{figure}"
  1299. "\\begin{flushleft}"
  1300. "\\begin{flushright}"
  1301. "\\begin{itemize}"
  1302. "\\begin{list}"
  1303. "\\begin{minipage}"
  1304. "\\begin{picture}"
  1305. "\\begin{quotation}"
  1306. "\\begin{quote}"
  1307. "\\begin{tabbing}"
  1308. "\\begin{table}"
  1309. "\\begin{tabular}"
  1310. "\\begin{thebibliography}"
  1311. "\\begin{theorem}"
  1312. "\\begin{titlepage}"
  1313. "\\begin{verbatim}"
  1314. "\\begin{verse}"
  1315. "\\bf"
  1316. "\\bf"
  1317. "\\bibitem"
  1318. "\\bigskip"
  1319. "\\cdots"
  1320. "\\centering"
  1321. "\\circle"
  1322. "\\cite"
  1323. "\\cleardoublepage"
  1324. "\\clearpage"
  1325. "\\cline"
  1326. "\\closing"
  1327. "\\dashbox"
  1328. "\\date"
  1329. "\\ddots"
  1330. "\\dotfill"
  1331. "\\em"
  1332. "\\fbox"
  1333. "\\flushbottom"
  1334. "\\fnsymbol"
  1335. "\\footnote"
  1336. "\\footnotemark"
  1337. "\\footnotesize"
  1338. "\\footnotetext"
  1339. "\\frac"
  1340. "\\frame"
  1341. "\\framebox"
  1342. "\\hfill"
  1343. "\\hline"
  1344. "\\hrulespace"
  1345. "\\hspace"
  1346. "\\huge"
  1347. "\\hyphenation"
  1348. "\\include"
  1349. "\\includeonly"
  1350. "\\indent"
  1351. "\\input"
  1352. "\\it"
  1353. "\\kill"
  1354. "\\label"
  1355. "\\large"
  1356. "\\ldots"
  1357. "\\line"
  1358. "\\linebreak"
  1359. "\\linethickness"
  1360. "\\listoffigures"
  1361. "\\listoftables"
  1362. "\\location"
  1363. "\\makebox"
  1364. "\\maketitle"
  1365. "\\mark"
  1366. "\\mbox"
  1367. "\\medskip"
  1368. "\\multicolumn"
  1369. "\\multiput"
  1370. "\\newcommand"
  1371. "\\newcounter"
  1372. "\\newenvironment"
  1373. "\\newfont"
  1374. "\\newlength"
  1375. "\\newline"
  1376. "\\newpage"
  1377. "\\newsavebox"
  1378. "\\newtheorem"
  1379. "\\nocite"
  1380. "\\nofiles"
  1381. "\\noindent"
  1382. "\\nolinebreak"
  1383. "\\nopagebreak"
  1384. "\\normalsize"
  1385. "\\onecolumn"
  1386. "\\opening"
  1387. "\\oval"
  1388. "\\overbrace"
  1389. "\\overline"
  1390. "\\pagebreak"
  1391. "\\pagenumbering"
  1392. "\\pageref"
  1393. "\\pagestyle"
  1394. "\\par"
  1395. "\\parbox"
  1396. "\\put"
  1397. "\\raggedbottom"
  1398. "\\raggedleft"
  1399. "\\raggedright"
  1400. "\\raisebox"
  1401. "\\ref"
  1402. "\\rm"
  1403. "\\roman"
  1404. "\\rule"
  1405. "\\savebox"
  1406. "\\sc"
  1407. "\\scriptsize"
  1408. "\\setcounter"
  1409. "\\setlength"
  1410. "\\settowidth"
  1411. "\\sf"
  1412. "\\shortstack"
  1413. "\\signature"
  1414. "\\sl"
  1415. "\\small"
  1416. "\\smallskip"
  1417. "\\sqrt"
  1418. "\\tableofcontents"
  1419. "\\telephone"
  1420. "\\thanks"
  1421. "\\thispagestyle"
  1422. "\\tiny"
  1423. "\\title"
  1424. "\\tt"
  1425. "\\twocolumn"
  1426. "\\typein"
  1427. "\\typeout"
  1428. "\\underbrace"
  1429. "\\underline"
  1430. "\\usebox"
  1431. "\\usecounter"
  1432. "\\value"
  1433. "\\vdots"
  1434. "\\vector"
  1435. "\\verb"
  1436. "\\vfill"
  1437. "\\vline"
  1438. "\\vspace")
  1439. "A list of LaTeX commands to be protected when performing conversion.")
  1440. (provide 'org-export-latex)
  1441. ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
  1442. ;;; org-export-latex.el ends here