ox-latex.el 118 KB

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