org-latex.el 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815
  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 "texi2dvi"
  574. ("texi2dvi -p -b -c -V %f"))
  575. (const :tag "rubber"
  576. ("rubber -d --into %o %f"))
  577. (function)))
  578. (defcustom org-export-pdf-logfiles
  579. '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
  580. "The list of file extensions to consider as LaTeX logfiles."
  581. :group 'org-export-pdf
  582. :version "24.1"
  583. :type '(repeat (string :tag "Extension")))
  584. (defcustom org-export-pdf-remove-logfiles t
  585. "Non-nil means remove the logfiles produced by PDF production.
  586. These are the .aux, .log, .out, and .toc files."
  587. :group 'org-export-pdf
  588. :type 'boolean)
  589. ;;; Hooks
  590. (defvar org-export-latex-after-initial-vars-hook nil
  591. "Hook run before LaTeX export.
  592. The exact moment is after the initial variables like org-export-latex-class
  593. have been determined from the environment.")
  594. (defvar org-export-latex-after-blockquotes-hook nil
  595. "Hook run during LaTeX export, after blockquote, verse, center are done.")
  596. (defvar org-export-latex-final-hook nil
  597. "Hook run in the finalized LaTeX buffer.")
  598. (defvar org-export-latex-after-save-hook nil
  599. "Hook run in the finalized LaTeX buffer, after it has been saved.")
  600. ;;; Autoload functions:
  601. ;;;###autoload
  602. (defun org-export-as-latex-batch ()
  603. "Call `org-export-as-latex', may be used in batch processing.
  604. For example:
  605. emacs --batch
  606. --load=$HOME/lib/emacs/org.el
  607. --eval \"(setq org-export-headline-levels 2)\"
  608. --visit=MyFile --funcall org-export-as-latex-batch"
  609. (org-export-as-latex org-export-headline-levels 'hidden))
  610. ;;;###autoload
  611. (defun org-export-as-latex-to-buffer (arg)
  612. "Call `org-export-as-latex` with output to a temporary buffer.
  613. No file is created. The prefix ARG is passed through to `org-export-as-latex'."
  614. (interactive "P")
  615. (org-export-as-latex arg nil nil "*Org LaTeX Export*")
  616. (when org-export-show-temporary-export-buffer
  617. (switch-to-buffer-other-window "*Org LaTeX Export*")))
  618. ;;;###autoload
  619. (defun org-replace-region-by-latex (beg end)
  620. "Replace the region from BEG to END with its LaTeX export.
  621. It assumes the region has `org-mode' syntax, and then convert it to
  622. LaTeX. This can be used in any buffer. For example, you could
  623. write an itemized list in `org-mode' syntax in an LaTeX buffer and
  624. then use this command to convert it."
  625. (interactive "r")
  626. (let (reg latex buf)
  627. (save-window-excursion
  628. (if (eq major-mode 'org-mode)
  629. (setq latex (org-export-region-as-latex
  630. beg end t 'string))
  631. (setq reg (buffer-substring beg end)
  632. buf (get-buffer-create "*Org tmp*"))
  633. (with-current-buffer buf
  634. (erase-buffer)
  635. (insert reg)
  636. (org-mode)
  637. (setq latex (org-export-region-as-latex
  638. (point-min) (point-max) t 'string)))
  639. (kill-buffer buf)))
  640. (delete-region beg end)
  641. (insert latex)))
  642. ;;;###autoload
  643. (defun org-export-region-as-latex (beg end &optional body-only buffer)
  644. "Convert region from BEG to END in `org-mode' buffer to LaTeX.
  645. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  646. contents, and only produce the region of converted text, useful for
  647. cut-and-paste operations.
  648. If BUFFER is a buffer or a string, use/create that buffer as a target
  649. of the converted LaTeX. If BUFFER is the symbol `string', return the
  650. produced LaTeX as a string and leave no buffer behind. For example,
  651. a Lisp program could call this function in the following way:
  652. (setq latex (org-export-region-as-latex beg end t 'string))
  653. When called interactively, the output buffer is selected, and shown
  654. in a window. A non-interactive call will only return the buffer."
  655. (interactive "r\nP")
  656. (when (org-called-interactively-p 'any)
  657. (setq buffer "*Org LaTeX Export*"))
  658. (let ((transient-mark-mode t) (zmacs-regions t)
  659. ext-plist rtn)
  660. (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
  661. (goto-char end)
  662. (set-mark (point)) ;; to activate the region
  663. (goto-char beg)
  664. (setq rtn (org-export-as-latex
  665. nil nil ext-plist
  666. buffer body-only))
  667. (if (fboundp 'deactivate-mark) (deactivate-mark))
  668. (if (and (org-called-interactively-p 'any) (bufferp rtn))
  669. (switch-to-buffer-other-window rtn)
  670. rtn)))
  671. ;;;###autoload
  672. (defun org-export-as-latex (arg &optional hidden ext-plist
  673. to-buffer body-only pub-dir)
  674. "Export current buffer to a LaTeX file.
  675. If there is an active region, export only the region. The prefix
  676. ARG specifies how many levels of the outline should become
  677. headlines. The default is 3. Lower levels will be exported
  678. depending on `org-export-latex-low-levels'. The default is to
  679. convert them as description lists.
  680. HIDDEN is obsolete and does nothing.
  681. EXT-PLIST is a property list with
  682. external parameters overriding org-mode's default settings, but
  683. still inferior to file-local settings. When TO-BUFFER is
  684. non-nil, create a buffer with that name and export to that
  685. buffer. If TO-BUFFER is the symbol `string', don't leave any
  686. buffer behind but just return the resulting LaTeX as a string.
  687. When BODY-ONLY is set, don't produce the file header and footer,
  688. simply return the content of \\begin{document}...\\end{document},
  689. without even the \\begin{document} and \\end{document} commands.
  690. when PUB-DIR is set, use this as the publishing directory."
  691. (interactive "P")
  692. (when (and (not body-only) arg (listp arg)) (setq body-only t))
  693. (run-hooks 'org-export-first-hook)
  694. ;; Make sure we have a file name when we need it.
  695. (when (and (not (or to-buffer body-only))
  696. (not buffer-file-name))
  697. (if (buffer-base-buffer)
  698. (org-set-local 'buffer-file-name
  699. (with-current-buffer (buffer-base-buffer)
  700. buffer-file-name))
  701. (error "Need a file name to be able to export")))
  702. (message "Exporting to LaTeX...")
  703. (org-unmodified
  704. (let ((inhibit-read-only t))
  705. (remove-text-properties (point-min) (point-max)
  706. '(:org-license-to-kill nil))))
  707. (org-update-radio-target-regexp)
  708. (org-export-latex-set-initial-vars ext-plist arg)
  709. (setq org-export-opt-plist org-export-latex-options-plist
  710. org-export-footnotes-data (org-footnote-all-labels 'with-defs)
  711. org-export-footnotes-seen nil
  712. org-export-latex-footmark-seen nil)
  713. (org-install-letbind)
  714. (run-hooks 'org-export-latex-after-initial-vars-hook)
  715. (let* ((wcf (current-window-configuration))
  716. (opt-plist
  717. (org-export-process-option-filters org-export-latex-options-plist))
  718. (region-p (org-region-active-p))
  719. (rbeg (and region-p (region-beginning)))
  720. (rend (and region-p (region-end)))
  721. (subtree-p
  722. (if (plist-get opt-plist :ignore-subtree-p)
  723. nil
  724. (when region-p
  725. (save-excursion
  726. (goto-char rbeg)
  727. (and (org-at-heading-p)
  728. (>= (org-end-of-subtree t t) rend))))))
  729. (opt-plist (setq org-export-opt-plist
  730. (if subtree-p
  731. (org-export-add-subtree-options opt-plist rbeg)
  732. opt-plist)))
  733. ;; Make sure the variable contains the updated values.
  734. (org-export-latex-options-plist (setq org-export-opt-plist opt-plist))
  735. ;; The following two are dynamically scoped into other
  736. ;; routines below.
  737. (org-current-export-dir
  738. (or pub-dir (org-export-directory :html opt-plist)))
  739. (org-current-export-file buffer-file-name)
  740. (title (or (and subtree-p (org-export-get-title-from-subtree))
  741. (plist-get opt-plist :title)
  742. (and (not
  743. (plist-get opt-plist :skip-before-1st-heading))
  744. (org-export-grab-title-from-buffer))
  745. (and buffer-file-name
  746. (file-name-sans-extension
  747. (file-name-nondirectory buffer-file-name)))
  748. "No Title"))
  749. (filename
  750. (and (not to-buffer)
  751. (concat
  752. (file-name-as-directory
  753. (or pub-dir
  754. (org-export-directory :LaTeX ext-plist)))
  755. (file-name-sans-extension
  756. (or (and subtree-p
  757. (org-entry-get rbeg "EXPORT_FILE_NAME" t))
  758. (file-name-nondirectory ;sans-extension
  759. (or buffer-file-name
  760. (error "Don't know which export file to use")))))
  761. ".tex")))
  762. (filename
  763. (and filename
  764. (if (equal (file-truename filename)
  765. (file-truename (or buffer-file-name "dummy.org")))
  766. (concat filename ".tex")
  767. filename)))
  768. (auto-insert nil); Avoid any auto-insert stuff for the new file
  769. (buffer (if to-buffer
  770. (cond
  771. ((eq to-buffer 'string) (get-buffer-create
  772. "*Org LaTeX Export*"))
  773. (t (get-buffer-create to-buffer)))
  774. (find-file-noselect filename)))
  775. (odd org-odd-levels-only)
  776. (header (org-export-latex-make-header title opt-plist))
  777. (skip (cond (subtree-p nil)
  778. (region-p nil)
  779. (t (plist-get opt-plist :skip-before-1st-heading))))
  780. (text (plist-get opt-plist :text))
  781. (org-export-preprocess-hook
  782. (cons
  783. `(lambda () (org-set-local 'org-complex-heading-regexp
  784. ,org-export-latex-complex-heading-re))
  785. org-export-preprocess-hook))
  786. (first-lines (if skip "" (org-export-latex-first-lines
  787. opt-plist
  788. (if subtree-p
  789. (save-excursion
  790. (goto-char rbeg)
  791. (point-at-bol 2))
  792. rbeg)
  793. (if region-p rend))))
  794. (coding-system (and (boundp 'buffer-file-coding-system)
  795. buffer-file-coding-system))
  796. (coding-system-for-write (or org-export-latex-coding-system
  797. coding-system))
  798. (save-buffer-coding-system (or org-export-latex-coding-system
  799. coding-system))
  800. (region (buffer-substring
  801. (if region-p (region-beginning) (point-min))
  802. (if region-p (region-end) (point-max))))
  803. (text
  804. (and text (string-match "\\S-" text)
  805. (org-export-preprocess-string
  806. text
  807. :emph-multiline t
  808. :for-backend 'latex
  809. :comments nil
  810. :tags (plist-get opt-plist :tags)
  811. :priority (plist-get opt-plist :priority)
  812. :footnotes (plist-get opt-plist :footnotes)
  813. :drawers (plist-get opt-plist :drawers)
  814. :timestamps (plist-get opt-plist :timestamps)
  815. :todo-keywords (plist-get opt-plist :todo-keywords)
  816. :tasks (plist-get opt-plist :tasks)
  817. :add-text nil
  818. :skip-before-1st-heading skip
  819. :select-tags nil
  820. :exclude-tags nil
  821. :LaTeX-fragments nil)))
  822. (string-for-export
  823. (org-export-preprocess-string
  824. region
  825. :emph-multiline t
  826. :for-backend 'latex
  827. :comments nil
  828. :tags (plist-get opt-plist :tags)
  829. :priority (plist-get opt-plist :priority)
  830. :footnotes (plist-get opt-plist :footnotes)
  831. :drawers (plist-get opt-plist :drawers)
  832. :timestamps (plist-get opt-plist :timestamps)
  833. :todo-keywords (plist-get opt-plist :todo-keywords)
  834. :tasks (plist-get opt-plist :tasks)
  835. :add-text (if (eq to-buffer 'string) nil text)
  836. :skip-before-1st-heading skip
  837. :select-tags (plist-get opt-plist :select-tags)
  838. :exclude-tags (plist-get opt-plist :exclude-tags)
  839. :LaTeX-fragments nil)))
  840. ;; Avoid the Query for TeX master from AUCTeX
  841. (if (boundp 'TeX-master) (setq TeX-master t))
  842. (set-buffer buffer)
  843. (erase-buffer)
  844. (org-install-letbind)
  845. (and (fboundp 'set-buffer-file-coding-system)
  846. (set-buffer-file-coding-system coding-system-for-write))
  847. ;; insert the header and initial document commands
  848. (unless (or (eq to-buffer 'string) body-only)
  849. (insert header))
  850. ;; insert text found in #+TEXT
  851. (when (and text (not (eq to-buffer 'string)))
  852. (insert (org-export-latex-content
  853. text '(lists tables fixed-width keywords))
  854. "\n\n"))
  855. ;; insert lines before the first headline
  856. (unless (or skip (string-match "^\\*" first-lines))
  857. (insert first-lines))
  858. ;; export the content of headlines
  859. (org-export-latex-global
  860. (with-temp-buffer
  861. (insert string-for-export)
  862. (goto-char (point-min))
  863. (when (re-search-forward "^\\(\\*+\\) " nil t)
  864. (let* ((asters (length (match-string 1)))
  865. (level (if odd (- asters 2) (- asters 1))))
  866. (setq org-export-latex-add-level
  867. (if odd (1- (/ (1+ asters) 2)) (1- asters)))
  868. (org-export-latex-parse-global level odd)))))
  869. ;; finalization
  870. (unless body-only (insert "\n\\end{document}"))
  871. ;; Attach description terms to the \item macro
  872. (goto-char (point-min))
  873. (while (re-search-forward "^[ \t]*\\\\item\\([ \t]+\\)\\[" nil t)
  874. (delete-region (match-beginning 1) (match-end 1)))
  875. ;; Relocate the table of contents
  876. (goto-char (point-min))
  877. (when (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
  878. (goto-char (point-min))
  879. (while (re-search-forward "\\\\tableofcontents\\>[ \t]*\n?" nil t)
  880. (replace-match ""))
  881. (goto-char (point-min))
  882. (and (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)
  883. (replace-match "\\tableofcontents" t t)))
  884. ;; Cleanup forced line ends in items where they are not needed
  885. (goto-char (point-min))
  886. (while (re-search-forward
  887. "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*\n\\\\begin"
  888. nil t)
  889. (delete-region (match-beginning 1) (match-end 1)))
  890. (goto-char (point-min))
  891. (while (re-search-forward
  892. "^[ \t]*\\\\item\\>.*\\(\\\\\\\\\\)[ \t]*\\(\n\\\\label.*\\)*"
  893. nil t)
  894. (if (looking-at "[\n \t]+")
  895. (replace-match "\n")))
  896. (run-hooks 'org-export-latex-final-hook)
  897. (if to-buffer
  898. (unless (eq major-mode 'latex-mode) (latex-mode))
  899. (save-buffer))
  900. (org-export-latex-fix-inputenc)
  901. (run-hooks 'org-export-latex-after-save-hook)
  902. (goto-char (point-min))
  903. (or (org-export-push-to-kill-ring "LaTeX")
  904. (message "Exporting to LaTeX...done"))
  905. (prog1
  906. (if (eq to-buffer 'string)
  907. (prog1 (buffer-substring (point-min) (point-max))
  908. (kill-buffer (current-buffer)))
  909. (current-buffer))
  910. (set-window-configuration wcf))))
  911. ;;;###autoload
  912. (defun org-export-as-pdf (arg &optional hidden ext-plist
  913. to-buffer body-only pub-dir)
  914. "Export as LaTeX, then process through to PDF."
  915. (interactive "P")
  916. (message "Exporting to PDF...")
  917. (let* ((wconfig (current-window-configuration))
  918. (lbuf (org-export-as-latex arg hidden ext-plist
  919. to-buffer body-only pub-dir))
  920. (file (buffer-file-name lbuf))
  921. (base (file-name-sans-extension (buffer-file-name lbuf)))
  922. (pdffile (concat base ".pdf"))
  923. (cmds (if (eq org-export-latex-listings 'minted)
  924. ;; automatically add -shell-escape when needed
  925. (mapcar (lambda (cmd)
  926. (replace-regexp-in-string
  927. "pdflatex " "pdflatex -shell-escape " cmd))
  928. org-latex-to-pdf-process)
  929. org-latex-to-pdf-process))
  930. (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
  931. (bibtex-p (with-current-buffer lbuf
  932. (save-excursion
  933. (goto-char (point-min))
  934. (re-search-forward "\\\\bibliography{" nil t))))
  935. cmd output-dir errors)
  936. (with-current-buffer outbuf (erase-buffer))
  937. (message (concat "Processing LaTeX file " file "..."))
  938. (setq output-dir (file-name-directory file))
  939. (with-current-buffer lbuf
  940. (save-excursion
  941. (if (and cmds (symbolp cmds))
  942. (funcall cmds (shell-quote-argument file))
  943. (while cmds
  944. (setq cmd (pop cmds))
  945. (while (string-match "%b" cmd)
  946. (setq cmd (replace-match
  947. (save-match-data
  948. (shell-quote-argument base))
  949. t t cmd)))
  950. (while (string-match "%f" cmd)
  951. (setq cmd (replace-match
  952. (save-match-data
  953. (shell-quote-argument file))
  954. t t cmd)))
  955. (while (string-match "%o" cmd)
  956. (setq cmd (replace-match
  957. (save-match-data
  958. (shell-quote-argument output-dir))
  959. t t cmd)))
  960. (shell-command cmd outbuf)))))
  961. (message (concat "Processing LaTeX file " file "...done"))
  962. (setq errors (org-export-latex-get-error outbuf))
  963. (if (not (file-exists-p pdffile))
  964. (error (concat "PDF file " pdffile " was not produced"
  965. (if errors (concat ":" errors "") "")))
  966. (set-window-configuration wconfig)
  967. (when org-export-pdf-remove-logfiles
  968. (dolist (ext org-export-pdf-logfiles)
  969. (setq file (concat base "." ext))
  970. (and (file-exists-p file) (delete-file file))))
  971. (message (concat
  972. "Exporting to PDF...done"
  973. (if errors
  974. (concat ", with some errors:" errors)
  975. "")))
  976. pdffile)))
  977. (defun org-export-latex-get-error (buf)
  978. "Collect the kinds of errors that remain in pdflatex processing."
  979. (with-current-buffer buf
  980. (save-excursion
  981. (goto-char (point-max))
  982. (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
  983. ;; OK, we are at the location of the final run
  984. (let ((pos (point)) (errors "") (case-fold-search t))
  985. (if (re-search-forward "Reference.*?undefined" nil t)
  986. (setq errors (concat errors " [undefined reference]")))
  987. (goto-char pos)
  988. (if (re-search-forward "Citation.*?undefined" nil t)
  989. (setq errors (concat errors " [undefined citation]")))
  990. (goto-char pos)
  991. (if (re-search-forward "Undefined control sequence" nil t)
  992. (setq errors (concat errors " [undefined control sequence]")))
  993. (and (org-string-nw-p errors) errors))))))
  994. ;;;###autoload
  995. (defun org-export-as-pdf-and-open (arg)
  996. "Export as LaTeX, then process through to PDF, and open."
  997. (interactive "P")
  998. (let ((pdffile (org-export-as-pdf arg)))
  999. (if pdffile
  1000. (progn
  1001. (org-open-file pdffile)
  1002. (when org-export-kill-product-buffer-when-displayed
  1003. (kill-buffer (find-buffer-visiting
  1004. (concat (file-name-sans-extension (buffer-file-name))
  1005. ".tex")))))
  1006. (error "PDF file was not produced"))))
  1007. ;;; Parsing functions:
  1008. (defun org-export-latex-parse-global (level odd)
  1009. "Parse the current buffer recursively, starting at LEVEL.
  1010. If ODD is non-nil, assume the buffer only contains odd sections.
  1011. Return a list reflecting the document structure."
  1012. (save-excursion
  1013. (goto-char (point-min))
  1014. (let* ((cnt 0) output
  1015. (depth org-export-latex-sectioning-depth))
  1016. (while (org-re-search-forward-unprotected
  1017. (concat "^\\(\\(?:\\*\\)\\{"
  1018. (number-to-string (+ (if odd 2 1) level))
  1019. "\\}\\) \\(.*\\)$")
  1020. ;; make sure that there is no upper heading
  1021. (when (> level 0)
  1022. (save-excursion
  1023. (save-match-data
  1024. (org-re-search-forward-unprotected
  1025. (concat "^\\(\\(?:\\*\\)\\{"
  1026. (number-to-string level)
  1027. "\\}\\) \\(.*\\)$") nil t)))) t)
  1028. (setq cnt (1+ cnt))
  1029. (let* ((pos (match-beginning 0))
  1030. (heading (match-string 2))
  1031. (nlevel (if odd (/ (+ 3 level) 2) (1+ level))))
  1032. (save-excursion
  1033. (narrow-to-region
  1034. (point)
  1035. (save-match-data
  1036. (if (org-re-search-forward-unprotected
  1037. (concat "^\\(\\(?:\\*\\)\\{"
  1038. (number-to-string (+ (if odd 2 1) level))
  1039. "\\}\\) \\(.*\\)$") nil t)
  1040. (match-beginning 0)
  1041. (point-max))))
  1042. (goto-char (point-min))
  1043. (setq output
  1044. (append output
  1045. (list
  1046. (list
  1047. `(pos . ,pos)
  1048. `(level . ,nlevel)
  1049. `(occur . ,cnt)
  1050. `(heading . ,heading)
  1051. `(content . ,(org-export-latex-parse-content))
  1052. `(subcontent . ,(org-export-latex-parse-subcontent
  1053. level odd)))))))
  1054. (widen)))
  1055. (list output))))
  1056. (defun org-export-latex-parse-content ()
  1057. "Extract the content of a section."
  1058. (let ((beg (point))
  1059. (end (if (org-re-search-forward-unprotected "^\\(\\*\\)+ .*$" nil t)
  1060. (progn (beginning-of-line) (point))
  1061. (point-max))))
  1062. (buffer-substring beg end)))
  1063. (defun org-export-latex-parse-subcontent (level odd)
  1064. "Extract the subcontent of a section at LEVEL.
  1065. If ODD Is non-nil, assume subcontent only contains odd sections."
  1066. (if (not (org-re-search-forward-unprotected
  1067. (concat "^\\(\\(?:\\*\\)\\{"
  1068. (number-to-string (+ (if odd 4 2) level))
  1069. "\\}\\) \\(.*\\)$")
  1070. nil t))
  1071. nil ; subcontent is nil
  1072. (org-export-latex-parse-global (+ (if odd 2 1) level) odd)))
  1073. ;;; Rendering functions:
  1074. (defun org-export-latex-global (content)
  1075. "Export CONTENT to LaTeX.
  1076. CONTENT is an element of the list produced by
  1077. `org-export-latex-parse-global'."
  1078. (if (eq (car content) 'subcontent)
  1079. (mapc 'org-export-latex-sub (cdr content))
  1080. (org-export-latex-sub (car content))))
  1081. (defun org-export-latex-sub (subcontent)
  1082. "Export the list SUBCONTENT to LaTeX.
  1083. SUBCONTENT is an alist containing information about the headline
  1084. and its content."
  1085. (let ((num (plist-get org-export-latex-options-plist :section-numbers)))
  1086. (mapc (lambda(x) (org-export-latex-subcontent x num)) subcontent)))
  1087. (defun org-export-latex-subcontent (subcontent num)
  1088. "Export each cell of SUBCONTENT to LaTeX.
  1089. If NUM is non-nil export numbered sections, otherwise use unnumbered
  1090. sections. If NUM is an integer, export the highest NUM levels as
  1091. numbered sections and lower levels as unnumbered sections."
  1092. (let* ((heading (cdr (assoc 'heading subcontent)))
  1093. (level (- (cdr (assoc 'level subcontent))
  1094. org-export-latex-add-level))
  1095. (occur (number-to-string (cdr (assoc 'occur subcontent))))
  1096. (content (cdr (assoc 'content subcontent)))
  1097. (subcontent (cadr (assoc 'subcontent subcontent)))
  1098. (label (org-get-text-property-any 0 'target heading))
  1099. (label-list (cons label (cdr (assoc label
  1100. org-export-target-aliases))))
  1101. (sectioning org-export-latex-sectioning)
  1102. (depth org-export-latex-sectioning-depth)
  1103. main-heading sub-heading)
  1104. (when (symbolp (car sectioning))
  1105. (setq sectioning (funcall (car sectioning) level heading))
  1106. (when sectioning
  1107. (setq heading (car sectioning)
  1108. sectioning (cdr sectioning)
  1109. ;; target property migh have changed...
  1110. label (org-get-text-property-any 0 'target heading)
  1111. label-list (cons label (cdr (assoc label
  1112. org-export-target-aliases)))))
  1113. (if sectioning (setq sectioning (make-list 10 sectioning)))
  1114. (setq depth (if sectioning 10000 0)))
  1115. (if (string-match "[ \t]*\\\\\\\\[ \t]*" heading)
  1116. (setq main-heading (substring heading 0 (match-beginning 0))
  1117. sub-heading (substring heading (match-end 0))))
  1118. (setq heading (org-export-latex-fontify-headline heading)
  1119. sub-heading (and sub-heading
  1120. (org-export-latex-fontify-headline sub-heading))
  1121. main-heading (and main-heading
  1122. (org-export-latex-fontify-headline main-heading)))
  1123. (cond
  1124. ;; Normal conversion
  1125. ((<= level depth)
  1126. (let* ((sec (nth (1- level) sectioning))
  1127. (num (if (integerp num)
  1128. (>= num level)
  1129. num))
  1130. start end)
  1131. (if (consp (cdr sec))
  1132. (setq start (nth (if num 0 2) sec)
  1133. end (nth (if num 1 3) sec))
  1134. (setq start (if num (car sec) (cdr sec))))
  1135. (insert (format start (if main-heading main-heading heading)
  1136. (or sub-heading "")))
  1137. (insert "\n")
  1138. (when label
  1139. (insert (mapconcat (lambda (l) (format "\\label{%s}" l))
  1140. label-list "\n") "\n"))
  1141. (insert (org-export-latex-content content))
  1142. (cond ((stringp subcontent) (insert subcontent))
  1143. ((listp subcontent)
  1144. (while (org-looking-back "\n\n") (backward-delete-char 1))
  1145. (org-export-latex-sub subcontent)))
  1146. (when (and end (string-match "[^ \t]" end))
  1147. (let ((hook (org-get-text-property-any 0 'org-insert-hook end)))
  1148. (and (functionp hook) (funcall hook)))
  1149. (insert end "\n"))))
  1150. ;; At a level under the hl option: we can drop this subsection
  1151. ((> level depth)
  1152. (cond ((eq org-export-latex-low-levels 'description)
  1153. (if (string-match "% ends low level$"
  1154. (buffer-substring (point-at-bol 0) (point)))
  1155. (delete-region (point-at-bol 0) (point))
  1156. (insert "\\begin{description}\n"))
  1157. (insert (format "\n\\item[%s]%s~\n"
  1158. heading
  1159. (if label (format "\\label{%s}" label) "")))
  1160. (insert (org-export-latex-content content))
  1161. (cond ((stringp subcontent) (insert subcontent))
  1162. ((listp subcontent) (org-export-latex-sub subcontent)))
  1163. (insert "\\end{description} % ends low level\n"))
  1164. ((memq org-export-latex-low-levels '(itemize enumerate))
  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 (format "\\begin{%s}\n"
  1169. (symbol-name org-export-latex-low-levels))))
  1170. (insert (format "\n\\item %s\\\\\n%s%%"
  1171. heading
  1172. (if label (format "\\label{%s}" label) "")))
  1173. (insert (org-export-latex-content content))
  1174. (cond ((stringp subcontent) (insert subcontent))
  1175. ((listp subcontent) (org-export-latex-sub subcontent)))
  1176. (insert (format "\\end{%s} %% ends low level\n"
  1177. (symbol-name org-export-latex-low-levels))))
  1178. ((listp org-export-latex-low-levels)
  1179. (if (string-match "% ends low level$"
  1180. (buffer-substring (point-at-bol 0) (point)))
  1181. (delete-region (point-at-bol 0) (point))
  1182. (insert (car org-export-latex-low-levels) "\n"))
  1183. (insert (format (nth 2 org-export-latex-low-levels)
  1184. heading
  1185. (if label (format "\\label{%s}" label) "")))
  1186. (insert (org-export-latex-content content))
  1187. (cond ((stringp subcontent) (insert subcontent))
  1188. ((listp subcontent) (org-export-latex-sub subcontent)))
  1189. (insert (nth 1 org-export-latex-low-levels)
  1190. " %% ends low level\n"))
  1191. ((stringp org-export-latex-low-levels)
  1192. (insert (format org-export-latex-low-levels heading) "\n")
  1193. (when label (insert (format "\\label{%s}\n" label)))
  1194. (insert (org-export-latex-content content))
  1195. (cond ((stringp subcontent) (insert subcontent))
  1196. ((listp subcontent) (org-export-latex-sub subcontent)))))))))
  1197. ;;; Exporting internals:
  1198. (defun org-export-latex-set-initial-vars (ext-plist level)
  1199. "Store org local variables required for LaTeX export.
  1200. EXT-PLIST is an optional additional plist.
  1201. LEVEL indicates the default depth for export."
  1202. (setq org-export-latex-todo-keywords-1 org-todo-keywords-1
  1203. org-export-latex-done-keywords org-done-keywords
  1204. org-export-latex-not-done-keywords org-not-done-keywords
  1205. org-export-latex-complex-heading-re org-complex-heading-regexp
  1206. org-export-latex-display-custom-times org-display-custom-times
  1207. org-export-latex-all-targets-re
  1208. (org-make-target-link-regexp (org-all-targets))
  1209. org-export-latex-options-plist
  1210. (org-combine-plists (org-default-export-plist) ext-plist
  1211. (org-infile-export-plist))
  1212. org-export-latex-class
  1213. (or (and (org-region-active-p)
  1214. (save-excursion
  1215. (goto-char (region-beginning))
  1216. (and (looking-at org-complex-heading-regexp)
  1217. (org-entry-get nil "LaTeX_CLASS" 'selective))))
  1218. (save-excursion
  1219. (save-restriction
  1220. (widen)
  1221. (goto-char (point-min))
  1222. (and (re-search-forward "^#\\+LaTeX_CLASS:[ \t]*\\([-/a-zA-Z]+\\)" nil t)
  1223. (match-string 1))))
  1224. (plist-get org-export-latex-options-plist :latex-class)
  1225. org-export-latex-default-class)
  1226. org-export-latex-class-options
  1227. (or (and (org-region-active-p)
  1228. (save-excursion
  1229. (goto-char (region-beginning))
  1230. (and (looking-at org-complex-heading-regexp)
  1231. (org-entry-get nil "LaTeX_CLASS_OPTIONS" 'selective))))
  1232. (save-excursion
  1233. (save-restriction
  1234. (widen)
  1235. (goto-char (point-min))
  1236. (and (re-search-forward "^#\\+LaTeX_CLASS_OPTIONS:[ \t]*\\(.*?\\)[ \t]*$" nil t)
  1237. (match-string 1))))
  1238. (plist-get org-export-latex-options-plist :latex-class-options))
  1239. org-export-latex-class
  1240. (or (car (assoc org-export-latex-class org-export-latex-classes))
  1241. (error "No definition for class `%s' in `org-export-latex-classes'"
  1242. org-export-latex-class))
  1243. org-export-latex-header
  1244. (cadr (assoc org-export-latex-class org-export-latex-classes))
  1245. org-export-latex-sectioning
  1246. (cddr (assoc org-export-latex-class org-export-latex-classes))
  1247. org-export-latex-sectioning-depth
  1248. (or level
  1249. (let ((hl-levels
  1250. (plist-get org-export-latex-options-plist :headline-levels))
  1251. (sec-depth (length org-export-latex-sectioning)))
  1252. (if (> hl-levels sec-depth) sec-depth hl-levels))))
  1253. (when (and org-export-latex-class-options
  1254. (string-match "\\S-" org-export-latex-class-options)
  1255. (string-match "^[ \t]*\\(\\\\documentclass\\)\\(\\[.*?\\]\\)?"
  1256. org-export-latex-header))
  1257. (setq org-export-latex-header
  1258. (concat (substring org-export-latex-header 0 (match-end 1))
  1259. org-export-latex-class-options
  1260. (substring org-export-latex-header (match-end 0))))))
  1261. (defvar org-export-latex-format-toc-function
  1262. 'org-export-latex-format-toc-default
  1263. "The function formatting returning the string to create the table of contents.
  1264. The function mus take one parameter, the depth of the table of contents.")
  1265. (defun org-export-latex-make-header (title opt-plist)
  1266. "Make the LaTeX header and return it as a string.
  1267. TITLE is the current title from the buffer or region.
  1268. OPT-PLIST is the options plist for current buffer."
  1269. (let ((toc (plist-get opt-plist :table-of-contents))
  1270. (author (org-export-apply-macros-in-string
  1271. (plist-get opt-plist :author)))
  1272. (email (replace-regexp-in-string
  1273. "_" "\\\\_"
  1274. (org-export-apply-macros-in-string
  1275. (plist-get opt-plist :email))))
  1276. (description (org-export-apply-macros-in-string
  1277. (plist-get opt-plist :description)))
  1278. (keywords (org-export-apply-macros-in-string
  1279. (plist-get opt-plist :keywords))))
  1280. (concat
  1281. (if (plist-get opt-plist :time-stamp-file)
  1282. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  1283. ;; insert LaTeX custom header and packages from the list
  1284. (org-splice-latex-header
  1285. (org-export-apply-macros-in-string org-export-latex-header)
  1286. org-export-latex-default-packages-alist
  1287. org-export-latex-packages-alist nil
  1288. (org-export-apply-macros-in-string
  1289. (plist-get opt-plist :latex-header-extra)))
  1290. ;; append another special variable
  1291. (org-export-apply-macros-in-string org-export-latex-append-header)
  1292. ;; define alert if not yet defined
  1293. "\n\\providecommand{\\alert}[1]{\\textbf{#1}}"
  1294. ;; insert the title
  1295. (format
  1296. "\n\n\\title{%s}\n"
  1297. (org-export-latex-fontify-headline title))
  1298. ;; insert author info
  1299. (if (plist-get opt-plist :author-info)
  1300. (format "\\author{%s%s}\n"
  1301. (org-export-latex-fontify-headline (or author user-full-name))
  1302. (if (and (plist-get opt-plist :email-info) email
  1303. (string-match "\\S-" email))
  1304. (format "\\thanks{%s}" email)
  1305. ""))
  1306. (format "%%\\author{%s}\n"
  1307. (org-export-latex-fontify-headline (or author user-full-name))))
  1308. ;; insert the date
  1309. (format "\\date{%s}\n"
  1310. (format-time-string
  1311. (or (plist-get opt-plist :date)
  1312. org-export-latex-date-format)))
  1313. ;; add some hyperref options
  1314. ;; FIXME: let's have a defcustom for this?
  1315. (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
  1316. (org-export-latex-fontify-headline keywords)
  1317. (org-export-latex-fontify-headline description)
  1318. (concat "Emacs Org-mode version " org-version))
  1319. ;; beginning of the document
  1320. "\n\\begin{document}\n\n"
  1321. ;; insert the title command
  1322. (when (string-match "\\S-" title)
  1323. (if (string-match "%s" org-export-latex-title-command)
  1324. (format org-export-latex-title-command title)
  1325. org-export-latex-title-command))
  1326. "\n\n"
  1327. ;; table of contents
  1328. (when (and org-export-with-toc
  1329. (plist-get opt-plist :section-numbers))
  1330. (funcall org-export-latex-format-toc-function
  1331. (cond ((numberp toc)
  1332. (min toc (plist-get opt-plist :headline-levels)))
  1333. (toc (plist-get opt-plist :headline-levels))))))))
  1334. (defun org-export-latex-format-toc-default (depth)
  1335. (when depth
  1336. (format "\\setcounter{tocdepth}{%s}\n\\tableofcontents\n\\vspace*{1cm}\n"
  1337. depth)))
  1338. (defun org-export-latex-first-lines (opt-plist &optional beg end)
  1339. "Export the first lines before first headline.
  1340. If BEG is non-nil, it is the beginning of the region.
  1341. If END is non-nil, it is the end of the region."
  1342. (save-excursion
  1343. (goto-char (or beg (point-min)))
  1344. (let* ((pt (point))
  1345. (end (if (re-search-forward
  1346. (concat "^" (org-get-limited-outline-regexp)) end t)
  1347. (goto-char (match-beginning 0))
  1348. (goto-char (or end (point-max))))))
  1349. (prog1
  1350. (org-export-latex-content
  1351. (org-export-preprocess-string
  1352. (buffer-substring pt end)
  1353. :for-backend 'latex
  1354. :emph-multiline t
  1355. :add-text nil
  1356. :comments nil
  1357. :skip-before-1st-heading nil
  1358. :LaTeX-fragments nil
  1359. :timestamps (plist-get opt-plist :timestamps)
  1360. :footnotes (plist-get opt-plist :footnotes)))
  1361. (org-unmodified
  1362. (let ((inhibit-read-only t)
  1363. (limit (max pt (1- end))))
  1364. (add-text-properties pt limit
  1365. '(:org-license-to-kill t))
  1366. (save-excursion
  1367. (goto-char pt)
  1368. (while (re-search-forward "^[ \t]*#\\+.*\n?" limit t)
  1369. (let ((case-fold-search t))
  1370. (unless (org-string-match-p
  1371. "^[ \t]*#\\+\\(attr_\\|caption\\>\\|label\\>\\)"
  1372. (match-string 0))
  1373. (remove-text-properties (match-beginning 0) (match-end 0)
  1374. '(:org-license-to-kill t))))))))))))
  1375. (defvar org-export-latex-header-defs nil
  1376. "The header definitions that might be used in the LaTeX body.")
  1377. (defun org-export-latex-content (content &optional exclude-list)
  1378. "Convert CONTENT string to LaTeX.
  1379. Don't perform conversions that are in EXCLUDE-LIST. Recognized
  1380. conversion types are: quotation-marks, emphasis, sub-superscript,
  1381. links, keywords, lists, tables, fixed-width"
  1382. (with-temp-buffer
  1383. (org-install-letbind)
  1384. (insert content)
  1385. (unless (memq 'timestamps exclude-list)
  1386. (org-export-latex-time-stamps))
  1387. (unless (memq 'quotation-marks exclude-list)
  1388. (org-export-latex-quotation-marks))
  1389. (unless (memq 'emphasis exclude-list)
  1390. (when (plist-get org-export-latex-options-plist :emphasize)
  1391. (org-export-latex-fontify)))
  1392. (unless (memq 'sub-superscript exclude-list)
  1393. (org-export-latex-special-chars
  1394. (plist-get org-export-latex-options-plist :sub-superscript)))
  1395. (unless (memq 'links exclude-list)
  1396. (org-export-latex-links))
  1397. (unless (memq 'keywords exclude-list)
  1398. (org-export-latex-keywords))
  1399. (unless (memq 'lists exclude-list)
  1400. (org-export-latex-lists))
  1401. (unless (memq 'tables exclude-list)
  1402. (org-export-latex-tables
  1403. (plist-get org-export-latex-options-plist :tables)))
  1404. (unless (memq 'fixed-width exclude-list)
  1405. (org-export-latex-fixed-width
  1406. (plist-get org-export-latex-options-plist :fixed-width)))
  1407. ;; return string
  1408. (buffer-substring (point-min) (point-max))))
  1409. (defun org-export-latex-protect-string (s)
  1410. "Add the org-protected property to string S."
  1411. (add-text-properties 0 (length s) '(org-protected t) s) s)
  1412. (defun org-export-latex-protect-char-in-string (char-list string)
  1413. "Add org-protected text-property to char from CHAR-LIST in STRING."
  1414. (with-temp-buffer
  1415. (save-match-data
  1416. (insert string)
  1417. (goto-char (point-min))
  1418. (while (re-search-forward (regexp-opt char-list) nil t)
  1419. (add-text-properties (match-beginning 0)
  1420. (match-end 0) '(org-protected t)))
  1421. (buffer-string))))
  1422. (defun org-export-latex-keywords-maybe (&optional remove-list)
  1423. "Maybe remove keywords depending on rules in REMOVE-LIST."
  1424. (goto-char (point-min))
  1425. (let ((re-todo (mapconcat 'identity org-export-latex-todo-keywords-1 "\\|"))
  1426. (case-fold-search nil)
  1427. (todo-markup org-export-latex-todo-keyword-markup)
  1428. fmt)
  1429. ;; convert TODO keywords
  1430. (when (re-search-forward (concat "^\\(" re-todo "\\)") nil t)
  1431. (if (plist-get remove-list :todo)
  1432. (replace-match "")
  1433. (setq fmt (cond
  1434. ((stringp todo-markup) todo-markup)
  1435. ((and (consp todo-markup) (stringp (car todo-markup)))
  1436. (if (member (match-string 1) org-export-latex-done-keywords)
  1437. (cdr todo-markup) (car todo-markup)))
  1438. (t (cdr (or (assoc (match-string 1) todo-markup)
  1439. (car todo-markup))))))
  1440. (replace-match (org-export-latex-protect-string
  1441. (format fmt (match-string 1))) t t)))
  1442. ;; convert priority string
  1443. (when (re-search-forward "\\[\\\\#.\\]" nil t)
  1444. (if (plist-get remove-list :priority)
  1445. (replace-match "")
  1446. (replace-match (format "\\textbf{%s}" (match-string 0)) t t)))
  1447. ;; convert tags
  1448. (when (re-search-forward "\\(:[a-zA-Z0-9_@#%]+\\)+:" nil t)
  1449. (if (or (not org-export-with-tags)
  1450. (plist-get remove-list :tags))
  1451. (replace-match "")
  1452. (replace-match
  1453. (org-export-latex-protect-string
  1454. (format org-export-latex-tag-markup
  1455. (save-match-data
  1456. (replace-regexp-in-string
  1457. "\\([_#]\\)" "\\\\\\1" (match-string 0)))))
  1458. t t)))))
  1459. (defun org-export-latex-fontify-headline (string)
  1460. "Fontify special words in STRING."
  1461. (with-temp-buffer
  1462. ;; FIXME: org-inside-LaTeX-fragment-p doesn't work when the $...$ is at
  1463. ;; the beginning of the buffer - inserting "\n" is safe here though.
  1464. (insert "\n" string)
  1465. ;; Preserve math snippets
  1466. (let* ((matchers (plist-get org-format-latex-options :matchers))
  1467. (re-list org-latex-regexps)
  1468. beg end re e m n block off)
  1469. ;; Check the different regular expressions
  1470. (while (setq e (pop re-list))
  1471. (setq m (car e) re (nth 1 e) n (nth 2 e)
  1472. block (if (nth 3 e) "\n\n" ""))
  1473. (setq off (if (member m '("$" "$1")) 1 0))
  1474. (when (and (member m matchers) (not (equal m "begin")))
  1475. (goto-char (point-min))
  1476. (while (re-search-forward re nil t)
  1477. (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
  1478. (add-text-properties beg end
  1479. '(org-protected t org-latex-math t))))))
  1480. ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
  1481. (goto-char (point-min))
  1482. (let ((case-fold-search nil))
  1483. (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t)
  1484. (unless (eq (char-before (match-beginning 1)) ?\\)
  1485. (org-if-unprotected-1
  1486. (replace-match (org-export-latex-protect-string
  1487. (concat "\\" (match-string 1)
  1488. "{}")) t t)))))
  1489. (goto-char (point-min))
  1490. (let ((re (concat "\\\\\\([a-zA-Z]+\\)"
  1491. "\\(?:<[^<>\n]*>\\)*"
  1492. "\\(?:\\[[^][\n]*?\\]\\)*"
  1493. "\\(?:<[^<>\n]*>\\)*"
  1494. "\\("
  1495. (org-create-multibrace-regexp "{" "}" 3)
  1496. "\\)\\{1,3\\}")))
  1497. (while (re-search-forward re nil t)
  1498. (unless (or
  1499. ;; check for comment line
  1500. (save-excursion (goto-char (match-beginning 0))
  1501. (org-in-indented-comment-line))
  1502. ;; Check if this is a defined entity, so that is may need conversion
  1503. (org-entity-get (match-string 1)))
  1504. (add-text-properties (match-beginning 0) (match-end 0)
  1505. '(org-protected t)))))
  1506. (when (plist-get org-export-latex-options-plist :emphasize)
  1507. (org-export-latex-fontify))
  1508. (org-export-latex-time-stamps)
  1509. (org-export-latex-quotation-marks)
  1510. (org-export-latex-keywords-maybe)
  1511. (org-export-latex-special-chars
  1512. (plist-get org-export-latex-options-plist :sub-superscript))
  1513. (org-export-latex-links)
  1514. (org-trim (buffer-string))))
  1515. (defun org-export-latex-time-stamps ()
  1516. "Format time stamps."
  1517. (goto-char (point-min))
  1518. (let ((org-display-custom-times org-export-latex-display-custom-times))
  1519. (while (re-search-forward org-ts-regexp-both nil t)
  1520. (org-if-unprotected-at (1- (point))
  1521. (replace-match
  1522. (org-export-latex-protect-string
  1523. (format (if (string= "<" (substring (match-string 0) 0 1))
  1524. org-export-latex-timestamp-markup
  1525. org-export-latex-timestamp-inactive-markup)
  1526. (substring (org-translate-time (match-string 0)) 1 -1)))
  1527. t t)))))
  1528. (defun org-export-latex-quotation-marks ()
  1529. "Export quotation marks depending on language conventions."
  1530. (mapc (lambda(l)
  1531. (goto-char (point-min))
  1532. (while (re-search-forward (car l) nil t)
  1533. (let ((rpl (concat (match-string 1)
  1534. (org-export-latex-protect-string
  1535. (copy-sequence (cdr l))))))
  1536. (org-if-unprotected-1
  1537. (replace-match rpl t t)))))
  1538. (cdr (or (assoc (plist-get org-export-latex-options-plist :language)
  1539. org-export-latex-quotes)
  1540. ;; falls back on english
  1541. (assoc "en" org-export-latex-quotes)))))
  1542. (defun org-export-latex-special-chars (sub-superscript)
  1543. "Export special characters to LaTeX.
  1544. If SUB-SUPERSCRIPT is non-nil, convert \\ and ^.
  1545. See the `org-export-latex.el' code for a complete conversion table."
  1546. (goto-char (point-min))
  1547. (mapc (lambda(c)
  1548. (goto-char (point-min))
  1549. (while (re-search-forward c nil t)
  1550. ;; Put the point where to check for org-protected
  1551. (unless (or (get-text-property (match-beginning 2) 'org-protected)
  1552. (save-match-data (org-at-table.el-p)))
  1553. (cond ((member (match-string 2) '("\\$" "$"))
  1554. (if (equal (match-string 2) "\\$")
  1555. nil
  1556. (replace-match "\\$" t t)))
  1557. ((member (match-string 2) '("&" "%" "#"))
  1558. (if (equal (match-string 1) "\\")
  1559. (replace-match (match-string 2) t t)
  1560. (replace-match (concat (match-string 1) "\\"
  1561. (match-string 2)) t t)
  1562. (backward-char 1)))
  1563. ((equal (match-string 2) "...")
  1564. (replace-match
  1565. (concat (match-string 1)
  1566. (org-export-latex-protect-string "\\ldots{}")) t t))
  1567. ((equal (match-string 2) "~")
  1568. (cond ((equal (match-string 1) "\\") nil)
  1569. ((eq 'org-link (get-text-property 0 'face (match-string 2)))
  1570. (replace-match (concat (match-string 1) "\\~") t t))
  1571. (t (replace-match
  1572. (org-export-latex-protect-string
  1573. (concat (match-string 1) "\\~{}")) t t))))
  1574. ((member (match-string 2) '("{" "}"))
  1575. (unless (save-match-data (org-inside-latex-math-p))
  1576. (if (equal (match-string 1) "\\")
  1577. (replace-match (match-string 2) t t)
  1578. (replace-match (concat (match-string 1) "\\"
  1579. (match-string 2)) t t)))))
  1580. (unless (save-match-data (org-inside-latex-math-p))
  1581. (cond ((equal (match-string 2) "\\")
  1582. (replace-match (or (save-match-data
  1583. (org-export-latex-treat-backslash-char
  1584. (match-string 1)
  1585. (or (match-string 3) "")))
  1586. "") t t)
  1587. (when (and (get-text-property (1- (point)) 'org-entity)
  1588. (looking-at "{}"))
  1589. ;; OK, this was an entity replacement, and the user
  1590. ;; had terminated the entity with {}. Make sure
  1591. ;; {} is protected as well, and remove the extra {}
  1592. ;; inserted by the conversion.
  1593. (put-text-property (point) (+ 2 (point)) 'org-protected t)
  1594. (if (save-excursion (goto-char (max (- (point) 2) (point-min)))
  1595. (looking-at "{}"))
  1596. (replace-match ""))
  1597. (forward-char 2))
  1598. (backward-char 1))
  1599. ((member (match-string 2) '("_" "^"))
  1600. (replace-match (or (save-match-data
  1601. (org-export-latex-treat-sub-super-char
  1602. sub-superscript
  1603. (match-string 2)
  1604. (match-string 1)
  1605. (match-string 3))) "") t t)
  1606. (backward-char 1)))))))
  1607. '(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
  1608. "\\(\\(\\\\?\\$\\)\\)"
  1609. "\\([a-zA-Z0-9()]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\({[^{}]+}\\|[a-zA-Z0-9]+\\|[ \t\n]\\|[:punct:]\\|)\\|{[a-zA-Z0-9]+}\\|([a-zA-Z0-9]+)\\)"
  1610. "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|\\([&#%{}\"]\\|[a-zA-Z][a-zA-Z0-9]*\\)\\)"
  1611. "\\(^\\|.\\)\\([&#%{}~]\\|\\.\\.\\.\\)"
  1612. ;; (?\< . "\\textless{}")
  1613. ;; (?\> . "\\textgreater{}")
  1614. )))
  1615. (defun org-inside-latex-math-p ()
  1616. (get-text-property (point) 'org-latex-math))
  1617. (defun org-export-latex-treat-sub-super-char
  1618. (subsup char string-before string-after)
  1619. "Convert the \"_\" and \"^\" characters to LaTeX.
  1620. SUBSUP corresponds to the ^: option in the #+OPTIONS line.
  1621. Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
  1622. (cond ((equal string-before "\\")
  1623. (concat string-before char string-after))
  1624. ((and (string-match "\\S-+" string-after))
  1625. ;; this is part of a math formula
  1626. (cond ((eq 'org-link (get-text-property 0 'face char))
  1627. (concat string-before "\\" char string-after))
  1628. ((save-match-data (org-inside-latex-math-p))
  1629. (if subsup
  1630. (cond ((eq 1 (length string-after))
  1631. (concat string-before char string-after))
  1632. ((string-match "[({]?\\([^)}]+\\)[)}]?" string-after)
  1633. (format "%s%s{%s}" string-before char
  1634. (match-string 1 string-after))))))
  1635. ((and (> (length string-after) 1)
  1636. (or (eq subsup t)
  1637. (and (equal subsup '{}) (eq (string-to-char string-after) ?\{)))
  1638. (or (string-match "[{]?\\([^}]+\\)[}]?" string-after)
  1639. (string-match "[(]?\\([^)]+\\)[)]?" string-after)))
  1640. (org-export-latex-protect-string
  1641. (format "%s$%s{%s}$" string-before char
  1642. (if (and (> (match-end 1) (1+ (match-beginning 1)))
  1643. (not (equal (substring string-after 0 2) "{\\")))
  1644. (concat "\\mathrm{" (match-string 1 string-after) "}")
  1645. (match-string 1 string-after)))))
  1646. ((eq subsup t) (concat string-before "$" char string-after "$"))
  1647. (t (org-export-latex-protect-string
  1648. (concat string-before "\\" char "{}" string-after)))))
  1649. (t (org-export-latex-protect-string
  1650. (concat string-before "\\" char "{}" string-after)))))
  1651. (defun org-export-latex-treat-backslash-char (string-before string-after)
  1652. "Convert the \"$\" special character to LaTeX.
  1653. The conversion is made depending of STRING-BEFORE and STRING-AFTER."
  1654. (let ((ass (org-entity-get string-after)))
  1655. (cond
  1656. (ass (org-add-props
  1657. (if (nth 2 ass)
  1658. (concat string-before
  1659. (org-export-latex-protect-string
  1660. (concat "$" (nth 1 ass) "$")))
  1661. (concat string-before (org-export-latex-protect-string
  1662. (nth 1 ass))))
  1663. nil 'org-entity t))
  1664. ((and (not (string-match "^[ \n\t]" string-after))
  1665. (not (string-match "[ \t]\\'\\|^" string-before)))
  1666. ;; backslash is inside a word
  1667. (concat string-before
  1668. (org-export-latex-protect-string
  1669. (concat "\\textbackslash{}" string-after))))
  1670. ((not (or (equal string-after "")
  1671. (string-match "^[ \t\n]" string-after)))
  1672. ;; backslash might escape a character (like \#) or a user TeX
  1673. ;; macro (like \setcounter)
  1674. (concat string-before
  1675. (org-export-latex-protect-string (concat "\\" string-after))))
  1676. ((and (string-match "^[ \t\n]" string-after)
  1677. (string-match "[ \t\n]\\'" string-before))
  1678. ;; backslash is alone, convert it to $\backslash$
  1679. (org-export-latex-protect-string
  1680. (concat string-before "\\textbackslash{}" string-after)))
  1681. (t (org-export-latex-protect-string
  1682. (concat string-before "\\textbackslash{}" string-after))))))
  1683. (defun org-export-latex-keywords ()
  1684. "Convert special keywords to LaTeX."
  1685. (goto-char (point-min))
  1686. (while (re-search-forward org-export-latex-special-keyword-regexp nil t)
  1687. (replace-match (format org-export-latex-timestamp-keyword-markup
  1688. (match-string 0)) t t)
  1689. (save-excursion
  1690. (beginning-of-line 1)
  1691. (unless (looking-at ".*\n[ \t]*\n")
  1692. (end-of-line 1)
  1693. (insert "\n")))))
  1694. (defun org-export-latex-fixed-width (opt)
  1695. "When OPT is non-nil convert fixed-width sections to LaTeX."
  1696. (goto-char (point-min))
  1697. (while (re-search-forward "^[ \t]*:\\([ \t]\\|$\\)" nil t)
  1698. (unless (get-text-property (point) 'org-example)
  1699. (if opt
  1700. (progn (goto-char (match-beginning 0))
  1701. (insert "\\begin{verbatim}\n")
  1702. (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
  1703. (replace-match (concat (match-string 1)
  1704. (match-string 2)) t t)
  1705. (forward-line))
  1706. (insert "\\end{verbatim}\n"))
  1707. (progn (goto-char (match-beginning 0))
  1708. (while (looking-at "^\\([ \t]*\\):\\(\\([ \t]\\|$\\).*\\)$")
  1709. (replace-match (concat "%" (match-string 1)
  1710. (match-string 2)) t t)
  1711. (forward-line)))))))
  1712. (defvar org-table-last-alignment) ; defined in org-table.el
  1713. (defvar org-table-last-column-widths) ; defined in org-table.el
  1714. (declare-function orgtbl-to-latex "org-table" (table params) t)
  1715. (defun org-export-latex-tables (insert)
  1716. "Convert tables to LaTeX and INSERT it."
  1717. ;; First, get the table.el tables
  1718. (goto-char (point-min))
  1719. (while (re-search-forward "^[ \t]*\\(\\+-[-+]*\\+\\)[ \t]*\n[ \t]*|" nil t)
  1720. (org-if-unprotected
  1721. (require 'table)
  1722. (org-export-latex-convert-table.el-table)))
  1723. ;; And now the Org-mode tables
  1724. (goto-char (point-min))
  1725. (while (re-search-forward "^\\([ \t]*\\)|" nil t)
  1726. (org-if-unprotected-at (1- (point))
  1727. (org-table-align)
  1728. (let* ((beg (org-table-begin))
  1729. (end (org-table-end))
  1730. (raw-table (buffer-substring beg end))
  1731. (org-table-last-alignment (copy-sequence org-table-last-alignment))
  1732. (org-table-last-column-widths (copy-sequence
  1733. org-table-last-column-widths))
  1734. fnum fields line lines olines gr colgropen line-fmt align
  1735. caption width shortn label attr floatp placement
  1736. longtblp tblenv tabular-env)
  1737. (if org-export-latex-tables-verbatim
  1738. (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
  1739. "\\end{verbatim}\n")))
  1740. (apply 'delete-region (list beg end))
  1741. (insert (org-export-latex-protect-string tbl)))
  1742. (progn
  1743. (setq caption (org-find-text-property-in-string
  1744. 'org-caption raw-table)
  1745. shortn (org-find-text-property-in-string
  1746. 'org-caption-shortn raw-table)
  1747. attr (org-find-text-property-in-string
  1748. 'org-attributes raw-table)
  1749. label (org-find-text-property-in-string
  1750. 'org-label raw-table)
  1751. longtblp (and attr (stringp attr)
  1752. (string-match "\\<longtable\\>" attr))
  1753. tblenv (if (and attr (stringp attr))
  1754. (cond ((string-match "\\<sidewaystable\\>" attr)
  1755. "sidewaystable")
  1756. ((or (string-match (regexp-quote "table*") attr)
  1757. (string-match "\\<multicolumn\\>" attr))
  1758. "table*")
  1759. (t "table"))
  1760. "table")
  1761. tabular-env
  1762. (if (and attr (stringp attr)
  1763. (string-match "\\(tabular.\\)" attr))
  1764. (match-string 1 attr)
  1765. org-export-latex-tabular-environment)
  1766. width (and attr (stringp attr)
  1767. (string-match "\\<width=\\([^ \t\n\r]+\\)" attr)
  1768. (match-string 1 attr))
  1769. align (and attr (stringp attr)
  1770. (string-match "\\<align=\\([^ \t\n\r]+\\)" attr)
  1771. (match-string 1 attr))
  1772. floatp (or caption label (string= "table*" tblenv))
  1773. placement (if (and attr
  1774. (stringp attr)
  1775. (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
  1776. (match-string 1 attr)
  1777. (concat
  1778. "[" org-latex-default-figure-position "]")))
  1779. (setq caption (and caption (org-export-latex-fontify-headline caption)))
  1780. (setq lines (org-split-string raw-table "\n"))
  1781. (apply 'delete-region (list beg end))
  1782. (when org-export-table-remove-special-lines
  1783. (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
  1784. (when org-table-clean-did-remove-column
  1785. (pop org-table-last-alignment)
  1786. (pop org-table-last-column-widths))
  1787. ;; make a formatting string to reflect alignment
  1788. (setq olines lines)
  1789. (while (and (not line-fmt) (setq line (pop olines)))
  1790. (unless (string-match "^[ \t]*|-" line)
  1791. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1792. (setq fnum (make-vector (length fields) 0))
  1793. (setq line-fmt
  1794. (mapconcat
  1795. (lambda (x)
  1796. (setq gr (pop org-table-colgroup-info))
  1797. (format "%s%%s%s"
  1798. (cond ((eq gr :start)
  1799. (prog1 (if colgropen "|" "|")
  1800. (setq colgropen t)))
  1801. ((eq gr :startend)
  1802. (prog1 (if colgropen "|" "|")
  1803. (setq colgropen nil)))
  1804. (t ""))
  1805. (if (memq gr '(:end :startend))
  1806. (progn (setq colgropen nil) "|")
  1807. "")))
  1808. fnum ""))))
  1809. ;; fix double || in line-fmt
  1810. (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
  1811. ;; maybe remove the first and last "|"
  1812. (when (and (not org-export-latex-tables-column-borders)
  1813. (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
  1814. (setq line-fmt (match-string 2 line-fmt)))
  1815. ;; format alignment
  1816. (unless align
  1817. (setq align (apply 'format
  1818. (cons line-fmt
  1819. (mapcar (lambda (x) (if x "r" "l"))
  1820. org-table-last-alignment)))))
  1821. ;; prepare the table to send to orgtbl-to-latex
  1822. (setq lines
  1823. (mapcar
  1824. (lambda(elem)
  1825. (or (and (string-match "[ \t]*|-+" elem) 'hline)
  1826. (org-split-string
  1827. (progn (set-text-properties 0 (length elem) nil elem)
  1828. (org-trim elem)) "|")))
  1829. lines))
  1830. (when insert
  1831. (insert (org-export-latex-protect-string
  1832. (concat
  1833. (if longtblp
  1834. (concat "\\begin{longtable}{" align "}\n")
  1835. (if floatp
  1836. (format "\\begin{%s}%s\n" tblenv placement)))
  1837. (if (and floatp org-export-latex-table-caption-above)
  1838. (format
  1839. "\\caption%s{%s} %s"
  1840. (if shortn (concat "[" shortn "]") "")
  1841. (or caption "")
  1842. (if label (format "\\label{%s}" label) "")))
  1843. (if (and longtblp caption org-export-latex-table-caption-above)
  1844. "\\\\\n" "\n")
  1845. (if (and org-export-latex-tables-centered (not longtblp))
  1846. "\\begin{center}\n")
  1847. (if (not longtblp)
  1848. (format "\\begin{%s}%s{%s}\n"
  1849. tabular-env
  1850. (if width (format "{%s}" width) "")
  1851. align))
  1852. (orgtbl-to-latex
  1853. lines
  1854. `(:tstart nil :tend nil
  1855. :hlend ,(if longtblp
  1856. (format "\\\\
  1857. \\hline
  1858. \\endhead
  1859. \\hline\\multicolumn{%d}{r}{Continued on next page}\\
  1860. \\endfoot
  1861. \\endlastfoot" (length org-table-last-alignment))
  1862. nil)))
  1863. (if (not longtblp) (format "\n\\end{%s}" tabular-env))
  1864. (if longtblp "\n" (if org-export-latex-tables-centered
  1865. "\n\\end{center}\n" "\n"))
  1866. (if (and floatp (not org-export-latex-table-caption-above))
  1867. (format
  1868. "\\caption%s{%s} %s"
  1869. (if shortn (concat "[" shortn "]") "")
  1870. (or caption "")
  1871. (if label (format "\\label{%s}" label) "")))
  1872. (if longtblp
  1873. "\\end{longtable}"
  1874. (if floatp (format "\\end{%s}" tblenv)))))
  1875. "\n\n"))))))))
  1876. (defun org-export-latex-convert-table.el-table ()
  1877. "Replace table.el table at point with LaTeX code."
  1878. (let (tbl caption shortn label line floatp attr align rmlines)
  1879. (setq line (buffer-substring (point-at-bol) (point-at-eol))
  1880. label (org-get-text-property-any 0 'org-label line)
  1881. caption (org-get-text-property-any 0 'org-caption line)
  1882. shortn (org-get-text-property-any 0 'org-caption-shortn line)
  1883. attr (org-get-text-property-any 0 'org-attributes line)
  1884. align (and attr (stringp attr)
  1885. (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
  1886. (match-string 1 attr))
  1887. rmlines (and attr (stringp attr)
  1888. (string-match "\\<rmlines\\>" attr))
  1889. floatp (or label caption))
  1890. (and (get-buffer "*org-export-table*")
  1891. (kill-buffer (get-buffer "*org-export-table*")))
  1892. (table-generate-source 'latex "*org-export-table*" "caption")
  1893. (setq tbl (with-current-buffer "*org-export-table*"
  1894. (buffer-string)))
  1895. (while (string-match "^%.*\n" tbl)
  1896. (setq tbl (replace-match "" t t tbl)))
  1897. ;; fix the hlines
  1898. (when rmlines
  1899. (let ((n 0) lines)
  1900. (setq lines (mapcar (lambda (x)
  1901. (if (string-match "^\\\\hline$" x)
  1902. (progn
  1903. (setq n (1+ n))
  1904. (if (= n 2) x nil))
  1905. x))
  1906. (org-split-string tbl "\n")))
  1907. (setq tbl (mapconcat 'identity (delq nil lines) "\n"))))
  1908. (when (and align (string-match "\\\\begin{tabular}{.*}" tbl))
  1909. (setq tbl (replace-match (concat "\\begin{tabular}{" align "}")
  1910. t t tbl)))
  1911. (and (get-buffer "*org-export-table*")
  1912. (kill-buffer (get-buffer "*org-export-table*")))
  1913. (beginning-of-line 0)
  1914. (while (looking-at "[ \t]*\\(|\\|\\+-\\)")
  1915. (delete-region (point) (1+ (point-at-eol))))
  1916. (when org-export-latex-tables-centered
  1917. (setq tbl (concat "\\begin{center}\n" tbl "\\end{center}")))
  1918. (when floatp
  1919. (setq tbl (concat "\\begin{table}\n"
  1920. (if (not org-export-latex-table-caption-above) tbl)
  1921. (format "\\caption%s{%s%s}\n"
  1922. (if shortn (format "[%s]" shortn) "")
  1923. (if label (format "\\label{%s}" label) "")
  1924. (or caption ""))
  1925. (if org-export-latex-table-caption-above tbl)
  1926. "\n\\end{table}\n")))
  1927. (insert (org-export-latex-protect-string tbl))))
  1928. (defun org-export-latex-fontify ()
  1929. "Convert fontification to LaTeX."
  1930. (goto-char (point-min))
  1931. (while (re-search-forward org-emph-re nil t)
  1932. ;; The match goes one char after the *string*, except at the end of a line
  1933. (let ((emph (assoc (match-string 3)
  1934. org-export-latex-emphasis-alist))
  1935. (beg (match-beginning 0))
  1936. (end (match-end 0))
  1937. rpl s)
  1938. (unless emph
  1939. (message "`org-export-latex-emphasis-alist' has no entry for formatting triggered by \"%s\""
  1940. (match-string 3)))
  1941. (unless (or (and (get-text-property (- (point) 2) 'org-protected)
  1942. (not (get-text-property
  1943. (- (point) 2) 'org-verbatim-emph)))
  1944. (equal (char-after (match-beginning 3))
  1945. (char-after (1+ (match-beginning 3))))
  1946. (save-excursion
  1947. (goto-char (match-beginning 1))
  1948. (save-match-data
  1949. (and (org-at-table-p)
  1950. (string-match
  1951. "[|\n]" (buffer-substring beg end)))))
  1952. (and (equal (match-string 3) "+")
  1953. (save-match-data
  1954. (string-match "\\`-+\\'" (match-string 4)))))
  1955. (setq s (match-string 4))
  1956. (setq rpl (concat (match-string 1)
  1957. (org-export-latex-emph-format (cadr emph)
  1958. (match-string 4))
  1959. (match-string 5)))
  1960. (if (caddr emph)
  1961. (setq rpl (org-export-latex-protect-string rpl))
  1962. (save-match-data
  1963. (if (string-match "\\`.?\\(\\\\[a-z]+{\\)\\(.*\\)\\(}\\).?\\'" rpl)
  1964. (progn
  1965. (add-text-properties (match-beginning 1) (match-end 1)
  1966. '(org-protected t) rpl)
  1967. (add-text-properties (match-beginning 3) (match-end 3)
  1968. '(org-protected t) rpl)))))
  1969. (replace-match rpl t t)))
  1970. (backward-char)))
  1971. (defun org-export-latex-emph-format (format string)
  1972. "Format an emphasis string and handle the \\verb special case."
  1973. (when (member format '("\\verb" "\\protectedtexttt"))
  1974. (save-match-data
  1975. (if (equal format "\\verb")
  1976. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1977. (catch 'exit
  1978. (loop for i from 0 to (1- (length ll)) do
  1979. (if (not (string-match (regexp-quote (substring ll i (1+ i)))
  1980. string))
  1981. (progn
  1982. (setq format (concat "\\verb" (substring ll i (1+ i))
  1983. "%s" (substring ll i (1+ i))))
  1984. (throw 'exit nil))))))
  1985. (let ((start 0)
  1986. (trans '(("\\" . "\\textbackslash{}")
  1987. ("~" . "\\textasciitilde{}")
  1988. ("^" . "\\textasciicircum{}")))
  1989. (rtn "") char)
  1990. (while (string-match "[\\{}$%&_#~^]" string)
  1991. (setq char (match-string 0 string))
  1992. (if (> (match-beginning 0) 0)
  1993. (setq rtn (concat rtn (substring string
  1994. 0 (match-beginning 0)))))
  1995. (setq string (substring string (1+ (match-beginning 0))))
  1996. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  1997. rtn (concat rtn char)))
  1998. (setq string (concat rtn string) format "\\texttt{%s}")
  1999. (while (string-match "--" string)
  2000. (setq string (replace-match "-{}-" t t string)))))))
  2001. (format format string))
  2002. (defun org-export-latex-links ()
  2003. ;; Make sure to use the LaTeX hyperref and graphicx package
  2004. ;; or send some warnings.
  2005. "Convert links to LaTeX."
  2006. (goto-char (point-min))
  2007. (while (re-search-forward org-bracket-link-analytic-regexp++ nil t)
  2008. (org-if-unprotected-1
  2009. (goto-char (match-beginning 0))
  2010. (let* ((re-radio org-export-latex-all-targets-re)
  2011. (remove (list (match-beginning 0) (match-end 0)))
  2012. (raw-path (org-extract-attributes (match-string 3)))
  2013. (full-raw-path (concat (match-string 1) raw-path))
  2014. (desc (match-string 5))
  2015. (type (or (match-string 2)
  2016. (if (or (file-name-absolute-p raw-path)
  2017. (string-match "^\\.\\.?/" raw-path))
  2018. "file")))
  2019. (coderefp (equal type "coderef"))
  2020. (caption (org-find-text-property-in-string 'org-caption raw-path))
  2021. (shortn (org-find-text-property-in-string 'org-caption-shortn raw-path))
  2022. (attr (or (org-find-text-property-in-string 'org-attributes raw-path)
  2023. (plist-get org-export-latex-options-plist :latex-image-options)))
  2024. (label (org-find-text-property-in-string 'org-label raw-path))
  2025. imgp radiop fnc
  2026. ;; define the path of the link
  2027. (path (cond
  2028. ((member type '("coderef"))
  2029. raw-path)
  2030. ((member type '("http" "https" "ftp"))
  2031. (concat type ":" raw-path))
  2032. ((and re-radio (string-match re-radio raw-path))
  2033. (setq radiop t))
  2034. ((equal type "mailto")
  2035. (concat type ":" raw-path))
  2036. ((equal type "file")
  2037. (if (and (org-file-image-p
  2038. (expand-file-name
  2039. raw-path)
  2040. org-export-latex-inline-image-extensions)
  2041. (or (get-text-property 0 'org-no-description
  2042. raw-path)
  2043. (equal desc full-raw-path)))
  2044. (setq imgp t)
  2045. (progn (when (string-match "\\(.+\\)::.+" raw-path)
  2046. (setq raw-path (match-string 1 raw-path)))
  2047. (if (file-exists-p raw-path)
  2048. (concat type "://" (expand-file-name raw-path))
  2049. (concat type "://" (org-export-directory
  2050. :LaTeX org-export-latex-options-plist)
  2051. raw-path))))))))
  2052. ;; process with link inserting
  2053. (apply 'delete-region remove)
  2054. (setq caption (and caption (org-export-latex-fontify-headline caption)))
  2055. (cond ((and imgp
  2056. (plist-get org-export-latex-options-plist :inline-images))
  2057. ;; OK, we need to inline an image
  2058. (insert
  2059. (org-export-latex-format-image raw-path caption label attr shortn)))
  2060. (coderefp
  2061. (insert (format
  2062. (org-export-get-coderef-format path desc)
  2063. (cdr (assoc path org-export-code-refs)))))
  2064. (radiop (insert (format org-export-latex-hyperref-format
  2065. (org-solidify-link-text raw-path) desc)))
  2066. ((not type)
  2067. (insert (format org-export-latex-hyperref-format
  2068. (org-remove-initial-hash
  2069. (org-solidify-link-text raw-path))
  2070. desc)))
  2071. (path
  2072. (when (org-at-table-p)
  2073. ;; There is a strange problem when we have a link in a table,
  2074. ;; ampersands then cause a problem. I think this must be
  2075. ;; a LaTeX issue, but we here implement a work-around anyway.
  2076. (setq path (org-export-latex-protect-amp path)
  2077. desc (org-export-latex-protect-amp desc)))
  2078. (insert
  2079. (if (string-match "%s.*%s" org-export-latex-href-format)
  2080. (format org-export-latex-href-format path desc)
  2081. (format org-export-latex-href-format path))))
  2082. ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
  2083. ;; The link protocol has a function for formatting the link
  2084. (insert
  2085. (save-match-data
  2086. (funcall fnc (org-link-unescape raw-path) desc 'latex))))
  2087. (t (insert "\\texttt{" desc "}")))))))
  2088. (defun org-export-latex-format-image (path caption label attr &optional shortn)
  2089. "Format the image element, depending on user settings."
  2090. (let (ind floatp wrapp multicolumnp placement figenv)
  2091. (setq floatp (or caption label))
  2092. (setq ind (org-get-text-property-any 0 'original-indentation path))
  2093. (when (and attr (stringp attr))
  2094. (if (string-match "[ \t]*\\<wrap\\>" attr)
  2095. (setq wrapp t floatp nil attr (replace-match "" t t attr)))
  2096. (if (string-match "[ \t]*\\<float\\>" attr)
  2097. (setq wrapp nil floatp t attr (replace-match "" t t attr)))
  2098. (if (string-match "[ \t]*\\<multicolumn\\>" attr)
  2099. (setq multicolumnp t attr (replace-match "" t t attr))))
  2100. (setq placement
  2101. (cond
  2102. (wrapp "{l}{0.5\\textwidth}")
  2103. (floatp (concat "[" org-latex-default-figure-position "]"))
  2104. (t "")))
  2105. (when (and attr (stringp attr)
  2106. (string-match "[ \t]*\\<placement=\\(\\S-+\\)" attr))
  2107. (setq placement (match-string 1 attr)
  2108. attr (replace-match "" t t attr)))
  2109. (setq attr (and attr (org-trim attr)))
  2110. (when (or (not attr) (= (length attr) 0))
  2111. (setq attr (cond (floatp "width=0.7\\textwidth")
  2112. (wrapp "width=0.48\\textwidth")
  2113. (t attr))))
  2114. (setq figenv
  2115. (cond
  2116. (wrapp "\\begin{wrapfigure}%placement
  2117. \\centering
  2118. \\includegraphics[%attr]{%path}
  2119. \\caption%shortn{%labelcmd%caption}
  2120. \\end{wrapfigure}")
  2121. (multicolumnp "\\begin{figure*}%placement
  2122. \\centering
  2123. \\includegraphics[%attr]{%path}
  2124. \\caption%shortn{%labelcmd%caption}
  2125. \\end{figure*}")
  2126. (floatp "\\begin{figure}%placement
  2127. \\centering
  2128. \\includegraphics[%attr]{%path}
  2129. \\caption%shortn{%labelcmd%caption}
  2130. \\end{figure}")
  2131. (t "\\includegraphics[%attr]{%path}")))
  2132. (setq figenv (mapconcat 'identity (split-string figenv "\n")
  2133. (save-excursion (beginning-of-line 1)
  2134. (looking-at "[ \t]*")
  2135. (concat "\n" (match-string 0)))))
  2136. (if (and (not label) (not caption)
  2137. (string-match "^\\\\caption{.*\n" figenv))
  2138. (setq figenv (replace-match "" t t figenv)))
  2139. (org-add-props
  2140. (org-fill-template
  2141. figenv
  2142. (list (cons "path"
  2143. (if (file-name-absolute-p path)
  2144. (expand-file-name path)
  2145. path))
  2146. (cons "attr" attr)
  2147. (cons "shortn" (if shortn (format "[%s]" shortn) ""))
  2148. (cons "labelcmd" (if label (format "\\label{%s}"
  2149. label)""))
  2150. (cons "caption" (or caption ""))
  2151. (cons "placement" (or placement ""))))
  2152. nil 'original-indentation ind)))
  2153. (defun org-export-latex-protect-amp (s)
  2154. (while (string-match "\\([^\\\\]\\)\\(&\\)" s)
  2155. (setq s (replace-match (concat (match-string 1 s) "\\" (match-string 2 s))
  2156. t t s)))
  2157. s)
  2158. (defun org-remove-initial-hash (s)
  2159. (if (string-match "\\`#" s)
  2160. (substring s 1)
  2161. s))
  2162. (defvar org-latex-entities) ; defined below
  2163. (defvar org-latex-entities-regexp) ; defined below
  2164. (defun org-export-latex-preprocess (parameters)
  2165. "Clean stuff in the LaTeX export."
  2166. ;; Replace footnotes.
  2167. (when (plist-get parameters :footnotes)
  2168. (goto-char (point-min))
  2169. (let (ref)
  2170. (while (setq ref (org-footnote-get-next-reference))
  2171. (let* ((beg (nth 1 ref))
  2172. (lbl (car ref))
  2173. (def (nth 1 (assoc (string-to-number lbl)
  2174. (mapcar (lambda (e) (cdr e))
  2175. org-export-footnotes-seen)))))
  2176. ;; Fix body for footnotes ending on a link or a list and
  2177. ;; remove definition from buffer.
  2178. (setq def
  2179. (concat def
  2180. (if (string-match "ORG-LIST-END-MARKER\\'" def)
  2181. "\n" " ")))
  2182. (org-footnote-delete-definitions lbl)
  2183. ;; Compute string to insert (FNOTE), and protect the outside
  2184. ;; macro from further transformation. When footnote at
  2185. ;; point is referring to a previously defined footnote, use
  2186. ;; \footnotemark. Otherwise, use \footnote.
  2187. (let ((fnote (if (member lbl org-export-latex-footmark-seen)
  2188. (org-export-latex-protect-string
  2189. (format "\\footnotemark[%s]" lbl))
  2190. (push lbl org-export-latex-footmark-seen)
  2191. (concat (org-export-latex-protect-string "\\footnote{")
  2192. def
  2193. (org-export-latex-protect-string "}"))))
  2194. ;; Check if another footnote is immediately following.
  2195. ;; If so, add a separator in-between.
  2196. (sep (org-export-latex-protect-string
  2197. (if (save-excursion (goto-char (1- (nth 2 ref)))
  2198. (let ((next (org-footnote-get-next-reference)))
  2199. (and next (= (nth 1 next) (nth 2 ref)))))
  2200. org-export-latex-footnote-separator ""))))
  2201. (when (org-at-heading-p)
  2202. (setq fnote (concat (org-export-latex-protect-string "\\protect")
  2203. fnote)))
  2204. ;; Ensure a footnote at column 0 cannot end a list
  2205. ;; containing it.
  2206. (put-text-property 0 (length fnote) 'original-indentation 1000 fnote)
  2207. ;; Replace footnote reference with FNOTE and, maybe, SEP.
  2208. ;; `save-excursion' is required if there are two footnotes
  2209. ;; in a row. In that case, point would be left at the
  2210. ;; beginning of the second one, and
  2211. ;; `org-footnote-get-next-reference' would then skip it.
  2212. (goto-char beg)
  2213. (delete-region beg (nth 2 ref))
  2214. (save-excursion (insert fnote sep)))))))
  2215. ;; Remove footnote section tag for LaTeX
  2216. (goto-char (point-min))
  2217. (while (re-search-forward
  2218. (concat "^" footnote-section-tag-regexp) nil t)
  2219. (org-if-unprotected
  2220. (replace-match "")))
  2221. ;; Remove any left-over footnote definition.
  2222. (mapc (lambda (fn) (org-footnote-delete-definitions (car fn)))
  2223. org-export-footnotes-data)
  2224. (mapc (lambda (fn) (org-footnote-delete-definitions fn))
  2225. org-export-latex-footmark-seen)
  2226. ;; Preserve line breaks
  2227. (goto-char (point-min))
  2228. (while (re-search-forward "\\\\\\\\" nil t)
  2229. (add-text-properties (match-beginning 0) (match-end 0)
  2230. '(org-protected t)))
  2231. ;; Preserve latex environments
  2232. (goto-char (point-min))
  2233. (while (re-search-forward "^[ \t]*\\\\begin{\\([a-zA-Z]+\\*?\\)}" nil t)
  2234. (org-if-unprotected
  2235. (let* ((start (progn (beginning-of-line) (point)))
  2236. (end (and (re-search-forward
  2237. (concat "^[ \t]*\\\\end{"
  2238. (regexp-quote (match-string 1))
  2239. "}") nil t)
  2240. (point-at-eol))))
  2241. (if end
  2242. (add-text-properties start end '(org-protected t))
  2243. (goto-char (point-at-eol))))))
  2244. ;; Preserve math snippets
  2245. (let* ((matchers (plist-get org-format-latex-options :matchers))
  2246. (re-list org-latex-regexps)
  2247. beg end re e m n block off)
  2248. ;; Check the different regular expressions
  2249. (while (setq e (pop re-list))
  2250. (setq m (car e) re (nth 1 e) n (nth 2 e)
  2251. block (if (nth 3 e) "\n\n" ""))
  2252. (setq off (if (member m '("$" "$1")) 1 0))
  2253. (when (and (member m matchers) (not (equal m "begin")))
  2254. (goto-char (point-min))
  2255. (while (re-search-forward re nil t)
  2256. (setq beg (+ (match-beginning 0) off) end (- (match-end 0) 0))
  2257. (add-text-properties beg end '(org-protected t org-latex-math t))))))
  2258. ;; Convert LaTeX to \LaTeX{} and TeX to \TeX{}
  2259. (goto-char (point-min))
  2260. (let ((case-fold-search nil))
  2261. (while (re-search-forward "\\<\\(\\(La\\)?TeX\\)\\>" nil t)
  2262. (unless (eq (char-before (match-beginning 1)) ?\\)
  2263. (org-if-unprotected-1
  2264. (replace-match (org-export-latex-protect-string
  2265. (concat "\\" (match-string 1)
  2266. "{}")) t t)))))
  2267. ;; Convert blockquotes
  2268. (goto-char (point-min))
  2269. (while (search-forward "ORG-BLOCKQUOTE-START" nil t)
  2270. (org-replace-match-keep-properties "\\begin{quote}" t t))
  2271. (goto-char (point-min))
  2272. (while (search-forward "ORG-BLOCKQUOTE-END" nil t)
  2273. (org-replace-match-keep-properties "\\end{quote}" t t))
  2274. ;; Convert verse
  2275. (goto-char (point-min))
  2276. (while (search-forward "ORG-VERSE-START" nil t)
  2277. (org-replace-match-keep-properties "\\begin{verse}" t t)
  2278. (beginning-of-line 2)
  2279. (while (and (not (looking-at "[ \t]*ORG-VERSE-END.*")) (not (eobp)))
  2280. (when (looking-at "\\([ \t]+\\)\\([^ \t\n]\\)")
  2281. (goto-char (match-end 1))
  2282. (org-replace-match-keep-properties
  2283. (org-export-latex-protect-string
  2284. (concat "\\hspace*{1cm}" (match-string 2))) t t)
  2285. (beginning-of-line 1))
  2286. (if (looking-at "[ \t]*$")
  2287. (insert (org-export-latex-protect-string "\\vspace*{1em}"))
  2288. (unless (looking-at ".*?[^ \t\n].*?\\\\\\\\[ \t]*$")
  2289. (end-of-line 1)
  2290. (insert "\\\\")))
  2291. (beginning-of-line 2))
  2292. (and (looking-at "[ \t]*ORG-VERSE-END.*")
  2293. (org-replace-match-keep-properties "\\end{verse}" t t)))
  2294. ;; Convert #+INDEX to LaTeX \\index.
  2295. (goto-char (point-min))
  2296. (let ((case-fold-search t) entry)
  2297. (while (re-search-forward
  2298. "^[ \t]*#\\+index:[ \t]*\\([^ \t\r\n].*?\\)[ \t]*$"
  2299. nil t)
  2300. (setq entry
  2301. (save-match-data
  2302. (org-export-latex-protect-string
  2303. (org-export-latex-fontify-headline (match-string 1)))))
  2304. (replace-match (format "\\index{%s}" entry) t t)))
  2305. ;; Convert center
  2306. (goto-char (point-min))
  2307. (while (search-forward "ORG-CENTER-START" nil t)
  2308. (org-replace-match-keep-properties "\\begin{center}" t t))
  2309. (goto-char (point-min))
  2310. (while (search-forward "ORG-CENTER-END" nil t)
  2311. (org-replace-match-keep-properties "\\end{center}" t t))
  2312. (run-hooks 'org-export-latex-after-blockquotes-hook)
  2313. ;; Convert horizontal rules
  2314. (goto-char (point-min))
  2315. (while (re-search-forward "^[ \t]*-\\{5,\\}[ \t]*$" nil t)
  2316. (org-if-unprotected
  2317. (replace-match (org-export-latex-protect-string "\\hrule") t t)))
  2318. ;; Protect LaTeX commands like \command[...]{...} or \command{...}
  2319. (goto-char (point-min))
  2320. (let ((re (concat
  2321. "\\\\\\([a-zA-Z]+\\*?\\)"
  2322. "\\(?:<[^<>\n]*>\\)*"
  2323. "\\(?:\\[[^][\n]*?\\]\\)*"
  2324. "\\(?:<[^<>\n]*>\\)*"
  2325. "\\(" (org-create-multibrace-regexp "{" "}" 3) "\\)\\{1,3\\}")))
  2326. (while (re-search-forward re nil t)
  2327. (unless (or
  2328. ;; Check for comment line.
  2329. (save-excursion (goto-char (match-beginning 0))
  2330. (org-in-indented-comment-line))
  2331. ;; Check if this is a defined entity, so that is may
  2332. ;; need conversion.
  2333. (org-entity-get (match-string 1))
  2334. ;; Do not protect interior of footnotes. Those have
  2335. ;; already been taken care of earlier in the function.
  2336. ;; Yet, keep looking inside them for more commands.
  2337. (and (equal (match-string 1) "footnote")
  2338. (goto-char (match-end 1))))
  2339. (add-text-properties (match-beginning 0) (match-end 0)
  2340. '(org-protected t)))))
  2341. ;; Special case for \nbsp
  2342. (goto-char (point-min))
  2343. (while (re-search-forward "\\\\nbsp\\({}\\|\\>\\)" nil t)
  2344. (org-if-unprotected
  2345. (replace-match (org-export-latex-protect-string "~"))))
  2346. ;; Protect LaTeX entities
  2347. (goto-char (point-min))
  2348. (while (re-search-forward org-latex-entities-regexp nil t)
  2349. (org-if-unprotected
  2350. (add-text-properties (match-beginning 0) (match-end 0)
  2351. '(org-protected t))))
  2352. ;; Replace radio links
  2353. (goto-char (point-min))
  2354. (while (re-search-forward
  2355. (concat "<<<?" org-export-latex-all-targets-re
  2356. ">>>?\\((INVISIBLE)\\)?") nil t)
  2357. (org-if-unprotected-at (+ (match-beginning 0) 2)
  2358. (replace-match
  2359. (concat
  2360. (org-export-latex-protect-string
  2361. (format "\\label{%s}" (save-match-data (org-solidify-link-text
  2362. (match-string 1)))))
  2363. (if (match-string 2) "" (match-string 1)))
  2364. t t)))
  2365. ;; Delete @<...> constructs
  2366. ;; Thanks to Daniel Clemente for this regexp
  2367. (goto-char (point-min))
  2368. (while (re-search-forward "@<\\(?:[^\"\n]\\|\".*\"\\)*?>" nil t)
  2369. (org-if-unprotected
  2370. (replace-match ""))))
  2371. (defun org-export-latex-fix-inputenc ()
  2372. "Set the coding system in inputenc to what the buffer is."
  2373. (let* ((cs buffer-file-coding-system)
  2374. (opt (or (ignore-errors (latexenc-coding-system-to-inputenc cs))
  2375. "utf8")))
  2376. (when opt
  2377. ;; Translate if that is requested
  2378. (setq opt (or (cdr (assoc opt org-export-latex-inputenc-alist)) opt))
  2379. ;; find the \usepackage statement and replace the option
  2380. (goto-char (point-min))
  2381. (while (re-search-forward "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  2382. nil t)
  2383. (goto-char (match-beginning 1))
  2384. (delete-region (match-beginning 1) (match-end 1))
  2385. (insert opt))
  2386. (and buffer-file-name
  2387. (save-buffer)))))
  2388. ;;; List handling:
  2389. (defun org-export-latex-lists ()
  2390. "Convert plain text lists in current buffer into LaTeX lists."
  2391. ;; `org-list-end-re' output has changed since preprocess from
  2392. ;; org-exp.el. Make sure it is taken into account.
  2393. (let ((org-list-end-re "^ORG-LIST-END-MARKER\n"))
  2394. (mapc
  2395. (lambda (e)
  2396. ;; For each type of context allowed for list export (E), find
  2397. ;; every list, parse it, delete it and insert resulting
  2398. ;; conversion to latex (RES), while keeping the same
  2399. ;; `original-indentation' property.
  2400. (let (res)
  2401. (goto-char (point-min))
  2402. (while (re-search-forward (org-item-beginning-re) nil t)
  2403. (when (and (eq (get-text-property (point) 'list-context) e)
  2404. (not (get-text-property (point) 'org-example)))
  2405. (beginning-of-line)
  2406. (setq res
  2407. (org-list-to-latex
  2408. ;; Narrowing is needed because we're converting
  2409. ;; from inner functions to outer ones.
  2410. (save-restriction
  2411. (narrow-to-region (point) (point-max))
  2412. (org-list-parse-list t))
  2413. org-export-latex-list-parameters))
  2414. ;; Extend previous value of original-indentation to the
  2415. ;; whole string
  2416. (insert (org-add-props res nil 'original-indentation
  2417. (org-find-text-property-in-string
  2418. 'original-indentation res)))))))
  2419. ;; List of allowed contexts for export, and the default one.
  2420. (append org-list-export-context '(nil)))))
  2421. (defconst org-latex-entities
  2422. '("\\!"
  2423. "\\'"
  2424. "\\+"
  2425. "\\,"
  2426. "\\-"
  2427. "\\:"
  2428. "\\;"
  2429. "\\<"
  2430. "\\="
  2431. "\\>"
  2432. "\\Huge"
  2433. "\\LARGE"
  2434. "\\Large"
  2435. "\\Styles"
  2436. "\\\\"
  2437. "\\`"
  2438. "\\\""
  2439. "\\addcontentsline"
  2440. "\\address"
  2441. "\\addtocontents"
  2442. "\\addtocounter"
  2443. "\\addtolength"
  2444. "\\addvspace"
  2445. "\\alph"
  2446. "\\appendix"
  2447. "\\arabic"
  2448. "\\author"
  2449. "\\begin{array}"
  2450. "\\begin{center}"
  2451. "\\begin{description}"
  2452. "\\begin{enumerate}"
  2453. "\\begin{eqnarray}"
  2454. "\\begin{equation}"
  2455. "\\begin{figure}"
  2456. "\\begin{flushleft}"
  2457. "\\begin{flushright}"
  2458. "\\begin{itemize}"
  2459. "\\begin{list}"
  2460. "\\begin{minipage}"
  2461. "\\begin{picture}"
  2462. "\\begin{quotation}"
  2463. "\\begin{quote}"
  2464. "\\begin{tabbing}"
  2465. "\\begin{table}"
  2466. "\\begin{tabular}"
  2467. "\\begin{thebibliography}"
  2468. "\\begin{theorem}"
  2469. "\\begin{titlepage}"
  2470. "\\begin{verbatim}"
  2471. "\\begin{verse}"
  2472. "\\bf"
  2473. "\\bf"
  2474. "\\bibitem"
  2475. "\\bigskip"
  2476. "\\cdots"
  2477. "\\centering"
  2478. "\\circle"
  2479. "\\cite"
  2480. "\\cleardoublepage"
  2481. "\\clearpage"
  2482. "\\cline"
  2483. "\\closing"
  2484. "\\dashbox"
  2485. "\\date"
  2486. "\\ddots"
  2487. "\\dotfill"
  2488. "\\em"
  2489. "\\fbox"
  2490. "\\flushbottom"
  2491. "\\fnsymbol"
  2492. "\\footnote"
  2493. "\\footnotemark"
  2494. "\\footnotesize"
  2495. "\\footnotetext"
  2496. "\\frac"
  2497. "\\frame"
  2498. "\\framebox"
  2499. "\\hfill"
  2500. "\\hline"
  2501. "\\hrulespace"
  2502. "\\hspace"
  2503. "\\huge"
  2504. "\\hyphenation"
  2505. "\\include"
  2506. "\\includeonly"
  2507. "\\indent"
  2508. "\\input"
  2509. "\\it"
  2510. "\\kill"
  2511. "\\label"
  2512. "\\large"
  2513. "\\ldots"
  2514. "\\line"
  2515. "\\linebreak"
  2516. "\\linethickness"
  2517. "\\listoffigures"
  2518. "\\listoftables"
  2519. "\\location"
  2520. "\\makebox"
  2521. "\\maketitle"
  2522. "\\mark"
  2523. "\\mbox"
  2524. "\\medskip"
  2525. "\\multicolumn"
  2526. "\\multiput"
  2527. "\\newcommand"
  2528. "\\newcounter"
  2529. "\\newenvironment"
  2530. "\\newfont"
  2531. "\\newlength"
  2532. "\\newline"
  2533. "\\newpage"
  2534. "\\newsavebox"
  2535. "\\newtheorem"
  2536. "\\nocite"
  2537. "\\nofiles"
  2538. "\\noindent"
  2539. "\\nolinebreak"
  2540. "\\nopagebreak"
  2541. "\\normalsize"
  2542. "\\onecolumn"
  2543. "\\opening"
  2544. "\\oval"
  2545. "\\overbrace"
  2546. "\\overline"
  2547. "\\pagebreak"
  2548. "\\pagenumbering"
  2549. "\\pageref"
  2550. "\\pagestyle"
  2551. "\\par"
  2552. "\\parbox"
  2553. "\\put"
  2554. "\\raggedbottom"
  2555. "\\raggedleft"
  2556. "\\raggedright"
  2557. "\\raisebox"
  2558. "\\ref"
  2559. "\\rm"
  2560. "\\roman"
  2561. "\\rule"
  2562. "\\savebox"
  2563. "\\sc"
  2564. "\\scriptsize"
  2565. "\\setcounter"
  2566. "\\setlength"
  2567. "\\settowidth"
  2568. "\\sf"
  2569. "\\shortstack"
  2570. "\\signature"
  2571. "\\sl"
  2572. "\\small"
  2573. "\\smallskip"
  2574. "\\sqrt"
  2575. "\\tableofcontents"
  2576. "\\telephone"
  2577. "\\thanks"
  2578. "\\thispagestyle"
  2579. "\\tiny"
  2580. "\\title"
  2581. "\\tt"
  2582. "\\twocolumn"
  2583. "\\typein"
  2584. "\\typeout"
  2585. "\\underbrace"
  2586. "\\underline"
  2587. "\\usebox"
  2588. "\\usecounter"
  2589. "\\value"
  2590. "\\vdots"
  2591. "\\vector"
  2592. "\\verb"
  2593. "\\vfill"
  2594. "\\vline"
  2595. "\\vspace")
  2596. "A list of LaTeX commands to be protected when performing conversion.")
  2597. (defconst org-latex-entities-regexp
  2598. (let (names rest)
  2599. (dolist (x org-latex-entities)
  2600. (if (string-match "[a-zA-Z]$" x)
  2601. (push x names)
  2602. (push x rest)))
  2603. (concat "\\(" (regexp-opt (nreverse names)) "\\>\\)"
  2604. "\\|\\(" (regexp-opt (nreverse rest)) "\\)")))
  2605. (provide 'org-export-latex)
  2606. (provide 'org-latex)
  2607. ;;; org-latex.el ends here