ox-latex.el 115 KB

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