org-latex.el 83 KB

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