org-latex.el 92 KB

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