org-latex.el 102 KB

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