org-latex.el 103 KB

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