org-export-latex.el 53 KB

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