org-latex.el 92 KB

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