ox-latex.el 112 KB

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