org-latex.el 89 KB

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