ox-latex.el 114 KB

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