ox-latex.el 114 KB

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