ox-latex.el 110 KB

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