org-latex.el 61 KB

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