ox-latex.el 109 KB

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