org-latex.el 81 KB

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