org-latex.el 69 KB

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