ox-latex.el 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922
  1. ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
  2. ;; Copyright (C) 2011-2013 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; See Org manual for details.
  18. ;;; Code:
  19. (eval-when-compile (require 'cl))
  20. (require 'ox)
  21. (require 'ox-publish)
  22. (defvar org-latex-default-packages-alist)
  23. (defvar org-latex-packages-alist)
  24. (defvar orgtbl-exp-regexp)
  25. ;;; Define Back-End
  26. (org-export-define-backend 'latex
  27. '((bold . org-latex-bold)
  28. (center-block . org-latex-center-block)
  29. (clock . org-latex-clock)
  30. (code . org-latex-code)
  31. (comment . (lambda (&rest args) ""))
  32. (comment-block . (lambda (&rest args) ""))
  33. (drawer . org-latex-drawer)
  34. (dynamic-block . org-latex-dynamic-block)
  35. (entity . org-latex-entity)
  36. (example-block . org-latex-example-block)
  37. (export-block . org-latex-export-block)
  38. (export-snippet . org-latex-export-snippet)
  39. (fixed-width . org-latex-fixed-width)
  40. (footnote-definition . org-latex-footnote-definition)
  41. (footnote-reference . org-latex-footnote-reference)
  42. (headline . org-latex-headline)
  43. (horizontal-rule . org-latex-horizontal-rule)
  44. (inline-src-block . org-latex-inline-src-block)
  45. (inlinetask . org-latex-inlinetask)
  46. (italic . org-latex-italic)
  47. (item . org-latex-item)
  48. (keyword . org-latex-keyword)
  49. (latex-environment . org-latex-latex-environment)
  50. (latex-fragment . org-latex-latex-fragment)
  51. (line-break . org-latex-line-break)
  52. (link . org-latex-link)
  53. (paragraph . org-latex-paragraph)
  54. (plain-list . org-latex-plain-list)
  55. (plain-text . org-latex-plain-text)
  56. (planning . org-latex-planning)
  57. (property-drawer . (lambda (&rest args) ""))
  58. (quote-block . org-latex-quote-block)
  59. (quote-section . org-latex-quote-section)
  60. (radio-target . org-latex-radio-target)
  61. (section . org-latex-section)
  62. (special-block . org-latex-special-block)
  63. (src-block . org-latex-src-block)
  64. (statistics-cookie . org-latex-statistics-cookie)
  65. (strike-through . org-latex-strike-through)
  66. (subscript . org-latex-subscript)
  67. (superscript . org-latex-superscript)
  68. (table . org-latex-table)
  69. (table-cell . org-latex-table-cell)
  70. (table-row . org-latex-table-row)
  71. (target . org-latex-target)
  72. (template . org-latex-template)
  73. (timestamp . org-latex-timestamp)
  74. (underline . org-latex-underline)
  75. (verbatim . org-latex-verbatim)
  76. (verse-block . org-latex-verse-block))
  77. :export-block '("LATEX" "TEX")
  78. :menu-entry
  79. '(?l "Export to LaTeX"
  80. ((?L "As LaTeX buffer" org-latex-export-as-latex)
  81. (?l "As LaTeX file" org-latex-export-to-latex)
  82. (?p "As PDF file" org-latex-export-to-pdf)
  83. (?o "As PDF file and open"
  84. (lambda (a s v b)
  85. (if a (org-latex-export-to-pdf t s v b)
  86. (org-open-file (org-latex-export-to-pdf nil s v b)))))))
  87. :options-alist '((:latex-class "LATEX_CLASS" nil org-latex-default-class t)
  88. (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
  89. (:latex-header "LATEX_HEADER" nil nil newline)
  90. (:latex-header-extra "LATEX_HEADER_EXTRA" nil nil newline)
  91. (:latex-hyperref-p nil "texht" org-latex-with-hyperref t)
  92. ;; Redefine regular options.
  93. (:date "DATE" nil "\\today" t)))
  94. ;;; Internal Variables
  95. (defconst org-latex-babel-language-alist
  96. '(("af" . "afrikaans")
  97. ("bg" . "bulgarian")
  98. ("bt-br" . "brazilian")
  99. ("ca" . "catalan")
  100. ("cs" . "czech")
  101. ("cy" . "welsh")
  102. ("da" . "danish")
  103. ("de" . "germanb")
  104. ("de-at" . "naustrian")
  105. ("de-de" . "ngerman")
  106. ("el" . "greek")
  107. ("en" . "english")
  108. ("en-au" . "australian")
  109. ("en-ca" . "canadian")
  110. ("en-gb" . "british")
  111. ("en-ie" . "irish")
  112. ("en-nz" . "newzealand")
  113. ("en-us" . "american")
  114. ("es" . "spanish")
  115. ("et" . "estonian")
  116. ("eu" . "basque")
  117. ("fi" . "finnish")
  118. ("fr" . "frenchb")
  119. ("fr-ca" . "canadien")
  120. ("gl" . "galician")
  121. ("hr" . "croatian")
  122. ("hu" . "hungarian")
  123. ("id" . "indonesian")
  124. ("is" . "icelandic")
  125. ("it" . "italian")
  126. ("la" . "latin")
  127. ("ms" . "malay")
  128. ("nl" . "dutch")
  129. ("no-no" . "nynorsk")
  130. ("pl" . "polish")
  131. ("pt" . "portuguese")
  132. ("ro" . "romanian")
  133. ("ru" . "russian")
  134. ("sa" . "sanskrit")
  135. ("sb" . "uppersorbian")
  136. ("sk" . "slovak")
  137. ("sl" . "slovene")
  138. ("sq" . "albanian")
  139. ("sr" . "serbian")
  140. ("sv" . "swedish")
  141. ("ta" . "tamil")
  142. ("tr" . "turkish")
  143. ("uk" . "ukrainian"))
  144. "Alist between language code and corresponding Babel option.")
  145. (defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
  146. ("qbordermatrix" . "\\cr")
  147. ("kbordermatrix" . "\\\\"))
  148. "Alist between matrix macros and their row ending.")
  149. ;;; User Configurable Variables
  150. (defgroup org-export-latex nil
  151. "Options for exporting Org mode files to LaTeX."
  152. :tag "Org Export LaTeX"
  153. :group 'org-export)
  154. ;;;; Preamble
  155. (defcustom org-latex-default-class "article"
  156. "The default LaTeX class."
  157. :group 'org-export-latex
  158. :type '(string :tag "LaTeX class"))
  159. (defcustom org-latex-classes
  160. '(("article"
  161. "\\documentclass[11pt]{article}"
  162. ("\\section{%s}" . "\\section*{%s}")
  163. ("\\subsection{%s}" . "\\subsection*{%s}")
  164. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
  165. ("\\paragraph{%s}" . "\\paragraph*{%s}")
  166. ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
  167. ("report"
  168. "\\documentclass[11pt]{report}"
  169. ("\\part{%s}" . "\\part*{%s}")
  170. ("\\chapter{%s}" . "\\chapter*{%s}")
  171. ("\\section{%s}" . "\\section*{%s}")
  172. ("\\subsection{%s}" . "\\subsection*{%s}")
  173. ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
  174. ("book"
  175. "\\documentclass[11pt]{book}"
  176. ("\\part{%s}" . "\\part*{%s}")
  177. ("\\chapter{%s}" . "\\chapter*{%s}")
  178. ("\\section{%s}" . "\\section*{%s}")
  179. ("\\subsection{%s}" . "\\subsection*{%s}")
  180. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
  181. "Alist of LaTeX classes and associated header and structure.
  182. If #+LATEX_CLASS is set in the buffer, use its value and the
  183. associated information. Here is the structure of each cell:
  184. \(class-name
  185. header-string
  186. \(numbered-section . unnumbered-section)
  187. ...)
  188. The header string
  189. -----------------
  190. The HEADER-STRING is the header that will be inserted into the
  191. LaTeX file. It should contain the \\documentclass macro, and
  192. anything else that is needed for this setup. To this header, the
  193. following commands will be added:
  194. - Calls to \\usepackage for all packages mentioned in the
  195. variables `org-latex-default-packages-alist' and
  196. `org-latex-packages-alist'. Thus, your header definitions
  197. should avoid to also request these packages.
  198. - Lines specified via \"#+LATEX_HEADER:\" and
  199. \"#+LATEX_HEADER_EXTRA:\" keywords.
  200. If you need more control about the sequence in which the header
  201. is built up, or if you want to exclude one of these building
  202. blocks for a particular class, you can use the following
  203. macro-like placeholders.
  204. [DEFAULT-PACKAGES] \\usepackage statements for default packages
  205. [NO-DEFAULT-PACKAGES] do not include any of the default packages
  206. [PACKAGES] \\usepackage statements for packages
  207. [NO-PACKAGES] do not include the packages
  208. [EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
  209. [NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
  210. So a header like
  211. \\documentclass{article}
  212. [NO-DEFAULT-PACKAGES]
  213. [EXTRA]
  214. \\providecommand{\\alert}[1]{\\textbf{#1}}
  215. [PACKAGES]
  216. will omit the default packages, and will include the
  217. #+LATEX_HEADER and #+LATEX_HEADER_EXTRA lines, then have a call
  218. to \\providecommand, and then place \\usepackage commands based
  219. on the content of `org-latex-packages-alist'.
  220. If your header, `org-latex-default-packages-alist' or
  221. `org-latex-packages-alist' inserts \"\\usepackage[AUTO]{inputenc}\",
  222. AUTO will automatically be replaced with a coding system derived
  223. from `buffer-file-coding-system'. See also the variable
  224. `org-latex-inputenc-alist' for a way to influence this mechanism.
  225. Likewise, if your header contains \"\\usepackage[AUTO]{babel}\",
  226. AUTO will be replaced with the language related to the language
  227. code specified by `org-export-default-language', which see. Note
  228. that constructions such as \"\\usepackage[french,AUTO,english]{babel}\"
  229. are permitted.
  230. The sectioning structure
  231. ------------------------
  232. The sectioning structure of the class is given by the elements
  233. following the header string. For each sectioning level, a number
  234. of strings is specified. A %s formatter is mandatory in each
  235. section string and will be replaced by the title of the section.
  236. Instead of a cons cell (numbered . unnumbered), you can also
  237. provide a list of 2 or 4 elements,
  238. \(numbered-open numbered-close)
  239. or
  240. \(numbered-open numbered-close unnumbered-open unnumbered-close)
  241. providing opening and closing strings for a LaTeX environment
  242. that should represent the document section. The opening clause
  243. should have a %s to represent the section title.
  244. Instead of a list of sectioning commands, you can also specify
  245. a function name. That function will be called with two
  246. parameters, the (reduced) level of the headline, and a predicate
  247. non-nil when the headline should be numbered. It must return
  248. a format string in which the section title will be added."
  249. :group 'org-export-latex
  250. :type '(repeat
  251. (list (string :tag "LaTeX class")
  252. (string :tag "LaTeX header")
  253. (repeat :tag "Levels" :inline t
  254. (choice
  255. (cons :tag "Heading"
  256. (string :tag " numbered")
  257. (string :tag "unnumbered"))
  258. (list :tag "Environment"
  259. (string :tag "Opening (numbered)")
  260. (string :tag "Closing (numbered)")
  261. (string :tag "Opening (unnumbered)")
  262. (string :tag "Closing (unnumbered)"))
  263. (function :tag "Hook computing sectioning"))))))
  264. (defcustom org-latex-inputenc-alist nil
  265. "Alist of inputenc coding system names, and what should really be used.
  266. For example, adding an entry
  267. (\"utf8\" . \"utf8x\")
  268. will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
  269. are written as utf8 files."
  270. :group 'org-export-latex
  271. :type '(repeat
  272. (cons
  273. (string :tag "Derived from buffer")
  274. (string :tag "Use this instead"))))
  275. (defcustom org-latex-title-command "\\maketitle"
  276. "The command used to insert the title just after \\begin{document}.
  277. If this string contains the formatting specification \"%s\" then
  278. it will be used as a formatting string, passing the title as an
  279. argument."
  280. :group 'org-export-latex
  281. :type 'string)
  282. (defcustom org-latex-toc-command "\\tableofcontents\n\n"
  283. "LaTeX command to set the table of contents, list of figures, etc.
  284. This command only applies to the table of contents generated with
  285. the toc:nil option, not to those generated with #+TOC keyword."
  286. :group 'org-export-latex
  287. :type 'string)
  288. (defcustom org-latex-with-hyperref t
  289. "Toggle insertion of \\hypersetup{...} in the preamble."
  290. :group 'org-export-latex
  291. :type 'boolean)
  292. ;;;; Headline
  293. (defcustom org-latex-format-headline-function
  294. 'org-latex-format-headline-default-function
  295. "Function for formatting the headline's text.
  296. This function will be called with 5 arguments:
  297. TODO the todo keyword (string or nil).
  298. TODO-TYPE the type of todo (symbol: `todo', `done', nil)
  299. PRIORITY the priority of the headline (integer or nil)
  300. TEXT the main headline text (string).
  301. TAGS the tags as a list of strings (list of strings or nil).
  302. The function result will be used in the section format string.
  303. Use `org-latex-format-headline-default-function' by default,
  304. which format headlines like for Org version prior to 8.0."
  305. :group 'org-export-latex
  306. :version "24.4"
  307. :package-version '(Org . "8.0")
  308. :type 'function)
  309. ;;;; Footnotes
  310. (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
  311. "Text used to separate footnotes."
  312. :group 'org-export-latex
  313. :type 'string)
  314. ;;;; Timestamps
  315. (defcustom org-latex-active-timestamp-format "\\textit{%s}"
  316. "A printf format string to be applied to active timestamps."
  317. :group 'org-export-latex
  318. :type 'string)
  319. (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
  320. "A printf format string to be applied to inactive timestamps."
  321. :group 'org-export-latex
  322. :type 'string)
  323. (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
  324. "A printf format string to be applied to diary timestamps."
  325. :group 'org-export-latex
  326. :type 'string)
  327. ;;;; Links
  328. (defcustom org-latex-image-default-option ""
  329. "Default option for images."
  330. :group 'org-export-latex
  331. :version "24.4"
  332. :package-version '(Org . "8.0")
  333. :type 'string)
  334. (defcustom org-latex-image-default-width ".9\\linewidth"
  335. "Default width for images.
  336. This value will not be used if a height is provided."
  337. :group 'org-export-latex
  338. :version "24.4"
  339. :package-version '(Org . "8.0")
  340. :type 'string)
  341. (defcustom org-latex-image-default-height ""
  342. "Default height for images.
  343. This value will not be used if a width is provided, or if the
  344. image is wrapped within a \"figure\" or \"wrapfigure\"
  345. environment."
  346. :group 'org-export-latex
  347. :version "24.4"
  348. :package-version '(Org . "8.0")
  349. :type 'string)
  350. (defcustom org-latex-default-figure-position "htb"
  351. "Default position for latex figures."
  352. :group 'org-export-latex
  353. :type 'string)
  354. (defcustom org-latex-inline-image-rules
  355. '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\|pgf\\)\\'"))
  356. "Rules characterizing image files that can be inlined into LaTeX.
  357. A rule consists in an association whose key is the type of link
  358. to consider, and value is a regexp that will be matched against
  359. link's path.
  360. Note that, by default, the image extension *actually* allowed
  361. depend on the way the LaTeX file is processed. When used with
  362. pdflatex, pdf, jpg and png images are OK. When processing
  363. through dvi to Postscript, only ps and eps are allowed. The
  364. default we use here encompasses both."
  365. :group 'org-export-latex
  366. :version "24.4"
  367. :package-version '(Org . "8.0")
  368. :type '(alist :key-type (string :tag "Type")
  369. :value-type (regexp :tag "Path")))
  370. (defcustom org-latex-link-with-unknown-path-format "\\texttt{%s}"
  371. "Format string for links with unknown path type."
  372. :group 'org-export-latex
  373. :type 'string)
  374. ;;;; Tables
  375. (defcustom org-latex-default-table-environment "tabular"
  376. "Default environment used to build tables."
  377. :group 'org-export-latex
  378. :version "24.4"
  379. :package-version '(Org . "8.0")
  380. :type 'string)
  381. (defcustom org-latex-default-table-mode 'table
  382. "Default mode for tables.
  383. Value can be a symbol among:
  384. `table' Regular LaTeX table.
  385. `math' In this mode, every cell is considered as being in math
  386. mode and the complete table will be wrapped within a math
  387. environment. It is particularly useful to write matrices.
  388. `inline-math' This mode is almost the same as `math', but the
  389. math environment will be inlined.
  390. `verbatim' The table is exported as it appears in the Org
  391. buffer, within a verbatim environment.
  392. This value can be overridden locally with, i.e. \":mode math\" in
  393. LaTeX attributes.
  394. When modifying this variable, it may be useful to change
  395. `org-latex-default-table-environment' accordingly."
  396. :group 'org-export-latex
  397. :version "24.4"
  398. :package-version '(Org . "8.0")
  399. :type '(choice (const :tag "Table" table)
  400. (const :tag "Matrix" math)
  401. (const :tag "Inline matrix" inline-math)
  402. (const :tag "Verbatim" verbatim)))
  403. (defcustom org-latex-tables-centered t
  404. "When non-nil, tables are exported in a center environment."
  405. :group 'org-export-latex
  406. :type 'boolean)
  407. (defcustom org-latex-tables-booktabs nil
  408. "When non-nil, display tables in a formal \"booktabs\" style.
  409. This option assumes that the \"booktabs\" package is properly
  410. loaded in the header of the document. This value can be ignored
  411. locally with \":booktabs t\" and \":booktabs nil\" LaTeX
  412. attributes."
  413. :group 'org-export-latex
  414. :version "24.4"
  415. :package-version '(Org . "8.0")
  416. :type 'boolean)
  417. (defcustom org-latex-table-caption-above t
  418. "When non-nil, place caption string at the beginning of the table.
  419. Otherwise, place it near the end."
  420. :group 'org-export-latex
  421. :type 'boolean)
  422. (defcustom org-latex-table-scientific-notation "%s\\,(%s)"
  423. "Format string to display numbers in scientific notation.
  424. The format should have \"%s\" twice, for mantissa and exponent
  425. \(i.e., \"%s\\\\times10^{%s}\").
  426. When nil, no transformation is made."
  427. :group 'org-export-latex
  428. :version "24.4"
  429. :package-version '(Org . "8.0")
  430. :type '(choice
  431. (string :tag "Format string")
  432. (const :tag "No formatting")))
  433. ;;;; Text markup
  434. (defcustom org-latex-text-markup-alist '((bold . "\\textbf{%s}")
  435. (code . verb)
  436. (italic . "\\emph{%s}")
  437. (strike-through . "\\sout{%s}")
  438. (underline . "\\uline{%s}")
  439. (verbatim . protectedtexttt))
  440. "Alist of LaTeX expressions to convert text markup.
  441. The key must be a symbol among `bold', `code', `italic',
  442. `strike-through', `underline' and `verbatim'. The value is
  443. a formatting string to wrap fontified text with.
  444. Value can also be set to the following symbols: `verb' and
  445. `protectedtexttt'. For the former, Org will use \"\\verb\" to
  446. create a format string and select a delimiter character that
  447. isn't in the string. For the latter, Org will use \"\\texttt\"
  448. to typeset and try to protect special characters.
  449. If no association can be found for a given markup, text will be
  450. returned as-is."
  451. :group 'org-export-latex
  452. :type 'alist
  453. :options '(bold code italic strike-through underline verbatim))
  454. ;;;; Drawers
  455. (defcustom org-latex-format-drawer-function nil
  456. "Function called to format a drawer in LaTeX code.
  457. The function must accept two parameters:
  458. NAME the drawer name, like \"LOGBOOK\"
  459. CONTENTS the contents of the drawer.
  460. The function should return the string to be exported.
  461. For example, the variable could be set to the following function
  462. in order to mimic default behaviour:
  463. \(defun org-latex-format-drawer-default \(name contents\)
  464. \"Format a drawer element for LaTeX export.\"
  465. contents\)"
  466. :group 'org-export-latex
  467. :type 'function)
  468. ;;;; Inlinetasks
  469. (defcustom org-latex-format-inlinetask-function nil
  470. "Function called to format an inlinetask in LaTeX code.
  471. The function must accept six parameters:
  472. TODO the todo keyword, as a string
  473. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  474. PRIORITY the inlinetask priority, as a string
  475. NAME the inlinetask name, as a string.
  476. TAGS the inlinetask tags, as a list of strings.
  477. CONTENTS the contents of the inlinetask, as a string.
  478. The function should return the string to be exported.
  479. For example, the variable could be set to the following function
  480. in order to mimic default behaviour:
  481. \(defun org-latex-format-inlinetask \(todo type priority name tags contents\)
  482. \"Format an inline task element for LaTeX export.\"
  483. \(let ((full-title
  484. \(concat
  485. \(when todo
  486. \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
  487. \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
  488. title
  489. \(when tags
  490. \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
  491. \(mapconcat 'identity tags \":\")))))
  492. \(format (concat \"\\\\begin{center}\\n\"
  493. \"\\\\fbox{\\n\"
  494. \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
  495. \"%s\\n\\n\"
  496. \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
  497. \"%s\"
  498. \"\\\\end{minipage}}\"
  499. \"\\\\end{center}\")
  500. full-title contents))"
  501. :group 'org-export-latex
  502. :type 'function)
  503. ;; Src blocks
  504. (defcustom org-latex-listings nil
  505. "Non-nil means export source code using the listings package.
  506. This package will fontify source code, possibly even with color.
  507. If you want to use this, you also need to make LaTeX use the
  508. listings package, and if you want to have color, the color
  509. package. Just add these to `org-latex-packages-alist', for
  510. example using customize, or with something like:
  511. \(require 'ox-latex)
  512. \(add-to-list 'org-latex-packages-alist '\(\"\" \"listings\"))
  513. \(add-to-list 'org-latex-packages-alist '\(\"\" \"color\"))
  514. Alternatively,
  515. \(setq org-latex-listings 'minted)
  516. causes source code to be exported using the minted package as
  517. opposed to listings. If you want to use minted, you need to add
  518. the minted package to `org-latex-packages-alist', for example
  519. using customize, or with
  520. \(require 'ox-latex)
  521. \(add-to-list 'org-latex-packages-alist '\(\"\" \"minted\"))
  522. In addition, it is necessary to install pygments
  523. \(http://pygments.org), and to configure the variable
  524. `org-latex-pdf-process' so that the -shell-escape option is
  525. passed to pdflatex."
  526. :group 'org-export-latex
  527. :type '(choice
  528. (const :tag "Use listings" t)
  529. (const :tag "Use minted" 'minted)
  530. (const :tag "Export verbatim" nil)))
  531. (defcustom org-latex-listings-langs
  532. '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
  533. (c "C") (cc "C++")
  534. (fortran "fortran")
  535. (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
  536. (html "HTML") (xml "XML")
  537. (tex "TeX") (latex "[LaTeX]TeX")
  538. (shell-script "bash")
  539. (gnuplot "Gnuplot")
  540. (ocaml "Caml") (caml "Caml")
  541. (sql "SQL") (sqlite "sql"))
  542. "Alist mapping languages to their listing language counterpart.
  543. The key is a symbol, the major mode symbol without the \"-mode\".
  544. The value is the string that should be inserted as the language
  545. parameter for the listings package. If the mode name and the
  546. listings name are the same, the language does not need an entry
  547. in this list - but it does not hurt if it is present."
  548. :group 'org-export-latex
  549. :type '(repeat
  550. (list
  551. (symbol :tag "Major mode ")
  552. (string :tag "Listings language"))))
  553. (defcustom org-latex-listings-options nil
  554. "Association list of options for the latex listings package.
  555. These options are supplied as a comma-separated list to the
  556. \\lstset command. Each element of the association list should be
  557. a list containing two strings: the name of the option, and the
  558. value. For example,
  559. (setq org-latex-listings-options
  560. '((\"basicstyle\" \"\\small\")
  561. (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
  562. will typeset the code in a small size font with underlined, bold
  563. black keywords.
  564. Note that the same options will be applied to blocks of all
  565. languages."
  566. :group 'org-export-latex
  567. :type '(repeat
  568. (list
  569. (string :tag "Listings option name ")
  570. (string :tag "Listings option value"))))
  571. (defcustom org-latex-minted-langs
  572. '((emacs-lisp "common-lisp")
  573. (cc "c++")
  574. (cperl "perl")
  575. (shell-script "bash")
  576. (caml "ocaml"))
  577. "Alist mapping languages to their minted language counterpart.
  578. The key is a symbol, the major mode symbol without the \"-mode\".
  579. The value is the string that should be inserted as the language
  580. parameter for the minted package. If the mode name and the
  581. listings name are the same, the language does not need an entry
  582. in this list - but it does not hurt if it is present.
  583. Note that minted uses all lower case for language identifiers,
  584. and that the full list of language identifiers can be obtained
  585. with:
  586. pygmentize -L lexers"
  587. :group 'org-export-latex
  588. :type '(repeat
  589. (list
  590. (symbol :tag "Major mode ")
  591. (string :tag "Minted language"))))
  592. (defcustom org-latex-minted-options nil
  593. "Association list of options for the latex minted package.
  594. These options are supplied within square brackets in
  595. \\begin{minted} environments. Each element of the alist should
  596. be a list containing two strings: the name of the option, and the
  597. value. For example,
  598. \(setq org-latex-minted-options
  599. '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
  600. will result in src blocks being exported with
  601. \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
  602. as the start of the minted environment. Note that the same
  603. options will be applied to blocks of all languages."
  604. :group 'org-export-latex
  605. :type '(repeat
  606. (list
  607. (string :tag "Minted option name ")
  608. (string :tag "Minted option value"))))
  609. (defvar org-latex-custom-lang-environments nil
  610. "Alist mapping languages to language-specific LaTeX environments.
  611. It is used during export of src blocks by the listings and minted
  612. latex packages. For example,
  613. \(setq org-latex-custom-lang-environments
  614. '\(\(python \"pythoncode\"\)\)\)
  615. would have the effect that if org encounters begin_src python
  616. during latex export it will output
  617. \\begin{pythoncode}
  618. <src block body>
  619. \\end{pythoncode}")
  620. ;;;; Compilation
  621. (defcustom org-latex-pdf-process
  622. '("pdflatex -interaction nonstopmode -output-directory %o %f"
  623. "pdflatex -interaction nonstopmode -output-directory %o %f"
  624. "pdflatex -interaction nonstopmode -output-directory %o %f")
  625. "Commands to process a LaTeX file to a PDF file.
  626. This is a list of strings, each of them will be given to the
  627. shell as a command. %f in the command will be replaced by the
  628. full file name, %b by the file base name (i.e. without directory
  629. and extension parts) and %o by the base directory of the file.
  630. The reason why this is a list is that it usually takes several
  631. runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
  632. does not have a clever mechanism to detect which of these
  633. commands have to be run to get to a stable result, and it also
  634. does not do any error checking.
  635. By default, Org uses 3 runs of `pdflatex' to do the processing.
  636. If you have texi2dvi on your system and if that does not cause
  637. the infamous egrep/locale bug:
  638. http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
  639. then `texi2dvi' is the superior choice as it automates the LaTeX
  640. build process by calling the \"correct\" combinations of
  641. auxiliary programs. Org does offer `texi2dvi' as one of the
  642. customize options. Alternatively, `rubber' and `latexmk' also
  643. provide similar functionality. The latter supports `biber' out
  644. of the box.
  645. Alternatively, this may be a Lisp function that does the
  646. processing, so you could use this to apply the machinery of
  647. AUCTeX or the Emacs LaTeX mode. This function should accept the
  648. file name as its single argument."
  649. :group 'org-export-pdf
  650. :type '(choice
  651. (repeat :tag "Shell command sequence"
  652. (string :tag "Shell command"))
  653. (const :tag "2 runs of pdflatex"
  654. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  655. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  656. (const :tag "3 runs of pdflatex"
  657. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  658. "pdflatex -interaction nonstopmode -output-directory %o %f"
  659. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  660. (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
  661. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  662. "bibtex %b"
  663. "pdflatex -interaction nonstopmode -output-directory %o %f"
  664. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  665. (const :tag "2 runs of xelatex"
  666. ("xelatex -interaction nonstopmode -output-directory %o %f"
  667. "xelatex -interaction nonstopmode -output-directory %o %f"))
  668. (const :tag "3 runs of xelatex"
  669. ("xelatex -interaction nonstopmode -output-directory %o %f"
  670. "xelatex -interaction nonstopmode -output-directory %o %f"
  671. "xelatex -interaction nonstopmode -output-directory %o %f"))
  672. (const :tag "xelatex,bibtex,xelatex,xelatex"
  673. ("xelatex -interaction nonstopmode -output-directory %o %f"
  674. "bibtex %b"
  675. "xelatex -interaction nonstopmode -output-directory %o %f"
  676. "xelatex -interaction nonstopmode -output-directory %o %f"))
  677. (const :tag "texi2dvi"
  678. ("texi2dvi -p -b -V %f"))
  679. (const :tag "rubber"
  680. ("rubber -d --into %o %f"))
  681. (const :tag "latexmk"
  682. ("latexmk -g -pdf %f"))
  683. (function)))
  684. (defcustom org-latex-logfiles-extensions
  685. '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
  686. "The list of file extensions to consider as LaTeX logfiles.
  687. The logfiles will be remove if `org-latex-remove-logfiles' is
  688. non-nil."
  689. :group 'org-export-latex
  690. :type '(repeat (string :tag "Extension")))
  691. (defcustom org-latex-remove-logfiles t
  692. "Non-nil means remove the logfiles produced by PDF production.
  693. By default, logfiles are files with these extensions: .aux, .idx,
  694. .log, .out, .toc, .nav, .snm and .vrb. To define the set of
  695. logfiles to remove, set `org-latex-logfiles-extensions'."
  696. :group 'org-export-latex
  697. :type 'boolean)
  698. (defcustom org-latex-known-errors
  699. '(("Reference.*?undefined" . "[undefined reference]")
  700. ("Citation.*?undefined" . "[undefined citation]")
  701. ("Undefined control sequence" . "[undefined control sequence]")
  702. ("^! LaTeX.*?Error" . "[LaTeX error]")
  703. ("^! Package.*?Error" . "[package error]")
  704. ("Runaway argument" . "Runaway argument"))
  705. "Alist of regular expressions and associated messages for the user.
  706. The regular expressions are used to find possible errors in the
  707. log of a latex-run."
  708. :group 'org-export-latex
  709. :version "24.4"
  710. :package-version '(Org . "8.0")
  711. :type '(repeat
  712. (cons
  713. (string :tag "Regexp")
  714. (string :tag "Message"))))
  715. ;;; Internal Functions
  716. (defun org-latex--caption/label-string (element info)
  717. "Return caption and label LaTeX string for ELEMENT.
  718. INFO is a plist holding contextual information. If there's no
  719. caption nor label, return the empty string.
  720. For non-floats, see `org-latex--wrap-label'."
  721. (let* ((label (org-element-property :name element))
  722. (label-str (if (not (org-string-nw-p label)) ""
  723. (format "\\label{%s}"
  724. (org-export-solidify-link-text label))))
  725. (main (org-export-get-caption element))
  726. (short (org-export-get-caption element t)))
  727. (cond
  728. ((and (not main) (equal label-str "")) "")
  729. ((not main) (concat label-str "\n"))
  730. ;; Option caption format with short name.
  731. (short (format "\\caption[%s]{%s%s}\n"
  732. (org-export-data short info)
  733. label-str
  734. (org-export-data main info)))
  735. ;; Standard caption format.
  736. (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
  737. (defun org-latex-guess-inputenc (header)
  738. "Set the coding system in inputenc to what the buffer is.
  739. HEADER is the LaTeX header string. This function only applies
  740. when specified inputenc option is \"AUTO\".
  741. Return the new header, as a string."
  742. (let* ((cs (or (ignore-errors
  743. (latexenc-coding-system-to-inputenc
  744. (or org-export-coding-system buffer-file-coding-system)))
  745. "utf8")))
  746. (if (not cs) header
  747. ;; First translate if that is requested.
  748. (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
  749. ;; Then find the \usepackage statement and replace the option.
  750. (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  751. cs header t nil 1))))
  752. (defun org-latex-guess-babel-language (header info)
  753. "Set Babel's language according to LANGUAGE keyword.
  754. HEADER is the LaTeX header string. INFO is the plist used as
  755. a communication channel.
  756. Insertion of guessed language only happens when Babel package has
  757. explicitly been loaded. Then it is added to the rest of
  758. package's options.
  759. The argument to Babel may be \"AUTO\" which is then replaced with
  760. the language of the document or `org-export-default-language'
  761. unless language in question is already loaded.
  762. Return the new header."
  763. (let ((language-code (plist-get info :language)))
  764. ;; If no language is set or Babel package is not loaded, return
  765. ;; HEADER as-is.
  766. (if (or (not (stringp language-code))
  767. (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
  768. header
  769. (let ((options (save-match-data
  770. (org-split-string (match-string 1 header) ",[ \t]*")))
  771. (language (cdr (assoc language-code
  772. org-latex-babel-language-alist))))
  773. ;; If LANGUAGE is already loaded, return header without AUTO.
  774. ;; Otherwise, replace AUTO with language or append language if
  775. ;; AUTO is not present.
  776. (replace-match
  777. (mapconcat (lambda (option) (if (equal "AUTO" option) language option))
  778. (cond ((member language options) (delete "AUTO" options))
  779. ((member "AUTO" options) options)
  780. (t (append options (list language))))
  781. ", ")
  782. t nil header 1)))))
  783. (defun org-latex--find-verb-separator (s)
  784. "Return a character not used in string S.
  785. This is used to choose a separator for constructs like \\verb."
  786. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  787. (loop for c across ll
  788. when (not (string-match (regexp-quote (char-to-string c)) s))
  789. return (char-to-string c))))
  790. (defun org-latex--make-option-string (options)
  791. "Return a comma separated string of keywords and values.
  792. OPTIONS is an alist where the key is the options keyword as
  793. a string, and the value a list containing the keyword value, or
  794. nil."
  795. (mapconcat (lambda (pair)
  796. (concat (first pair)
  797. (when (> (length (second pair)) 0)
  798. (concat "=" (second pair)))))
  799. options
  800. ","))
  801. (defun org-latex--wrap-label (element output)
  802. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  803. This function shouldn't be used for floats. See
  804. `org-latex--caption/label-string'."
  805. (let ((label (org-element-property :name element)))
  806. (if (not (and (org-string-nw-p output) (org-string-nw-p label))) output
  807. (concat (format "\\label{%s}\n" (org-export-solidify-link-text label))
  808. output))))
  809. (defun org-latex--text-markup (text markup)
  810. "Format TEXT depending on MARKUP text markup.
  811. See `org-latex-text-markup-alist' for details."
  812. (let ((fmt (cdr (assq markup org-latex-text-markup-alist))))
  813. (cond
  814. ;; No format string: Return raw text.
  815. ((not fmt) text)
  816. ;; Handle the `verb' special case: Find and appropriate separator
  817. ;; and use "\\verb" command.
  818. ((eq 'verb fmt)
  819. (let ((separator (org-latex--find-verb-separator text)))
  820. (concat "\\verb" separator text separator)))
  821. ;; Handle the `protectedtexttt' special case: Protect some
  822. ;; special chars and use "\texttt{%s}" format string.
  823. ((eq 'protectedtexttt fmt)
  824. (let ((start 0)
  825. (trans '(("\\" . "\\textbackslash{}")
  826. ("~" . "\\textasciitilde{}")
  827. ("^" . "\\textasciicircum{}")))
  828. (rtn "")
  829. char)
  830. (while (string-match "[\\{}$%&_#~^]" text)
  831. (setq char (match-string 0 text))
  832. (if (> (match-beginning 0) 0)
  833. (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
  834. (setq text (substring text (1+ (match-beginning 0))))
  835. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  836. rtn (concat rtn char)))
  837. (setq text (concat rtn text)
  838. fmt "\\texttt{%s}")
  839. (while (string-match "--" text)
  840. (setq text (replace-match "-{}-" t t text)))
  841. (format fmt text)))
  842. ;; Else use format string.
  843. (t (format fmt text)))))
  844. (defun org-latex--delayed-footnotes-definitions (element info)
  845. "Return footnotes definitions in ELEMENT as a string.
  846. INFO is a plist used as a communication channel.
  847. Footnotes definitions are returned within \"\\footnotetxt{}\"
  848. commands.
  849. This function is used within constructs that don't support
  850. \"\\footnote{}\" command (i.e. an item's tag). In that case,
  851. \"\\footnotemark\" is used within the construct and the function
  852. just outside of it."
  853. (mapconcat
  854. (lambda (ref)
  855. (format
  856. "\\footnotetext[%s]{%s}"
  857. (org-export-get-footnote-number ref info)
  858. (org-trim
  859. (org-export-data
  860. (org-export-get-footnote-definition ref info) info))))
  861. ;; Find every footnote reference in ELEMENT.
  862. (let* (all-refs
  863. search-refs ; For byte-compiler.
  864. (search-refs
  865. (function
  866. (lambda (data)
  867. ;; Return a list of all footnote references never seen
  868. ;; before in DATA.
  869. (org-element-map data 'footnote-reference
  870. (lambda (ref)
  871. (when (org-export-footnote-first-reference-p ref info)
  872. (push ref all-refs)
  873. (when (eq (org-element-property :type ref) 'standard)
  874. (funcall search-refs
  875. (org-export-get-footnote-definition ref info)))))
  876. info)
  877. (reverse all-refs)))))
  878. (funcall search-refs element))
  879. ""))
  880. ;;; Template
  881. (defun org-latex-template (contents info)
  882. "Return complete document string after LaTeX conversion.
  883. CONTENTS is the transcoded contents string. INFO is a plist
  884. holding export options."
  885. (let ((title (org-export-data (plist-get info :title) info)))
  886. (concat
  887. ;; Time-stamp.
  888. (and (plist-get info :time-stamp-file)
  889. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  890. ;; Document class and packages.
  891. (let ((class (plist-get info :latex-class))
  892. (class-options (plist-get info :latex-class-options)))
  893. (org-element-normalize-string
  894. (let* ((header (nth 1 (assoc class org-latex-classes)))
  895. (document-class-string
  896. (and (stringp header)
  897. (if (not class-options) header
  898. (replace-regexp-in-string
  899. "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
  900. class-options header t nil 1)))))
  901. (if (not document-class-string)
  902. (user-error "Unknown LaTeX class `%s'" class)
  903. (org-latex-guess-babel-language
  904. (org-latex-guess-inputenc
  905. (org-splice-latex-header
  906. document-class-string
  907. org-latex-default-packages-alist
  908. org-latex-packages-alist nil
  909. (concat (plist-get info :latex-header)
  910. (plist-get info :latex-header-extra))))
  911. info)))))
  912. ;; Possibly limit depth for headline numbering.
  913. (let ((sec-num (plist-get info :section-numbers)))
  914. (when (integerp sec-num)
  915. (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
  916. ;; Author.
  917. (let ((author (and (plist-get info :with-author)
  918. (let ((auth (plist-get info :author)))
  919. (and auth (org-export-data auth info)))))
  920. (email (and (plist-get info :with-email)
  921. (org-export-data (plist-get info :email) info))))
  922. (cond ((and author email (not (string= "" email)))
  923. (format "\\author{%s\\thanks{%s}}\n" author email))
  924. ((or author email) (format "\\author{%s}\n" (or author email)))))
  925. ;; Date.
  926. (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
  927. (format "\\date{%s}\n" (org-export-data date info)))
  928. ;; Title
  929. (format "\\title{%s}\n" title)
  930. ;; Hyperref options.
  931. (when (plist-get info :latex-hyperref-p)
  932. (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
  933. (or (plist-get info :keywords) "")
  934. (or (plist-get info :description) "")
  935. (if (not (plist-get info :with-creator)) ""
  936. (plist-get info :creator))))
  937. ;; Document start.
  938. "\\begin{document}\n\n"
  939. ;; Title command.
  940. (org-element-normalize-string
  941. (cond ((string= "" title) nil)
  942. ((not (stringp org-latex-title-command)) nil)
  943. ((string-match "\\(?:[^%]\\|^\\)%s"
  944. org-latex-title-command)
  945. (format org-latex-title-command title))
  946. (t org-latex-title-command)))
  947. ;; Table of contents.
  948. (let ((depth (plist-get info :with-toc)))
  949. (when depth
  950. (concat (when (wholenump depth)
  951. (format "\\setcounter{tocdepth}{%d}\n" depth))
  952. org-latex-toc-command)))
  953. ;; Document's body.
  954. contents
  955. ;; Creator.
  956. (let ((creator-info (plist-get info :with-creator)))
  957. (cond
  958. ((not creator-info) "")
  959. ((eq creator-info 'comment)
  960. (format "%% %s\n" (plist-get info :creator)))
  961. (t (concat (plist-get info :creator) "\n"))))
  962. ;; Document end.
  963. "\\end{document}")))
  964. ;;; Transcode Functions
  965. ;;;; Bold
  966. (defun org-latex-bold (bold contents info)
  967. "Transcode BOLD from Org to LaTeX.
  968. CONTENTS is the text with bold markup. INFO is a plist holding
  969. contextual information."
  970. (org-latex--text-markup contents 'bold))
  971. ;;;; Center Block
  972. (defun org-latex-center-block (center-block contents info)
  973. "Transcode a CENTER-BLOCK element from Org to LaTeX.
  974. CONTENTS holds the contents of the center block. INFO is a plist
  975. holding contextual information."
  976. (org-latex--wrap-label
  977. center-block
  978. (format "\\begin{center}\n%s\\end{center}" contents)))
  979. ;;;; Clock
  980. (defun org-latex-clock (clock contents info)
  981. "Transcode a CLOCK element from Org to LaTeX.
  982. CONTENTS is nil. INFO is a plist holding contextual
  983. information."
  984. (concat
  985. "\\noindent"
  986. (format "\\textbf{%s} " org-clock-string)
  987. (format org-latex-inactive-timestamp-format
  988. (concat (org-translate-time
  989. (org-element-property :raw-value
  990. (org-element-property :value clock)))
  991. (let ((time (org-element-property :duration clock)))
  992. (and time (format " (%s)" time)))))
  993. "\\\\"))
  994. ;;;; Code
  995. (defun org-latex-code (code contents info)
  996. "Transcode a CODE object from Org to LaTeX.
  997. CONTENTS is nil. INFO is a plist used as a communication
  998. channel."
  999. (org-latex--text-markup (org-element-property :value code) 'code))
  1000. ;;;; Drawer
  1001. (defun org-latex-drawer (drawer contents info)
  1002. "Transcode a DRAWER element from Org to LaTeX.
  1003. CONTENTS holds the contents of the block. INFO is a plist
  1004. holding contextual information."
  1005. (let* ((name (org-element-property :drawer-name drawer))
  1006. (output (if (functionp org-latex-format-drawer-function)
  1007. (funcall org-latex-format-drawer-function
  1008. name contents)
  1009. ;; If there's no user defined function: simply
  1010. ;; display contents of the drawer.
  1011. contents)))
  1012. (org-latex--wrap-label drawer output)))
  1013. ;;;; Dynamic Block
  1014. (defun org-latex-dynamic-block (dynamic-block contents info)
  1015. "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
  1016. CONTENTS holds the contents of the block. INFO is a plist
  1017. holding contextual information. See `org-export-data'."
  1018. (org-latex--wrap-label dynamic-block contents))
  1019. ;;;; Entity
  1020. (defun org-latex-entity (entity contents info)
  1021. "Transcode an ENTITY object from Org to LaTeX.
  1022. CONTENTS are the definition itself. INFO is a plist holding
  1023. contextual information."
  1024. (let ((ent (org-element-property :latex entity)))
  1025. (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
  1026. ;;;; Example Block
  1027. (defun org-latex-example-block (example-block contents info)
  1028. "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
  1029. CONTENTS is nil. INFO is a plist holding contextual
  1030. information."
  1031. (when (org-string-nw-p (org-element-property :value example-block))
  1032. (org-latex--wrap-label
  1033. example-block
  1034. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1035. (org-export-format-code-default example-block info)))))
  1036. ;;;; Export Block
  1037. (defun org-latex-export-block (export-block contents info)
  1038. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  1039. CONTENTS is nil. INFO is a plist holding contextual information."
  1040. (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
  1041. (org-remove-indentation (org-element-property :value export-block))))
  1042. ;;;; Export Snippet
  1043. (defun org-latex-export-snippet (export-snippet contents info)
  1044. "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
  1045. CONTENTS is nil. INFO is a plist holding contextual information."
  1046. (when (eq (org-export-snippet-backend export-snippet) 'latex)
  1047. (org-element-property :value export-snippet)))
  1048. ;;;; Fixed Width
  1049. (defun org-latex-fixed-width (fixed-width contents info)
  1050. "Transcode a FIXED-WIDTH element from Org to LaTeX.
  1051. CONTENTS is nil. INFO is a plist holding contextual information."
  1052. (org-latex--wrap-label
  1053. fixed-width
  1054. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1055. (org-remove-indentation
  1056. (org-element-property :value fixed-width)))))
  1057. ;;;; Footnote Reference
  1058. (defun org-latex-footnote-reference (footnote-reference contents info)
  1059. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  1060. CONTENTS is nil. INFO is a plist holding contextual information."
  1061. (concat
  1062. ;; Insert separator between two footnotes in a row.
  1063. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1064. (when (eq (org-element-type prev) 'footnote-reference)
  1065. org-latex-footnote-separator))
  1066. (cond
  1067. ;; Use \footnotemark if the footnote has already been defined.
  1068. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1069. (format "\\footnotemark[%s]{}"
  1070. (org-export-get-footnote-number footnote-reference info)))
  1071. ;; Use \footnotemark if reference is within another footnote
  1072. ;; reference, footnote definition or table cell.
  1073. ((loop for parent in (org-export-get-genealogy footnote-reference)
  1074. thereis (memq (org-element-type parent)
  1075. '(footnote-reference footnote-definition table-cell)))
  1076. "\\footnotemark")
  1077. ;; Otherwise, define it with \footnote command.
  1078. (t
  1079. (let ((def (org-export-get-footnote-definition footnote-reference info)))
  1080. (concat
  1081. (format "\\footnote{%s}" (org-trim (org-export-data def info)))
  1082. ;; Retrieve all footnote references within the footnote and
  1083. ;; add their definition after it, since LaTeX doesn't support
  1084. ;; them inside.
  1085. (org-latex--delayed-footnotes-definitions def info)))))))
  1086. ;;;; Headline
  1087. (defun org-latex-headline (headline contents info)
  1088. "Transcode a HEADLINE element from Org to LaTeX.
  1089. CONTENTS holds the contents of the headline. INFO is a plist
  1090. holding contextual information."
  1091. (unless (org-element-property :footnote-section-p headline)
  1092. (let* ((class (plist-get info :latex-class))
  1093. (level (org-export-get-relative-level headline info))
  1094. (numberedp (org-export-numbered-headline-p headline info))
  1095. (class-sectionning (assoc class org-latex-classes))
  1096. ;; Section formatting will set two placeholders: one for
  1097. ;; the title and the other for the contents.
  1098. (section-fmt
  1099. (let ((sec (if (functionp (nth 2 class-sectionning))
  1100. (funcall (nth 2 class-sectionning) level numberedp)
  1101. (nth (1+ level) class-sectionning))))
  1102. (cond
  1103. ;; No section available for that LEVEL.
  1104. ((not sec) nil)
  1105. ;; Section format directly returned by a function. Add
  1106. ;; placeholder for contents.
  1107. ((stringp sec) (concat sec "\n%s"))
  1108. ;; (numbered-section . unnumbered-section)
  1109. ((not (consp (cdr sec)))
  1110. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  1111. ;; (numbered-open numbered-close)
  1112. ((= (length sec) 2)
  1113. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  1114. ;; (num-in num-out no-num-in no-num-out)
  1115. ((= (length sec) 4)
  1116. (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
  1117. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  1118. (text (org-export-data (org-element-property :title headline) info))
  1119. (todo
  1120. (and (plist-get info :with-todo-keywords)
  1121. (let ((todo (org-element-property :todo-keyword headline)))
  1122. (and todo (org-export-data todo info)))))
  1123. (todo-type (and todo (org-element-property :todo-type headline)))
  1124. (tags (and (plist-get info :with-tags)
  1125. (org-export-get-tags headline info)))
  1126. (priority (and (plist-get info :with-priority)
  1127. (org-element-property :priority headline)))
  1128. ;; Create the headline text along with a no-tag version.
  1129. ;; The latter is required to remove tags from toc.
  1130. (full-text (funcall org-latex-format-headline-function
  1131. todo todo-type priority text tags))
  1132. ;; Associate \label to the headline for internal links.
  1133. (headline-label
  1134. (format "\\label{sec-%s}\n"
  1135. (mapconcat 'number-to-string
  1136. (org-export-get-headline-number headline info)
  1137. "-")))
  1138. (pre-blanks
  1139. (make-string (org-element-property :pre-blank headline) 10)))
  1140. (if (or (not section-fmt) (org-export-low-level-p headline info))
  1141. ;; This is a deep sub-tree: export it as a list item. Also
  1142. ;; export as items headlines for which no section format has
  1143. ;; been found.
  1144. (let ((low-level-body
  1145. (concat
  1146. ;; If headline is the first sibling, start a list.
  1147. (when (org-export-first-sibling-p headline info)
  1148. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  1149. ;; Itemize headline
  1150. "\\item " full-text "\n" headline-label pre-blanks contents)))
  1151. ;; If headline is not the last sibling simply return
  1152. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
  1153. ;; any blank line.
  1154. (if (not (org-export-last-sibling-p headline info)) low-level-body
  1155. (replace-regexp-in-string
  1156. "[ \t\n]*\\'"
  1157. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  1158. low-level-body)))
  1159. ;; This is a standard headline. Export it as a section. Add
  1160. ;; an alternative heading when possible, and when this is not
  1161. ;; identical to the usual heading.
  1162. (let ((opt-title
  1163. (funcall org-latex-format-headline-function
  1164. todo todo-type priority
  1165. (org-export-data
  1166. (org-export-get-alt-title headline info) info)
  1167. (and (eq (plist-get info :with-tags) t) tags))))
  1168. (if (and numberedp opt-title
  1169. (not (equal opt-title full-text))
  1170. (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
  1171. (format (replace-match "\\1[%s]" nil nil section-fmt 1)
  1172. ;; Replace square brackets with parenthesis
  1173. ;; since square brackets are not supported in
  1174. ;; optional arguments.
  1175. (replace-regexp-in-string
  1176. "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
  1177. full-text
  1178. (concat headline-label pre-blanks contents))
  1179. ;; Impossible to add an alternative heading. Fallback to
  1180. ;; regular sectioning format string.
  1181. (format section-fmt full-text
  1182. (concat headline-label pre-blanks contents))))))))
  1183. (defun org-latex-format-headline-default-function
  1184. (todo todo-type priority text tags)
  1185. "Default format function for a headline.
  1186. See `org-latex-format-headline-function' for details."
  1187. (concat
  1188. (and todo (format "{\\bfseries\\sffamily %s} " todo))
  1189. (and priority (format "\\framebox{\\#%c} " priority))
  1190. text
  1191. (and tags
  1192. (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags ":")))))
  1193. ;;;; Horizontal Rule
  1194. (defun org-latex-horizontal-rule (horizontal-rule contents info)
  1195. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  1196. CONTENTS is nil. INFO is a plist holding contextual information."
  1197. (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
  1198. (prev (org-export-get-previous-element horizontal-rule info)))
  1199. (concat
  1200. ;; Make sure the rule doesn't start at the end of the current
  1201. ;; line by separating it with a blank line from previous element.
  1202. (when (and prev
  1203. (let ((prev-blank (org-element-property :post-blank prev)))
  1204. (or (not prev-blank) (zerop prev-blank))))
  1205. "\n")
  1206. (org-latex--wrap-label
  1207. horizontal-rule
  1208. (format "\\rule{%s}{%s}"
  1209. (or (plist-get attr :width) "\\linewidth")
  1210. (or (plist-get attr :thickness) "0.5pt"))))))
  1211. ;;;; Inline Src Block
  1212. (defun org-latex-inline-src-block (inline-src-block contents info)
  1213. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  1214. CONTENTS holds the contents of the item. INFO is a plist holding
  1215. contextual information."
  1216. (let* ((code (org-element-property :value inline-src-block))
  1217. (separator (org-latex--find-verb-separator code)))
  1218. (cond
  1219. ;; Do not use a special package: transcode it verbatim.
  1220. ((not org-latex-listings)
  1221. (concat "\\verb" separator code separator))
  1222. ;; Use minted package.
  1223. ((eq org-latex-listings 'minted)
  1224. (let* ((org-lang (org-element-property :language inline-src-block))
  1225. (mint-lang (or (cadr (assq (intern org-lang)
  1226. org-latex-minted-langs))
  1227. org-lang))
  1228. (options (org-latex--make-option-string
  1229. org-latex-minted-options)))
  1230. (concat (format "\\mint%s{%s}"
  1231. (if (string= options "") "" (format "[%s]" options))
  1232. mint-lang)
  1233. separator code separator)))
  1234. ;; Use listings package.
  1235. (t
  1236. ;; Maybe translate language's name.
  1237. (let* ((org-lang (org-element-property :language inline-src-block))
  1238. (lst-lang (or (cadr (assq (intern org-lang)
  1239. org-latex-listings-langs))
  1240. org-lang))
  1241. (options (org-latex--make-option-string
  1242. (append org-latex-listings-options
  1243. `(("language" ,lst-lang))))))
  1244. (concat (format "\\lstinline[%s]" options)
  1245. separator code separator))))))
  1246. ;;;; Inlinetask
  1247. (defun org-latex-inlinetask (inlinetask contents info)
  1248. "Transcode an INLINETASK element from Org to LaTeX.
  1249. CONTENTS holds the contents of the block. INFO is a plist
  1250. holding contextual information."
  1251. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  1252. (todo (and (plist-get info :with-todo-keywords)
  1253. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1254. (and todo (org-export-data todo info)))))
  1255. (todo-type (org-element-property :todo-type inlinetask))
  1256. (tags (and (plist-get info :with-tags)
  1257. (org-export-get-tags inlinetask info)))
  1258. (priority (and (plist-get info :with-priority)
  1259. (org-element-property :priority inlinetask))))
  1260. ;; If `org-latex-format-inlinetask-function' is provided, call it
  1261. ;; with appropriate arguments.
  1262. (if (functionp org-latex-format-inlinetask-function)
  1263. (funcall org-latex-format-inlinetask-function
  1264. todo todo-type priority title tags contents)
  1265. ;; Otherwise, use a default template.
  1266. (org-latex--wrap-label
  1267. inlinetask
  1268. (let ((full-title
  1269. (concat
  1270. (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1271. (when priority (format "\\framebox{\\#%c} " priority))
  1272. title
  1273. (when tags (format "\\hfill{}\\textsc{:%s:}"
  1274. (mapconcat 'identity tags ":"))))))
  1275. (format (concat "\\begin{center}\n"
  1276. "\\fbox{\n"
  1277. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1278. "%s\n\n"
  1279. "\\rule[.8em]{\\textwidth}{2pt}\n\n"
  1280. "%s"
  1281. "\\end{minipage}\n"
  1282. "}\n"
  1283. "\\end{center}")
  1284. full-title contents))))))
  1285. ;;;; Italic
  1286. (defun org-latex-italic (italic contents info)
  1287. "Transcode ITALIC from Org to LaTeX.
  1288. CONTENTS is the text with italic markup. INFO is a plist holding
  1289. contextual information."
  1290. (org-latex--text-markup contents 'italic))
  1291. ;;;; Item
  1292. (defun org-latex-item (item contents info)
  1293. "Transcode an ITEM element from Org to LaTeX.
  1294. CONTENTS holds the contents of the item. INFO is a plist holding
  1295. contextual information."
  1296. (let* ((counter
  1297. (let ((count (org-element-property :counter item))
  1298. (level
  1299. ;; Determine level of current item to determine the
  1300. ;; correct LaTeX counter to use (enumi, enumii...).
  1301. (let ((parent item) (level 0))
  1302. (while (memq (org-element-type
  1303. (setq parent (org-export-get-parent parent)))
  1304. '(plain-list item))
  1305. (when (and (eq (org-element-type parent) 'plain-list)
  1306. (eq (org-element-property :type parent)
  1307. 'ordered))
  1308. (incf level)))
  1309. level)))
  1310. (and count
  1311. (< level 5)
  1312. (format "\\setcounter{enum%s}{%s}\n"
  1313. (nth (1- level) '("i" "ii" "iii" "iv"))
  1314. (1- count)))))
  1315. (checkbox (case (org-element-property :checkbox item)
  1316. (on "$\\boxtimes$ ")
  1317. (off "$\\Box$ ")
  1318. (trans "$\\boxminus$ ")))
  1319. (tag (let ((tag (org-element-property :tag item)))
  1320. ;; Check-boxes must belong to the tag.
  1321. (and tag (format "[{%s}] "
  1322. (concat checkbox
  1323. (org-export-data tag info)))))))
  1324. (concat counter "\\item" (or tag (concat " " checkbox))
  1325. (and contents (org-trim contents))
  1326. ;; If there are footnotes references in tag, be sure to
  1327. ;; add their definition at the end of the item. This
  1328. ;; workaround is necessary since "\footnote{}" command is
  1329. ;; not supported in tags.
  1330. (and tag
  1331. (org-latex--delayed-footnotes-definitions
  1332. (org-element-property :tag item) info)))))
  1333. ;;;; Keyword
  1334. (defun org-latex-keyword (keyword contents info)
  1335. "Transcode a KEYWORD element from Org to LaTeX.
  1336. CONTENTS is nil. INFO is a plist holding contextual information."
  1337. (let ((key (org-element-property :key keyword))
  1338. (value (org-element-property :value keyword)))
  1339. (cond
  1340. ((string= key "LATEX") value)
  1341. ((string= key "INDEX") (format "\\index{%s}" value))
  1342. ((string= key "TOC")
  1343. (let ((value (downcase value)))
  1344. (cond
  1345. ((string-match "\\<headlines\\>" value)
  1346. (let ((depth (or (and (string-match "[0-9]+" value)
  1347. (string-to-number (match-string 0 value)))
  1348. (plist-get info :with-toc))))
  1349. (concat
  1350. (when (wholenump depth)
  1351. (format "\\setcounter{tocdepth}{%s}\n" depth))
  1352. "\\tableofcontents")))
  1353. ((string= "tables" value) "\\listoftables")
  1354. ((string= "listings" value)
  1355. (cond
  1356. ((eq org-latex-listings 'minted) "\\listoflistings")
  1357. (org-latex-listings "\\lstlistoflistings")
  1358. ;; At the moment, src blocks with a caption are wrapped
  1359. ;; into a figure environment.
  1360. (t "\\listoffigures")))))))))
  1361. ;;;; Latex Environment
  1362. (defun org-latex-latex-environment (latex-environment contents info)
  1363. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1364. CONTENTS is nil. INFO is a plist holding contextual information."
  1365. (when (plist-get info :with-latex)
  1366. (let ((label (org-element-property :name latex-environment))
  1367. (value (org-remove-indentation
  1368. (org-element-property :value latex-environment))))
  1369. (if (not (org-string-nw-p label)) value
  1370. ;; Environment is labelled: label must be within the environment
  1371. ;; (otherwise, a reference pointing to that element will count
  1372. ;; the section instead).
  1373. (with-temp-buffer
  1374. (insert value)
  1375. (goto-char (point-min))
  1376. (forward-line)
  1377. (insert
  1378. (format "\\label{%s}\n" (org-export-solidify-link-text label)))
  1379. (buffer-string))))))
  1380. ;;;; Latex Fragment
  1381. (defun org-latex-latex-fragment (latex-fragment contents info)
  1382. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1383. CONTENTS is nil. INFO is a plist holding contextual information."
  1384. (when (plist-get info :with-latex)
  1385. (org-element-property :value latex-fragment)))
  1386. ;;;; Line Break
  1387. (defun org-latex-line-break (line-break contents info)
  1388. "Transcode a LINE-BREAK object from Org to LaTeX.
  1389. CONTENTS is nil. INFO is a plist holding contextual information."
  1390. "\\\\\n")
  1391. ;;;; Link
  1392. (defun org-latex--inline-image (link info)
  1393. "Return LaTeX code for an inline image.
  1394. LINK is the link pointing to the inline image. INFO is a plist
  1395. used as a communication channel."
  1396. (let* ((parent (org-export-get-parent-element link))
  1397. (path (let ((raw-path (org-element-property :path link)))
  1398. (if (not (file-name-absolute-p raw-path)) raw-path
  1399. (expand-file-name raw-path))))
  1400. (filetype (file-name-extension path))
  1401. (caption (org-latex--caption/label-string parent info))
  1402. ;; Retrieve latex attributes from the element around.
  1403. (attr (org-export-read-attribute :attr_latex parent))
  1404. (float (let ((float (plist-get attr :float)))
  1405. (cond ((and (not float) (plist-member attr :float)) nil)
  1406. ((string= float "wrap") 'wrap)
  1407. ((string= float "multicolumn") 'multicolumn)
  1408. ((or float (org-element-property :caption parent))
  1409. 'figure))))
  1410. (placement
  1411. (let ((place (plist-get attr :placement)))
  1412. (cond (place (format "%s" place))
  1413. ((eq float 'wrap) "{l}{0.5\\textwidth}")
  1414. ((eq float 'figure)
  1415. (format "[%s]" org-latex-default-figure-position))
  1416. (t ""))))
  1417. (comment-include (if (plist-get attr :comment-include) "%" ""))
  1418. ;; It is possible to specify width and height in the
  1419. ;; ATTR_LATEX line, and also via default variables.
  1420. (width (cond ((plist-get attr :width))
  1421. ((plist-get attr :height) "")
  1422. ((eq float 'wrap) "0.48\\textwidth")
  1423. (t org-latex-image-default-width)))
  1424. (height (cond ((plist-get attr :height))
  1425. ((or (plist-get attr :width)
  1426. (memq float '(figure wrap))) "")
  1427. (t org-latex-image-default-height)))
  1428. (options (let ((opt (or (plist-get attr :options)
  1429. org-latex-image-default-option)))
  1430. (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
  1431. (match-string 1 opt))))
  1432. image-code)
  1433. (if (member filetype '("tikz" "pgf"))
  1434. ;; For tikz images:
  1435. ;; - use \input to read in image file.
  1436. ;; - if options are present, wrap in a tikzpicture environment.
  1437. ;; - if width or height are present, use \resizebox to change
  1438. ;; the image size.
  1439. (progn
  1440. (setq image-code (format "\\input{%s}" path))
  1441. (when (org-string-nw-p options)
  1442. (setq image-code
  1443. (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
  1444. options
  1445. image-code)))
  1446. (when (or (org-string-nw-p width) (org-string-nw-p height))
  1447. (setq image-code (format "\\resizebox{%s}{%s}{%s}"
  1448. (if (org-string-nw-p width) width "!")
  1449. (if (org-string-nw-p height) height "!")
  1450. image-code))))
  1451. ;; For other images:
  1452. ;; - add width and height to options.
  1453. ;; - include the image with \includegraphics.
  1454. (when (org-string-nw-p width)
  1455. (setq options (concat options ",width=" width)))
  1456. (when (org-string-nw-p height)
  1457. (setq options (concat options ",height=" height)))
  1458. (setq image-code
  1459. (format "\\includegraphics%s{%s}"
  1460. (cond ((not (org-string-nw-p options)) "")
  1461. ((= (aref options 0) ?,)
  1462. (format "[%s]"(substring options 1)))
  1463. (t (format "[%s]" options)))
  1464. path)))
  1465. ;; Return proper string, depending on FLOAT.
  1466. (case float
  1467. (wrap (format "\\begin{wrapfigure}%s
  1468. \\centering
  1469. %s%s
  1470. %s\\end{wrapfigure}" placement comment-include image-code caption))
  1471. (multicolumn (format "\\begin{figure*}%s
  1472. \\centering
  1473. %s%s
  1474. %s\\end{figure*}" placement comment-include image-code caption))
  1475. (figure (format "\\begin{figure}%s
  1476. \\centering
  1477. %s%s
  1478. %s\\end{figure}" placement comment-include image-code caption))
  1479. (otherwise image-code))))
  1480. (defun org-latex-link (link desc info)
  1481. "Transcode a LINK object from Org to LaTeX.
  1482. DESC is the description part of the link, or the empty string.
  1483. INFO is a plist holding contextual information. See
  1484. `org-export-data'."
  1485. (let* ((type (org-element-property :type link))
  1486. (raw-path (org-element-property :path link))
  1487. ;; Ensure DESC really exists, or set it to nil.
  1488. (desc (and (not (string= desc "")) desc))
  1489. (imagep (org-export-inline-image-p
  1490. link org-latex-inline-image-rules))
  1491. (path (cond
  1492. ((member type '("http" "https" "ftp" "mailto"))
  1493. (concat type ":" raw-path))
  1494. ((string= type "file")
  1495. (if (not (file-name-absolute-p raw-path)) raw-path
  1496. (concat "file://" (expand-file-name raw-path))))
  1497. (t raw-path)))
  1498. protocol)
  1499. (cond
  1500. ;; Image file.
  1501. (imagep (org-latex--inline-image link info))
  1502. ;; Radio link: Transcode target's contents and use them as link's
  1503. ;; description.
  1504. ((string= type "radio")
  1505. (let ((destination (org-export-resolve-radio-link link info)))
  1506. (when destination
  1507. (format "\\hyperref[%s]{%s}"
  1508. (org-export-solidify-link-text path)
  1509. (org-export-data (org-element-contents destination) info)))))
  1510. ;; Links pointing to a headline: Find destination and build
  1511. ;; appropriate referencing command.
  1512. ((member type '("custom-id" "fuzzy" "id"))
  1513. (let ((destination (if (string= type "fuzzy")
  1514. (org-export-resolve-fuzzy-link link info)
  1515. (org-export-resolve-id-link link info))))
  1516. (case (org-element-type destination)
  1517. ;; Id link points to an external file.
  1518. (plain-text
  1519. (if desc (format "\\href{%s}{%s}" destination desc)
  1520. (format "\\url{%s}" destination)))
  1521. ;; Fuzzy link points nowhere.
  1522. ('nil
  1523. (format org-latex-link-with-unknown-path-format
  1524. (or desc
  1525. (org-export-data
  1526. (org-element-property :raw-link link) info))))
  1527. ;; LINK points to a headline. If headlines are numbered
  1528. ;; and the link has no description, display headline's
  1529. ;; number. Otherwise, display description or headline's
  1530. ;; title.
  1531. (headline
  1532. (let ((label
  1533. (format "sec-%s"
  1534. (mapconcat
  1535. 'number-to-string
  1536. (org-export-get-headline-number destination info)
  1537. "-"))))
  1538. (if (and (plist-get info :section-numbers) (not desc))
  1539. (format "\\ref{%s}" label)
  1540. (format "\\hyperref[%s]{%s}" label
  1541. (or desc
  1542. (org-export-data
  1543. (org-element-property :title destination) info))))))
  1544. ;; Fuzzy link points to a target. Do as above.
  1545. (otherwise
  1546. (let ((path (org-export-solidify-link-text path)))
  1547. (if (not desc) (format "\\ref{%s}" path)
  1548. (format "\\hyperref[%s]{%s}" path desc)))))))
  1549. ;; Coderef: replace link with the reference name or the
  1550. ;; equivalent line number.
  1551. ((string= type "coderef")
  1552. (format (org-export-get-coderef-format path desc)
  1553. (org-export-resolve-coderef path info)))
  1554. ;; Link type is handled by a special function.
  1555. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  1556. (funcall protocol (org-link-unescape path) desc 'latex))
  1557. ;; External link with a description part.
  1558. ((and path desc) (format "\\href{%s}{%s}" path desc))
  1559. ;; External link without a description part.
  1560. (path (format "\\url{%s}" path))
  1561. ;; No path, only description. Try to do something useful.
  1562. (t (format org-latex-link-with-unknown-path-format desc)))))
  1563. ;;;; Paragraph
  1564. (defun org-latex-paragraph (paragraph contents info)
  1565. "Transcode a PARAGRAPH element from Org to LaTeX.
  1566. CONTENTS is the contents of the paragraph, as a string. INFO is
  1567. the plist used as a communication channel."
  1568. contents)
  1569. ;;;; Plain List
  1570. (defun org-latex-plain-list (plain-list contents info)
  1571. "Transcode a PLAIN-LIST element from Org to LaTeX.
  1572. CONTENTS is the contents of the list. INFO is a plist holding
  1573. contextual information."
  1574. (let* ((type (org-element-property :type plain-list))
  1575. (attr (org-export-read-attribute :attr_latex plain-list))
  1576. (latex-type (let ((env (plist-get attr :environment)))
  1577. (cond (env (format "%s" env))
  1578. ((eq type 'ordered) "enumerate")
  1579. ((eq type 'unordered) "itemize")
  1580. ((eq type 'descriptive) "description")))))
  1581. (org-latex--wrap-label
  1582. plain-list
  1583. (format "\\begin{%s}%s\n%s\\end{%s}"
  1584. latex-type
  1585. ;; Put optional arguments, if any inside square brackets
  1586. ;; when necessary.
  1587. (let ((options (format "%s" (or (plist-get attr :options) ""))))
  1588. (cond ((equal options "") "")
  1589. ((string-match "\\`\\[.*\\]\\'" options) options)
  1590. (t (format "[%s]" options))))
  1591. contents
  1592. latex-type))))
  1593. ;;;; Plain Text
  1594. (defun org-latex-plain-text (text info)
  1595. "Transcode a TEXT string from Org to LaTeX.
  1596. TEXT is the string to transcode. INFO is a plist holding
  1597. contextual information."
  1598. (let ((specialp (plist-get info :with-special-strings))
  1599. (output text))
  1600. ;; Protect %, #, &, $, _, { and }.
  1601. (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output)
  1602. (setq output
  1603. (replace-match
  1604. (format "\\%s" (match-string 2 output)) nil t output 2)))
  1605. ;; Protect ^.
  1606. (setq output
  1607. (replace-regexp-in-string
  1608. "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil 2))
  1609. ;; Protect \. If special strings are used, be careful not to
  1610. ;; protect "\" in "\-" constructs.
  1611. (let ((symbols (if specialp "-%$#&{}^_\\" "%$#&{}^_\\")))
  1612. (setq output
  1613. (replace-regexp-in-string
  1614. (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
  1615. "$\\backslash$" output nil t 1)))
  1616. ;; Protect ~.
  1617. (setq output
  1618. (replace-regexp-in-string
  1619. "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t 2))
  1620. ;; Activate smart quotes. Be sure to provide original TEXT string
  1621. ;; since OUTPUT may have been modified.
  1622. (when (plist-get info :with-smart-quotes)
  1623. (setq output (org-export-activate-smart-quotes output :latex info text)))
  1624. ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
  1625. (let ((case-fold-search nil)
  1626. (start 0))
  1627. (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
  1628. (setq output (replace-match
  1629. (format "\\%s{}" (match-string 1 output)) nil t output)
  1630. start (match-end 0))))
  1631. ;; Convert special strings.
  1632. (when specialp
  1633. (setq output
  1634. (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
  1635. ;; Handle break preservation if required.
  1636. (when (plist-get info :preserve-breaks)
  1637. (setq output (replace-regexp-in-string
  1638. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
  1639. ;; Return value.
  1640. output))
  1641. ;;;; Planning
  1642. (defun org-latex-planning (planning contents info)
  1643. "Transcode a PLANNING element from Org to LaTeX.
  1644. CONTENTS is nil. INFO is a plist holding contextual
  1645. information."
  1646. (concat
  1647. "\\noindent"
  1648. (mapconcat
  1649. 'identity
  1650. (delq nil
  1651. (list
  1652. (let ((closed (org-element-property :closed planning)))
  1653. (when closed
  1654. (concat
  1655. (format "\\textbf{%s} " org-closed-string)
  1656. (format org-latex-inactive-timestamp-format
  1657. (org-translate-time
  1658. (org-element-property :raw-value closed))))))
  1659. (let ((deadline (org-element-property :deadline planning)))
  1660. (when deadline
  1661. (concat
  1662. (format "\\textbf{%s} " org-deadline-string)
  1663. (format org-latex-active-timestamp-format
  1664. (org-translate-time
  1665. (org-element-property :raw-value deadline))))))
  1666. (let ((scheduled (org-element-property :scheduled planning)))
  1667. (when scheduled
  1668. (concat
  1669. (format "\\textbf{%s} " org-scheduled-string)
  1670. (format org-latex-active-timestamp-format
  1671. (org-translate-time
  1672. (org-element-property :raw-value scheduled))))))))
  1673. " ")
  1674. "\\\\"))
  1675. ;;;; Quote Block
  1676. (defun org-latex-quote-block (quote-block contents info)
  1677. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  1678. CONTENTS holds the contents of the block. INFO is a plist
  1679. holding contextual information."
  1680. (org-latex--wrap-label
  1681. quote-block
  1682. (format "\\begin{quote}\n%s\\end{quote}" contents)))
  1683. ;;;; Quote Section
  1684. (defun org-latex-quote-section (quote-section contents info)
  1685. "Transcode a QUOTE-SECTION element from Org to LaTeX.
  1686. CONTENTS is nil. INFO is a plist holding contextual information."
  1687. (let ((value (org-remove-indentation
  1688. (org-element-property :value quote-section))))
  1689. (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  1690. ;;;; Radio Target
  1691. (defun org-latex-radio-target (radio-target text info)
  1692. "Transcode a RADIO-TARGET object from Org to LaTeX.
  1693. TEXT is the text of the target. INFO is a plist holding
  1694. contextual information."
  1695. (format "\\label{%s}%s"
  1696. (org-export-solidify-link-text
  1697. (org-element-property :value radio-target))
  1698. text))
  1699. ;;;; Section
  1700. (defun org-latex-section (section contents info)
  1701. "Transcode a SECTION element from Org to LaTeX.
  1702. CONTENTS holds the contents of the section. INFO is a plist
  1703. holding contextual information."
  1704. contents)
  1705. ;;;; Special Block
  1706. (defun org-latex-special-block (special-block contents info)
  1707. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  1708. CONTENTS holds the contents of the block. INFO is a plist
  1709. holding contextual information."
  1710. (let ((type (downcase (org-element-property :type special-block)))
  1711. (opt (org-export-read-attribute :attr_latex special-block :options)))
  1712. (concat (format "\\begin{%s}%s\n" type (or opt ""))
  1713. ;; Insert any label or caption within the block
  1714. ;; (otherwise, a reference pointing to that element will
  1715. ;; count the section instead).
  1716. (org-latex--caption/label-string special-block info)
  1717. contents
  1718. (format "\\end{%s}" type))))
  1719. ;;;; Src Block
  1720. (defun org-latex-src-block (src-block contents info)
  1721. "Transcode a SRC-BLOCK element from Org to LaTeX.
  1722. CONTENTS holds the contents of the item. INFO is a plist holding
  1723. contextual information."
  1724. (when (org-string-nw-p (org-element-property :value src-block))
  1725. (let* ((lang (org-element-property :language src-block))
  1726. (caption (org-element-property :caption src-block))
  1727. (label (org-element-property :name src-block))
  1728. (custom-env (and lang
  1729. (cadr (assq (intern lang)
  1730. org-latex-custom-lang-environments))))
  1731. (num-start (case (org-element-property :number-lines src-block)
  1732. (continued (org-export-get-loc src-block info))
  1733. (new 0)))
  1734. (retain-labels (org-element-property :retain-labels src-block))
  1735. (attributes (org-export-read-attribute :attr_latex src-block))
  1736. (float (plist-get attributes :float)))
  1737. (cond
  1738. ;; Case 1. No source fontification.
  1739. ((not org-latex-listings)
  1740. (let* ((caption-str (org-latex--caption/label-string src-block info))
  1741. (float-env
  1742. (cond ((and (not float) (plist-member attributes :float)) "%s")
  1743. ((string= "multicolumn" float)
  1744. (format "\\begin{figure*}[%s]\n%s%%s\n\\end{figure*}"
  1745. org-latex-default-figure-position
  1746. caption-str))
  1747. ((or caption float)
  1748. (format "\\begin{figure}[H]\n%s%%s\n\\end{figure}"
  1749. caption-str))
  1750. (t "%s"))))
  1751. (format
  1752. float-env
  1753. (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1754. (org-export-format-code-default src-block info))))))
  1755. ;; Case 2. Custom environment.
  1756. (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
  1757. custom-env
  1758. (org-export-format-code-default src-block info)
  1759. custom-env))
  1760. ;; Case 3. Use minted package.
  1761. ((eq org-latex-listings 'minted)
  1762. (let* ((caption-str (org-latex--caption/label-string src-block info))
  1763. (float-env
  1764. (cond ((and (not float) (plist-member attributes :float)) "%s")
  1765. ((string= "multicolumn" float)
  1766. (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
  1767. caption-str))
  1768. ((or caption float)
  1769. (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
  1770. caption-str))
  1771. (t "%s")))
  1772. (body
  1773. (format
  1774. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  1775. ;; Options.
  1776. (org-latex--make-option-string
  1777. (if (or (not num-start)
  1778. (assoc "linenos" org-latex-minted-options))
  1779. org-latex-minted-options
  1780. (append
  1781. `(("linenos")
  1782. ("firstnumber" ,(number-to-string (1+ num-start))))
  1783. org-latex-minted-options)))
  1784. ;; Language.
  1785. (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
  1786. ;; Source code.
  1787. (let* ((code-info (org-export-unravel-code src-block))
  1788. (max-width
  1789. (apply 'max
  1790. (mapcar 'length
  1791. (org-split-string (car code-info)
  1792. "\n")))))
  1793. (org-export-format-code
  1794. (car code-info)
  1795. (lambda (loc num ref)
  1796. (concat
  1797. loc
  1798. (when ref
  1799. ;; Ensure references are flushed to the right,
  1800. ;; separated with 6 spaces from the widest line
  1801. ;; of code.
  1802. (concat (make-string (+ (- max-width (length loc)) 6)
  1803. ?\s)
  1804. (format "(%s)" ref)))))
  1805. nil (and retain-labels (cdr code-info)))))))
  1806. ;; Return value.
  1807. (format float-env body)))
  1808. ;; Case 4. Use listings package.
  1809. (t
  1810. (let ((lst-lang
  1811. (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
  1812. (caption-str
  1813. (when caption
  1814. (let ((main (org-export-get-caption src-block))
  1815. (secondary (org-export-get-caption src-block t)))
  1816. (if (not secondary)
  1817. (format "{%s}" (org-export-data main info))
  1818. (format "{[%s]%s}"
  1819. (org-export-data secondary info)
  1820. (org-export-data main info)))))))
  1821. (concat
  1822. ;; Options.
  1823. (format
  1824. "\\lstset{%s}\n"
  1825. (org-latex--make-option-string
  1826. (append
  1827. org-latex-listings-options
  1828. (cond
  1829. ((and (not float) (plist-member attributes :float)) nil)
  1830. ((string= "multicolumn" float) '(("float" "*")))
  1831. ((and float (not (assoc "float" org-latex-listings-options)))
  1832. `(("float" ,org-latex-default-figure-position))))
  1833. `(("language" ,lst-lang))
  1834. (when label `(("label" ,label)))
  1835. (when caption-str `(("caption" ,caption-str)))
  1836. (cond ((assoc "numbers" org-latex-listings-options) nil)
  1837. ((not num-start) '(("numbers" "none")))
  1838. ((zerop num-start) '(("numbers" "left")))
  1839. (t `(("numbers" "left")
  1840. ("firstnumber"
  1841. ,(number-to-string (1+ num-start)))))))))
  1842. ;; Source code.
  1843. (format
  1844. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  1845. (let* ((code-info (org-export-unravel-code src-block))
  1846. (max-width
  1847. (apply 'max
  1848. (mapcar 'length
  1849. (org-split-string (car code-info) "\n")))))
  1850. (org-export-format-code
  1851. (car code-info)
  1852. (lambda (loc num ref)
  1853. (concat
  1854. loc
  1855. (when ref
  1856. ;; Ensure references are flushed to the right,
  1857. ;; separated with 6 spaces from the widest line of
  1858. ;; code
  1859. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1860. (format "(%s)" ref)))))
  1861. nil (and retain-labels (cdr code-info))))))))))))
  1862. ;;;; Statistics Cookie
  1863. (defun org-latex-statistics-cookie (statistics-cookie contents info)
  1864. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  1865. CONTENTS is nil. INFO is a plist holding contextual information."
  1866. (replace-regexp-in-string
  1867. "%" "\\%" (org-element-property :value statistics-cookie) nil t))
  1868. ;;;; Strike-Through
  1869. (defun org-latex-strike-through (strike-through contents info)
  1870. "Transcode STRIKE-THROUGH from Org to LaTeX.
  1871. CONTENTS is the text with strike-through markup. INFO is a plist
  1872. holding contextual information."
  1873. (org-latex--text-markup contents 'strike-through))
  1874. ;;;; Subscript
  1875. (defun org-latex--script-size (object info)
  1876. "Transcode a subscript or superscript object.
  1877. OBJECT is an Org object. INFO is a plist used as a communication
  1878. channel."
  1879. (let ((in-script-p
  1880. ;; Non-nil if object is already in a sub/superscript.
  1881. (let ((parent object))
  1882. (catch 'exit
  1883. (while (setq parent (org-export-get-parent parent))
  1884. (let ((type (org-element-type parent)))
  1885. (cond ((memq type '(subscript superscript))
  1886. (throw 'exit t))
  1887. ((memq type org-element-all-elements)
  1888. (throw 'exit nil))))))))
  1889. (type (org-element-type object))
  1890. (output ""))
  1891. (org-element-map (org-element-contents object)
  1892. (cons 'plain-text org-element-all-objects)
  1893. (lambda (obj)
  1894. (case (org-element-type obj)
  1895. ((entity latex-fragment)
  1896. (let ((data (org-trim (org-export-data obj info))))
  1897. (string-match
  1898. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  1899. data)
  1900. (setq output
  1901. (concat output
  1902. (match-string 1 data)
  1903. (let ((blank (org-element-property :post-blank obj)))
  1904. (and blank (> blank 0) "\\ "))))))
  1905. (plain-text
  1906. (setq output
  1907. (format "%s\\text{%s}" output (org-export-data obj info))))
  1908. (otherwise
  1909. (setq output
  1910. (concat output
  1911. (org-export-data obj info)
  1912. (let ((blank (org-element-property :post-blank obj)))
  1913. (and blank (> blank 0) "\\ ")))))))
  1914. info nil org-element-recursive-objects)
  1915. ;; Result. Do not wrap into math mode if already in a subscript
  1916. ;; or superscript. Do not wrap into curly brackets if OUTPUT is
  1917. ;; a single character. Also merge consecutive subscript and
  1918. ;; superscript into the same math snippet.
  1919. (concat (and (not in-script-p)
  1920. (let ((prev (org-export-get-previous-element object info)))
  1921. (or (not prev)
  1922. (not (eq (org-element-type prev)
  1923. (if (eq type 'subscript) 'superscript
  1924. 'subscript)))
  1925. (let ((blank (org-element-property :post-blank prev)))
  1926. (and blank (> blank 0)))))
  1927. "$")
  1928. (if (eq (org-element-type object) 'subscript) "_" "^")
  1929. (and (> (length output) 1) "{")
  1930. output
  1931. (and (> (length output) 1) "}")
  1932. (and (not in-script-p)
  1933. (or (let ((blank (org-element-property :post-blank object)))
  1934. (and blank (> blank 0)))
  1935. (not (eq (org-element-type
  1936. (org-export-get-next-element object info))
  1937. (if (eq type 'subscript) 'superscript
  1938. 'subscript))))
  1939. "$"))))
  1940. (defun org-latex-subscript (subscript contents info)
  1941. "Transcode a SUBSCRIPT object from Org to LaTeX.
  1942. CONTENTS is the contents of the object. INFO is a plist holding
  1943. contextual information."
  1944. (org-latex--script-size subscript info))
  1945. ;;;; Superscript
  1946. (defun org-latex-superscript (superscript contents info)
  1947. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  1948. CONTENTS is the contents of the object. INFO is a plist holding
  1949. contextual information."
  1950. (org-latex--script-size superscript info))
  1951. ;;;; Table
  1952. ;;
  1953. ;; `org-latex-table' is the entry point for table transcoding. It
  1954. ;; takes care of tables with a "verbatim" mode. Otherwise, it
  1955. ;; delegates the job to either `org-latex--table.el-table',
  1956. ;; `org-latex--org-table' or `org-latex--math-table' functions,
  1957. ;; depending of the type of the table and the mode requested.
  1958. ;;
  1959. ;; `org-latex--align-string' is a subroutine used to build alignment
  1960. ;; string for Org tables.
  1961. (defun org-latex-table (table contents info)
  1962. "Transcode a TABLE element from Org to LaTeX.
  1963. CONTENTS is the contents of the table. INFO is a plist holding
  1964. contextual information."
  1965. (if (eq (org-element-property :type table) 'table.el)
  1966. ;; "table.el" table. Convert it using appropriate tools.
  1967. (org-latex--table.el-table table info)
  1968. (let ((type (or (org-export-read-attribute :attr_latex table :mode)
  1969. org-latex-default-table-mode)))
  1970. (cond
  1971. ;; Case 1: Verbatim table.
  1972. ((string= type "verbatim")
  1973. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  1974. ;; Re-create table, without affiliated keywords.
  1975. (org-trim (org-element-interpret-data
  1976. `(table nil ,@(org-element-contents table))))))
  1977. ;; Case 2: Matrix.
  1978. ((or (string= type "math") (string= type "inline-math"))
  1979. (org-latex--math-table table info))
  1980. ;; Case 3: Standard table.
  1981. (t (concat (org-latex--org-table table contents info)
  1982. ;; When there are footnote references within the
  1983. ;; table, insert their definition just after it.
  1984. (org-latex--delayed-footnotes-definitions table info)))))))
  1985. (defun org-latex--align-string (table info)
  1986. "Return an appropriate LaTeX alignment string.
  1987. TABLE is the considered table. INFO is a plist used as
  1988. a communication channel."
  1989. (or (org-export-read-attribute :attr_latex table :align)
  1990. (let (align)
  1991. ;; Extract column groups and alignment from first (non-rule)
  1992. ;; row.
  1993. (org-element-map
  1994. (org-element-map table 'table-row
  1995. (lambda (row)
  1996. (and (eq (org-element-property :type row) 'standard) row))
  1997. info 'first-match)
  1998. 'table-cell
  1999. (lambda (cell)
  2000. (let ((borders (org-export-table-cell-borders cell info)))
  2001. ;; Check left border for the first cell only.
  2002. (when (and (memq 'left borders) (not align))
  2003. (push "|" align))
  2004. (push (case (org-export-table-cell-alignment cell info)
  2005. (left "l")
  2006. (right "r")
  2007. (center "c"))
  2008. align)
  2009. (when (memq 'right borders) (push "|" align))))
  2010. info)
  2011. (apply 'concat (nreverse align)))))
  2012. (defun org-latex--org-table (table contents info)
  2013. "Return appropriate LaTeX code for an Org table.
  2014. TABLE is the table type element to transcode. CONTENTS is its
  2015. contents, as a string. INFO is a plist used as a communication
  2016. channel.
  2017. This function assumes TABLE has `org' as its `:type' property and
  2018. `table' as its `:mode' attribute."
  2019. (let* ((caption (org-latex--caption/label-string table info))
  2020. (attr (org-export-read-attribute :attr_latex table))
  2021. ;; Determine alignment string.
  2022. (alignment (org-latex--align-string table info))
  2023. ;; Determine environment for the table: longtable, tabular...
  2024. (table-env (or (plist-get attr :environment)
  2025. org-latex-default-table-environment))
  2026. ;; If table is a float, determine environment: table, table*
  2027. ;; or sidewaystable.
  2028. (float-env (unless (member table-env '("longtable" "longtabu"))
  2029. (let ((float (plist-get attr :float)))
  2030. (cond
  2031. ((and (not float) (plist-member attr :float)) nil)
  2032. ((string= float "sidewaystable") "sidewaystable")
  2033. ((string= float "multicolumn") "table*")
  2034. ((or float (org-element-property :caption table))
  2035. "table")))))
  2036. ;; Extract others display options.
  2037. (fontsize (let ((font (plist-get attr :font)))
  2038. (and font (concat font "\n"))))
  2039. (width (plist-get attr :width))
  2040. (spreadp (plist-get attr :spread))
  2041. (placement (or (plist-get attr :placement)
  2042. (format "[%s]" org-latex-default-figure-position)))
  2043. (centerp (if (plist-member attr :center) (plist-get attr :center)
  2044. org-latex-tables-centered)))
  2045. ;; Prepare the final format string for the table.
  2046. (cond
  2047. ;; Longtable.
  2048. ((equal "longtable" table-env)
  2049. (concat (and fontsize (concat "{" fontsize))
  2050. (format "\\begin{longtable}{%s}\n" alignment)
  2051. (and org-latex-table-caption-above
  2052. (org-string-nw-p caption)
  2053. (concat caption "\\\\\n"))
  2054. contents
  2055. (and (not org-latex-table-caption-above)
  2056. (org-string-nw-p caption)
  2057. (concat caption "\\\\\n"))
  2058. "\\end{longtable}\n"
  2059. (and fontsize "}")))
  2060. ;; Longtabu
  2061. ((equal "longtabu" table-env)
  2062. (concat (and fontsize (concat "{" fontsize))
  2063. (format "\\begin{longtabu}%s{%s}\n"
  2064. (if width
  2065. (format " %s %s "
  2066. (if spreadp "spread" "to") width) "")
  2067. alignment)
  2068. (and org-latex-table-caption-above
  2069. (org-string-nw-p caption)
  2070. (concat caption "\\\\\n"))
  2071. contents
  2072. (and (not org-latex-table-caption-above)
  2073. (org-string-nw-p caption)
  2074. (concat caption "\\\\\n"))
  2075. "\\end{longtabu}\n"
  2076. (and fontsize "}")))
  2077. ;; Others.
  2078. (t (concat (cond
  2079. (float-env
  2080. (concat (format "\\begin{%s}%s\n" float-env placement)
  2081. (if org-latex-table-caption-above caption "")
  2082. (when centerp "\\centering\n")
  2083. fontsize))
  2084. (centerp (concat "\\begin{center}\n" fontsize))
  2085. (fontsize (concat "{" fontsize)))
  2086. (cond ((equal "tabu" table-env)
  2087. (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
  2088. (if width (format
  2089. (if spreadp " spread %s " " to %s ")
  2090. width) "")
  2091. alignment
  2092. contents))
  2093. (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  2094. table-env
  2095. (if width (format "{%s}" width) "")
  2096. alignment
  2097. contents
  2098. table-env)))
  2099. (cond
  2100. (float-env
  2101. (concat (if org-latex-table-caption-above "" caption)
  2102. (format "\n\\end{%s}" float-env)))
  2103. (centerp "\n\\end{center}")
  2104. (fontsize "}")))))))
  2105. (defun org-latex--table.el-table (table info)
  2106. "Return appropriate LaTeX code for a table.el table.
  2107. TABLE is the table type element to transcode. INFO is a plist
  2108. used as a communication channel.
  2109. This function assumes TABLE has `table.el' as its `:type'
  2110. property."
  2111. (require 'table)
  2112. ;; Ensure "*org-export-table*" buffer is empty.
  2113. (with-current-buffer (get-buffer-create "*org-export-table*")
  2114. (erase-buffer))
  2115. (let ((output (with-temp-buffer
  2116. (insert (org-element-property :value table))
  2117. (goto-char 1)
  2118. (re-search-forward "^[ \t]*|[^|]" nil t)
  2119. (table-generate-source 'latex "*org-export-table*")
  2120. (with-current-buffer "*org-export-table*"
  2121. (org-trim (buffer-string))))))
  2122. (kill-buffer (get-buffer "*org-export-table*"))
  2123. ;; Remove left out comments.
  2124. (while (string-match "^%.*\n" output)
  2125. (setq output (replace-match "" t t output)))
  2126. (let ((attr (org-export-read-attribute :attr_latex table)))
  2127. (when (plist-get attr :rmlines)
  2128. ;; When the "rmlines" attribute is provided, remove all hlines
  2129. ;; but the the one separating heading from the table body.
  2130. (let ((n 0) (pos 0))
  2131. (while (and (< (length output) pos)
  2132. (setq pos (string-match "^\\\\hline\n?" output pos)))
  2133. (incf n)
  2134. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  2135. (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
  2136. org-latex-tables-centered)))
  2137. (if (not centerp) output
  2138. (format "\\begin{center}\n%s\n\\end{center}" output))))))
  2139. (defun org-latex--math-table (table info)
  2140. "Return appropriate LaTeX code for a matrix.
  2141. TABLE is the table type element to transcode. INFO is a plist
  2142. used as a communication channel.
  2143. This function assumes TABLE has `org' as its `:type' property and
  2144. `inline-math' or `math' as its `:mode' attribute.."
  2145. (let* ((caption (org-latex--caption/label-string table info))
  2146. (attr (org-export-read-attribute :attr_latex table))
  2147. (inlinep (equal (plist-get attr :mode) "inline-math"))
  2148. (env (or (plist-get attr :environment)
  2149. org-latex-default-table-environment))
  2150. (contents
  2151. (mapconcat
  2152. (lambda (row)
  2153. ;; Ignore horizontal rules.
  2154. (when (eq (org-element-property :type row) 'standard)
  2155. ;; Return each cell unmodified.
  2156. (concat
  2157. (mapconcat
  2158. (lambda (cell)
  2159. (substring (org-element-interpret-data cell) 0 -1))
  2160. (org-element-map row 'table-cell 'identity info) "&")
  2161. (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
  2162. "\n")))
  2163. (org-element-map table 'table-row 'identity info) ""))
  2164. ;; Variables related to math clusters (contiguous math tables
  2165. ;; of the same type).
  2166. (mode (org-export-read-attribute :attr_latex table :mode))
  2167. (prev (org-export-get-previous-element table info))
  2168. (next (org-export-get-next-element table info))
  2169. (same-mode-p
  2170. (lambda (table)
  2171. ;; Non-nil when TABLE has the same mode as current table.
  2172. (string= (or (org-export-read-attribute :attr_latex table :mode)
  2173. org-latex-default-table-mode)
  2174. mode))))
  2175. (concat
  2176. ;; Opening string. If TABLE is in the middle of a table cluster,
  2177. ;; do not insert any.
  2178. (cond ((and prev
  2179. (eq (org-element-type prev) 'table)
  2180. (memq (org-element-property :post-blank prev) '(0 nil))
  2181. (funcall same-mode-p prev))
  2182. nil)
  2183. (inlinep "\\(")
  2184. ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
  2185. (t "\\["))
  2186. ;; Prefix.
  2187. (or (plist-get attr :math-prefix) "")
  2188. ;; Environment. Also treat special cases.
  2189. (cond ((equal env "array")
  2190. (let ((align (org-latex--align-string table info)))
  2191. (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
  2192. ((assoc env org-latex-table-matrix-macros)
  2193. (format "\\%s%s{\n%s}"
  2194. env
  2195. (or (plist-get attr :math-arguments) "")
  2196. contents))
  2197. (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
  2198. ;; Suffix.
  2199. (or (plist-get attr :math-suffix) "")
  2200. ;; Closing string. If TABLE is in the middle of a table cluster,
  2201. ;; do not insert any. If it closes such a cluster, be sure to
  2202. ;; close the cluster with a string matching the opening string.
  2203. (cond ((and next
  2204. (eq (org-element-type next) 'table)
  2205. (memq (org-element-property :post-blank table) '(0 nil))
  2206. (funcall same-mode-p next))
  2207. nil)
  2208. (inlinep "\\)")
  2209. ;; Find cluster beginning to know which environment to use.
  2210. ((let ((cluster-beg table) prev)
  2211. (while (and (setq prev (org-export-get-previous-element
  2212. cluster-beg info))
  2213. (memq (org-element-property :post-blank prev)
  2214. '(0 nil))
  2215. (funcall same-mode-p prev))
  2216. (setq cluster-beg prev))
  2217. (and (or (org-element-property :caption cluster-beg)
  2218. (org-element-property :name cluster-beg))
  2219. "\n\\end{equation}")))
  2220. (t "\\]")))))
  2221. ;;;; Table Cell
  2222. (defun org-latex-table-cell (table-cell contents info)
  2223. "Transcode a TABLE-CELL element from Org to LaTeX.
  2224. CONTENTS is the cell contents. INFO is a plist used as
  2225. a communication channel."
  2226. (concat (if (and contents
  2227. org-latex-table-scientific-notation
  2228. (string-match orgtbl-exp-regexp contents))
  2229. ;; Use appropriate format string for scientific
  2230. ;; notation.
  2231. (format org-latex-table-scientific-notation
  2232. (match-string 1 contents)
  2233. (match-string 2 contents))
  2234. contents)
  2235. (when (org-export-get-next-element table-cell info) " & ")))
  2236. ;;;; Table Row
  2237. (defun org-latex-table-row (table-row contents info)
  2238. "Transcode a TABLE-ROW element from Org to LaTeX.
  2239. CONTENTS is the contents of the row. INFO is a plist used as
  2240. a communication channel."
  2241. ;; Rules are ignored since table separators are deduced from
  2242. ;; borders of the current row.
  2243. (when (eq (org-element-property :type table-row) 'standard)
  2244. (let* ((attr (org-export-read-attribute :attr_latex
  2245. (org-export-get-parent table-row)))
  2246. (longtablep (member (or (plist-get attr :environment)
  2247. org-latex-default-table-environment)
  2248. '("longtable" "longtabu")))
  2249. (booktabsp (if (plist-member attr :booktabs)
  2250. (plist-get attr :booktabs)
  2251. org-latex-tables-booktabs))
  2252. ;; TABLE-ROW's borders are extracted from its first cell.
  2253. (borders (org-export-table-cell-borders
  2254. (car (org-element-contents table-row)) info)))
  2255. (concat
  2256. ;; When BOOKTABS are activated enforce top-rule even when no
  2257. ;; hline was specifically marked.
  2258. (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
  2259. ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
  2260. contents "\\\\\n"
  2261. (cond
  2262. ;; Special case for long tables. Define header and footers.
  2263. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  2264. (format "%s
  2265. \\endhead
  2266. %s\\multicolumn{%d}{r}{Continued on next page} \\\\
  2267. \\endfoot
  2268. \\endlastfoot"
  2269. (if booktabsp "\\midrule" "\\hline")
  2270. (if booktabsp "\\midrule" "\\hline")
  2271. ;; Number of columns.
  2272. (cdr (org-export-table-dimensions
  2273. (org-export-get-parent-table table-row) info))))
  2274. ;; When BOOKTABS are activated enforce bottom rule even when
  2275. ;; no hline was specifically marked.
  2276. ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
  2277. ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
  2278. ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
  2279. ;;;; Target
  2280. (defun org-latex-target (target contents info)
  2281. "Transcode a TARGET object from Org to LaTeX.
  2282. CONTENTS is nil. INFO is a plist holding contextual
  2283. information."
  2284. (format "\\label{%s}"
  2285. (org-export-solidify-link-text (org-element-property :value target))))
  2286. ;;;; Timestamp
  2287. (defun org-latex-timestamp (timestamp contents info)
  2288. "Transcode a TIMESTAMP object from Org to LaTeX.
  2289. CONTENTS is nil. INFO is a plist holding contextual
  2290. information."
  2291. (let ((value (org-latex-plain-text
  2292. (org-timestamp-translate timestamp) info)))
  2293. (case (org-element-property :type timestamp)
  2294. ((active active-range) (format org-latex-active-timestamp-format value))
  2295. ((inactive inactive-range)
  2296. (format org-latex-inactive-timestamp-format value))
  2297. (otherwise (format org-latex-diary-timestamp-format value)))))
  2298. ;;;; Underline
  2299. (defun org-latex-underline (underline contents info)
  2300. "Transcode UNDERLINE from Org to LaTeX.
  2301. CONTENTS is the text with underline markup. INFO is a plist
  2302. holding contextual information."
  2303. (org-latex--text-markup contents 'underline))
  2304. ;;;; Verbatim
  2305. (defun org-latex-verbatim (verbatim contents info)
  2306. "Transcode a VERBATIM object from Org to LaTeX.
  2307. CONTENTS is nil. INFO is a plist used as a communication
  2308. channel."
  2309. (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
  2310. ;;;; Verse Block
  2311. (defun org-latex-verse-block (verse-block contents info)
  2312. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  2313. CONTENTS is verse block contents. INFO is a plist holding
  2314. contextual information."
  2315. (org-latex--wrap-label
  2316. verse-block
  2317. ;; In a verse environment, add a line break to each newline
  2318. ;; character and change each white space at beginning of a line
  2319. ;; into a space of 1 em. Also change each blank line with
  2320. ;; a vertical space of 1 em.
  2321. (progn
  2322. (setq contents (replace-regexp-in-string
  2323. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  2324. (replace-regexp-in-string
  2325. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
  2326. (while (string-match "^[ \t]+" contents)
  2327. (let ((new-str (format "\\hspace*{%dem}"
  2328. (length (match-string 0 contents)))))
  2329. (setq contents (replace-match new-str nil t contents))))
  2330. (format "\\begin{verse}\n%s\\end{verse}" contents))))
  2331. ;;; End-user functions
  2332. ;;;###autoload
  2333. (defun org-latex-export-as-latex
  2334. (&optional async subtreep visible-only body-only ext-plist)
  2335. "Export current buffer as a LaTeX buffer.
  2336. If narrowing is active in the current buffer, only export its
  2337. narrowed part.
  2338. If a region is active, export that region.
  2339. A non-nil optional argument ASYNC means the process should happen
  2340. asynchronously. The resulting buffer should be accessible
  2341. through the `org-export-stack' interface.
  2342. When optional argument SUBTREEP is non-nil, export the sub-tree
  2343. at point, extracting information from the headline properties
  2344. first.
  2345. When optional argument VISIBLE-ONLY is non-nil, don't export
  2346. contents of hidden elements.
  2347. When optional argument BODY-ONLY is non-nil, only write code
  2348. between \"\\begin{document}\" and \"\\end{document}\".
  2349. EXT-PLIST, when provided, is a property list with external
  2350. parameters overriding Org default settings, but still inferior to
  2351. file-local settings.
  2352. Export is done in a buffer named \"*Org LATEX Export*\", which
  2353. will be displayed when `org-export-show-temporary-export-buffer'
  2354. is non-nil."
  2355. (interactive)
  2356. (if async
  2357. (org-export-async-start
  2358. (lambda (output)
  2359. (with-current-buffer (get-buffer-create "*Org LATEX Export*")
  2360. (erase-buffer)
  2361. (insert output)
  2362. (goto-char (point-min))
  2363. (LaTeX-mode)
  2364. (org-export-add-to-stack (current-buffer) 'latex)))
  2365. `(org-export-as 'latex ,subtreep ,visible-only ,body-only
  2366. ',ext-plist))
  2367. (let ((outbuf
  2368. (org-export-to-buffer 'latex "*Org LATEX Export*"
  2369. subtreep visible-only body-only ext-plist)))
  2370. (with-current-buffer outbuf (LaTeX-mode))
  2371. (when org-export-show-temporary-export-buffer
  2372. (switch-to-buffer-other-window outbuf)))))
  2373. ;;;###autoload
  2374. (defun org-latex-convert-region-to-latex ()
  2375. "Assume the current region has org-mode syntax, and convert it to LaTeX.
  2376. This can be used in any buffer. For example, you can write an
  2377. itemized list in org-mode syntax in an LaTeX buffer and use this
  2378. command to convert it."
  2379. (interactive)
  2380. (org-export-replace-region-by 'latex))
  2381. ;;;###autoload
  2382. (defun org-latex-export-to-latex
  2383. (&optional async subtreep visible-only body-only ext-plist)
  2384. "Export current buffer to a LaTeX file.
  2385. If narrowing is active in the current buffer, only export its
  2386. narrowed part.
  2387. If a region is active, export that region.
  2388. A non-nil optional argument ASYNC means the process should happen
  2389. asynchronously. The resulting file should be accessible through
  2390. the `org-export-stack' interface.
  2391. When optional argument SUBTREEP is non-nil, export the sub-tree
  2392. at point, extracting information from the headline properties
  2393. first.
  2394. When optional argument VISIBLE-ONLY is non-nil, don't export
  2395. contents of hidden elements.
  2396. When optional argument BODY-ONLY is non-nil, only write code
  2397. between \"\\begin{document}\" and \"\\end{document}\".
  2398. EXT-PLIST, when provided, is a property list with external
  2399. parameters overriding Org default settings, but still inferior to
  2400. file-local settings.
  2401. Return output file's name."
  2402. (interactive)
  2403. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  2404. (if async
  2405. (org-export-async-start
  2406. (lambda (f) (org-export-add-to-stack f 'latex))
  2407. `(expand-file-name
  2408. (org-export-to-file
  2409. 'latex ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))
  2410. (org-export-to-file
  2411. 'latex outfile subtreep visible-only body-only ext-plist))))
  2412. ;;;###autoload
  2413. (defun org-latex-export-to-pdf
  2414. (&optional async subtreep visible-only body-only ext-plist)
  2415. "Export current buffer to LaTeX then process through to PDF.
  2416. If narrowing is active in the current buffer, only export its
  2417. narrowed part.
  2418. If a region is active, export that region.
  2419. A non-nil optional argument ASYNC means the process should happen
  2420. asynchronously. The resulting file should be accessible through
  2421. the `org-export-stack' interface.
  2422. When optional argument SUBTREEP is non-nil, export the sub-tree
  2423. at point, extracting information from the headline properties
  2424. first.
  2425. When optional argument VISIBLE-ONLY is non-nil, don't export
  2426. contents of hidden elements.
  2427. When optional argument BODY-ONLY is non-nil, only write code
  2428. between \"\\begin{document}\" and \"\\end{document}\".
  2429. EXT-PLIST, when provided, is a property list with external
  2430. parameters overriding Org default settings, but still inferior to
  2431. file-local settings.
  2432. Return PDF file's name."
  2433. (interactive)
  2434. (if async
  2435. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  2436. (org-export-async-start
  2437. (lambda (f) (org-export-add-to-stack f 'latex))
  2438. `(expand-file-name
  2439. (org-latex-compile
  2440. (org-export-to-file
  2441. 'latex ,outfile ,subtreep ,visible-only ,body-only
  2442. ',ext-plist)))))
  2443. (org-latex-compile
  2444. (org-latex-export-to-latex
  2445. nil subtreep visible-only body-only ext-plist))))
  2446. (defun org-latex-compile (texfile &optional snippet)
  2447. "Compile a TeX file.
  2448. TEXFILE is the name of the file being compiled. Processing is
  2449. done through the command specified in `org-latex-pdf-process'.
  2450. When optional argument SNIPPET is non-nil, TEXFILE is a temporary
  2451. file used to preview a LaTeX snippet. In this case, do not
  2452. create a log buffer and do not bother removing log files.
  2453. Return PDF file name or an error if it couldn't be produced."
  2454. (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
  2455. (full-name (file-truename texfile))
  2456. (out-dir (file-name-directory texfile))
  2457. ;; Properly set working directory for compilation.
  2458. (default-directory (if (file-name-absolute-p texfile)
  2459. (file-name-directory full-name)
  2460. default-directory))
  2461. errors)
  2462. (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
  2463. (save-window-excursion
  2464. (cond
  2465. ;; A function is provided: Apply it.
  2466. ((functionp org-latex-pdf-process)
  2467. (funcall org-latex-pdf-process (shell-quote-argument texfile)))
  2468. ;; A list is provided: Replace %b, %f and %o with appropriate
  2469. ;; values in each command before applying it. Output is
  2470. ;; redirected to "*Org PDF LaTeX Output*" buffer.
  2471. ((consp org-latex-pdf-process)
  2472. (let ((outbuf (and (not snippet)
  2473. (get-buffer-create "*Org PDF LaTeX Output*"))))
  2474. (mapc
  2475. (lambda (command)
  2476. (shell-command
  2477. (replace-regexp-in-string
  2478. "%b" (shell-quote-argument base-name)
  2479. (replace-regexp-in-string
  2480. "%f" (shell-quote-argument full-name)
  2481. (replace-regexp-in-string
  2482. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  2483. outbuf))
  2484. org-latex-pdf-process)
  2485. ;; Collect standard errors from output buffer.
  2486. (setq errors (and (not snippet) (org-latex--collect-errors outbuf)))))
  2487. (t (error "No valid command to process to PDF")))
  2488. (let ((pdffile (concat out-dir base-name ".pdf")))
  2489. ;; Check for process failure. Provide collected errors if
  2490. ;; possible.
  2491. (if (not (file-exists-p pdffile))
  2492. (error (concat (format "PDF file %s wasn't produced" pdffile)
  2493. (when errors (concat ": " errors))))
  2494. ;; Else remove log files, when specified, and signal end of
  2495. ;; process to user, along with any error encountered.
  2496. (when (and (not snippet) org-latex-remove-logfiles)
  2497. (dolist (ext org-latex-logfiles-extensions)
  2498. (let ((file (concat out-dir base-name "." ext)))
  2499. (when (file-exists-p file) (delete-file file)))))
  2500. (message (concat "Process completed"
  2501. (if (not errors) "."
  2502. (concat " with errors: " errors)))))
  2503. ;; Return output file name.
  2504. pdffile))))
  2505. (defun org-latex--collect-errors (buffer)
  2506. "Collect some kind of errors from \"pdflatex\" command output.
  2507. BUFFER is the buffer containing output.
  2508. Return collected error types as a string, or nil if there was
  2509. none."
  2510. (with-current-buffer buffer
  2511. (save-excursion
  2512. (goto-char (point-max))
  2513. (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
  2514. (let ((case-fold-search t)
  2515. (errors ""))
  2516. (dolist (latex-error org-latex-known-errors)
  2517. (when (save-excursion (re-search-forward (car latex-error) nil t))
  2518. (setq errors (concat errors " " (cdr latex-error)))))
  2519. (and (org-string-nw-p errors) (org-trim errors)))))))
  2520. ;;;###autoload
  2521. (defun org-latex-publish-to-latex (plist filename pub-dir)
  2522. "Publish an Org file to LaTeX.
  2523. FILENAME is the filename of the Org file to be published. PLIST
  2524. is the property list for the given project. PUB-DIR is the
  2525. publishing directory.
  2526. Return output file name."
  2527. (org-publish-org-to 'latex filename ".tex" plist pub-dir))
  2528. ;;;###autoload
  2529. (defun org-latex-publish-to-pdf (plist filename pub-dir)
  2530. "Publish an Org file to PDF (via LaTeX).
  2531. FILENAME is the filename of the Org file to be published. PLIST
  2532. is the property list for the given project. PUB-DIR is the
  2533. publishing directory.
  2534. Return output file name."
  2535. ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
  2536. ;; in working directory and then moved to publishing directory.
  2537. (org-publish-attachment
  2538. plist
  2539. (org-latex-compile (org-publish-org-to 'latex filename ".tex" plist))
  2540. pub-dir))
  2541. (provide 'ox-latex)
  2542. ;; Local variables:
  2543. ;; generated-autoload-file: "org-loaddefs.el"
  2544. ;; End:
  2545. ;;; ox-latex.el ends here