org-latex.el 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  1. ;;; org-latex.el --- LaTeX exporter for org-mode
  2. ;;
  3. ;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-latex.el
  7. ;; Version: 6.34trans
  8. ;; Author: Bastien Guerry <bzg AT altern DOT org>
  9. ;; Maintainer: Carsten Dominik <carsten.dominik AT gmail DOT com>
  10. ;; Keywords: org, wp, tex
  11. ;; Description: Converts an org-mode buffer into LaTeX
  12. ;; This file is part of GNU Emacs.
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;; Commentary:
  24. ;;
  25. ;; This library implements a LaTeX exporter for org-mode.
  26. ;;
  27. ;; It is part of Org and will be autoloaded
  28. ;;
  29. ;; The interactive functions are similar to those of the HTML exporter:
  30. ;;
  31. ;; M-x `org-export-as-latex'
  32. ;; M-x `org-export-as-pdf'
  33. ;; M-x `org-export-as-pdf-and-open'
  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. (require 'org-macs)
  46. (require 'org-beamer)
  47. ;;; Variables:
  48. (defvar org-export-latex-class nil)
  49. (defvar org-export-latex-class-options nil)
  50. (defvar org-export-latex-header nil)
  51. (defvar org-export-latex-append-header nil)
  52. (defvar org-export-latex-options-plist nil)
  53. (defvar org-export-latex-todo-keywords-1 nil)
  54. (defvar org-export-latex-complex-heading-re nil)
  55. (defvar org-export-latex-not-done-keywords nil)
  56. (defvar org-export-latex-done-keywords nil)
  57. (defvar org-export-latex-display-custom-times nil)
  58. (defvar org-export-latex-all-targets-re nil)
  59. (defvar org-export-latex-add-level 0)
  60. (defvar org-export-latex-sectioning "")
  61. (defvar org-export-latex-sectioning-depth 0)
  62. (defvar org-export-latex-special-keyword-regexp
  63. (concat "\\<\\(" org-scheduled-string "\\|"
  64. org-deadline-string "\\|"
  65. org-closed-string"\\)")
  66. "Regexp matching special time planning keywords plus the time after it.")
  67. (defvar latexp) ; dynamically scoped from org.el
  68. (defvar re-quote) ; dynamically scoped from org.el
  69. (defvar commentsp) ; dynamically scoped from org.el
  70. ;;; User variables:
  71. (defgroup org-export-latex nil
  72. "Options for exporting Org-mode files to LaTeX."
  73. :tag "Org Export LaTeX"
  74. :group 'org-export)
  75. (defcustom org-export-latex-default-class "article"
  76. "The default LaTeX class."
  77. :group 'org-export-latex
  78. :type '(string :tag "LaTeX class"))
  79. (defcustom org-export-latex-classes
  80. '(("article"
  81. "\\documentclass[11pt]{article}
  82. \\usepackage[utf8]{inputenc}
  83. \\usepackage[T1]{fontenc}
  84. \\usepackage{graphicx}
  85. \\usepackage{longtable}
  86. \\usepackage{float}
  87. \\usepackage{wrapfig}
  88. \\usepackage{soul}
  89. \\usepackage{amssymb}
  90. \\usepackage{hyperref}"
  91. ("\\section{%s}" . "\\section*{%s}")
  92. ("\\subsection{%s}" . "\\subsection*{%s}")
  93. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
  94. ("\\paragraph{%s}" . "\\paragraph*{%s}")
  95. ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
  96. ("report"
  97. "\\documentclass[11pt]{report}
  98. \\usepackage[utf8]{inputenc}
  99. \\usepackage[T1]{fontenc}
  100. \\usepackage{graphicx}
  101. \\usepackage{longtable}
  102. \\usepackage{float}
  103. \\usepackage{wrapfig}
  104. \\usepackage{soul}
  105. \\usepackage{amssymb}
  106. \\usepackage{hyperref}"
  107. ("\\part{%s}" . "\\part*{%s}")
  108. ("\\chapter{%s}" . "\\chapter*{%s}")
  109. ("\\section{%s}" . "\\section*{%s}")
  110. ("\\subsection{%s}" . "\\subsection*{%s}")
  111. ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
  112. ("book"
  113. "\\documentclass[11pt]{book}
  114. \\usepackage[utf8]{inputenc}
  115. \\usepackage[T1]{fontenc}
  116. \\usepackage{graphicx}
  117. \\usepackage{longtable}
  118. \\usepackage{float}
  119. \\usepackage{wrapfig}
  120. \\usepackage{soul}
  121. \\usepackage{amssymb}
  122. \\usepackage{hyperref}"
  123. ("\\part{%s}" . "\\part*{%s}")
  124. ("\\chapter{%s}" . "\\chapter*{%s}")
  125. ("\\section{%s}" . "\\section*{%s}")
  126. ("\\subsection{%s}" . "\\subsection*{%s}")
  127. ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
  128. ("beamer"
  129. "\\documentclass{beamer}
  130. \\usepackage[utf8]{inputenc}
  131. \\usepackage[T1]{fontenc}
  132. \\usepackage{graphicx}
  133. \\usepackage{longtable}
  134. \\usepackage{float}
  135. \\usepackage{wrapfig}
  136. \\usepackage{soul}
  137. \\usepackage{amssymb}
  138. \\usepackage{hyperref}"
  139. org-beamer-sectioning
  140. ))
  141. "Alist of LaTeX classes and associated header and structure.
  142. If #+LaTeX_CLASS is set in the buffer, use its value and the
  143. associated information. Here is the structure of each cell:
  144. \(class-name
  145. header-string
  146. (numbered-section . unnumbered-section\)
  147. ...\)
  148. A %s formatter is mandatory in each section string and will be
  149. replaced by the title of the section.
  150. Instead of a cons cell (numbered . unnumbered), you can also provide a list
  151. of 2-4 elements,
  152. (numbered-open numbered-close)
  153. or
  154. (numbered-open numbered-close unnumbered-open unnumbered-close)
  155. providing opening and closing strings for an environment that should
  156. represent the document section. The opening clause should have a %s
  157. to represent the section title.
  158. Instead of a list of sectioning commands, you can also specify a
  159. function name. that function will be called with two parameters,
  160. the (reduced) level of the headline, and the headline text. The functions
  161. returns a cons cell with the (possibly modified) headline text, and the
  162. sectioning list in the cdr."
  163. :group 'org-export-latex
  164. :type '(repeat
  165. (list (string :tag "LaTeX class")
  166. (string :tag "LaTeX header")
  167. (repeat :tag "Levels" :inline t
  168. (choice
  169. (cons :tag "Heading"
  170. (string :tag " numbered")
  171. (string :tag "unnumbered"))
  172. (list :tag "Environment"
  173. (string :tag "Opening (numbered)")
  174. (string :tag "Closing (numbered)")
  175. (string :tag "Opening (unnumbered)")
  176. (string :tag "Closing (unnumbered)"))
  177. (function :tag "Hook computing sectioning"))))))
  178. (defcustom org-export-latex-inputenc-alist nil
  179. "Alist of inputenc coding system names, and what should really be used.
  180. For example, adding an entry
  181. (\"utf8\" . \"utf8x\")
  182. will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
  183. are written as utf8 files."
  184. :group 'org-export-latex
  185. :type '(repeat
  186. (cons
  187. (string :tag "Derived from buffer")
  188. (string :tag "Use this instead"))))
  189. (defcustom org-export-latex-emphasis-alist
  190. '(("*" "\\textbf{%s}" nil)
  191. ("/" "\\emph{%s}" nil)
  192. ("_" "\\underline{%s}" nil)
  193. ("+" "\\st{%s}" nil)
  194. ("=" "\\verb" t)
  195. ("~" "\\verb" t))
  196. "Alist of LaTeX expressions to convert emphasis fontifiers.
  197. Each element of the list is a list of three elements.
  198. The first element is the character used as a marker for fontification.
  199. The second element is a formatting string to wrap fontified text with.
  200. If it is \"\\verb\", Org will automatically select a delimiter
  201. character that is not in the string.
  202. The third element decides whether to protect converted text from other
  203. conversions."
  204. :group 'org-export-latex
  205. :type 'alist)
  206. (defcustom org-export-latex-title-command "\\maketitle"
  207. "The command used to insert the title just after \\begin{document}.
  208. If this string contains the formatting specification \"%s\" then
  209. it will be used as a formatting string, passing the title as an
  210. argument."
  211. :group 'org-export-latex
  212. :type 'string)
  213. (defcustom org-export-latex-import-inbuffer-stuff nil
  214. "Non-nil means define TeX macros for Org's inbuffer definitions.
  215. For example \orgTITLE for #+TITLE."
  216. :group 'org-export-latex
  217. :type 'boolean)
  218. (defcustom org-export-latex-date-format
  219. "%d %B %Y"
  220. "Format string for \\date{...}."
  221. :group 'org-export-latex
  222. :type 'string)
  223. (defcustom org-export-latex-todo-keyword-markup "\\textbf{%s}"
  224. "Markup for TODO keywords, as a printf format.
  225. This can be a single format for all keywords, a cons cell with separate
  226. formats for not-done and done states, or an association list with setup
  227. for individual keywords. If a keyword shows up for which there is no
  228. markup defined, the first one in the association list will be used."
  229. :group 'org-export-latex
  230. :type '(choice
  231. (string :tag "Default")
  232. (cons :tag "Distinguish undone and done"
  233. (string :tag "Not-DONE states")
  234. (string :tag "DONE states"))
  235. (repeat :tag "Per keyword markup"
  236. (cons
  237. (string :tag "Keyword")
  238. (string :tag "Markup")))))
  239. (defcustom org-export-latex-timestamp-markup "\\textit{%s}"
  240. "A printf format string to be applied to time stamps."
  241. :group 'org-export-latex
  242. :type 'string)
  243. (defcustom org-export-latex-timestamp-keyword-markup "\\texttt{%s}"
  244. "A printf format string to be applied to time stamps."
  245. :group 'org-export-latex
  246. :type 'string)
  247. (defcustom org-export-latex-hyperref-format "\\href{%s}{%s}"
  248. "A printf format string to be applied to hyperref links.
  249. The format must contain two %s instances. The first will be filled with
  250. the link, the second with the link description."
  251. :group 'org-export-latex
  252. :type 'string)
  253. (defcustom org-export-latex-tables-verbatim nil
  254. "When non-nil, tables are exported verbatim."
  255. :group 'org-export-latex
  256. :type 'boolean)
  257. (defcustom org-export-latex-tables-centered t
  258. "When non-nil, tables are exported in a center environment."
  259. :group 'org-export-latex
  260. :type 'boolean)
  261. (defcustom org-export-latex-tables-column-borders nil
  262. "When non-nil, grouping columns can cause outer vertical lines in tables.
  263. When nil, grouping causes only separation lines between groups."
  264. :group 'org-export-latex
  265. :type 'boolean)
  266. (defcustom org-export-latex-low-levels 'itemize
  267. "How to convert sections below the current level of sectioning.
  268. This is specified by the `org-export-headline-levels' option or the
  269. value of \"H:\" in Org's #+OPTION line.
  270. This can be either nil (skip the sections), `description', `itemize',
  271. or `enumerate' (convert the sections as the corresponding list type), or
  272. a string to be used instead of \\section{%s}. In this latter case,
  273. the %s stands here for the inserted headline and is mandatory.
  274. It may also be a list of three string to define a user-defined environment
  275. that should be used. The first string should be the like
  276. \"\\begin{itemize}\", the second should be like \"\\item %s %s\" with up
  277. to two occurrences of %s for the title and a label, respectively. The third
  278. string should be like \"\\end{itemize\"."
  279. :group 'org-export-latex
  280. :type '(choice (const :tag "Ignore" nil)
  281. (const :tag "Convert as descriptive list" description)
  282. (const :tag "Convert as itemized list" itemize)
  283. (const :tag "Convert as enumerated list" enumerate)
  284. (list :tag "User-defined environment"
  285. :value ("\\begin{itemize}" "\\end{itemize}" "\\item %s")
  286. (string :tag "Start")
  287. (string :tag "End")
  288. (string :tag "item"))
  289. (string :tag "Use a section string" :value "\\subparagraph{%s}")))
  290. (defcustom org-export-latex-list-parameters
  291. '(:cbon "$\\boxtimes$" :cboff "$\\Box$")
  292. "Parameters for the LaTeX list exporter.
  293. These parameters will be passed on to `org-list-to-latex', which in turn
  294. will pass them (combined with the LaTeX default list parameters) to
  295. `org-list-to-generic'."
  296. :group 'org-export-latex
  297. :type 'plist)
  298. (defcustom org-export-latex-verbatim-wrap
  299. '("\\begin{verbatim}\n" . "\\end{verbatim}\n")
  300. "Environment to be wrapped around a fixed-width section in LaTeX export.
  301. This is a cons with two strings, to be added before and after the
  302. fixed-with text.
  303. Defaults to \\begin{verbatim} and \\end{verbatim}."
  304. :group 'org-export-translation
  305. :group 'org-export-latex
  306. :type '(cons (string :tag "Open")
  307. (string :tag "Close")))
  308. (defcustom org-export-latex-listings nil
  309. "Non-nil means, export source code using the listings package.
  310. This package will fontify source code, possibly even with color.
  311. If you want to use this, you also need to make LaTeX use the
  312. listings package, and if you want to have color, the color
  313. package. Just add these to `org-export-latex-packages-alist',
  314. for example using customize, or with something like
  315. (require 'org-latex)
  316. (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
  317. (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))"
  318. :group 'org-export-latex
  319. :type 'boolean)
  320. (defcustom org-export-latex-listings-langs
  321. '((emacs-lisp "Lisp") (lisp "Lisp")
  322. (c "C") (cc "C++")
  323. (fortran "fortran")
  324. (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
  325. (html "HTML") (xml "XML")
  326. (tex "TeX") (latex "TeX")
  327. (shell-script "bash")
  328. (gnuplot "Gnuplot")
  329. (ocaml "Caml") (caml "Caml")
  330. (sql "SQL"))
  331. "Alist mapping languages to their listing language counterpart.
  332. The key is a symbol, the major mode symbol without the \"-mode\".
  333. The value is the string that should be inserted as the language parameter
  334. for the listings package. If the mode name and the listings name are
  335. the same, the language does not need an entry in this list - but it does not
  336. hurt if it is present."
  337. :group 'org-export-latex
  338. :type '(repeat
  339. (list
  340. (symbol :tag "Major mode ")
  341. (string :tag "Listings language"))))
  342. (defcustom org-export-latex-remove-from-headlines
  343. '(:todo nil :priority nil :tags nil)
  344. "A plist of keywords to remove from headlines. OBSOLETE.
  345. Non-nil means remove this keyword type from the headline.
  346. Don't remove the keys, just change their values.
  347. Obsolete, this variable is no longer used. Use the separate
  348. variables `org-export-with-todo-keywords', `org-export-with-priority',
  349. and `org-export-with-tags' instead."
  350. :type 'plist
  351. :group 'org-export-latex)
  352. (defcustom org-export-latex-image-default-option "width=10em"
  353. "Default option for images."
  354. :group 'org-export-latex
  355. :type 'string)
  356. (defcustom org-export-latex-inline-image-extensions
  357. '("pdf" "jpeg" "jpg" "png" "ps" "eps")
  358. "Extensions of image files that can be inlined into LaTeX.
  359. Note that the image extension *actually* allowed depend on the way the
  360. LaTeX file is processed. When used with pdflatex, pdf, jpg and png images
  361. are OK. When processing through dvi to Postscript, only ps and eps are
  362. allowed. The default we use here encompasses both."
  363. :group 'org-export-latex
  364. :type '(repeat (string :tag "Extension")))
  365. (defcustom org-export-latex-coding-system nil
  366. "Coding system for the exported LaTex file."
  367. :group 'org-export-latex
  368. :type 'coding-system)
  369. (defgroup org-export-pdf nil
  370. "Options for exporting Org-mode files to PDF, via LaTeX."
  371. :tag "Org Export PDF"
  372. :group 'org-export-latex
  373. :group 'org-export)
  374. (defcustom org-latex-to-pdf-process
  375. '("pdflatex -interaction nonstopmode %s"
  376. "pdflatex -interaction nonstopmode %s")
  377. "Commands to process a LaTeX file to a PDF file.
  378. This is a list of strings, each of them will be given to the shell
  379. as a command. %s in the command will be replaced by the full file name, %b
  380. by the file base name (i.e. without extension).
  381. The reason why this is a list is that it usually takes several runs of
  382. pdflatex, maybe mixed with a call to bibtex. Org does not have a clever
  383. mechanism to detect which of these commands have to be run to get to a stable
  384. result, and it also does not do any error checking.
  385. Alternatively, this may be a Lisp function that does the processing, so you
  386. could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
  387. This function should accept the file name as its single argument."
  388. :group 'org-export-pdf
  389. :type '(choice (repeat :tag "Shell command sequence"
  390. (string :tag "Shell command"))
  391. (function)))
  392. (defcustom org-export-pdf-logfiles
  393. '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
  394. "The list of file extensions to consider as LaTeX logfiles."
  395. :group 'org-export-pdf
  396. :type '(repeat (string :tag "Extension")))
  397. (defcustom org-export-pdf-remove-logfiles t
  398. "Non-nil means, remove the logfiles produced by PDF production.
  399. These are the .aux, .log, .out, and .toc files."
  400. :group 'org-export-pdf
  401. :type 'boolean)
  402. ;;; Hooks
  403. (defvar org-export-latex-after-initial-vars-hook nil
  404. "Hook run before LaTeX export.
  405. The exact moment is after the initial variables like org-export-latex-class
  406. have been determined from the environment.")
  407. (defvar org-export-latex-after-blockquotes-hook nil
  408. "Hook run during LaTeX export, after blockquote, verse, center are done.")
  409. (defvar org-export-latex-final-hook nil
  410. "Hook run in the finalized LaTeX buffer.")
  411. (defvar org-export-latex-after-save-hook nil
  412. "Hook run in the finalized LaTeX buffer, after it has been saved.")
  413. ;;; Autoload functions:
  414. ;;;###autoload
  415. (defun org-export-as-latex-batch ()
  416. "Call `org-export-as-latex', may be used in batch processing.
  417. For example:
  418. emacs --batch
  419. --load=$HOME/lib/emacs/org.el
  420. --eval \"(setq org-export-headline-levels 2)\"
  421. --visit=MyFile --funcall org-export-as-latex-batch"
  422. (org-export-as-latex org-export-headline-levels 'hidden))
  423. ;;;###autoload
  424. (defun org-export-as-latex-to-buffer (arg)
  425. "Call `org-export-as-latex` with output to a temporary buffer.
  426. No file is created. The prefix ARG is passed through to `org-export-as-latex'."
  427. (interactive "P")
  428. (org-export-as-latex arg nil nil "*Org LaTeX Export*")
  429. (when org-export-show-temporary-export-buffer
  430. (switch-to-buffer-other-window "*Org LaTeX Export*")))
  431. ;;;###autoload
  432. (defun org-replace-region-by-latex (beg end)
  433. "Replace the region from BEG to END with its LaTeX export.
  434. It assumes the region has `org-mode' syntax, and then convert it to
  435. LaTeX. This can be used in any buffer. For example, you could
  436. write an itemized list in `org-mode' syntax in an LaTeX buffer and
  437. then use this command to convert it."
  438. (interactive "r")
  439. (let (reg latex buf)
  440. (save-window-excursion
  441. (if (org-mode-p)
  442. (setq latex (org-export-region-as-latex
  443. beg end t 'string))
  444. (setq reg (buffer-substring beg end)
  445. buf (get-buffer-create "*Org tmp*"))
  446. (with-current-buffer buf
  447. (erase-buffer)
  448. (insert reg)
  449. (org-mode)
  450. (setq latex (org-export-region-as-latex
  451. (point-min) (point-max) t 'string)))
  452. (kill-buffer buf)))
  453. (delete-region beg end)
  454. (insert latex)))
  455. ;;;###autoload
  456. (defun org-export-region-as-latex (beg end &optional body-only buffer)
  457. "Convert region from BEG to END in `org-mode' buffer to LaTeX.
  458. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  459. contents, and only produce the region of converted text, useful for
  460. cut-and-paste operations.
  461. If BUFFER is a buffer or a string, use/create that buffer as a target
  462. of the converted LaTeX. If BUFFER is the symbol `string', return the
  463. produced LaTeX as a string and leave no buffer behind. For example,
  464. a Lisp program could call this function in the following way:
  465. (setq latex (org-export-region-as-latex beg end t 'string))
  466. When called interactively, the output buffer is selected, and shown
  467. in a window. A non-interactive call will only return the buffer."
  468. (interactive "r\nP")
  469. (when (interactive-p)
  470. (setq buffer "*Org LaTeX Export*"))
  471. (let ((transient-mark-mode t) (zmacs-regions t)
  472. ext-plist rtn)
  473. (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
  474. (goto-char end)
  475. (set-mark (point)) ;; to activate the region
  476. (goto-char beg)
  477. (setq rtn (org-export-as-latex
  478. nil nil ext-plist
  479. buffer body-only))
  480. (if (fboundp 'deactivate-mark) (deactivate-mark))
  481. (if (and (interactive-p) (bufferp rtn))
  482. (switch-to-buffer-other-window rtn)
  483. rtn)))
  484. ;;;###autoload
  485. (defun org-export-as-latex (arg &optional hidden ext-plist
  486. to-buffer body-only pub-dir)
  487. "Export current buffer to a LaTeX file.
  488. If there is an active region, export only the region. The prefix
  489. ARG specifies how many levels of the outline should become
  490. headlines. The default is 3. Lower levels will be exported
  491. depending on `org-export-latex-low-levels'. The default is to
  492. convert them as description lists.
  493. HIDDEN is obsolete and does nothing.
  494. EXT-PLIST is a property list with
  495. external parameters overriding org-mode's default settings, but
  496. still inferior to file-local settings. When TO-BUFFER is
  497. non-nil, create a buffer with that name and export to that
  498. buffer. If TO-BUFFER is the symbol `string', don't leave any
  499. buffer behind but just return the resulting LaTeX as a string.
  500. When BODY-ONLY is set, don't produce the file header and footer,
  501. simply return the content of \begin{document}...\end{document},
  502. without even the \begin{document} and \end{document} commands.
  503. when PUB-DIR is set, use this as the publishing directory."
  504. (interactive "P")
  505. (run-hooks 'org-export-first-hook)
  506. ;; Make sure we have a file name when we need it.
  507. (when (and (not (or to-buffer body-only))
  508. (not buffer-file-name))
  509. (if (buffer-base-buffer)
  510. (org-set-local 'buffer-file-name
  511. (with-current-buffer (buffer-base-buffer)
  512. buffer-file-name))
  513. (error "Need a file name to be able to export")))
  514. (message "Exporting to LaTeX...")
  515. (org-unmodified
  516. (let ((inhibit-read-only t))
  517. (remove-text-properties (point-min) (point-max)
  518. '(:org-license-to-kill nil))))
  519. (org-update-radio-target-regexp)
  520. (org-export-latex-set-initial-vars ext-plist arg)
  521. (setq org-export-opt-plist org-export-latex-options-plist)
  522. (org-install-letbind)
  523. (run-hooks 'org-export-latex-after-initial-vars-hook)
  524. (let* ((wcf (current-window-configuration))
  525. (opt-plist org-export-latex-options-plist)
  526. (region-p (org-region-active-p))
  527. (rbeg (and region-p (region-beginning)))
  528. (rend (and region-p (region-end)))
  529. (subtree-p
  530. (if (plist-get opt-plist :ignore-subtree-p)
  531. nil
  532. (when region-p
  533. (save-excursion
  534. (goto-char rbeg)
  535. (and (org-at-heading-p)
  536. (>= (org-end-of-subtree t t) rend))))))
  537. (opt-plist (setq org-export-opt-plist
  538. (if subtree-p
  539. (org-export-add-subtree-options opt-plist rbeg)
  540. opt-plist)))
  541. ;; Make sure the variable contains the updated values.
  542. (org-export-latex-options-plist (setq org-export-opt-plist opt-plist))
  543. (title (or (and subtree-p (org-export-get-title-from-subtree))
  544. (plist-get opt-plist :title)
  545. (and (not
  546. (plist-get opt-plist :skip-before-1st-heading))
  547. (org-export-grab-title-from-buffer))
  548. (file-name-sans-extension
  549. (file-name-nondirectory buffer-file-name))))
  550. (filename (concat (file-name-as-directory
  551. (or pub-dir
  552. (org-export-directory :LaTeX ext-plist)))
  553. (file-name-sans-extension
  554. (or (and subtree-p
  555. (org-entry-get rbeg "EXPORT_FILE_NAME" t))
  556. (file-name-nondirectory ;sans-extension
  557. buffer-file-name)))
  558. ".tex"))
  559. (filename (if (equal (file-truename filename)
  560. (file-truename buffer-file-name))
  561. (concat filename ".tex")
  562. filename))
  563. (buffer (if to-buffer
  564. (cond
  565. ((eq to-buffer 'string) (get-buffer-create
  566. "*Org LaTeX Export*"))
  567. (t (get-buffer-create to-buffer)))
  568. (find-file-noselect filename)))
  569. (odd org-odd-levels-only)
  570. (header (org-export-latex-make-header title opt-plist))
  571. (skip (cond (subtree-p nil)
  572. (region-p nil)
  573. (t (plist-get opt-plist :skip-before-1st-heading))))
  574. (text (plist-get opt-plist :text))
  575. (org-export-preprocess-hook
  576. (cons
  577. `(lambda () (org-set-local 'org-complex-heading-regexp
  578. ,org-export-latex-complex-heading-re))
  579. org-export-preprocess-hook))
  580. (first-lines (if skip "" (org-export-latex-first-lines
  581. opt-plist
  582. (if subtree-p
  583. (save-excursion
  584. (goto-char rbeg)
  585. (point-at-bol 2))
  586. rbeg)
  587. (if region-p rend))))
  588. (coding-system (and (boundp 'buffer-file-coding-system)
  589. buffer-file-coding-system))
  590. (coding-system-for-write (or org-export-latex-coding-system
  591. coding-system))
  592. (save-buffer-coding-system (or org-export-latex-coding-system
  593. coding-system))
  594. (region (buffer-substring
  595. (if region-p (region-beginning) (point-min))
  596. (if region-p (region-end) (point-max))))
  597. (text
  598. (and text (string-match "\\S-" text)
  599. (org-export-preprocess-string
  600. text
  601. :emph-multiline t
  602. :for-LaTeX t
  603. :comments nil
  604. :tags (plist-get opt-plist :tags)
  605. :priority (plist-get opt-plist :priority)
  606. :footnotes (plist-get opt-plist :footnotes)
  607. :drawers (plist-get opt-plist :drawers)
  608. :timestamps (plist-get opt-plist :timestamps)
  609. :todo-keywords (plist-get opt-plist :todo-keywords)
  610. :add-text nil
  611. :skip-before-1st-heading skip
  612. :select-tags nil
  613. :exclude-tags nil
  614. :LaTeX-fragments nil)))
  615. (string-for-export
  616. (org-export-preprocess-string
  617. region
  618. :emph-multiline t
  619. :for-LaTeX t
  620. :comments nil
  621. :tags (plist-get opt-plist :tags)
  622. :priority (plist-get opt-plist :priority)
  623. :footnotes (plist-get opt-plist :footnotes)
  624. :drawers (plist-get opt-plist :drawers)
  625. :timestamps (plist-get opt-plist :timestamps)
  626. :todo-keywords (plist-get opt-plist :todo-keywords)
  627. :add-text (if (eq to-buffer 'string) nil text)
  628. :skip-before-1st-heading skip
  629. :select-tags (plist-get opt-plist :select-tags)
  630. :exclude-tags (plist-get opt-plist :exclude-tags)
  631. :LaTeX-fragments nil)))
  632. (set-buffer buffer)
  633. (erase-buffer)
  634. (org-install-letbind)
  635. (and (fboundp 'set-buffer-file-coding-system)
  636. (set-buffer-file-coding-system coding-system-for-write))
  637. ;; insert the header and initial document commands
  638. (unless (or (eq to-buffer 'string) body-only)
  639. (insert header))
  640. ;; insert text found in #+TEXT
  641. (when (and text (not (eq to-buffer 'string)))
  642. (insert (org-export-latex-content
  643. text '(lists tables fixed-width keywords))
  644. "\n\n"))
  645. ;; insert lines before the first headline
  646. (unless skip
  647. (insert first-lines))
  648. ;; export the content of headlines
  649. (org-export-latex-global
  650. (with-temp-buffer
  651. (insert string-for-export)
  652. (goto-char (point-min))
  653. (when (re-search-forward "^\\(\\*+\\) " nil t)
  654. (let* ((asters (length (match-string 1)))
  655. (level (if odd (- asters 2) (- asters 1))))
  656. (setq org-export-latex-add-level
  657. (if odd (1- (/ (1+ asters) 2)) (1- asters)))
  658. (org-export-latex-parse-global level odd)))))
  659. ;; finalization
  660. (unless body-only (insert "\n\\end{document}"))
  661. ;; Attach description terms to the \item macro
  662. (goto-char (point-min))
  663. (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t)
  664. (delete-region (match-beginning 1) (match-end 1)))
  665. ;; Relocate the table of contents
  666. (goto-char (point-min))
  667. (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
  668. (goto-char (point-min))
  669. (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t)
  670. (replace-match ""))
  671. (goto-char (point-min))
  672. (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
  673. (replace-match "\\tableofcontents" t t)))
  674. ;; Cleanup forced line ends in items where they are not needed
  675. (goto-char (point-min))
  676. (while (re-search-forward
  677. "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
  678. nil t)
  679. (delete-region (match-beginning 1) (match-end 1)))
  680. (goto-char (point-min))
  681. (while (re-search-forward
  682. "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
  683. nil t)
  684. (if (looking-at "[\n \t]+")
  685. (replace-match "\n")))
  686. (run-hooks 'org-export-latex-final-hook)
  687. (or to-buffer (save-buffer))
  688. (org-export-latex-fix-inputenc)
  689. (run-hooks 'org-export-latex-after-save-hook)
  690. (goto-char (point-min))
  691. (or (org-export-push-to-kill-ring "LaTeX")
  692. (message "Exporting to LaTeX...done"))
  693. (prog1
  694. (if (eq to-buffer 'string)
  695. (prog1 (buffer-substring (point-min) (point-max))
  696. (kill-buffer (current-buffer)))
  697. (current-buffer))
  698. (set-window-configuration wcf))))
  699. ;;;###autoload
  700. (defun org-export-as-pdf (arg &optional hidden ext-plist
  701. to-buffer body-only pub-dir)
  702. "Export as LaTeX, then process through to PDF."
  703. (interactive "P")
  704. (message "Exporting to PDF...")
  705. (let* ((wconfig (current-window-configuration))
  706. (lbuf (org-export-as-latex arg hidden ext-plist
  707. to-buffer body-only pub-dir))
  708. (file (buffer-file-name lbuf))
  709. (base (file-name-sans-extension (buffer-file-name lbuf)))
  710. (pdffile (concat base ".pdf"))
  711. (cmds org-latex-to-pdf-process)
  712. (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
  713. (bibtex-p (with-current-buffer lbuf
  714. (save-excursion
  715. (goto-char (point-min))
  716. (re-search-forward "\\\\bibliography{" nil t))))
  717. cmd)
  718. (with-current-buffer outbuf (erase-buffer))
  719. (message "Processing LaTeX file...")
  720. (if (and cmds (symbolp cmds))
  721. (funcall cmds file)
  722. (while cmds
  723. (setq cmd (pop cmds))
  724. (while (string-match "%b" cmd)
  725. (setq cmd (replace-match
  726. (save-match-data
  727. (shell-quote-argument base))
  728. t t cmd)))
  729. (while (string-match "%s" cmd)
  730. (setq cmd (replace-match
  731. (save-match-data
  732. (shell-quote-argument file))
  733. t t cmd)))
  734. (shell-command cmd outbuf outbuf)))
  735. (message "Processing LaTeX file...done")
  736. (if (not (file-exists-p pdffile))
  737. (error "PDF file was not produced")
  738. (set-window-configuration wconfig)
  739. (when org-export-pdf-remove-logfiles
  740. (dolist (ext org-export-pdf-logfiles)
  741. (setq file (concat base "." ext))
  742. (and (file-exists-p file) (delete-file file))))
  743. (message "Exporting to PDF...done")
  744. pdffile)))
  745. ;;;###autoload
  746. (defun org-export-as-pdf-and-open (arg)
  747. "Export as LaTeX, then process through to PDF, and open."
  748. (interactive "P")
  749. (let ((pdffile (org-export-as-pdf arg)))
  750. (if pdffile
  751. (org-open-file pdffile)
  752. (error "PDF file was not produced"))))
  753. ;;; Parsing functions:
  754. (defun org-export-latex-parse-global (level odd)
  755. "Parse the current buffer recursively, starting at LEVEL.
  756. If ODD is non-nil, assume the buffer only contains odd sections.
  757. Return a list reflecting the document structure."
  758. (save-excursion
  759. (goto-char (point-min))
  760. (let* ((cnt 0) output
  761. (depth org-export-latex-sectioning-depth))
  762. (while (org-re-search-forward-unprotected
  763. (concat "^\\(\\(?:\\*\\)\\{"
  764. (number-to-string (+ (if odd 2 1) level))
  765. "\\}\\) \\(.*\\)$")
  766. ;; make sure that there is no upper heading
  767. (when (> level 0)
  768. (save-excursion
  769. (save-match-data
  770. (org-re-search-forward-unprotected
  771. (concat "^\\(\\(?:\\*\\)\\{"
  772. (number-to-string level)
  773. "\\}\\) \\(.*\\)$") nil t)))) t)
  774. (setq cnt (1+ cnt))
  775. (let* ((pos (match-beginning 0))
  776. (heading (match-string 2))
  777. (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
  778. (save-excursion
  779. (narrow-to-region
  780. (point)
  781. (save-match-data
  782. (if (org-re-search-forward-unprotected
  783. (concat "^\\(\\(?:\\*\\)\\{"
  784. (number-to-string (+ (if odd 2 1) level))
  785. "\\}\\) \\(.*\\)$") nil t)
  786. (match-beginning 0)
  787. (point-max))))
  788. (goto-char (point-min))
  789. (setq output
  790. (append output
  791. (list
  792. (list
  793. `(pos . ,pos)
  794. `(level . ,nlevel)
  795. `(occur . ,cnt)
  796. `(heading . ,heading)
  797. `(content . ,(org-export-latex-parse-content))
  798. `(subcontent . ,(org-export-latex-parse-subcontent
  799. level odd)))))))
  800. (widen)))
  801. (list output))))
  802. (defun org-export-latex-parse-content ()
  803. "Extract the content of a section."
  804. (let ((beg (point))
  805. (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t)
  806. (progn (beginning-of-line) (point))
  807. (point-max))))
  808. (buffer-substring beg end)))
  809. (defun org-export-latex-parse-subcontent (level odd)
  810. "Extract the subcontent of a section at LEVEL.
  811. If ODD Is non-nil, assume subcontent only contains odd sections."
  812. (if (not (org-re-search-forward-unprotected
  813. (concat "^\\(\\(?:\\*\\)\\{"
  814. (number-to-string (+ (if odd 4 2) level))
  815. "\\}\\) \\(.*\\)$")
  816. nil t))
  817. nil ; subcontent is nil
  818. (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
  819. ;;; Rendering functions:
  820. (defun org-export-latex-global (content)
  821. "Export CONTENT to LaTeX.
  822. CONTENT is an element of the list produced by
  823. `org-export-latex-parse-global'."
  824. (if (eq (car content) 'subcontent)
  825. (mapc 'org-export-latex-sub (cdr content))
  826. (org-export-latex-sub (car content))))
  827. (defun org-export-latex-sub (subcontent)
  828. "Export the list SUBCONTENT to LaTeX.
  829. SUBCONTENT is an alist containing information about the headline
  830. and its content."
  831. (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
  832. (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
  833. (defun org-export-latex-subcontent (subcontent num)
  834. "Export each cell of SUBCONTENT to LaTeX.
  835. If NUM, export sections as numerical sections."
  836. (let* ((heading (cdr (assoc 'heading subcontent)))
  837. (level (- (cdr (assoc 'level subcontent))
  838. org-export-latex-add-level))
  839. (occur (number-to-string (cdr (assoc 'occur subcontent))))
  840. (content (cdr (assoc 'content subcontent)))
  841. (subcontent (cadr (assoc 'subcontent subcontent)))
  842. (label (org-get-text-property-any 0 'target heading))
  843. (label-list (cons label (cdr (assoc label
  844. org-export-target-aliases))))
  845. (sectioning org-export-latex-sectioning)
  846. (depth org-export-latex-sectioning-depth)
  847. main-heading sub-heading)
  848. (when (symbolp (car sectioning))
  849. (setq sectioning (funcall (car sectioning) level heading))
  850. (when sectioning
  851. (setq heading (car sectioning)
  852. sectioning (cdr sectioning)
  853. ;; target property migh have changed...
  854. label (org-get-text-property-any 0 'target heading)
  855. label-list (cons label (cdr (assoc label
  856. org-export-target-aliases)))))
  857. (if sectioning (setq sectioning (make-list 10 sectioning)))
  858. (setq depth (if sectioning 10000 0)))
  859. (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading)
  860. (setq main-heading (substring heading 0 (match-beginning 0))
  861. sub-heading (substring heading (match-end 0))))
  862. (setq heading (org-export-latex-fontify-headline heading)
  863. sub-heading (and sub-heading
  864. (org-export-latex-fontify-headline sub-heading))
  865. main-heading (and main-heading
  866. (org-export-latex-fontify-headline main-heading)))
  867. (cond
  868. ;; Normal conversion
  869. ((<= level depth)
  870. (let* ((sec (nth (1- level) sectioning))
  871. start end)
  872. (if (consp (cdr sec))
  873. (setq start (nth (if num 0 2) sec)
  874. end (nth (if num 1 3) sec))
  875. (setq start (if num (car sec) (cdr sec))))
  876. (insert (format start (if main-heading main-heading heading)
  877. (or sub-heading "")))
  878. (insert "\n")
  879. (when label
  880. (insert (mapconcat (lambda (l) (format "\\label{%s}" l))
  881. label-list "\n") "\n"))
  882. (insert (org-export-latex-content content))
  883. (cond ((stringp subcontent) (insert subcontent))
  884. ((listp subcontent)
  885. (while (org-looking-back "\n\n") (backward-delete-char 1))
  886. (org-export-latex-sub subcontent)))
  887. (when (and end (string-match "[^ \t]" end))
  888. (let ((hook (org-get-text-property-any 0 'org-insert-hook end)))
  889. (and (functionp hook) (funcall hook)))
  890. (insert end "\n"))))
  891. ;; At a level under the hl option: we can drop this subsection
  892. ((> level depth)
  893. (cond ((eq org-export-latex-low-levels 'description)
  894. (if (string-match "% ends low level$"
  895. (buffer-substring (point-at-bol 0) (point)))
  896. (delete-region (point-at-bol 0) (point))
  897. (insert "\\begin{description}\n"))
  898. (insert (format "\n\\item[%s]%s~\n"
  899. heading
  900. (if label (format "\\label{%s}" label) "")))
  901. (insert (org-export-latex-content content))
  902. (cond ((stringp subcontent) (insert subcontent))
  903. ((listp subcontent) (org-export-latex-sub subcontent)))
  904. (insert "\\end{description} % ends low level\n"))
  905. ((memq org-export-latex-low-levels '(itemize enumerate))
  906. (if (string-match "% ends low level$"
  907. (buffer-substring (point-at-bol 0) (point)))
  908. (delete-region (point-at-bol 0) (point))
  909. (insert (format "\\begin{%s}\n"
  910. (symbol-name org-export-latex-low-levels))))
  911. (insert (format "\n\\item %s\\\\\n%s%%"
  912. heading
  913. (if label (format "\\label{%s}" label) "")))
  914. (insert (org-export-latex-content content))
  915. (cond ((stringp subcontent) (insert subcontent))
  916. ((listp subcontent) (org-export-latex-sub subcontent)))
  917. (insert (format "\\end{%s} %% ends low level\n"
  918. (symbol-name org-export-latex-low-levels))))
  919. ((listp org-export-latex-low-levels)
  920. (if (string-match "% ends low level$"
  921. (buffer-substring (point-at-bol 0) (point)))
  922. (delete-region (point-at-bol 0) (point))
  923. (insert (car org-export-latex-low-levels) "\n"))
  924. (insert (format (nth 2 org-export-latex-low-levels)
  925. heading
  926. (if label (format "\\label{%s}" label) "")))
  927. (insert (org-export-latex-content content))
  928. (cond ((stringp subcontent) (insert subcontent))
  929. ((listp subcontent) (org-export-latex-sub subcontent)))
  930. (insert (nth 1 org-export-latex-low-levels)
  931. " %% ends low level\n"))
  932. ((stringp org-export-latex-low-levels)
  933. (insert (format org-export-latex-low-levels heading) "\n")
  934. (when label (insert (format "\\label{%s}\n" label)))
  935. (insert (org-export-latex-content content))
  936. (cond ((stringp subcontent) (insert subcontent))
  937. ((listp subcontent) (org-export-latex-sub subcontent)))))))))
  938. ;;; Exporting internals:
  939. (defun org-export-latex-set-initial-vars (ext-plist level)
  940. "Store org local variables required for LaTeX export.
  941. EXT-PLIST is an optional additional plist.
  942. LEVEL indicates the default depth for export."
  943. (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
  944. org-export-latex-done-keywords org-done-keywords
  945. org-export-latex-not-done-keywords org-not-done-keywords
  946. org-export-latex-complex-heading-re org-complex-heading-regexp
  947. org-export-latex-display-custom-times org-display-custom-times
  948. org-export-latex-all-targets-re
  949. (org-make-target-link-regexp (org-all-targets))
  950. org-export-latex-options-plist
  951. (org-combine-plists (org-default-export-plist) ext-plist
  952. (org-infile-export-plist))
  953. org-export-latex-class
  954. (or (and (org-region-active-p)
  955. (save-excursion
  956. (goto-char (region-beginning))
  957. (and (looking-at org-complex-heading-regexp)
  958. (org-entry-get nil "LaTeX_CLASS" 'selective))))
  959. (save-excursion
  960. (save-restriction
  961. (widen)
  962. (goto-char (point-min))
  963. (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([a-zA-Z]+\\)" nil t)
  964. (match-string 1))))
  965. (plist-get org-export-latex-options-plist :latex-class)
  966. org-export-latex-default-class)
  967. org-export-latex-class-options
  968. (or (and (org-region-active-p)
  969. (save-excursion
  970. (goto-char (region-beginning))
  971. (and (looking-at org-complex-heading-regexp)
  972. (org-entry-get nil "LaTeX_CLASS_OPTIONS" 'selective))))
  973. (save-excursion
  974. (save-restriction
  975. (widen)
  976. (goto-char (point-min))
  977. (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t)
  978. (match-string 1))))
  979. (plist-get org-export-latex-options-plist :latex-class-options))
  980. org-export-latex-class
  981. (or (car (assoc org-export-latex-class org-export-latex-classes))
  982. (error "No definition for class `%s' in `org-export-latex-classes'"
  983. org-export-latex-class))
  984. org-export-latex-header
  985. (cadr (assoc org-export-latex-class org-export-latex-classes))
  986. org-export-latex-sectioning
  987. (cddr (assoc org-export-latex-class org-export-latex-classes))
  988. org-export-latex-sectioning-depth
  989. (or level
  990. (let ((hl-levels
  991. (plist-get org-export-latex-options-plist :headline-levels))
  992. (sec-depth (length org-export-latex-sectioning)))
  993. (if (> hl-levels sec-depth) sec-depth hl-levels))))
  994. (when (and org-export-latex-class-options
  995. (string-match "\\S-" org-export-latex-class-options)
  996. (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
  997. org-export-latex-header))
  998. (setq org-export-latex-header
  999. (concat (substring org-export-latex-header 0 (match-end 1))
  1000. org-export-latex-class-options
  1001. (substring org-export-latex-header (match-end 0))))))
  1002. (defvar org-export-latex-format-toc-function
  1003. 'org-export-latex-format-toc-default
  1004. "The function formatting returning the string to createthe table of contents.
  1005. The function mus take one parameter, the depth of the table of contents.")
  1006. (defun org-export-latex-make-header (title opt-plist)
  1007. "Make the LaTeX header and return it as a string.
  1008. TITLE is the current title from the buffer or region.
  1009. OPT-PLIST is the options plist for current buffer."
  1010. (let ((toc (plist-get opt-plist :table-of-contents))
  1011. (author (plist-get opt-plist :author)))
  1012. (concat
  1013. (if (plist-get opt-plist :time-stamp-file)
  1014. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  1015. ;; insert LaTeX custom header
  1016. (org-export-apply-macros-in-string org-export-latex-header)
  1017. "\n"
  1018. ;; insert information on LaTeX packages
  1019. (when org-export-latex-packages-alist
  1020. (concat
  1021. (mapconcat (lambda(p)
  1022. (if (equal "" (car p))
  1023. (format "\\usepackage{%s}" (cadr p))
  1024. (format "\\usepackage[%s]{%s}"
  1025. (car p) (cadr p))))
  1026. org-export-latex-packages-alist "\n")
  1027. "\n"))
  1028. ;; insert additional commands in the header
  1029. (org-export-apply-macros-in-string
  1030. (plist-get opt-plist :latex-header-extra))
  1031. (org-export-apply-macros-in-string org-export-latex-append-header)
  1032. ;; insert the title
  1033. (format
  1034. "\n\n\\title{%s}\n"
  1035. ;; convert the title
  1036. (org-export-latex-content
  1037. title '(lists tables fixed-width keywords)))
  1038. ;; insert author info
  1039. (if (plist-get opt-plist :author-info)
  1040. (format "\\author{%s}\n"
  1041. (org-export-latex-fontify-headline (or author user-full-name)))
  1042. (format "%%\\author{%s}\n"
  1043. (or author user-full-name)))
  1044. ;; insert the date
  1045. (format "\\date{%s}\n"
  1046. (format-time-string
  1047. (or (plist-get opt-plist :date)
  1048. org-export-latex-date-format)))
  1049. ;; beginning of the document
  1050. "\n\\begin{document}\n\n"
  1051. ;; insert the title command
  1052. (when (string-match "\\S-" title)
  1053. (if (string-match "%s" org-export-latex-title-command)
  1054. (format org-export-latex-title-command title)
  1055. org-export-latex-title-command))
  1056. "\n\n"
  1057. ;; table of contents
  1058. (when (and org-export-with-toc
  1059. (plist-get opt-plist :section-numbers))
  1060. (funcall org-export-latex-format-toc-function
  1061. (cond ((numberp toc)
  1062. (min toc (plist-get opt-plist :headline-levels)))
  1063. (toc (plist-get opt-plist :headline-levels))))))))
  1064. (defun org-export-latex-format-toc-default (depth)
  1065. (when depth
  1066. (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
  1067. depth)))
  1068. (defun org-export-latex-first-lines (opt-plist &optional beg end)
  1069. "Export the first lines before first headline.
  1070. If BEG is non-nil, it is the beginning of the region.
  1071. If END is non-nil, it is the end of the region."
  1072. (save-excursion
  1073. (goto-char (or beg (point-min)))
  1074. (let* ((pt (point))
  1075. (end (if (re-search-forward "^\\*+ " end t)
  1076. (goto-char (match-beginning 0))
  1077. (goto-char (or end (point-max))))))
  1078. (prog1
  1079. (org-export-latex-content
  1080. (org-export-preprocess-string
  1081. (buffer-substring pt end)
  1082. :for-LaTeX t
  1083. :emph-multiline t
  1084. :add-text nil
  1085. :comments nil
  1086. :skip-before-1st-heading nil
  1087. :LaTeX-fragments nil
  1088. :timestamps (plist-get opt-plist :timestamps)
  1089. :footnotes (plist-get opt-plist :footnotes)))
  1090. (org-unmodified
  1091. (let ((inhibit-read-only t))
  1092. (add-text-properties pt (max pt (1- end))
  1093. '(:org-license-to-kill t))))))))
  1094. (defvar org-export-latex-header-defs nil
  1095. "The header definitions that might be used in the LaTeX body.")
  1096. (defvar org-export-latex-header-defs-re nil
  1097. "The header definitions that might be used in the LaTeX body.")
  1098. (defun org-export-latex-content (content &optional exclude-list)
  1099. "Convert CONTENT string to LaTeX.
  1100. Don't perform conversions that are in EXCLUDE-LIST. Recognized
  1101. conversion types are: quotation-marks, emphasis, sub-superscript,
  1102. links, keywords, lists, tables, fixed-width"
  1103. (with-temp-buffer
  1104. (insert content)
  1105. (unless (memq 'timestamps exclude-list)
  1106. (org-export-latex-time-stamps))
  1107. (unless (memq 'quotation-marks exclude-list)
  1108. (org-export-latex-quotation-marks))
  1109. (unless (memq 'emphasis exclude-list)
  1110. (when (plist-get org-export-latex-options-plist :emphasize)
  1111. (org-export-latex-fontify)))
  1112. (unless (memq 'sub-superscript exclude-list)
  1113. (org-export-latex-special-chars
  1114. (plist-get org-export-latex-options-plist :sub-superscript)))
  1115. (unless (memq 'links exclude-list)
  1116. (org-export-latex-links))
  1117. (unless (memq 'keywords exclude-list)
  1118. (org-export-latex-keywords))
  1119. (unless (memq 'lists exclude-list)
  1120. (org-export-latex-lists))
  1121. (unless (memq 'tables exclude-list)
  1122. (org-export-latex-tables
  1123. (plist-get org-export-latex-options-plist :tables)))
  1124. (unless (memq 'fixed-width exclude-list)
  1125. (org-export-latex-fixed-width
  1126. (plist-get org-export-latex-options-plist :fixed-width)))
  1127. ;; return string
  1128. (buffer-substring (point-min) (point-max))))
  1129. (defun org-export-latex-protect-string (s)
  1130. "Add the org-protected property to string S."
  1131. (add-text-properties 0 (length s) '(org-protected t) s) s)
  1132. (defun org-export-latex-protect-char-in-string (char-list string)
  1133. "Add org-protected text-property to char from CHAR-LIST in STRING."
  1134. (with-temp-buffer
  1135. (save-match-data
  1136. (insert string)
  1137. (goto-char (point-min))
  1138. (while (re-search-forward (regexp-opt char-list) nil t)
  1139. (add-text-properties (match-beginning 0)
  1140. (match-end 0) '(org-protected t)))
  1141. (buffer-string))))
  1142. (defun org-export-latex-keywords-maybe (&optional remove-list)
  1143. "Maybe remove keywords depending on rules in REMOVE-LIST."
  1144. (goto-char (point-min))
  1145. (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
  1146. (case-fold-search nil)
  1147. (todo-markup org-export-latex-todo-keyword-markup)
  1148. fmt)
  1149. ;; convert TODO keywords
  1150. (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
  1151. (if (plist-get remove-list :todo)
  1152. (replace-match "")
  1153. (setq fmt (cond
  1154. ((stringp todo-markup) todo-markup)
  1155. ((and (consp todo-markup) (stringp (car todo-markup)))
  1156. (if (member (match-string 1) org-export-latex-done-keywords)
  1157. (cdr todo-markup) (car todo-markup)))
  1158. (t (cdr (or (assoc (match-string 1) todo-markup)
  1159. (car todo-markup))))))
  1160. (replace-match (format fmt (match-string 1)) t t)))
  1161. ;; convert priority string
  1162. (when (re-search-forward "\\[\\\\#.\\]" nil t)
  1163. (if (plist-get remove-list :priority)
  1164. (replace-match "")
  1165. (replace-match (format "\\textbf{%s}" (match-string 0)) t t)))
  1166. ;; convert tags
  1167. (when (re-search-forward "\\(:[a-zA-Z0-9_@]+\\)+:" nil t)
  1168. (if (or (not org-export-with-tags)
  1169. (plist-get remove-list :tags))
  1170. (replace-match "")
  1171. (replace-match
  1172. (org-export-latex-protect-string
  1173. (format "\\textbf{%s}"
  1174. (save-match-data
  1175. (replace-regexp-in-string
  1176. "_" "\\\\_" (match-string 0)))))
  1177. t t)))))
  1178. (defun org-export-latex-fontify-headline (string)
  1179. "Fontify special words in STRING."
  1180. (with-temp-buffer
  1181. ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
  1182. ;; the beginning of the buffer - inserting "\n" is safe here though.
  1183. (insert "\n" string)
  1184. (goto-char (point-min))
  1185. (let ((re (concat "\\\\[a-zA-Z]+\\(?:"
  1186. "\\[.*\\]"
  1187. "\\)?"
  1188. (org-create-multibrace-regexp "{" "}" 3))))
  1189. (while (re-search-forward re nil t)
  1190. (unless (save-excursion (goto-char (match-beginning 0))
  1191. (equal (char-after (point-at-bol)) ?#))
  1192. (add-text-properties (match-beginning 0) (match-end 0)
  1193. '(org-protected t)))))
  1194. (when (plist-get org-export-latex-options-plist :emphasize)
  1195. (org-export-latex-fontify))
  1196. (org-export-latex-keywords-maybe)
  1197. (org-export-latex-special-chars
  1198. (plist-get org-export-latex-options-plist :sub-superscript))
  1199. (org-export-latex-links)
  1200. (org-trim (buffer-string))))
  1201. (defun org-export-latex-time-stamps ()
  1202. "Format time stamps."
  1203. (goto-char (point-min))
  1204. (let ((org-display-custom-times org-export-latex-display-custom-times))
  1205. (while (re-search-forward org-ts-regexp-both nil t)
  1206. (org-if-unprotected-at (1- (point))
  1207. (replace-match
  1208. (org-export-latex-protect-string
  1209. (format org-export-latex-timestamp-markup
  1210. (substring (org-translate-time (match-string 0)) 1 -1)))
  1211. t t)))))
  1212. (defun org-export-latex-quotation-marks ()
  1213. "Export quotation marks depending on language conventions."
  1214. (let* ((lang (plist-get org-export-latex-options-plist :language))
  1215. (quote-rpl (if (equal lang "fr")
  1216. '(("\\(\\s-\\)\"" "«~")
  1217. ("\\(\\S-\\)\"" "~»")
  1218. ("\\(\\s-\\)'" "`"))
  1219. '(("\\(\\s-\\|[[(]\\)\"" "``")
  1220. ("\\(\\S-\\)\"" "''")
  1221. ("\\(\\s-\\|(\\)'" "`")))))
  1222. (mapc (lambda(l) (goto-char (point-min))
  1223. (while (re-search-forward (car l) nil t)
  1224. (let ((rpl (concat (match-string 1)
  1225. (org-export-latex-protect-string
  1226. (copy-sequence (cadr l))))))
  1227. (org-if-unprotected-1
  1228. (replace-match rpl t t))))) quote-rpl)))
  1229. (defun org-export-latex-special-chars (sub-superscript)
  1230. "Export special characters to LaTeX.
  1231. If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
  1232. See the `org-export-latex.el' code for a complete conversion table."
  1233. (goto-char (point-min))
  1234. (mapc (lambda(c)
  1235. (goto-char (point-min))
  1236. (while (re-search-forward c nil t)
  1237. ;; Put the point where to check for org-protected
  1238. (unless (get-text-property (match-beginning 2) 'org-protected)
  1239. (cond ((member (match-string 2) '("\\$" "$"))
  1240. (if (equal (match-string 2) "\\$")
  1241. nil
  1242. (replace-match "\\$" t t)))
  1243. ((member (match-string 2) '("&" "%" "#"))
  1244. (if (equal (match-string 1) "\\")
  1245. (replace-match (match-string 2) t t)
  1246. (replace-match (concat (match-string 1) "\\"
  1247. (match-string 2)) t t)))
  1248. ((equal (match-string 2) "...")
  1249. (replace-match
  1250. (concat (match-string 1)
  1251. (org-export-latex-protect-string "\\ldots{}")) t t))
  1252. ((equal (match-string 2) "~")
  1253. (cond ((equal (match-string 1) "\\") nil)
  1254. ((eq 'org-link (get-text-property 0 'face (match-string 2)))
  1255. (replace-match (concat (match-string 1) "\\~") t t))
  1256. (t (replace-match
  1257. (org-export-latex-protect-string
  1258. (concat (match-string 1) "\\~{}")) t t))))
  1259. ((member (match-string 2) '("{" "}"))
  1260. (unless (save-match-data (org-inside-latex-math-p))
  1261. (if (equal (match-string 1) "\\")
  1262. (replace-match (match-string 2) t t)
  1263. (replace-match (concat (match-string 1) "\\"
  1264. (match-string 2)) t t)))))
  1265. (unless (save-match-data (org-inside-latex-math-p))
  1266. (cond ((equal (match-string 2) "\\")
  1267. (replace-match (or (save-match-data
  1268. (org-export-latex-treat-backslash-char
  1269. (match-string 1)
  1270. (or (match-string 3) "")))
  1271. "") t t))
  1272. ((member (match-string 2) '("_" "^"))
  1273. (replace-match (or (save-match-data
  1274. (org-export-latex-treat-sub-super-char
  1275. sub-superscript
  1276. (match-string 2)
  1277. (match-string 1)
  1278. (match-string 3))) "") t t)
  1279. (backward-char 1)))))))
  1280. '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
  1281. "\\(\\(\\\\?\\$\\)\\)"
  1282. "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
  1283. "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
  1284. "\\(.\\|^\\)\\(&\\)"
  1285. "\\(.\\|^\\)\\(#\\)"
  1286. "\\(.\\|^\\)\\(%\\)"
  1287. "\\(.\\|^\\)\\({\\)"
  1288. "\\(.\\|^\\)\\(}\\)"
  1289. "\\(.\\|^\\)\\(~\\)"
  1290. "\\(.\\|^\\)\\(\\.\\.\\.\\)"
  1291. ;; (?\< . "\\textless{}")
  1292. ;; (?\> . "\\textgreater{}")
  1293. )))
  1294. (defun org-inside-latex-math-p ()
  1295. (get-text-property (point) 'org-latex-math))
  1296. (defun org-export-latex-treat-sub-super-char
  1297. (subsup char string-before string-after)
  1298. "Convert the \"_\" and \"^\" characters to LaTeX.
  1299. SUBSUP corresponds to the ^: option in the #+OPTIONS line.
  1300. Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
  1301. (cond ((equal string-before "\\")
  1302. (concat string-before char string-after))
  1303. ((and (string-match "\\S-+" string-after))
  1304. ;; this is part of a math formula
  1305. (cond ((eq 'org-link (get-text-property 0 'face char))
  1306. (concat string-before "\\" char string-after))
  1307. ((save-match-data (org-inside-latex-math-p))
  1308. (if subsup
  1309. (cond ((eq 1 (length string-after))
  1310. (concat string-before char string-after))
  1311. ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
  1312. (format "%s%s{%s}" string-before char
  1313. (match-string 1 string-after))))))
  1314. ((and (> (length string-after) 1)
  1315. (or (eq subsup t)
  1316. (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
  1317. (string-match "[({]?\\([^)}]+\\)[)}]?" string-after))
  1318. (org-export-latex-protect-string
  1319. (format "%s$%s{%s}$" string-before char
  1320. (if (and (> (match-end 1) (1+ (match-beginning 1)))
  1321. (not (equal (substring string-after 0 2) "{\\")))
  1322. (concat "\\mathrm{" (match-string 1 string-after) "}")
  1323. (match-string 1 string-after)))))
  1324. ((eq subsup t) (concat string-before "$" char string-after "$"))
  1325. (t (org-export-latex-protect-string
  1326. (concat string-before "\\" char "{}" string-after)))))
  1327. (t (org-export-latex-protect-string
  1328. (concat string-before "\\" char "{}" string-after)))))
  1329. (defun org-export-latex-treat-backslash-char (string-before string-after)
  1330. "Convert the \"$\" special character to LaTeX.
  1331. The conversion is made depending of STRING-BEFORE and STRING-AFTER."
  1332. (cond ((member (list string-after) org-html-entities)
  1333. ;; backslash is part of a special entity (like "\alpha")
  1334. (concat string-before "$\\"
  1335. (or (cdar (member (list string-after) org-html-entities))
  1336. string-after) "$"))
  1337. ((and (not (string-match "^[ \n\t]" string-after))
  1338. (not (string-match "[ \t]\\'\\|^" string-before)))
  1339. ;; backslash is inside a word
  1340. (concat string-before
  1341. (org-export-latex-protect-string
  1342. (concat "\\textbackslash{}" string-after))))
  1343. ((not (or (equal string-after "")
  1344. (string-match "^[ \t\n]" string-after)))
  1345. ;; backslash might escape a character (like \#) or a user TeX
  1346. ;; macro (like \setcounter)
  1347. (concat string-before
  1348. (org-export-latex-protect-string (concat "\\" string-after))))
  1349. ((and (string-match "^[ \t\n]" string-after)
  1350. (string-match "[ \t\n]\\'" string-before))
  1351. ;; backslash is alone, convert it to $\backslash$
  1352. (org-export-latex-protect-string
  1353. (concat string-before "\\textbackslash{}" string-after)))
  1354. (t (org-export-latex-protect-string
  1355. (concat string-before "\\textbackslash{}" string-after)))))
  1356. (defun org-export-latex-keywords ()
  1357. "Convert special keywords to LaTeX."
  1358. (goto-char (point-min))
  1359. (while (re-search-forward org-export-latex-special-keyword-regexp nil t)
  1360. (replace-match (format org-export-latex-timestamp-keyword-markup
  1361. (match-string 0)) t t)
  1362. (save-excursion
  1363. (beginning-of-line 1)
  1364. (unless (looking-at ".*\\\\newline[ \t]*$")
  1365. (end-of-line 1)
  1366. (insert "\\newline")))))
  1367. (defun org-export-latex-fixed-width (opt)
  1368. "When OPT is non-nil convert fixed-width sections to LaTeX."
  1369. (goto-char (point-min))
  1370. (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t)
  1371. (if opt
  1372. (progn (goto-char (match-beginning 0))
  1373. (insert "\\begin{verbatim}\n")
  1374. (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
  1375. (replace-match (concat (match-string 1)
  1376. (match-string 2)) t t)
  1377. (forward-line))
  1378. (insert "\\end{verbatim}\n\n"))
  1379. (progn (goto-char (match-beginning 0))
  1380. (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
  1381. (replace-match (concat "%" (match-string 1)
  1382. (match-string 2)) t t)
  1383. (forward-line))))))
  1384. (defvar org-table-last-alignment) ; defined in org-table.el
  1385. (defvar org-table-last-column-widths) ; defined in org-table.el
  1386. (declare-function orgtbl-to-latex "org-table" (table params) t)
  1387. (defun org-export-latex-tables (insert)
  1388. "Convert tables to LaTeX and INSERT it."
  1389. (goto-char (point-min))
  1390. (while (re-search-forward "^\\([ \t]*\\)|" nil t)
  1391. (org-if-unprotected-at (1- (point))
  1392. (org-table-align)
  1393. (let* ((beg (org-table-begin))
  1394. (end (org-table-end))
  1395. (raw-table (buffer-substring beg end))
  1396. (org-table-last-alignment (copy-sequence org-table-last-alignment))
  1397. (org-table-last-column-widths (copy-sequence
  1398. org-table-last-column-widths))
  1399. fnum fields line lines olines gr colgropen line-fmt align
  1400. caption label attr floatp longtblp)
  1401. (if org-export-latex-tables-verbatim
  1402. (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
  1403. "\\end{verbatim}\n")))
  1404. (apply 'delete-region (list beg end))
  1405. (insert (org-export-latex-protect-string tbl)))
  1406. (progn
  1407. (setq caption (org-find-text-property-in-string
  1408. 'org-caption raw-table)
  1409. attr (org-find-text-property-in-string
  1410. 'org-attributes raw-table)
  1411. label (org-find-text-property-in-string
  1412. 'org-label raw-table)
  1413. longtblp (and attr (stringp attr)
  1414. (string-match "\\<longtable\\>" attr))
  1415. align (and attr (stringp attr)
  1416. (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
  1417. (match-string 1 attr))
  1418. floatp (or caption label))
  1419. (setq lines (org-split-string raw-table "\n"))
  1420. (apply 'delete-region (list beg end))
  1421. (when org-export-table-remove-special-lines
  1422. (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
  1423. (when org-table-clean-did-remove-column
  1424. (pop org-table-last-alignment)
  1425. (pop org-table-last-column-widths))
  1426. ;; make a formatting string to reflect alignment
  1427. (setq olines lines)
  1428. (while (and (not line-fmt) (setq line (pop olines)))
  1429. (unless (string-match "^[ \t]*|-" line)
  1430. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1431. (setq fnum (make-vector (length fields) 0))
  1432. (setq line-fmt
  1433. (mapconcat
  1434. (lambda (x)
  1435. (setq gr (pop org-table-colgroup-info))
  1436. (format "%s%%s%s"
  1437. (cond ((eq gr :start)
  1438. (prog1 (if colgropen "|" "|")
  1439. (setq colgropen t)))
  1440. ((eq gr :startend)
  1441. (prog1 (if colgropen "|" "|")
  1442. (setq colgropen nil)))
  1443. (t ""))
  1444. (if (memq gr '(:end :startend))
  1445. (progn (setq colgropen nil) "|")
  1446. "")))
  1447. fnum ""))))
  1448. ;; fix double || in line-fmt
  1449. (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
  1450. ;; maybe remove the first and last "|"
  1451. (when (and (not org-export-latex-tables-column-borders)
  1452. (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
  1453. (setq line-fmt (match-string 2 line-fmt)))
  1454. ;; format alignment
  1455. (unless align
  1456. (setq align (apply 'format
  1457. (cons line-fmt
  1458. (mapcar (lambda (x) (if x "r" "l"))
  1459. org-table-last-alignment)))))
  1460. ;; prepare the table to send to orgtbl-to-latex
  1461. (setq lines
  1462. (mapcar
  1463. (lambda(elem)
  1464. (or (and (string-match "[ \t]*|-+" elem) 'hline)
  1465. (org-split-string (org-trim elem) "|")))
  1466. lines))
  1467. (when insert
  1468. (insert (org-export-latex-protect-string
  1469. (concat
  1470. (if longtblp
  1471. (concat "\\begin{longtable}{" align "}\n")
  1472. (if floatp "\\begin{table}[htb]\n"))
  1473. (if floatp
  1474. (format
  1475. "\\caption{%s%s}"
  1476. (if label (concat "\\\label{" label "}") "")
  1477. (or caption "")))
  1478. (if (and longtblp caption) "\\\\\n" "\n")
  1479. (if (and org-export-latex-tables-centered (not longtblp))
  1480. "\\begin{center}\n")
  1481. (if (not longtblp) (concat "\\begin{tabular}{" align "}\n"))
  1482. (orgtbl-to-latex
  1483. lines
  1484. `(:tstart nil :tend nil
  1485. :hlend ,(if longtblp
  1486. (format "\\\\
  1487. \\hline
  1488. \\endhead
  1489. \\hline\\multicolumn{%d}{r}{Continued on next page}\\
  1490. \\endfoot
  1491. \\endlastfoot" (length org-table-last-alignment))
  1492. nil)))
  1493. (if (not longtblp) (concat "\n\\end{tabular}"))
  1494. (if longtblp "\n" (if org-export-latex-tables-centered
  1495. "\n\\end{center}\n" "\n"))
  1496. (if longtblp
  1497. "\\end{longtable}"
  1498. (if floatp "\\end{table}"))))
  1499. "\n\n"))))))))
  1500. (defun org-export-latex-fontify ()
  1501. "Convert fontification to LaTeX."
  1502. (goto-char (point-min))
  1503. (while (re-search-forward org-emph-re nil t)
  1504. ;; The match goes one char after the *string*, except at the end of a line
  1505. (let ((emph (assoc (match-string 3)
  1506. org-export-latex-emphasis-alist))
  1507. (beg (match-beginning 0))
  1508. (end (match-end 0))
  1509. rpl s)
  1510. (unless emph
  1511. (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
  1512. (match-string 3)))
  1513. (unless (or (and (get-text-property (- (point) 2) 'org-protected)
  1514. (not (get-text-property
  1515. (- (point) 2) 'org-verbatim-emph)))
  1516. (save-excursion
  1517. (goto-char (match-beginning 1))
  1518. (save-match-data
  1519. (and (org-at-table-p)
  1520. (string-match
  1521. "[|\n]" (buffer-substring beg end))))))
  1522. (setq s (match-string 4))
  1523. (setq rpl (concat (match-string 1)
  1524. (org-export-latex-emph-format (cadr emph)
  1525. (match-string 4))
  1526. (match-string 5)))
  1527. (if (caddr emph)
  1528. (setq rpl (org-export-latex-protect-string rpl))
  1529. (save-match-data
  1530. (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl)
  1531. (progn
  1532. (add-text-properties (match-beginning 1) (match-end 1)
  1533. '(org-protected t) rpl)
  1534. (add-text-properties (match-beginning 3) (match-end 3)
  1535. '(org-protected t) rpl)))))
  1536. (replace-match rpl t t)))
  1537. (backward-char)))
  1538. (defvar org-export-latex-use-verb nil)
  1539. (defun org-export-latex-emph-format (format string)
  1540. "Format an emphasis string and handle the \\verb special case."
  1541. (when (equal format "\\verb")
  1542. (save-match-data
  1543. (if org-export-latex-use-verb
  1544. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1545. (catch 'exit
  1546. (loop for i from 0 to (1- (length ll)) do
  1547. (if (not (string-match (regexp-quote (substring ll i (1+ i)))
  1548. string))
  1549. (progn
  1550. (setq format (concat "\\verb" (substring ll i (1+ i))
  1551. "%s" (substring ll i (1+ i))))
  1552. (throw 'exit nil))))))
  1553. (let ((start 0)
  1554. (trans '(("\\" . "\\textbackslash{}")
  1555. ("~" . "\\textasciitilde{}")
  1556. ("^" . "\\textasciicircum{}")))
  1557. (rtn "") char)
  1558. (while (string-match "[\\{}$%&_#~^]" string)
  1559. (setq char (match-string 0 string))
  1560. (if (> (match-beginning 0) 0)
  1561. (setq rtn (concat rtn (substring string
  1562. 0 (match-beginning 0)))))
  1563. (setq string (substring string (1+ (match-beginning 0))))
  1564. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  1565. rtn (concat rtn char)))
  1566. (setq string (concat rtn string) format "\\texttt{%s}")))))
  1567. (format format string))
  1568. (defun org-export-latex-links ()
  1569. ;; Make sure to use the LaTeX hyperref and graphicx package
  1570. ;; or send some warnings.
  1571. "Convert links to LaTeX."
  1572. (goto-char (point-min))
  1573. (while (re-search-forward org-bracket-link-analytic-regexp++ nil t)
  1574. (org-if-unprotected-1
  1575. (goto-char (match-beginning 0))
  1576. (let* ((re-radio org-export-latex-all-targets-re)
  1577. (remove (list (match-beginning 0) (match-end 0)))
  1578. (raw-path (org-extract-attributes (match-string 3)))
  1579. (full-raw-path (concat (match-string 1) raw-path))
  1580. (desc (match-string 5))
  1581. (type (or (match-string 2)
  1582. (if (or (file-name-absolute-p raw-path)
  1583. (string-match "^\\.\\.?/" raw-path))
  1584. "file")))
  1585. (coderefp (equal type "coderef"))
  1586. (caption (org-find-text-property-in-string 'org-caption raw-path))
  1587. (attr (or (org-find-text-property-in-string 'org-attributes raw-path)
  1588. (plist-get org-export-latex-options-plist :latex-image-options)))
  1589. (label (org-find-text-property-in-string 'org-label raw-path))
  1590. imgp radiop
  1591. ;; define the path of the link
  1592. (path (cond
  1593. ((member type '("coderef"))
  1594. raw-path)
  1595. ((member type '("http" "https" "ftp"))
  1596. (concat type ":" raw-path))
  1597. ((and re-radio (string-match re-radio raw-path))
  1598. (setq radiop t))
  1599. ((equal type "mailto")
  1600. (concat type ":" raw-path))
  1601. ((equal type "file")
  1602. (if (and (org-file-image-p
  1603. (expand-file-name
  1604. raw-path)
  1605. org-export-latex-inline-image-extensions)
  1606. (or (get-text-property 0 'org-no-description
  1607. raw-path)
  1608. (equal desc full-raw-path)))
  1609. (setq imgp t)
  1610. (progn (when (string-match "\\(.+\\)::.+" raw-path)
  1611. (setq raw-path (match-string 1 raw-path)))
  1612. (if (file-exists-p raw-path)
  1613. (concat type "://" (expand-file-name raw-path))
  1614. (concat type "://" (org-export-directory
  1615. :LaTeX org-export-latex-options-plist)
  1616. raw-path))))))))
  1617. ;; process with link inserting
  1618. (apply 'delete-region remove)
  1619. (cond ((and imgp
  1620. (plist-get org-export-latex-options-plist :inline-images))
  1621. ;; OK, we need to inline an image
  1622. (insert
  1623. (org-export-latex-format-image raw-path caption label attr)))
  1624. (coderefp
  1625. (insert (format
  1626. (org-export-get-coderef-format path desc)
  1627. (cdr (assoc path org-export-code-refs)))))
  1628. (radiop (insert (format "\\hyperref[%s]{%s}"
  1629. (org-solidify-link-text raw-path) desc)))
  1630. ((not type)
  1631. (insert (format "\\hyperref[%s]{%s}"
  1632. (org-remove-initial-hash
  1633. (org-solidify-link-text raw-path))
  1634. desc)))
  1635. (path
  1636. (when (org-at-table-p)
  1637. ;; There is a strange problem when we have a link in a table,
  1638. ;; ampersands then cause a problem. I think this must be
  1639. ;; a LaTeX issue, but we here implement a work-around anyway.
  1640. (setq path (org-export-latex-protect-amp path)
  1641. desc (org-export-latex-protect-amp desc)))
  1642. (insert (format org-export-latex-hyperref-format path desc)))
  1643. (t (insert "\\texttt{" desc "}")))))))
  1644. (defun org-export-latex-format-image (path caption label attr)
  1645. "Format the image element, depending on user settings."
  1646. (let (ind floatp wrapp placement figenv)
  1647. (setq floatp (or caption label))
  1648. (setq ind (org-get-text-property-any 0 'original-indentation path))
  1649. (when (and attr (stringp attr))
  1650. (if (string-match "[ \t]*\\<wrap\\>" attr)
  1651. (setq wrapp t floatp nil attr (replace-match "" t t attr)))
  1652. (if (string-match "[ \t]*\\<float\\>" attr)
  1653. (setq wrapp nil floatp t attr (replace-match "" t t attr))))
  1654. (setq placement
  1655. (cond
  1656. (wrapp "{l}{0.5\\textwidth}")
  1657. (floatp "[htb]")
  1658. (t "")))
  1659. (when (and attr (stringp attr)
  1660. (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
  1661. (setq placement (match-string 1 attr)
  1662. attr (replace-match "" t t attr)))
  1663. (setq attr (and attr (org-trim attr)))
  1664. (when (or (not attr) (= (length attr) 0))
  1665. (setq attr (cond (floatp "width=0.7\\textwidth")
  1666. (wrapp "width=0.48\\textwidth")
  1667. (t attr))))
  1668. (setq figenv
  1669. (cond
  1670. (wrapp "\\begin{wrapfigure}%placement
  1671. \\centering
  1672. \\includegraphics[%attr]{%path}
  1673. \\caption{%labelcmd%caption}
  1674. \\end{wrapfigure}")
  1675. (floatp "\\begin{figure}%placement
  1676. \\centering
  1677. \\includegraphics[%attr]{%path}
  1678. \\caption{%labelcmd%caption}
  1679. \\end{figure}")
  1680. (t "\\includegraphics[%attr]{%path}")))
  1681. (setq figenv (mapconcat 'identity (split-string figenv "\n")
  1682. (save-excursion (beginning-of-line 1)
  1683. (looking-at "[ \t]*")
  1684. (concat "\n" (match-string 0)))))
  1685. (if (and (not label) (not caption)
  1686. (string-match "^\\\\caption{.*\n" figenv))
  1687. (setq figenv (replace-match "" t t figenv)))
  1688. (org-add-props
  1689. (org-fill-template
  1690. figenv
  1691. (list (cons "path"
  1692. (if (file-name-absolute-p path)
  1693. (expand-file-name path)
  1694. path))
  1695. (cons "attr" attr)
  1696. (cons "labelcmd" (if label (format "\\label{%s}"
  1697. label)""))
  1698. (cons "caption" (or caption ""))
  1699. (cons "placement" (or placement ""))))
  1700. nil 'original-indentation ind)))
  1701. (defun org-export-latex-protect-amp (s)
  1702. (while (string-match "\\([^\\\\]\\)\\(&\\)" s)
  1703. (setq s (replace-match (concat (match-string 1 s) "\\" (match-string 2 s))
  1704. t t s)))
  1705. s)
  1706. (defun org-remove-initial-hash (s)
  1707. (if (string-match "\\`#" s)
  1708. (substring s 1)
  1709. s))
  1710. (defvar org-latex-entities) ; defined below
  1711. (defvar org-latex-entities-regexp) ; defined below
  1712. (defvar org-latex-entities-exceptions) ; defined below
  1713. (defun org-export-latex-preprocess (parameters)
  1714. "Clean stuff in the LaTeX export."
  1715. ;; Preserve line breaks
  1716. (goto-char (point-min))
  1717. (while (re-search-forward "\\\\\\\\" nil t)
  1718. (add-text-properties (match-beginning 0) (match-end 0)
  1719. '(org-protected t)))
  1720. ;; Preserve latex environments
  1721. (goto-char (point-min))
  1722. (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t)
  1723. (let* ((start (progn (beginning-of-line) (point)))
  1724. (end (and (re-search-forward
  1725. (concat "^[ \t]*\\\\end{"
  1726. (regexp-quote (match-string 1))
  1727. "}") nil t)
  1728. (point-at-eol))))
  1729. (if end
  1730. (add-text-properties start end '(org-protected t))
  1731. (goto-char (point-at-eol)))))
  1732. ;; Preserve math snippets
  1733. (let* ((matchers (plist-get org-format-latex-options :matchers))
  1734. (re-list org-latex-regexps)
  1735. beg end re e m n block off)
  1736. ;; Check the different regular expressions
  1737. (while (setq e (pop re-list))
  1738. (setq m (car e) re (nth 1 e) n (nth 2 e)
  1739. block (if (nth 3 e) "\n\n" ""))
  1740. (setq off (if (member m '("$" "$1")) 1 0))
  1741. (when (and (member m matchers) (not (equal m "begin")))
  1742. (goto-char (point-min))
  1743. (while (re-search-forward re nil t)
  1744. (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
  1745. (add-text-properties beg end '(org-protected t org-latex-math t))))))
  1746. ;; Convert LaTeX to \LaTeX{}
  1747. (goto-char (point-min))
  1748. (let ((case-fold-search nil))
  1749. (while (re-search-forward "\\([^+_]\\)LaTeX" nil t)
  1750. (org-if-unprotected
  1751. (replace-match (org-export-latex-protect-string
  1752. (concat (match-string 1) "\\LaTeX{}")) t t))))
  1753. ;; Convert blockquotes
  1754. (goto-char (point-min))
  1755. (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
  1756. (org-replace-match-keep-properties "\\begin{quote}" t t))
  1757. (goto-char (point-min))
  1758. (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
  1759. (org-replace-match-keep-properties "\\end{quote}" t t))
  1760. ;; Convert verse
  1761. (goto-char (point-min))
  1762. (while (search-forward "ORG-VERSE-START" nil t)
  1763. (org-replace-match-keep-properties "\\begin{verse}" t t)
  1764. (beginning-of-line 2)
  1765. (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
  1766. (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
  1767. (goto-char (match-end 1))
  1768. (org-replace-match-keep-properties
  1769. (org-export-latex-protect-string
  1770. (concat "\\hspace*{1cm}" (match-string 2))) t t)
  1771. (beginning-of-line 1))
  1772. (if (looking-at "[ \t]*$")
  1773. (insert (org-export-latex-protect-string "\\vspace*{1em}"))
  1774. (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
  1775. (end-of-line 1)
  1776. (insert "\\\\")))
  1777. (beginning-of-line 2))
  1778. (and (looking-at "[ \t]*ORG-VERSE-END.*")
  1779. (org-replace-match-keep-properties "\\end{verse}" t t)))
  1780. ;; Convert center
  1781. (goto-char (point-min))
  1782. (while (search-forward "ORG-CENTER-START" nil t)
  1783. (org-replace-match-keep-properties "\\begin{center}" t t))
  1784. (goto-char (point-min))
  1785. (while (search-forward "ORG-CENTER-END" nil t)
  1786. (org-replace-match-keep-properties "\\end{center}" t t))
  1787. (run-hooks 'org-export-latex-after-blockquotes-hook)
  1788. ;; Convert horizontal rules
  1789. (goto-char (point-min))
  1790. (while (re-search-forward "^----+.$" nil t)
  1791. (org-if-unprotected
  1792. (replace-match (org-export-latex-protect-string "\\hrule") t t)))
  1793. ;; Protect LaTeX commands like \command[...]{...} or \command{...}
  1794. (goto-char (point-min))
  1795. (let ((re (concat
  1796. "\\\\[a-zA-Z]+"
  1797. "\\(?:\\[[^][\n]*?\\]\\)*"
  1798. "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
  1799. (while (re-search-forward re nil t)
  1800. (unless (save-excursion (goto-char (match-beginning 0))
  1801. (equal (char-after (point-at-bol)) ?#))
  1802. (add-text-properties (match-beginning 0) (match-end 0)
  1803. '(org-protected t)))))
  1804. ;; Protect LaTeX entities
  1805. (goto-char (point-min))
  1806. (let (a)
  1807. (while (re-search-forward org-latex-entities-regexp nil t)
  1808. (if (setq a (assoc (match-string 0) org-latex-entities-exceptions))
  1809. (replace-match (org-add-props (nth 1 a) nil 'org-protected t)
  1810. t t)
  1811. (add-text-properties (match-beginning 0) (match-end 0)
  1812. '(org-protected t)))))
  1813. ;; Replace radio links
  1814. (goto-char (point-min))
  1815. (while (re-search-forward
  1816. (concat "<<<?" org-export-latex-all-targets-re
  1817. ">>>?\\((INVISIBLE)\\)?") nil t)
  1818. (org-if-unprotected-at (+ (match-beginning 0) 2)
  1819. (replace-match
  1820. (concat
  1821. (org-export-latex-protect-string
  1822. (format "\\label{%s}" (save-match-data (org-solidify-link-text
  1823. (match-string 1)))))
  1824. (if (match-string 2) "" (match-string 1)))
  1825. t t)))
  1826. ;; Delete @<...> constructs
  1827. ;; Thanks to Daniel Clemente for this regexp
  1828. (goto-char (point-min))
  1829. (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
  1830. (org-if-unprotected
  1831. (replace-match "")))
  1832. ;; When converting to LaTeX, replace footnotes
  1833. ;; FIXME: don't protect footnotes from conversion
  1834. (when (plist-get org-export-latex-options-plist :footnotes)
  1835. (goto-char (point-min))
  1836. (while (re-search-forward "\\[\\([0-9]+\\)\\]" nil t)
  1837. (org-if-unprotected
  1838. (when (and (save-match-data
  1839. (save-excursion (beginning-of-line)
  1840. (looking-at "[^:|#]")))
  1841. (not (org-in-verbatim-emphasis)))
  1842. (let ((foot-beg (match-beginning 0))
  1843. (foot-end (match-end 0))
  1844. (foot-prefix (match-string 0))
  1845. footnote footnote-rpl)
  1846. (save-excursion
  1847. (if (not (re-search-forward (concat "^" (regexp-quote foot-prefix))
  1848. nil t))
  1849. (replace-match (org-export-latex-protect-string
  1850. (concat "$^{" (match-string 1) "}$")))
  1851. (replace-match "")
  1852. (let ((end (save-excursion
  1853. (if (re-search-forward "^$\\|^#.*$\\|\\[[0-9]+\\]" nil t)
  1854. (match-beginning 0) (point-max)))))
  1855. (setq footnote (concat (org-trim (buffer-substring (point) end))
  1856. " ")) ; prevent last } being part of a link
  1857. (delete-region (point) end))
  1858. (goto-char foot-beg)
  1859. (delete-region foot-beg foot-end)
  1860. (unless (null footnote)
  1861. (setq footnote-rpl (format "\\footnote{%s}" footnote))
  1862. (add-text-properties 0 10 '(org-protected t) footnote-rpl)
  1863. (add-text-properties (1- (length footnote-rpl))
  1864. (length footnote-rpl)
  1865. '(org-protected t) footnote-rpl)
  1866. (insert footnote-rpl)))
  1867. )))))
  1868. ;; Remove footnote section tag for LaTeX
  1869. (goto-char (point-min))
  1870. (while (re-search-forward
  1871. (concat "^" footnote-section-tag-regexp) nil t)
  1872. (org-if-unprotected
  1873. (replace-match "")))))
  1874. (defun org-export-latex-fix-inputenc ()
  1875. "Set the codingsystem in inputenc to what the buffer is."
  1876. (let* ((cs buffer-file-coding-system)
  1877. (opt (latexenc-coding-system-to-inputenc cs)))
  1878. (when opt
  1879. ;; Translate if that is requested
  1880. (setq opt (or (cdr (assoc opt org-export-latex-inputenc-alist)) opt))
  1881. ;; find the \usepackage statement and replace the option
  1882. (goto-char (point-min))
  1883. (while (re-search-forward "\\\\usepackage\\[\\(.*?\\)\\]{inputenc}"
  1884. nil t)
  1885. (goto-char (match-beginning 1))
  1886. (delete-region (match-beginning 1) (match-end 1))
  1887. (insert opt))
  1888. (save-buffer))))
  1889. ;;; List handling:
  1890. (defun org-export-latex-lists ()
  1891. "Convert plain text lists in current buffer into LaTeX lists."
  1892. (goto-char (point-min))
  1893. (while (re-search-forward org-list-beginning-re nil t)
  1894. (org-if-unprotected
  1895. (beginning-of-line)
  1896. (insert (org-list-to-latex (org-list-parse-list t)
  1897. org-export-latex-list-parameters))
  1898. "\n")))
  1899. (defconst org-latex-entities
  1900. '("\\!"
  1901. "\\'"
  1902. "\\+"
  1903. "\\,"
  1904. "\\-"
  1905. "\\:"
  1906. "\\;"
  1907. "\\<"
  1908. "\\="
  1909. "\\>"
  1910. "\\Huge"
  1911. "\\LARGE"
  1912. "\\Large"
  1913. "\\Styles"
  1914. "\\\\"
  1915. "\\`"
  1916. "\\addcontentsline"
  1917. "\\address"
  1918. "\\addtocontents"
  1919. "\\addtocounter"
  1920. "\\addtolength"
  1921. "\\addvspace"
  1922. "\\alph"
  1923. "\\appendix"
  1924. "\\arabic"
  1925. "\\author"
  1926. "\\begin{array}"
  1927. "\\begin{center}"
  1928. "\\begin{description}"
  1929. "\\begin{enumerate}"
  1930. "\\begin{eqnarray}"
  1931. "\\begin{equation}"
  1932. "\\begin{figure}"
  1933. "\\begin{flushleft}"
  1934. "\\begin{flushright}"
  1935. "\\begin{itemize}"
  1936. "\\begin{list}"
  1937. "\\begin{minipage}"
  1938. "\\begin{picture}"
  1939. "\\begin{quotation}"
  1940. "\\begin{quote}"
  1941. "\\begin{tabbing}"
  1942. "\\begin{table}"
  1943. "\\begin{tabular}"
  1944. "\\begin{thebibliography}"
  1945. "\\begin{theorem}"
  1946. "\\begin{titlepage}"
  1947. "\\begin{verbatim}"
  1948. "\\begin{verse}"
  1949. "\\bf"
  1950. "\\bf"
  1951. "\\bibitem"
  1952. "\\bigskip"
  1953. "\\cdots"
  1954. "\\centering"
  1955. "\\circle"
  1956. "\\cite"
  1957. "\\cleardoublepage"
  1958. "\\clearpage"
  1959. "\\cline"
  1960. "\\closing"
  1961. "\\dashbox"
  1962. "\\date"
  1963. "\\ddots"
  1964. "\\dotfill"
  1965. "\\em"
  1966. "\\fbox"
  1967. "\\flushbottom"
  1968. "\\fnsymbol"
  1969. "\\footnote"
  1970. "\\footnotemark"
  1971. "\\footnotesize"
  1972. "\\footnotetext"
  1973. "\\frac"
  1974. "\\frame"
  1975. "\\framebox"
  1976. "\\hfill"
  1977. "\\hline"
  1978. "\\hrulespace"
  1979. "\\hspace"
  1980. "\\huge"
  1981. "\\hyphenation"
  1982. "\\include"
  1983. "\\includeonly"
  1984. "\\indent"
  1985. "\\input"
  1986. "\\it"
  1987. "\\kill"
  1988. "\\label"
  1989. "\\large"
  1990. "\\ldots"
  1991. "\\line"
  1992. "\\linebreak"
  1993. "\\linethickness"
  1994. "\\listoffigures"
  1995. "\\listoftables"
  1996. "\\location"
  1997. "\\makebox"
  1998. "\\maketitle"
  1999. "\\mark"
  2000. "\\mbox"
  2001. "\\medskip"
  2002. "\\multicolumn"
  2003. "\\multiput"
  2004. ("\\nbsp" "~")
  2005. "\\newcommand"
  2006. "\\newcounter"
  2007. "\\newenvironment"
  2008. "\\newfont"
  2009. "\\newlength"
  2010. "\\newline"
  2011. "\\newpage"
  2012. "\\newsavebox"
  2013. "\\newtheorem"
  2014. "\\nocite"
  2015. "\\nofiles"
  2016. "\\noindent"
  2017. "\\nolinebreak"
  2018. "\\nopagebreak"
  2019. "\\normalsize"
  2020. "\\onecolumn"
  2021. "\\opening"
  2022. "\\oval"
  2023. "\\overbrace"
  2024. "\\overline"
  2025. "\\pagebreak"
  2026. "\\pagenumbering"
  2027. "\\pageref"
  2028. "\\pagestyle"
  2029. "\\par"
  2030. "\\parbox"
  2031. "\\put"
  2032. "\\raggedbottom"
  2033. "\\raggedleft"
  2034. "\\raggedright"
  2035. "\\raisebox"
  2036. "\\ref"
  2037. "\\rm"
  2038. "\\roman"
  2039. "\\rule"
  2040. "\\savebox"
  2041. "\\sc"
  2042. "\\scriptsize"
  2043. "\\setcounter"
  2044. "\\setlength"
  2045. "\\settowidth"
  2046. "\\sf"
  2047. "\\shortstack"
  2048. "\\signature"
  2049. "\\sl"
  2050. "\\small"
  2051. "\\smallskip"
  2052. "\\sqrt"
  2053. "\\tableofcontents"
  2054. "\\telephone"
  2055. "\\thanks"
  2056. "\\thispagestyle"
  2057. "\\tiny"
  2058. "\\title"
  2059. "\\tt"
  2060. "\\twocolumn"
  2061. "\\typein"
  2062. "\\typeout"
  2063. "\\underbrace"
  2064. "\\underline"
  2065. "\\usebox"
  2066. "\\usecounter"
  2067. "\\value"
  2068. "\\vdots"
  2069. "\\vector"
  2070. "\\verb"
  2071. "\\vfill"
  2072. "\\vline"
  2073. "\\vspace")
  2074. "A list of LaTeX commands to be protected when performing conversion.")
  2075. (defvar org-latex-entities-exceptions nil)
  2076. (defconst org-latex-entities-regexp
  2077. (let (names rest)
  2078. (dolist (x org-latex-entities)
  2079. (when (consp x)
  2080. (add-to-list 'org-latex-entities-exceptions x)
  2081. (setq x (car x)))
  2082. (if (string-match "[a-zA-Z]$" x)
  2083. (push x names)
  2084. (push x rest)))
  2085. (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
  2086. "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
  2087. (provide 'org-export-latex)
  2088. (provide 'org-latex)
  2089. ;; arch-tag: 23c2b87d-da04-4c2d-ad2d-1eb6487bc3ad
  2090. ;;; org-latex.el ends here