org-latex.el 92 KB

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