org-latex.el 99 KB

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