org-latex.el 69 KB

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