ox-latex.el 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096
  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 . "\\uline{%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 and appropriate separator
  887. ;; and use "\\verb" command.
  888. ((eq 'verb fmt)
  889. (let ((separator (org-latex--find-verb-separator text)))
  890. (concat "\\verb" separator text separator)))
  891. ;; Handle the `protectedtexttt' special case: Protect some
  892. ;; special chars and use "\texttt{%s}" format string.
  893. ((eq 'protectedtexttt fmt)
  894. (let ((start 0)
  895. (trans '(("\\" . "\\textbackslash{}")
  896. ("~" . "\\textasciitilde{}")
  897. ("^" . "\\textasciicircum{}")))
  898. (rtn "")
  899. char)
  900. (while (string-match "[\\{}$%&_#~^]" text)
  901. (setq char (match-string 0 text))
  902. (if (> (match-beginning 0) 0)
  903. (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
  904. (setq text (substring text (1+ (match-beginning 0))))
  905. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  906. rtn (concat rtn char)))
  907. (setq text (concat rtn text)
  908. fmt "\\texttt{%s}")
  909. (while (string-match "--" text)
  910. (setq text (replace-match "-{}-" t t text)))
  911. (format fmt text)))
  912. ;; Else use format string.
  913. (t (format fmt text)))))
  914. (defun org-latex--delayed-footnotes-definitions (element info)
  915. "Return footnotes definitions in ELEMENT as a string.
  916. INFO is a plist used as a communication channel.
  917. Footnotes definitions are returned within \"\\footnotetxt{}\"
  918. commands.
  919. This function is used within constructs that don't support
  920. \"\\footnote{}\" command (i.e. an item's tag). In that case,
  921. \"\\footnotemark\" is used within the construct and the function
  922. just outside of it."
  923. (mapconcat
  924. (lambda (ref)
  925. (format
  926. "\\footnotetext[%s]{%s}"
  927. (org-export-get-footnote-number ref info)
  928. (org-trim
  929. (org-export-data
  930. (org-export-get-footnote-definition ref info) info))))
  931. ;; Find every footnote reference in ELEMENT.
  932. (let* (all-refs
  933. search-refs ; For byte-compiler.
  934. (search-refs
  935. (function
  936. (lambda (data)
  937. ;; Return a list of all footnote references never seen
  938. ;; before in DATA.
  939. (org-element-map data 'footnote-reference
  940. (lambda (ref)
  941. (when (org-export-footnote-first-reference-p ref info)
  942. (push ref all-refs)
  943. (when (eq (org-element-property :type ref) 'standard)
  944. (funcall search-refs
  945. (org-export-get-footnote-definition ref info)))))
  946. info)
  947. (reverse all-refs)))))
  948. (funcall search-refs element))
  949. ""))
  950. (defun org-latex--translate (s info)
  951. "Translate string S according to specified language.
  952. INFO is a plist used as a communication channel."
  953. (org-export-translate s :latex info))
  954. ;;; Template
  955. (defun org-latex-template (contents info)
  956. "Return complete document string after LaTeX conversion.
  957. CONTENTS is the transcoded contents string. INFO is a plist
  958. holding export options."
  959. (let ((title (org-export-data (plist-get info :title) info)))
  960. (concat
  961. ;; Time-stamp.
  962. (and (plist-get info :time-stamp-file)
  963. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  964. ;; Document class and packages.
  965. (let* ((class (plist-get info :latex-class))
  966. (class-options (plist-get info :latex-class-options))
  967. (header (nth 1 (assoc class org-latex-classes)))
  968. (document-class-string
  969. (and (stringp header)
  970. (if (not class-options) header
  971. (replace-regexp-in-string
  972. "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
  973. class-options header t nil 1)))))
  974. (if (not document-class-string)
  975. (user-error "Unknown LaTeX class `%s'" class)
  976. (org-latex-guess-babel-language
  977. (org-latex-guess-inputenc
  978. (org-element-normalize-string
  979. (org-splice-latex-header
  980. document-class-string
  981. org-latex-default-packages-alist
  982. org-latex-packages-alist nil
  983. (concat (org-element-normalize-string
  984. (plist-get info :latex-header))
  985. (plist-get info :latex-header-extra)))))
  986. info)))
  987. ;; Possibly limit depth for headline numbering.
  988. (let ((sec-num (plist-get info :section-numbers)))
  989. (when (integerp sec-num)
  990. (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
  991. ;; Author.
  992. (let ((author (and (plist-get info :with-author)
  993. (let ((auth (plist-get info :author)))
  994. (and auth (org-export-data auth info)))))
  995. (email (and (plist-get info :with-email)
  996. (org-export-data (plist-get info :email) info))))
  997. (cond ((and author email (not (string= "" email)))
  998. (format "\\author{%s\\thanks{%s}}\n" author email))
  999. ((or author email) (format "\\author{%s}\n" (or author email)))))
  1000. ;; Date.
  1001. (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
  1002. (format "\\date{%s}\n" (org-export-data date info)))
  1003. ;; Title
  1004. (format "\\title{%s}\n" title)
  1005. ;; Hyperref options.
  1006. (format-spec (plist-get info :latex-hyperref)
  1007. (format-spec-make
  1008. ?k (or (plist-get info :keywords) "")
  1009. ?d (or (plist-get info :description)"")
  1010. ?c (if (plist-get info :with-creator)
  1011. (plist-get info :creator)
  1012. "")))
  1013. ;; Document start.
  1014. "\\begin{document}\n\n"
  1015. ;; Title command.
  1016. (org-element-normalize-string
  1017. (cond ((string= "" title) nil)
  1018. ((not (stringp org-latex-title-command)) nil)
  1019. ((string-match "\\(?:[^%]\\|^\\)%s"
  1020. org-latex-title-command)
  1021. (format org-latex-title-command title))
  1022. (t org-latex-title-command)))
  1023. ;; Table of contents.
  1024. (let ((depth (plist-get info :with-toc)))
  1025. (when depth
  1026. (concat (when (wholenump depth)
  1027. (format "\\setcounter{tocdepth}{%d}\n" depth))
  1028. org-latex-toc-command)))
  1029. ;; Document's body.
  1030. contents
  1031. ;; Creator.
  1032. (let ((creator-info (plist-get info :with-creator)))
  1033. (cond
  1034. ((not creator-info) "")
  1035. ((eq creator-info 'comment)
  1036. (format "%% %s\n" (plist-get info :creator)))
  1037. (t (concat (plist-get info :creator) "\n"))))
  1038. ;; Document end.
  1039. "\\end{document}")))
  1040. ;;; Transcode Functions
  1041. ;;;; Bold
  1042. (defun org-latex-bold (bold contents info)
  1043. "Transcode BOLD from Org to LaTeX.
  1044. CONTENTS is the text with bold markup. INFO is a plist holding
  1045. contextual information."
  1046. (org-latex--text-markup contents 'bold))
  1047. ;;;; Center Block
  1048. (defun org-latex-center-block (center-block contents info)
  1049. "Transcode a CENTER-BLOCK element from Org to LaTeX.
  1050. CONTENTS holds the contents of the center block. INFO is a plist
  1051. holding contextual information."
  1052. (org-latex--wrap-label
  1053. center-block
  1054. (format "\\begin{center}\n%s\\end{center}" contents)))
  1055. ;;;; Clock
  1056. (defun org-latex-clock (clock contents info)
  1057. "Transcode a CLOCK element from Org to LaTeX.
  1058. CONTENTS is nil. INFO is a plist holding contextual
  1059. information."
  1060. (concat
  1061. "\\noindent"
  1062. (format "\\textbf{%s} " org-clock-string)
  1063. (format org-latex-inactive-timestamp-format
  1064. (concat (org-translate-time
  1065. (org-element-property :raw-value
  1066. (org-element-property :value clock)))
  1067. (let ((time (org-element-property :duration clock)))
  1068. (and time (format " (%s)" time)))))
  1069. "\\\\"))
  1070. ;;;; Code
  1071. (defun org-latex-code (code contents info)
  1072. "Transcode a CODE object from Org to LaTeX.
  1073. CONTENTS is nil. INFO is a plist used as a communication
  1074. channel."
  1075. (org-latex--text-markup (org-element-property :value code) 'code))
  1076. ;;;; Drawer
  1077. (defun org-latex-drawer (drawer contents info)
  1078. "Transcode a DRAWER element from Org to LaTeX.
  1079. CONTENTS holds the contents of the block. INFO is a plist
  1080. holding contextual information."
  1081. (let* ((name (org-element-property :drawer-name drawer))
  1082. (output (funcall org-latex-format-drawer-function
  1083. name contents)))
  1084. (org-latex--wrap-label drawer output)))
  1085. ;;;; Dynamic Block
  1086. (defun org-latex-dynamic-block (dynamic-block contents info)
  1087. "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
  1088. CONTENTS holds the contents of the block. INFO is a plist
  1089. holding contextual information. See `org-export-data'."
  1090. (org-latex--wrap-label dynamic-block contents))
  1091. ;;;; Entity
  1092. (defun org-latex-entity (entity contents info)
  1093. "Transcode an ENTITY object from Org to LaTeX.
  1094. CONTENTS are the definition itself. INFO is a plist holding
  1095. contextual information."
  1096. (org-element-property :latex entity))
  1097. ;;;; Example Block
  1098. (defun org-latex-example-block (example-block contents info)
  1099. "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
  1100. CONTENTS is nil. INFO is a plist holding contextual
  1101. information."
  1102. (when (org-string-nw-p (org-element-property :value example-block))
  1103. (org-latex--wrap-label
  1104. example-block
  1105. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1106. (org-export-format-code-default example-block info)))))
  1107. ;;;; Export Block
  1108. (defun org-latex-export-block (export-block contents info)
  1109. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  1110. CONTENTS is nil. INFO is a plist holding contextual information."
  1111. (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
  1112. (org-remove-indentation (org-element-property :value export-block))))
  1113. ;;;; Export Snippet
  1114. (defun org-latex-export-snippet (export-snippet contents info)
  1115. "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
  1116. CONTENTS is nil. INFO is a plist holding contextual information."
  1117. (when (eq (org-export-snippet-backend export-snippet) 'latex)
  1118. (org-element-property :value export-snippet)))
  1119. ;;;; Fixed Width
  1120. (defun org-latex-fixed-width (fixed-width contents info)
  1121. "Transcode a FIXED-WIDTH element from Org to LaTeX.
  1122. CONTENTS is nil. INFO is a plist holding contextual information."
  1123. (org-latex--wrap-label
  1124. fixed-width
  1125. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1126. (org-remove-indentation
  1127. (org-element-property :value fixed-width)))))
  1128. ;;;; Footnote Reference
  1129. (defun org-latex-footnote-reference (footnote-reference contents info)
  1130. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  1131. CONTENTS is nil. INFO is a plist holding contextual information."
  1132. (concat
  1133. ;; Insert separator between two footnotes in a row.
  1134. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1135. (when (eq (org-element-type prev) 'footnote-reference)
  1136. org-latex-footnote-separator))
  1137. (cond
  1138. ;; Use \footnotemark if the footnote has already been defined.
  1139. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1140. (format "\\footnotemark[%s]{}"
  1141. (org-export-get-footnote-number footnote-reference info)))
  1142. ;; Use \footnotemark if reference is within another footnote
  1143. ;; reference, footnote definition or table cell.
  1144. ((loop for parent in (org-export-get-genealogy footnote-reference)
  1145. thereis (memq (org-element-type parent)
  1146. '(footnote-reference footnote-definition table-cell)))
  1147. "\\footnotemark")
  1148. ;; Otherwise, define it with \footnote command.
  1149. (t
  1150. (let ((def (org-export-get-footnote-definition footnote-reference info)))
  1151. (concat
  1152. (format "\\footnote{%s}" (org-trim (org-export-data def info)))
  1153. ;; Retrieve all footnote references within the footnote and
  1154. ;; add their definition after it, since LaTeX doesn't support
  1155. ;; them inside.
  1156. (org-latex--delayed-footnotes-definitions def info)))))))
  1157. ;;;; Headline
  1158. (defun org-latex-headline (headline contents info)
  1159. "Transcode a HEADLINE element from Org to LaTeX.
  1160. CONTENTS holds the contents of the headline. INFO is a plist
  1161. holding contextual information."
  1162. (unless (org-element-property :footnote-section-p headline)
  1163. (let* ((class (plist-get info :latex-class))
  1164. (level (org-export-get-relative-level headline info))
  1165. (numberedp (org-export-numbered-headline-p headline info))
  1166. (class-sectioning (assoc class org-latex-classes))
  1167. ;; Section formatting will set two placeholders: one for
  1168. ;; the title and the other for the contents.
  1169. (section-fmt
  1170. (let ((sec (if (functionp (nth 2 class-sectioning))
  1171. (funcall (nth 2 class-sectioning) level numberedp)
  1172. (nth (1+ level) class-sectioning))))
  1173. (cond
  1174. ;; No section available for that LEVEL.
  1175. ((not sec) nil)
  1176. ;; Section format directly returned by a function. Add
  1177. ;; placeholder for contents.
  1178. ((stringp sec) (concat sec "\n%s"))
  1179. ;; (numbered-section . unnumbered-section)
  1180. ((not (consp (cdr sec)))
  1181. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  1182. ;; (numbered-open numbered-close)
  1183. ((= (length sec) 2)
  1184. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  1185. ;; (num-in num-out no-num-in no-num-out)
  1186. ((= (length sec) 4)
  1187. (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
  1188. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  1189. (text (org-export-data (org-element-property :title headline) info))
  1190. (todo
  1191. (and (plist-get info :with-todo-keywords)
  1192. (let ((todo (org-element-property :todo-keyword headline)))
  1193. (and todo (org-export-data todo info)))))
  1194. (todo-type (and todo (org-element-property :todo-type headline)))
  1195. (tags (and (plist-get info :with-tags)
  1196. (org-export-get-tags headline info)))
  1197. (priority (and (plist-get info :with-priority)
  1198. (org-element-property :priority headline)))
  1199. ;; Create the headline text along with a no-tag version.
  1200. ;; The latter is required to remove tags from toc.
  1201. (full-text (funcall org-latex-format-headline-function
  1202. todo todo-type priority text tags))
  1203. ;; Associate \label to the headline for internal links.
  1204. (headline-label
  1205. (let ((custom-label
  1206. (and (plist-get info :latex-custom-id-labels)
  1207. (org-element-property :CUSTOM_ID headline))))
  1208. (if custom-label (format "\\label{%s}\n" custom-label)
  1209. (format "\\label{sec-%s}\n"
  1210. (mapconcat
  1211. #'number-to-string
  1212. (org-export-get-headline-number headline info)
  1213. "-")))))
  1214. (pre-blanks
  1215. (make-string (org-element-property :pre-blank headline) 10)))
  1216. (if (or (not section-fmt) (org-export-low-level-p headline info))
  1217. ;; This is a deep sub-tree: export it as a list item. Also
  1218. ;; export as items headlines for which no section format has
  1219. ;; been found.
  1220. (let ((low-level-body
  1221. (concat
  1222. ;; If headline is the first sibling, start a list.
  1223. (when (org-export-first-sibling-p headline info)
  1224. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  1225. ;; Itemize headline
  1226. "\\item"
  1227. (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
  1228. "\\relax")
  1229. " " full-text "\n"
  1230. headline-label
  1231. pre-blanks
  1232. contents)))
  1233. ;; If headline is not the last sibling simply return
  1234. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
  1235. ;; any blank line.
  1236. (if (not (org-export-last-sibling-p headline info)) low-level-body
  1237. (replace-regexp-in-string
  1238. "[ \t\n]*\\'"
  1239. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  1240. low-level-body)))
  1241. ;; This is a standard headline. Export it as a section. Add
  1242. ;; an alternative heading when possible, and when this is not
  1243. ;; identical to the usual heading.
  1244. (let ((opt-title
  1245. (funcall org-latex-format-headline-function
  1246. todo todo-type priority
  1247. (org-export-data
  1248. (org-export-get-alt-title headline info) info)
  1249. (and (eq (plist-get info :with-tags) t) tags))))
  1250. (if (and numberedp opt-title
  1251. (not (equal opt-title full-text))
  1252. (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
  1253. (format (replace-match "\\1[%s]" nil nil section-fmt 1)
  1254. ;; Replace square brackets with parenthesis
  1255. ;; since square brackets are not supported in
  1256. ;; optional arguments.
  1257. (replace-regexp-in-string
  1258. "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
  1259. full-text
  1260. (concat headline-label pre-blanks contents))
  1261. ;; Impossible to add an alternative heading. Fallback to
  1262. ;; regular sectioning format string.
  1263. (format section-fmt full-text
  1264. (concat headline-label pre-blanks contents))))))))
  1265. (defun org-latex-format-headline-default-function
  1266. (todo todo-type priority text tags)
  1267. "Default format function for a headline.
  1268. See `org-latex-format-headline-function' for details."
  1269. (concat
  1270. (and todo (format "{\\bfseries\\sffamily %s} " todo))
  1271. (and priority (format "\\framebox{\\#%c} " priority))
  1272. text
  1273. (and tags
  1274. (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags ":")))))
  1275. ;;;; Horizontal Rule
  1276. (defun org-latex-horizontal-rule (horizontal-rule contents info)
  1277. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  1278. CONTENTS is nil. INFO is a plist holding contextual information."
  1279. (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
  1280. (prev (org-export-get-previous-element horizontal-rule info)))
  1281. (concat
  1282. ;; Make sure the rule doesn't start at the end of the current
  1283. ;; line by separating it with a blank line from previous element.
  1284. (when (and prev
  1285. (let ((prev-blank (org-element-property :post-blank prev)))
  1286. (or (not prev-blank) (zerop prev-blank))))
  1287. "\n")
  1288. (org-latex--wrap-label
  1289. horizontal-rule
  1290. (format "\\rule{%s}{%s}"
  1291. (or (plist-get attr :width) "\\linewidth")
  1292. (or (plist-get attr :thickness) "0.5pt"))))))
  1293. ;;;; Inline Src Block
  1294. (defun org-latex-inline-src-block (inline-src-block contents info)
  1295. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  1296. CONTENTS holds the contents of the item. INFO is a plist holding
  1297. contextual information."
  1298. (let* ((code (org-element-property :value inline-src-block))
  1299. (separator (org-latex--find-verb-separator code)))
  1300. (cond
  1301. ;; Do not use a special package: transcode it verbatim.
  1302. ((not org-latex-listings)
  1303. (concat "\\verb" separator code separator))
  1304. ;; Use minted package.
  1305. ((eq org-latex-listings 'minted)
  1306. (let* ((org-lang (org-element-property :language inline-src-block))
  1307. (mint-lang (or (cadr (assq (intern org-lang)
  1308. org-latex-minted-langs))
  1309. org-lang))
  1310. (options (org-latex--make-option-string
  1311. org-latex-minted-options)))
  1312. (concat (format "\\mint%s{%s}"
  1313. (if (string= options "") "" (format "[%s]" options))
  1314. mint-lang)
  1315. separator code separator)))
  1316. ;; Use listings package.
  1317. (t
  1318. ;; Maybe translate language's name.
  1319. (let* ((org-lang (org-element-property :language inline-src-block))
  1320. (lst-lang (or (cadr (assq (intern org-lang)
  1321. org-latex-listings-langs))
  1322. org-lang))
  1323. (options (org-latex--make-option-string
  1324. (append org-latex-listings-options
  1325. `(("language" ,lst-lang))))))
  1326. (concat (format "\\lstinline[%s]" options)
  1327. separator code separator))))))
  1328. ;;;; Inlinetask
  1329. (defun org-latex-inlinetask (inlinetask contents info)
  1330. "Transcode an INLINETASK element from Org to LaTeX.
  1331. CONTENTS holds the contents of the block. INFO is a plist
  1332. holding contextual information."
  1333. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  1334. (todo (and (plist-get info :with-todo-keywords)
  1335. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1336. (and todo (org-export-data todo info)))))
  1337. (todo-type (org-element-property :todo-type inlinetask))
  1338. (tags (and (plist-get info :with-tags)
  1339. (org-export-get-tags inlinetask info)))
  1340. (priority (and (plist-get info :with-priority)
  1341. (org-element-property :priority inlinetask))))
  1342. ;; If `org-latex-format-inlinetask-function' is provided, call it
  1343. ;; with appropriate arguments.
  1344. (if (not (eq org-latex-format-inlinetask-function 'ignore))
  1345. (funcall org-latex-format-inlinetask-function
  1346. todo todo-type priority title tags contents)
  1347. ;; Otherwise, use a default template.
  1348. (org-latex--wrap-label
  1349. inlinetask
  1350. (let ((full-title
  1351. (concat
  1352. (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1353. (when priority (format "\\framebox{\\#%c} " priority))
  1354. title
  1355. (when tags (format "\\hfill{}\\textsc{:%s:}"
  1356. (mapconcat 'identity tags ":"))))))
  1357. (format (concat "\\begin{center}\n"
  1358. "\\fbox{\n"
  1359. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1360. "%s\n\n"
  1361. "\\rule[.8em]{\\textwidth}{2pt}\n\n"
  1362. "%s"
  1363. "\\end{minipage}\n"
  1364. "}\n"
  1365. "\\end{center}")
  1366. full-title contents))))))
  1367. ;;;; Italic
  1368. (defun org-latex-italic (italic contents info)
  1369. "Transcode ITALIC from Org to LaTeX.
  1370. CONTENTS is the text with italic markup. INFO is a plist holding
  1371. contextual information."
  1372. (org-latex--text-markup contents 'italic))
  1373. ;;;; Item
  1374. (defun org-latex-item (item contents info)
  1375. "Transcode an ITEM element from Org to LaTeX.
  1376. CONTENTS holds the contents of the item. INFO is a plist holding
  1377. contextual information."
  1378. (let* ((counter
  1379. (let ((count (org-element-property :counter item))
  1380. (level
  1381. ;; Determine level of current item to determine the
  1382. ;; correct LaTeX counter to use (enumi, enumii...).
  1383. (let ((parent item) (level 0))
  1384. (while (memq (org-element-type
  1385. (setq parent (org-export-get-parent parent)))
  1386. '(plain-list item))
  1387. (when (and (eq (org-element-type parent) 'plain-list)
  1388. (eq (org-element-property :type parent)
  1389. 'ordered))
  1390. (incf level)))
  1391. level)))
  1392. (and count
  1393. (< level 5)
  1394. (format "\\setcounter{enum%s}{%s}\n"
  1395. (nth (1- level) '("i" "ii" "iii" "iv"))
  1396. (1- count)))))
  1397. (checkbox (case (org-element-property :checkbox item)
  1398. (on "$\\boxtimes$ ")
  1399. (off "$\\square$ ")
  1400. (trans "$\\boxminus$ ")))
  1401. (tag (let ((tag (org-element-property :tag item)))
  1402. ;; Check-boxes must belong to the tag.
  1403. (and tag (format "[{%s}] "
  1404. (concat checkbox
  1405. (org-export-data tag info)))))))
  1406. (concat counter
  1407. "\\item"
  1408. (cond
  1409. (tag)
  1410. (checkbox (concat " " checkbox))
  1411. ;; Without a tag or a check-box, if CONTENTS starts with
  1412. ;; an opening square bracket, add "\relax" to "\item",
  1413. ;; unless the brackets comes from an initial export
  1414. ;; snippet (i.e. it is inserted willingly by the user).
  1415. ((and contents
  1416. (org-string-match-p "\\`[ \t]*\\[" contents)
  1417. (not (let ((e (car (org-element-contents item))))
  1418. (and (eq (org-element-type e) 'paragraph)
  1419. (let ((o (car (org-element-contents e))))
  1420. (and (eq (org-element-type o) 'export-snippet)
  1421. (eq (org-export-snippet-backend o)
  1422. 'latex)))))))
  1423. "\\relax ")
  1424. (t " "))
  1425. (and contents (org-trim contents))
  1426. ;; If there are footnotes references in tag, be sure to
  1427. ;; add their definition at the end of the item. This
  1428. ;; workaround is necessary since "\footnote{}" command is
  1429. ;; not supported in tags.
  1430. (and tag
  1431. (org-latex--delayed-footnotes-definitions
  1432. (org-element-property :tag item) info)))))
  1433. ;;;; Keyword
  1434. (defun org-latex-keyword (keyword contents info)
  1435. "Transcode a KEYWORD element from Org to LaTeX.
  1436. CONTENTS is nil. INFO is a plist holding contextual information."
  1437. (let ((key (org-element-property :key keyword))
  1438. (value (org-element-property :value keyword)))
  1439. (cond
  1440. ((string= key "LATEX") value)
  1441. ((string= key "INDEX") (format "\\index{%s}" value))
  1442. ((string= key "TOC")
  1443. (let ((value (downcase value)))
  1444. (cond
  1445. ((string-match "\\<headlines\\>" value)
  1446. (let ((depth (or (and (string-match "[0-9]+" value)
  1447. (string-to-number (match-string 0 value)))
  1448. (plist-get info :with-toc))))
  1449. (concat
  1450. (when (wholenump depth)
  1451. (format "\\setcounter{tocdepth}{%s}\n" depth))
  1452. "\\tableofcontents")))
  1453. ((string= "tables" value) "\\listoftables")
  1454. ((string= "listings" value)
  1455. (cond
  1456. ((eq org-latex-listings 'minted) "\\listoflistings")
  1457. (org-latex-listings "\\lstlistoflistings")
  1458. ;; At the moment, src blocks with a caption are wrapped
  1459. ;; into a figure environment.
  1460. (t "\\listoffigures")))))))))
  1461. ;;;; Latex Environment
  1462. (defun org-latex-latex-environment (latex-environment contents info)
  1463. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1464. CONTENTS is nil. INFO is a plist holding contextual information."
  1465. (when (plist-get info :with-latex)
  1466. (let ((label (org-element-property :name latex-environment))
  1467. (value (org-remove-indentation
  1468. (org-element-property :value latex-environment))))
  1469. (if (not (org-string-nw-p label)) value
  1470. ;; Environment is labeled: label must be within the environment
  1471. ;; (otherwise, a reference pointing to that element will count
  1472. ;; the section instead).
  1473. (with-temp-buffer
  1474. (insert value)
  1475. (goto-char (point-min))
  1476. (forward-line)
  1477. (insert
  1478. (format "\\label{%s}\n" (org-export-solidify-link-text label)))
  1479. (buffer-string))))))
  1480. ;;;; Latex Fragment
  1481. (defun org-latex-latex-fragment (latex-fragment contents info)
  1482. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1483. CONTENTS is nil. INFO is a plist holding contextual information."
  1484. (let ((value (org-element-property :value latex-fragment)))
  1485. ;; Trim math markers since the fragment is enclosed within
  1486. ;; a latex-math-block object anyway.
  1487. (cond ((string-match "\\`\\(\\$\\{1,2\\}\\)\\([^\000]*\\)\\1\\'" value)
  1488. (match-string 2 value))
  1489. ((string-match "\\`\\\\(\\([^\000]*\\)\\\\)\\'" value)
  1490. (match-string 1 value))
  1491. (t value))))
  1492. ;;;; Line Break
  1493. (defun org-latex-line-break (line-break contents info)
  1494. "Transcode a LINE-BREAK object from Org to LaTeX.
  1495. CONTENTS is nil. INFO is a plist holding contextual information."
  1496. "\\\\\n")
  1497. ;;;; Link
  1498. (defun org-latex--inline-image (link info)
  1499. "Return LaTeX code for an inline image.
  1500. LINK is the link pointing to the inline image. INFO is a plist
  1501. used as a communication channel."
  1502. (let* ((parent (org-export-get-parent-element link))
  1503. (path (let ((raw-path (org-element-property :path link)))
  1504. (if (not (file-name-absolute-p raw-path)) raw-path
  1505. (expand-file-name raw-path))))
  1506. (filetype (file-name-extension path))
  1507. (caption (org-latex--caption/label-string parent info))
  1508. ;; Retrieve latex attributes from the element around.
  1509. (attr (org-export-read-attribute :attr_latex parent))
  1510. (float (let ((float (plist-get attr :float)))
  1511. (cond ((and (not float) (plist-member attr :float)) nil)
  1512. ((string= float "wrap") 'wrap)
  1513. ((string= float "multicolumn") 'multicolumn)
  1514. ((or float
  1515. (org-element-property :caption parent)
  1516. (org-string-nw-p (plist-get attr :caption)))
  1517. 'figure))))
  1518. (placement
  1519. (let ((place (plist-get attr :placement)))
  1520. (cond (place (format "%s" place))
  1521. ((eq float 'wrap) "{l}{0.5\\textwidth}")
  1522. ((eq float 'figure)
  1523. (format "[%s]" org-latex-default-figure-position))
  1524. (t ""))))
  1525. (comment-include (if (plist-get attr :comment-include) "%" ""))
  1526. ;; It is possible to specify width and height in the
  1527. ;; ATTR_LATEX line, and also via default variables.
  1528. (width (cond ((plist-get attr :width))
  1529. ((plist-get attr :height) "")
  1530. ((eq float 'wrap) "0.48\\textwidth")
  1531. (t org-latex-image-default-width)))
  1532. (height (cond ((plist-get attr :height))
  1533. ((or (plist-get attr :width)
  1534. (memq float '(figure wrap))) "")
  1535. (t org-latex-image-default-height)))
  1536. (options (let ((opt (or (plist-get attr :options)
  1537. org-latex-image-default-option)))
  1538. (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
  1539. (match-string 1 opt))))
  1540. image-code)
  1541. (if (member filetype '("tikz" "pgf"))
  1542. ;; For tikz images:
  1543. ;; - use \input to read in image file.
  1544. ;; - if options are present, wrap in a tikzpicture environment.
  1545. ;; - if width or height are present, use \resizebox to change
  1546. ;; the image size.
  1547. (progn
  1548. (setq image-code (format "\\input{%s}" path))
  1549. (when (org-string-nw-p options)
  1550. (setq image-code
  1551. (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
  1552. options
  1553. image-code)))
  1554. (when (or (org-string-nw-p width) (org-string-nw-p height))
  1555. (setq image-code (format "\\resizebox{%s}{%s}{%s}"
  1556. (if (org-string-nw-p width) width "!")
  1557. (if (org-string-nw-p height) height "!")
  1558. image-code))))
  1559. ;; For other images:
  1560. ;; - add width and height to options.
  1561. ;; - include the image with \includegraphics.
  1562. (when (org-string-nw-p width)
  1563. (setq options (concat options ",width=" width)))
  1564. (when (org-string-nw-p height)
  1565. (setq options (concat options ",height=" height)))
  1566. (setq image-code
  1567. (format "\\includegraphics%s{%s}"
  1568. (cond ((not (org-string-nw-p options)) "")
  1569. ((= (aref options 0) ?,)
  1570. (format "[%s]"(substring options 1)))
  1571. (t (format "[%s]" options)))
  1572. path))
  1573. (when (equal filetype "svg")
  1574. (setq image-code (replace-regexp-in-string "^\\\\includegraphics"
  1575. "\\includesvg"
  1576. image-code
  1577. nil t))
  1578. (setq image-code (replace-regexp-in-string "\\.svg}"
  1579. "}"
  1580. image-code
  1581. nil t))))
  1582. ;; Return proper string, depending on FLOAT.
  1583. (case float
  1584. (wrap (format "\\begin{wrapfigure}%s
  1585. \\centering
  1586. %s%s
  1587. %s\\end{wrapfigure}" placement comment-include image-code caption))
  1588. (multicolumn (format "\\begin{figure*}%s
  1589. \\centering
  1590. %s%s
  1591. %s\\end{figure*}" placement comment-include image-code caption))
  1592. (figure (format "\\begin{figure}%s
  1593. \\centering
  1594. %s%s
  1595. %s\\end{figure}" placement comment-include image-code caption))
  1596. (otherwise image-code))))
  1597. (defun org-latex-link (link desc info)
  1598. "Transcode a LINK object from Org to LaTeX.
  1599. DESC is the description part of the link, or the empty string.
  1600. INFO is a plist holding contextual information. See
  1601. `org-export-data'."
  1602. (let* ((type (org-element-property :type link))
  1603. (raw-path (org-element-property :path link))
  1604. ;; Ensure DESC really exists, or set it to nil.
  1605. (desc (and (not (string= desc "")) desc))
  1606. (imagep (org-export-inline-image-p
  1607. link org-latex-inline-image-rules))
  1608. (path (cond
  1609. ((member type '("http" "https" "ftp" "mailto"))
  1610. (concat type ":" raw-path))
  1611. ((string= type "file")
  1612. (if (not (file-name-absolute-p raw-path)) raw-path
  1613. (concat "file://" (expand-file-name raw-path))))
  1614. (t raw-path)))
  1615. protocol)
  1616. (cond
  1617. ;; Image file.
  1618. (imagep (org-latex--inline-image link info))
  1619. ;; Radio link: Transcode target's contents and use them as link's
  1620. ;; description.
  1621. ((string= type "radio")
  1622. (let ((destination (org-export-resolve-radio-link link info)))
  1623. (when destination
  1624. (format "\\hyperref[%s]{%s}"
  1625. (org-export-solidify-link-text path)
  1626. (org-export-data (org-element-contents destination) info)))))
  1627. ;; Links pointing to a headline: Find destination and build
  1628. ;; appropriate referencing command.
  1629. ((member type '("custom-id" "fuzzy" "id"))
  1630. (let ((destination (if (string= type "fuzzy")
  1631. (org-export-resolve-fuzzy-link link info)
  1632. (org-export-resolve-id-link link info))))
  1633. (case (org-element-type destination)
  1634. ;; Id link points to an external file.
  1635. (plain-text
  1636. (if desc (format "\\href{%s}{%s}" destination desc)
  1637. (format "\\url{%s}" destination)))
  1638. ;; Fuzzy link points nowhere.
  1639. ('nil
  1640. (format org-latex-link-with-unknown-path-format
  1641. (or desc
  1642. (org-export-data
  1643. (org-element-property :raw-link link) info))))
  1644. ;; LINK points to a headline. If headlines are numbered
  1645. ;; and the link has no description, display headline's
  1646. ;; number. Otherwise, display description or headline's
  1647. ;; title.
  1648. (headline
  1649. (let* ((custom-label
  1650. (and (plist-get info :latex-custom-id-labels)
  1651. (org-element-property :CUSTOM_ID destination)))
  1652. (label
  1653. (or
  1654. custom-label
  1655. (format "sec-%s"
  1656. (mapconcat
  1657. #'number-to-string
  1658. (org-export-get-headline-number destination info)
  1659. "-")))))
  1660. (if (and (plist-get info :section-numbers) (not desc))
  1661. (format "\\ref{%s}" label)
  1662. (format "\\hyperref[%s]{%s}" label
  1663. (or desc
  1664. (org-export-data
  1665. (org-element-property :title destination) info))))))
  1666. ;; Fuzzy link points to a target. Do as above.
  1667. (otherwise
  1668. (let ((path (org-export-solidify-link-text path)))
  1669. (if (not desc) (format "\\ref{%s}" path)
  1670. (format "\\hyperref[%s]{%s}" path desc)))))))
  1671. ;; Coderef: replace link with the reference name or the
  1672. ;; equivalent line number.
  1673. ((string= type "coderef")
  1674. (format (org-export-get-coderef-format path desc)
  1675. (org-export-resolve-coderef path info)))
  1676. ;; Link type is handled by a special function.
  1677. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  1678. (funcall protocol (org-link-unescape path) desc 'latex))
  1679. ;; External link with a description part.
  1680. ((and path desc) (format "\\href{%s}{%s}" path desc))
  1681. ;; External link without a description part.
  1682. (path (format "\\url{%s}" path))
  1683. ;; No path, only description. Try to do something useful.
  1684. (t (format org-latex-link-with-unknown-path-format desc)))))
  1685. ;;;; Node Property
  1686. (defun org-latex-node-property (node-property contents info)
  1687. "Transcode a NODE-PROPERTY element from Org to LaTeX.
  1688. CONTENTS is nil. INFO is a plist holding contextual
  1689. information."
  1690. (format "%s:%s"
  1691. (org-element-property :key node-property)
  1692. (let ((value (org-element-property :value node-property)))
  1693. (if value (concat " " value) ""))))
  1694. ;;;; Paragraph
  1695. (defun org-latex-paragraph (paragraph contents info)
  1696. "Transcode a PARAGRAPH element from Org to LaTeX.
  1697. CONTENTS is the contents of the paragraph, as a string. INFO is
  1698. the plist used as a communication channel."
  1699. contents)
  1700. ;;;; Plain List
  1701. (defun org-latex-plain-list (plain-list contents info)
  1702. "Transcode a PLAIN-LIST element from Org to LaTeX.
  1703. CONTENTS is the contents of the list. INFO is a plist holding
  1704. contextual information."
  1705. (let* ((type (org-element-property :type plain-list))
  1706. (attr (org-export-read-attribute :attr_latex plain-list))
  1707. (latex-type (let ((env (plist-get attr :environment)))
  1708. (cond (env (format "%s" env))
  1709. ((eq type 'ordered) "enumerate")
  1710. ((eq type 'descriptive) "description")
  1711. (t "itemize")))))
  1712. (org-latex--wrap-label
  1713. plain-list
  1714. (format "\\begin{%s}%s\n%s\\end{%s}"
  1715. latex-type
  1716. (or (plist-get attr :options) "")
  1717. contents
  1718. latex-type))))
  1719. ;;;; Plain Text
  1720. (defun org-latex-plain-text (text info)
  1721. "Transcode a TEXT string from Org to LaTeX.
  1722. TEXT is the string to transcode. INFO is a plist holding
  1723. contextual information."
  1724. (let ((specialp (plist-get info :with-special-strings))
  1725. (output text))
  1726. ;; Protect %, #, &, $, _, { and }.
  1727. (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output)
  1728. (setq output
  1729. (replace-match
  1730. (format "\\%s" (match-string 2 output)) nil t output 2)))
  1731. ;; Protect ^.
  1732. (setq output
  1733. (replace-regexp-in-string
  1734. "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil 2))
  1735. ;; Protect \. If special strings are used, be careful not to
  1736. ;; protect "\" in "\-" constructs.
  1737. (let ((symbols (if specialp "-%$#&{}^_\\" "%$#&{}^_\\")))
  1738. (setq output
  1739. (replace-regexp-in-string
  1740. (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
  1741. "$\\backslash$" output nil t 1)))
  1742. ;; Protect ~.
  1743. (setq output
  1744. (replace-regexp-in-string
  1745. "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t 2))
  1746. ;; Activate smart quotes. Be sure to provide original TEXT string
  1747. ;; since OUTPUT may have been modified.
  1748. (when (plist-get info :with-smart-quotes)
  1749. (setq output (org-export-activate-smart-quotes output :latex info text)))
  1750. ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
  1751. (let ((case-fold-search nil)
  1752. (start 0))
  1753. (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
  1754. (setq output (replace-match
  1755. (format "\\%s{}" (match-string 1 output)) nil t output)
  1756. start (match-end 0))))
  1757. ;; Convert special strings.
  1758. (when specialp
  1759. (setq output
  1760. (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
  1761. ;; Handle break preservation if required.
  1762. (when (plist-get info :preserve-breaks)
  1763. (setq output (replace-regexp-in-string
  1764. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
  1765. ;; Return value.
  1766. output))
  1767. ;;;; Planning
  1768. (defun org-latex-planning (planning contents info)
  1769. "Transcode a PLANNING element from Org to LaTeX.
  1770. CONTENTS is nil. INFO is a plist holding contextual
  1771. information."
  1772. (concat
  1773. "\\noindent"
  1774. (mapconcat
  1775. 'identity
  1776. (delq nil
  1777. (list
  1778. (let ((closed (org-element-property :closed planning)))
  1779. (when closed
  1780. (concat
  1781. (format "\\textbf{%s} " org-closed-string)
  1782. (format org-latex-inactive-timestamp-format
  1783. (org-translate-time
  1784. (org-element-property :raw-value closed))))))
  1785. (let ((deadline (org-element-property :deadline planning)))
  1786. (when deadline
  1787. (concat
  1788. (format "\\textbf{%s} " org-deadline-string)
  1789. (format org-latex-active-timestamp-format
  1790. (org-translate-time
  1791. (org-element-property :raw-value deadline))))))
  1792. (let ((scheduled (org-element-property :scheduled planning)))
  1793. (when scheduled
  1794. (concat
  1795. (format "\\textbf{%s} " org-scheduled-string)
  1796. (format org-latex-active-timestamp-format
  1797. (org-translate-time
  1798. (org-element-property :raw-value scheduled))))))))
  1799. " ")
  1800. "\\\\"))
  1801. ;;;; Property Drawer
  1802. (defun org-latex-property-drawer (property-drawer contents info)
  1803. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  1804. CONTENTS holds the contents of the drawer. INFO is a plist
  1805. holding contextual information."
  1806. (and (org-string-nw-p contents)
  1807. (format "\\begin{verbatim}\n%s\\end{verbatim}" contents)))
  1808. ;;;; Pseudo Object: LaTeX Math Block
  1809. (defun org-latex--wrap-latex-math-block (data info)
  1810. "Merge contiguous math objects in a pseudo-object container.
  1811. DATA is a parse tree or a secondary string. INFO is a plist
  1812. containing export options. Modify DATA by side-effect and return it."
  1813. (let ((valid-object-p
  1814. (function
  1815. ;; Non-nil when OBJ can be added to the latex math block.
  1816. (lambda (obj)
  1817. (case (org-element-type obj)
  1818. (entity (org-element-property :latex-math-p obj))
  1819. (latex-fragment
  1820. (let ((value (org-element-property :value obj)))
  1821. (or (org-string-match-p "\\`\\\\([^\000]*\\\\)\\'" value)
  1822. (org-string-match-p "\\`\\$[^\000]*\\$\\'" value))))
  1823. ((subscript superscript) t))))))
  1824. (org-element-map data '(entity latex-fragment subscript superscript)
  1825. (lambda (object)
  1826. ;; Skip objects already wrapped.
  1827. (when (and (not (eq (org-element-type
  1828. (org-element-property :parent object))
  1829. 'latex-math-block))
  1830. (funcall valid-object-p object))
  1831. (let ((math-block (list 'latex-math-block nil))
  1832. (next-elements (org-export-get-next-element object info t))
  1833. (last object))
  1834. ;; Wrap MATH-BLOCK around OBJECT in DATA.
  1835. (org-element-insert-before math-block object)
  1836. (org-element-extract-element object)
  1837. (org-element-adopt-elements math-block object)
  1838. (when (zerop (or (org-element-property :post-blank object) 0))
  1839. ;; MATH-BLOCK swallows consecutive math objects.
  1840. (catch 'exit
  1841. (dolist (next next-elements)
  1842. (if (not (funcall valid-object-p next)) (throw 'exit nil)
  1843. (org-element-extract-element next)
  1844. (org-element-adopt-elements math-block next)
  1845. ;; Eschew the case: \beta$x$ -> \(\betax\).
  1846. (unless (memq (org-element-type next)
  1847. '(subscript superscript))
  1848. (org-element-put-property last :post-blank 1))
  1849. (setq last next)
  1850. (when (> (or (org-element-property :post-blank next) 0) 0)
  1851. (throw 'exit nil))))))
  1852. (org-element-put-property
  1853. math-block :post-blank (org-element-property :post-blank last)))))
  1854. info nil '(subscript superscript latex-math-block) t)
  1855. ;; Return updated DATA.
  1856. data))
  1857. (defun org-latex-math-block-tree-filter (tree backend info)
  1858. (org-latex--wrap-latex-math-block tree info))
  1859. (defun org-latex-math-block-options-filter (info backend)
  1860. (dolist (prop '(:author :date :title) info)
  1861. (plist-put info prop
  1862. (org-latex--wrap-latex-math-block (plist-get info prop) info))))
  1863. (defun org-latex-math-block (math-block contents info)
  1864. "Transcode a MATH-BLOCK object from Org to LaTeX.
  1865. CONTENTS is a string. INFO is a plist used as a communication
  1866. channel."
  1867. (when (org-string-nw-p contents)
  1868. (format "\\(%s\\)" (org-trim contents))))
  1869. ;;;; Quote Block
  1870. (defun org-latex-quote-block (quote-block contents info)
  1871. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  1872. CONTENTS holds the contents of the block. INFO is a plist
  1873. holding contextual information."
  1874. (org-latex--wrap-label
  1875. quote-block
  1876. (format "\\begin{quote}\n%s\\end{quote}" contents)))
  1877. ;;;; Radio Target
  1878. (defun org-latex-radio-target (radio-target text info)
  1879. "Transcode a RADIO-TARGET object from Org to LaTeX.
  1880. TEXT is the text of the target. INFO is a plist holding
  1881. contextual information."
  1882. (format "\\label{%s}%s"
  1883. (org-export-solidify-link-text
  1884. (org-element-property :value radio-target))
  1885. text))
  1886. ;;;; Section
  1887. (defun org-latex-section (section contents info)
  1888. "Transcode a SECTION element from Org to LaTeX.
  1889. CONTENTS holds the contents of the section. INFO is a plist
  1890. holding contextual information."
  1891. contents)
  1892. ;;;; Special Block
  1893. (defun org-latex-special-block (special-block contents info)
  1894. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  1895. CONTENTS holds the contents of the block. INFO is a plist
  1896. holding contextual information."
  1897. (let ((type (downcase (org-element-property :type special-block)))
  1898. (opt (org-export-read-attribute :attr_latex special-block :options)))
  1899. (concat (format "\\begin{%s}%s\n" type (or opt ""))
  1900. ;; Insert any label or caption within the block
  1901. ;; (otherwise, a reference pointing to that element will
  1902. ;; count the section instead).
  1903. (org-latex--caption/label-string special-block info)
  1904. contents
  1905. (format "\\end{%s}" type))))
  1906. ;;;; Src Block
  1907. (defun org-latex-src-block (src-block contents info)
  1908. "Transcode a SRC-BLOCK element from Org to LaTeX.
  1909. CONTENTS holds the contents of the item. INFO is a plist holding
  1910. contextual information."
  1911. (when (org-string-nw-p (org-element-property :value src-block))
  1912. (let* ((lang (org-element-property :language src-block))
  1913. (caption (org-element-property :caption src-block))
  1914. (label (org-element-property :name src-block))
  1915. (custom-env (and lang
  1916. (cadr (assq (intern lang)
  1917. org-latex-custom-lang-environments))))
  1918. (num-start (case (org-element-property :number-lines src-block)
  1919. (continued (org-export-get-loc src-block info))
  1920. (new 0)))
  1921. (retain-labels (org-element-property :retain-labels src-block))
  1922. (attributes (org-export-read-attribute :attr_latex src-block))
  1923. (float (plist-get attributes :float)))
  1924. (cond
  1925. ;; Case 1. No source fontification.
  1926. ((not org-latex-listings)
  1927. (let* ((caption-str (org-latex--caption/label-string src-block info))
  1928. (float-env
  1929. (cond ((and (not float) (plist-member attributes :float)) "%s")
  1930. ((string= "multicolumn" float)
  1931. (format "\\begin{figure*}[%s]\n%%s%s\n\\end{figure*}"
  1932. org-latex-default-figure-position
  1933. caption-str))
  1934. ((or caption float)
  1935. (format "\\begin{figure}[H]\n%%s%s\n\\end{figure}"
  1936. caption-str))
  1937. (t "%s"))))
  1938. (format
  1939. float-env
  1940. (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1941. (org-export-format-code-default src-block info))))))
  1942. ;; Case 2. Custom environment.
  1943. (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
  1944. custom-env
  1945. (org-export-format-code-default src-block info)
  1946. custom-env))
  1947. ;; Case 3. Use minted package.
  1948. ((eq org-latex-listings 'minted)
  1949. (let* ((caption-str (org-latex--caption/label-string src-block info))
  1950. (float-env
  1951. (cond ((and (not float) (plist-member attributes :float)) "%s")
  1952. ((string= "multicolumn" float)
  1953. (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
  1954. caption-str))
  1955. ((or caption float)
  1956. (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
  1957. caption-str))
  1958. (t "%s")))
  1959. (body
  1960. (format
  1961. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  1962. ;; Options.
  1963. (org-latex--make-option-string
  1964. (if (or (not num-start)
  1965. (assoc "linenos" org-latex-minted-options))
  1966. org-latex-minted-options
  1967. (append
  1968. `(("linenos")
  1969. ("firstnumber" ,(number-to-string (1+ num-start))))
  1970. org-latex-minted-options)))
  1971. ;; Language.
  1972. (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
  1973. ;; Source code.
  1974. (let* ((code-info (org-export-unravel-code src-block))
  1975. (max-width
  1976. (apply 'max
  1977. (mapcar 'length
  1978. (org-split-string (car code-info)
  1979. "\n")))))
  1980. (org-export-format-code
  1981. (car code-info)
  1982. (lambda (loc num ref)
  1983. (concat
  1984. loc
  1985. (when ref
  1986. ;; Ensure references are flushed to the right,
  1987. ;; separated with 6 spaces from the widest line
  1988. ;; of code.
  1989. (concat (make-string (+ (- max-width (length loc)) 6)
  1990. ?\s)
  1991. (format "(%s)" ref)))))
  1992. nil (and retain-labels (cdr code-info)))))))
  1993. ;; Return value.
  1994. (format float-env body)))
  1995. ;; Case 4. Use listings package.
  1996. (t
  1997. (let ((lst-lang
  1998. (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
  1999. (caption-str
  2000. (when caption
  2001. (let ((main (org-export-get-caption src-block))
  2002. (secondary (org-export-get-caption src-block t)))
  2003. (if (not secondary)
  2004. (format "{%s}" (org-export-data main info))
  2005. (format "{[%s]%s}"
  2006. (org-export-data secondary info)
  2007. (org-export-data main info)))))))
  2008. (concat
  2009. ;; Options.
  2010. (format
  2011. "\\lstset{%s}\n"
  2012. (org-latex--make-option-string
  2013. (append
  2014. org-latex-listings-options
  2015. (cond
  2016. ((and (not float) (plist-member attributes :float)) nil)
  2017. ((string= "multicolumn" float) '(("float" "*")))
  2018. ((and float (not (assoc "float" org-latex-listings-options)))
  2019. `(("float" ,org-latex-default-figure-position))))
  2020. `(("language" ,lst-lang))
  2021. (when label `(("label" ,label)))
  2022. (when caption-str `(("caption" ,caption-str)))
  2023. (cond ((assoc "numbers" org-latex-listings-options) nil)
  2024. ((not num-start) '(("numbers" "none")))
  2025. ((zerop num-start) '(("numbers" "left")))
  2026. (t `(("numbers" "left")
  2027. ("firstnumber"
  2028. ,(number-to-string (1+ num-start)))))))))
  2029. ;; Source code.
  2030. (format
  2031. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  2032. (let* ((code-info (org-export-unravel-code src-block))
  2033. (max-width
  2034. (apply 'max
  2035. (mapcar 'length
  2036. (org-split-string (car code-info) "\n")))))
  2037. (org-export-format-code
  2038. (car code-info)
  2039. (lambda (loc num ref)
  2040. (concat
  2041. loc
  2042. (when ref
  2043. ;; Ensure references are flushed to the right,
  2044. ;; separated with 6 spaces from the widest line of
  2045. ;; code
  2046. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  2047. (format "(%s)" ref)))))
  2048. nil (and retain-labels (cdr code-info))))))))))))
  2049. ;;;; Statistics Cookie
  2050. (defun org-latex-statistics-cookie (statistics-cookie contents info)
  2051. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  2052. CONTENTS is nil. INFO is a plist holding contextual information."
  2053. (replace-regexp-in-string
  2054. "%" "\\%" (org-element-property :value statistics-cookie) nil t))
  2055. ;;;; Strike-Through
  2056. (defun org-latex-strike-through (strike-through contents info)
  2057. "Transcode STRIKE-THROUGH from Org to LaTeX.
  2058. CONTENTS is the text with strike-through markup. INFO is a plist
  2059. holding contextual information."
  2060. (org-latex--text-markup contents 'strike-through))
  2061. ;;;; Subscript
  2062. (defun org-latex--script-size (object info)
  2063. "Transcode a subscript or superscript object.
  2064. OBJECT is an Org object. INFO is a plist used as a communication
  2065. channel."
  2066. (let ((type (org-element-type object))
  2067. (output ""))
  2068. (org-element-map (org-element-contents object)
  2069. (cons 'plain-text org-element-all-objects)
  2070. (lambda (obj)
  2071. (case (org-element-type obj)
  2072. ((entity latex-fragment)
  2073. (let ((data (org-trim (org-export-data obj info))))
  2074. (string-match
  2075. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  2076. data)
  2077. (setq output
  2078. (concat output
  2079. (match-string 1 data)
  2080. (let ((blank (org-element-property :post-blank obj)))
  2081. (and blank (> blank 0) "\\ "))))))
  2082. (plain-text
  2083. (setq output
  2084. (format "%s\\text{%s}" output (org-export-data obj info))))
  2085. (otherwise
  2086. (setq output
  2087. (concat output
  2088. (org-export-data obj info)
  2089. (let ((blank (org-element-property :post-blank obj)))
  2090. (and blank (> blank 0) "\\ ")))))))
  2091. info nil org-element-recursive-objects)
  2092. ;; Result. Do not wrap into curly brackets if OUTPUT is a single
  2093. ;; character.
  2094. (concat (if (eq (org-element-type object) 'subscript) "_" "^")
  2095. (and (> (length output) 1) "{")
  2096. output
  2097. (and (> (length output) 1) "}"))))
  2098. (defun org-latex-subscript (subscript contents info)
  2099. "Transcode a SUBSCRIPT object from Org to LaTeX.
  2100. CONTENTS is the contents of the object. INFO is a plist holding
  2101. contextual information."
  2102. (org-latex--script-size subscript info))
  2103. ;;;; Superscript
  2104. (defun org-latex-superscript (superscript contents info)
  2105. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  2106. CONTENTS is the contents of the object. INFO is a plist holding
  2107. contextual information."
  2108. (org-latex--script-size superscript info))
  2109. ;;;; Table
  2110. ;;
  2111. ;; `org-latex-table' is the entry point for table transcoding. It
  2112. ;; takes care of tables with a "verbatim" mode. Otherwise, it
  2113. ;; delegates the job to either `org-latex--table.el-table',
  2114. ;; `org-latex--org-table' or `org-latex--math-table' functions,
  2115. ;; depending of the type of the table and the mode requested.
  2116. ;;
  2117. ;; `org-latex--align-string' is a subroutine used to build alignment
  2118. ;; string for Org tables.
  2119. (defun org-latex-table (table contents info)
  2120. "Transcode a TABLE element from Org to LaTeX.
  2121. CONTENTS is the contents of the table. INFO is a plist holding
  2122. contextual information."
  2123. (if (eq (org-element-property :type table) 'table.el)
  2124. ;; "table.el" table. Convert it using appropriate tools.
  2125. (org-latex--table.el-table table info)
  2126. (let ((type (or (org-export-read-attribute :attr_latex table :mode)
  2127. org-latex-default-table-mode)))
  2128. (cond
  2129. ;; Case 1: Verbatim table.
  2130. ((string= type "verbatim")
  2131. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  2132. ;; Re-create table, without affiliated keywords.
  2133. (org-trim (org-element-interpret-data
  2134. `(table nil ,@(org-element-contents table))
  2135. org-latex-pseudo-objects))))
  2136. ;; Case 2: Matrix.
  2137. ((or (string= type "math") (string= type "inline-math"))
  2138. (org-latex--math-table table info))
  2139. ;; Case 3: Standard table.
  2140. (t (concat (org-latex--org-table table contents info)
  2141. ;; When there are footnote references within the
  2142. ;; table, insert their definition just after it.
  2143. (org-latex--delayed-footnotes-definitions table info)))))))
  2144. (defun org-latex--align-string (table info)
  2145. "Return an appropriate LaTeX alignment string.
  2146. TABLE is the considered table. INFO is a plist used as
  2147. a communication channel."
  2148. (or (org-export-read-attribute :attr_latex table :align)
  2149. (let (align)
  2150. ;; Extract column groups and alignment from first (non-rule)
  2151. ;; row.
  2152. (org-element-map
  2153. (org-element-map table 'table-row
  2154. (lambda (row)
  2155. (and (eq (org-element-property :type row) 'standard) row))
  2156. info 'first-match)
  2157. 'table-cell
  2158. (lambda (cell)
  2159. (let ((borders (org-export-table-cell-borders cell info)))
  2160. ;; Check left border for the first cell only.
  2161. (when (and (memq 'left borders) (not align))
  2162. (push "|" align))
  2163. (push (case (org-export-table-cell-alignment cell info)
  2164. (left "l")
  2165. (right "r")
  2166. (center "c"))
  2167. align)
  2168. (when (memq 'right borders) (push "|" align))))
  2169. info)
  2170. (apply 'concat (nreverse align)))))
  2171. (defun org-latex--org-table (table contents info)
  2172. "Return appropriate LaTeX code for an Org table.
  2173. TABLE is the table type element to transcode. CONTENTS is its
  2174. contents, as a string. INFO is a plist used as a communication
  2175. channel.
  2176. This function assumes TABLE has `org' as its `:type' property and
  2177. `table' as its `:mode' attribute."
  2178. (let* ((caption (org-latex--caption/label-string table info))
  2179. (attr (org-export-read-attribute :attr_latex table))
  2180. ;; Determine alignment string.
  2181. (alignment (org-latex--align-string table info))
  2182. ;; Determine environment for the table: longtable, tabular...
  2183. (table-env (or (plist-get attr :environment)
  2184. org-latex-default-table-environment))
  2185. ;; If table is a float, determine environment: table, table*
  2186. ;; or sidewaystable.
  2187. (float-env (unless (member table-env '("longtable" "longtabu"))
  2188. (let ((float (plist-get attr :float)))
  2189. (cond
  2190. ((and (not float) (plist-member attr :float)) nil)
  2191. ((string= float "sidewaystable") "sidewaystable")
  2192. ((string= float "multicolumn") "table*")
  2193. ((or float
  2194. (org-element-property :caption table)
  2195. (org-string-nw-p (plist-get attr :caption)))
  2196. "table")))))
  2197. ;; Extract others display options.
  2198. (fontsize (let ((font (plist-get attr :font)))
  2199. (and font (concat font "\n"))))
  2200. (width (plist-get attr :width))
  2201. (spreadp (plist-get attr :spread))
  2202. (placement (or (plist-get attr :placement)
  2203. (format "[%s]" org-latex-default-figure-position)))
  2204. (centerp (if (plist-member attr :center) (plist-get attr :center)
  2205. org-latex-tables-centered)))
  2206. ;; Prepare the final format string for the table.
  2207. (cond
  2208. ;; Longtable.
  2209. ((equal "longtable" table-env)
  2210. (concat (and fontsize (concat "{" fontsize))
  2211. (format "\\begin{longtable}{%s}\n" alignment)
  2212. (and org-latex-table-caption-above
  2213. (org-string-nw-p caption)
  2214. (concat caption "\\\\\n"))
  2215. contents
  2216. (and (not org-latex-table-caption-above)
  2217. (org-string-nw-p caption)
  2218. (concat caption "\\\\\n"))
  2219. "\\end{longtable}\n"
  2220. (and fontsize "}")))
  2221. ;; Longtabu
  2222. ((equal "longtabu" table-env)
  2223. (concat (and fontsize (concat "{" fontsize))
  2224. (format "\\begin{longtabu}%s{%s}\n"
  2225. (if width
  2226. (format " %s %s "
  2227. (if spreadp "spread" "to") width) "")
  2228. alignment)
  2229. (and org-latex-table-caption-above
  2230. (org-string-nw-p caption)
  2231. (concat caption "\\\\\n"))
  2232. contents
  2233. (and (not org-latex-table-caption-above)
  2234. (org-string-nw-p caption)
  2235. (concat caption "\\\\\n"))
  2236. "\\end{longtabu}\n"
  2237. (and fontsize "}")))
  2238. ;; Others.
  2239. (t (concat (cond
  2240. (float-env
  2241. (concat (format "\\begin{%s}%s\n" float-env placement)
  2242. (if org-latex-table-caption-above caption "")
  2243. (when centerp "\\centering\n")
  2244. fontsize))
  2245. (centerp (concat "\\begin{center}\n" fontsize))
  2246. (fontsize (concat "{" fontsize)))
  2247. (cond ((equal "tabu" table-env)
  2248. (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
  2249. (if width (format
  2250. (if spreadp " spread %s " " to %s ")
  2251. width) "")
  2252. alignment
  2253. contents))
  2254. (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  2255. table-env
  2256. (if width (format "{%s}" width) "")
  2257. alignment
  2258. contents
  2259. table-env)))
  2260. (cond
  2261. (float-env
  2262. (concat (if org-latex-table-caption-above "" caption)
  2263. (format "\n\\end{%s}" float-env)))
  2264. (centerp "\n\\end{center}")
  2265. (fontsize "}")))))))
  2266. (defun org-latex--table.el-table (table info)
  2267. "Return appropriate LaTeX code for a table.el table.
  2268. TABLE is the table type element to transcode. INFO is a plist
  2269. used as a communication channel.
  2270. This function assumes TABLE has `table.el' as its `:type'
  2271. property."
  2272. (require 'table)
  2273. ;; Ensure "*org-export-table*" buffer is empty.
  2274. (with-current-buffer (get-buffer-create "*org-export-table*")
  2275. (erase-buffer))
  2276. (let ((output (with-temp-buffer
  2277. (insert (org-element-property :value table))
  2278. (goto-char 1)
  2279. (re-search-forward "^[ \t]*|[^|]" nil t)
  2280. (table-generate-source 'latex "*org-export-table*")
  2281. (with-current-buffer "*org-export-table*"
  2282. (org-trim (buffer-string))))))
  2283. (kill-buffer (get-buffer "*org-export-table*"))
  2284. ;; Remove left out comments.
  2285. (while (string-match "^%.*\n" output)
  2286. (setq output (replace-match "" t t output)))
  2287. (let ((attr (org-export-read-attribute :attr_latex table)))
  2288. (when (plist-get attr :rmlines)
  2289. ;; When the "rmlines" attribute is provided, remove all hlines
  2290. ;; but the the one separating heading from the table body.
  2291. (let ((n 0) (pos 0))
  2292. (while (and (< (length output) pos)
  2293. (setq pos (string-match "^\\\\hline\n?" output pos)))
  2294. (incf n)
  2295. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  2296. (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
  2297. org-latex-tables-centered)))
  2298. (if (not centerp) output
  2299. (format "\\begin{center}\n%s\n\\end{center}" output))))))
  2300. (defun org-latex--math-table (table info)
  2301. "Return appropriate LaTeX code for a matrix.
  2302. TABLE is the table type element to transcode. INFO is a plist
  2303. used as a communication channel.
  2304. This function assumes TABLE has `org' as its `:type' property and
  2305. `inline-math' or `math' as its `:mode' attribute.."
  2306. (let* ((caption (org-latex--caption/label-string table info))
  2307. (attr (org-export-read-attribute :attr_latex table))
  2308. (inlinep (equal (plist-get attr :mode) "inline-math"))
  2309. (env (or (plist-get attr :environment)
  2310. org-latex-default-table-environment))
  2311. (contents
  2312. (mapconcat
  2313. (lambda (row)
  2314. ;; Ignore horizontal rules.
  2315. (when (eq (org-element-property :type row) 'standard)
  2316. ;; Return each cell unmodified.
  2317. (concat
  2318. (mapconcat
  2319. (lambda (cell)
  2320. (substring
  2321. (org-element-interpret-data cell org-latex-pseudo-objects)
  2322. 0 -1))
  2323. (org-element-map row 'table-cell 'identity info) "&")
  2324. (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
  2325. "\n")))
  2326. (org-element-map table 'table-row 'identity info) ""))
  2327. ;; Variables related to math clusters (contiguous math tables
  2328. ;; of the same type).
  2329. (mode (org-export-read-attribute :attr_latex table :mode))
  2330. (prev (org-export-get-previous-element table info))
  2331. (next (org-export-get-next-element table info))
  2332. (same-mode-p
  2333. (lambda (table)
  2334. ;; Non-nil when TABLE has the same mode as current table.
  2335. (string= (or (org-export-read-attribute :attr_latex table :mode)
  2336. org-latex-default-table-mode)
  2337. mode))))
  2338. (concat
  2339. ;; Opening string. If TABLE is in the middle of a table cluster,
  2340. ;; do not insert any.
  2341. (cond ((and prev
  2342. (eq (org-element-type prev) 'table)
  2343. (memq (org-element-property :post-blank prev) '(0 nil))
  2344. (funcall same-mode-p prev))
  2345. nil)
  2346. (inlinep "\\(")
  2347. ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
  2348. (t "\\["))
  2349. ;; Prefix.
  2350. (or (plist-get attr :math-prefix) "")
  2351. ;; Environment. Also treat special cases.
  2352. (cond ((equal env "array")
  2353. (let ((align (org-latex--align-string table info)))
  2354. (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
  2355. ((assoc env org-latex-table-matrix-macros)
  2356. (format "\\%s%s{\n%s}"
  2357. env
  2358. (or (plist-get attr :math-arguments) "")
  2359. contents))
  2360. (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
  2361. ;; Suffix.
  2362. (or (plist-get attr :math-suffix) "")
  2363. ;; Closing string. If TABLE is in the middle of a table cluster,
  2364. ;; do not insert any. If it closes such a cluster, be sure to
  2365. ;; close the cluster with a string matching the opening string.
  2366. (cond ((and next
  2367. (eq (org-element-type next) 'table)
  2368. (memq (org-element-property :post-blank table) '(0 nil))
  2369. (funcall same-mode-p next))
  2370. nil)
  2371. (inlinep "\\)")
  2372. ;; Find cluster beginning to know which environment to use.
  2373. ((let ((cluster-beg table) prev)
  2374. (while (and (setq prev (org-export-get-previous-element
  2375. cluster-beg info))
  2376. (memq (org-element-property :post-blank prev)
  2377. '(0 nil))
  2378. (funcall same-mode-p prev))
  2379. (setq cluster-beg prev))
  2380. (and (or (org-element-property :caption cluster-beg)
  2381. (org-element-property :name cluster-beg))
  2382. "\n\\end{equation}")))
  2383. (t "\\]")))))
  2384. ;;;; Table Cell
  2385. (defun org-latex-table-cell (table-cell contents info)
  2386. "Transcode a TABLE-CELL element from Org to LaTeX.
  2387. CONTENTS is the cell contents. INFO is a plist used as
  2388. a communication channel."
  2389. (concat (if (and contents
  2390. org-latex-table-scientific-notation
  2391. (string-match orgtbl-exp-regexp contents))
  2392. ;; Use appropriate format string for scientific
  2393. ;; notation.
  2394. (format org-latex-table-scientific-notation
  2395. (match-string 1 contents)
  2396. (match-string 2 contents))
  2397. contents)
  2398. (when (org-export-get-next-element table-cell info) " & ")))
  2399. ;;;; Table Row
  2400. (defun org-latex-table-row (table-row contents info)
  2401. "Transcode a TABLE-ROW element from Org to LaTeX.
  2402. CONTENTS is the contents of the row. INFO is a plist used as
  2403. a communication channel."
  2404. ;; Rules are ignored since table separators are deduced from
  2405. ;; borders of the current row.
  2406. (when (eq (org-element-property :type table-row) 'standard)
  2407. (let* ((attr (org-export-read-attribute :attr_latex
  2408. (org-export-get-parent table-row)))
  2409. (longtablep (member (or (plist-get attr :environment)
  2410. org-latex-default-table-environment)
  2411. '("longtable" "longtabu")))
  2412. (booktabsp (if (plist-member attr :booktabs)
  2413. (plist-get attr :booktabs)
  2414. org-latex-tables-booktabs))
  2415. ;; TABLE-ROW's borders are extracted from its first cell.
  2416. (borders (org-export-table-cell-borders
  2417. (car (org-element-contents table-row)) info)))
  2418. (concat
  2419. ;; When BOOKTABS are activated enforce top-rule even when no
  2420. ;; hline was specifically marked.
  2421. (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
  2422. ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
  2423. contents "\\\\\n"
  2424. (cond
  2425. ;; Special case for long tables. Define header and footers.
  2426. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  2427. (format "%s
  2428. \\endfirsthead
  2429. \\multicolumn{%d}{l}{%s} \\\\
  2430. %s
  2431. %s \\\\\n
  2432. %s
  2433. \\endhead
  2434. %s\\multicolumn{%d}{r}{%s} \\\\
  2435. \\endfoot
  2436. \\endlastfoot"
  2437. (if booktabsp "\\midrule" "\\hline")
  2438. (cdr (org-export-table-dimensions
  2439. (org-export-get-parent-table table-row) info))
  2440. (org-latex--translate "Continued from previous page" info)
  2441. (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
  2442. ((and (memq 'top borders)
  2443. (memq 'above borders)) "\\hline\n")
  2444. (t ""))
  2445. contents
  2446. (if booktabsp "\\midrule" "\\hline")
  2447. (if booktabsp "\\midrule" "\\hline")
  2448. ;; Number of columns.
  2449. (cdr (org-export-table-dimensions
  2450. (org-export-get-parent-table table-row) info))
  2451. (org-latex--translate "Continued on next page" info)))
  2452. ;; When BOOKTABS are activated enforce bottom rule even when
  2453. ;; no hline was specifically marked.
  2454. ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
  2455. ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
  2456. ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
  2457. ;;;; Target
  2458. (defun org-latex-target (target contents info)
  2459. "Transcode a TARGET object from Org to LaTeX.
  2460. CONTENTS is nil. INFO is a plist holding contextual
  2461. information."
  2462. (format "\\label{%s}"
  2463. (org-export-solidify-link-text (org-element-property :value target))))
  2464. ;;;; Timestamp
  2465. (defun org-latex-timestamp (timestamp contents info)
  2466. "Transcode a TIMESTAMP object from Org to LaTeX.
  2467. CONTENTS is nil. INFO is a plist holding contextual
  2468. information."
  2469. (let ((value (org-latex-plain-text
  2470. (org-timestamp-translate timestamp) info)))
  2471. (case (org-element-property :type timestamp)
  2472. ((active active-range) (format org-latex-active-timestamp-format value))
  2473. ((inactive inactive-range)
  2474. (format org-latex-inactive-timestamp-format value))
  2475. (otherwise (format org-latex-diary-timestamp-format value)))))
  2476. ;;;; Underline
  2477. (defun org-latex-underline (underline contents info)
  2478. "Transcode UNDERLINE from Org to LaTeX.
  2479. CONTENTS is the text with underline markup. INFO is a plist
  2480. holding contextual information."
  2481. (org-latex--text-markup contents 'underline))
  2482. ;;;; Verbatim
  2483. (defun org-latex-verbatim (verbatim contents info)
  2484. "Transcode a VERBATIM object from Org to LaTeX.
  2485. CONTENTS is nil. INFO is a plist used as a communication
  2486. channel."
  2487. (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
  2488. ;;;; Verse Block
  2489. (defun org-latex-verse-block (verse-block contents info)
  2490. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  2491. CONTENTS is verse block contents. INFO is a plist holding
  2492. contextual information."
  2493. (org-latex--wrap-label
  2494. verse-block
  2495. ;; In a verse environment, add a line break to each newline
  2496. ;; character and change each white space at beginning of a line
  2497. ;; into a space of 1 em. Also change each blank line with
  2498. ;; a vertical space of 1 em.
  2499. (progn
  2500. (setq contents (replace-regexp-in-string
  2501. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  2502. (replace-regexp-in-string
  2503. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
  2504. (while (string-match "^[ \t]+" contents)
  2505. (let ((new-str (format "\\hspace*{%dem}"
  2506. (length (match-string 0 contents)))))
  2507. (setq contents (replace-match new-str nil t contents))))
  2508. (format "\\begin{verse}\n%s\\end{verse}" contents))))
  2509. ;;; End-user functions
  2510. ;;;###autoload
  2511. (defun org-latex-export-as-latex
  2512. (&optional async subtreep visible-only body-only ext-plist)
  2513. "Export current buffer as a LaTeX buffer.
  2514. If narrowing is active in the current buffer, only export its
  2515. narrowed part.
  2516. If a region is active, export that region.
  2517. A non-nil optional argument ASYNC means the process should happen
  2518. asynchronously. The resulting buffer should be accessible
  2519. through the `org-export-stack' interface.
  2520. When optional argument SUBTREEP is non-nil, export the sub-tree
  2521. at point, extracting information from the headline properties
  2522. first.
  2523. When optional argument VISIBLE-ONLY is non-nil, don't export
  2524. contents of hidden elements.
  2525. When optional argument BODY-ONLY is non-nil, only write code
  2526. between \"\\begin{document}\" and \"\\end{document}\".
  2527. EXT-PLIST, when provided, is a property list with external
  2528. parameters overriding Org default settings, but still inferior to
  2529. file-local settings.
  2530. Export is done in a buffer named \"*Org LATEX Export*\", which
  2531. will be displayed when `org-export-show-temporary-export-buffer'
  2532. is non-nil."
  2533. (interactive)
  2534. (org-export-to-buffer 'latex "*Org LATEX Export*"
  2535. async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
  2536. ;;;###autoload
  2537. (defun org-latex-convert-region-to-latex ()
  2538. "Assume the current region has org-mode syntax, and convert it to LaTeX.
  2539. This can be used in any buffer. For example, you can write an
  2540. itemized list in org-mode syntax in an LaTeX buffer and use this
  2541. command to convert it."
  2542. (interactive)
  2543. (org-export-replace-region-by 'latex))
  2544. ;;;###autoload
  2545. (defun org-latex-export-to-latex
  2546. (&optional async subtreep visible-only body-only ext-plist)
  2547. "Export current buffer to a LaTeX file.
  2548. If narrowing is active in the current buffer, only export its
  2549. narrowed part.
  2550. If a region is active, export that region.
  2551. A non-nil optional argument ASYNC means the process should happen
  2552. asynchronously. The resulting file should be accessible through
  2553. the `org-export-stack' interface.
  2554. When optional argument SUBTREEP is non-nil, export the sub-tree
  2555. at point, extracting information from the headline properties
  2556. first.
  2557. When optional argument VISIBLE-ONLY is non-nil, don't export
  2558. contents of hidden elements.
  2559. When optional argument BODY-ONLY is non-nil, only write code
  2560. between \"\\begin{document}\" and \"\\end{document}\".
  2561. EXT-PLIST, when provided, is a property list with external
  2562. parameters overriding Org default settings, but still inferior to
  2563. file-local settings."
  2564. (interactive)
  2565. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  2566. (org-export-to-file 'latex outfile
  2567. async subtreep visible-only body-only ext-plist)))
  2568. ;;;###autoload
  2569. (defun org-latex-export-to-pdf
  2570. (&optional async subtreep visible-only body-only ext-plist)
  2571. "Export current buffer to LaTeX then process through to PDF.
  2572. If narrowing is active in the current buffer, only export its
  2573. narrowed part.
  2574. If a region is active, export that region.
  2575. A non-nil optional argument ASYNC means the process should happen
  2576. asynchronously. The resulting file should be accessible through
  2577. the `org-export-stack' interface.
  2578. When optional argument SUBTREEP is non-nil, export the sub-tree
  2579. at point, extracting information from the headline properties
  2580. first.
  2581. When optional argument VISIBLE-ONLY is non-nil, don't export
  2582. contents of hidden elements.
  2583. When optional argument BODY-ONLY is non-nil, only write code
  2584. between \"\\begin{document}\" and \"\\end{document}\".
  2585. EXT-PLIST, when provided, is a property list with external
  2586. parameters overriding Org default settings, but still inferior to
  2587. file-local settings.
  2588. Return PDF file's name."
  2589. (interactive)
  2590. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  2591. (org-export-to-file 'latex outfile
  2592. async subtreep visible-only body-only ext-plist
  2593. (lambda (file) (org-latex-compile file)))))
  2594. (defun org-latex-compile (texfile &optional snippet)
  2595. "Compile a TeX file.
  2596. TEXFILE is the name of the file being compiled. Processing is
  2597. done through the command specified in `org-latex-pdf-process'.
  2598. When optional argument SNIPPET is non-nil, TEXFILE is a temporary
  2599. file used to preview a LaTeX snippet. In this case, do not
  2600. create a log buffer and do not bother removing log files.
  2601. Return PDF file name or an error if it couldn't be produced."
  2602. (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
  2603. (full-name (file-truename texfile))
  2604. (out-dir (file-name-directory texfile))
  2605. ;; Properly set working directory for compilation.
  2606. (default-directory (if (file-name-absolute-p texfile)
  2607. (file-name-directory full-name)
  2608. default-directory))
  2609. errors)
  2610. (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
  2611. (save-window-excursion
  2612. (cond
  2613. ;; A function is provided: Apply it.
  2614. ((functionp org-latex-pdf-process)
  2615. (funcall org-latex-pdf-process (shell-quote-argument texfile)))
  2616. ;; A list is provided: Replace %b, %f and %o with appropriate
  2617. ;; values in each command before applying it. Output is
  2618. ;; redirected to "*Org PDF LaTeX Output*" buffer.
  2619. ((consp org-latex-pdf-process)
  2620. (let ((outbuf (and (not snippet)
  2621. (get-buffer-create "*Org PDF LaTeX Output*"))))
  2622. (mapc
  2623. (lambda (command)
  2624. (shell-command
  2625. (replace-regexp-in-string
  2626. "%b" (shell-quote-argument base-name)
  2627. (replace-regexp-in-string
  2628. "%f" (shell-quote-argument full-name)
  2629. (replace-regexp-in-string
  2630. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  2631. outbuf))
  2632. org-latex-pdf-process)
  2633. ;; Collect standard errors from output buffer.
  2634. (setq errors (and (not snippet) (org-latex--collect-errors outbuf)))))
  2635. (t (error "No valid command to process to PDF")))
  2636. (let ((pdffile (concat out-dir base-name ".pdf")))
  2637. ;; Check for process failure. Provide collected errors if
  2638. ;; possible.
  2639. (if (not (file-exists-p pdffile))
  2640. (error (concat (format "PDF file %s wasn't produced" pdffile)
  2641. (when errors (concat ": " errors))))
  2642. ;; Else remove log files, when specified, and signal end of
  2643. ;; process to user, along with any error encountered.
  2644. (when (and (not snippet) org-latex-remove-logfiles)
  2645. (dolist (file (directory-files
  2646. out-dir t
  2647. (concat (regexp-quote base-name)
  2648. "\\(?:\\.[0-9]+\\)?"
  2649. "\\."
  2650. (regexp-opt org-latex-logfiles-extensions))))
  2651. (delete-file file)))
  2652. (message (concat "Process completed"
  2653. (if (not errors) "."
  2654. (concat " with errors: " errors)))))
  2655. ;; Return output file name.
  2656. pdffile))))
  2657. (defun org-latex--collect-errors (buffer)
  2658. "Collect some kind of errors from \"pdflatex\" command output.
  2659. BUFFER is the buffer containing output.
  2660. Return collected error types as a string, or nil if there was
  2661. none."
  2662. (with-current-buffer buffer
  2663. (save-excursion
  2664. (goto-char (point-max))
  2665. (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
  2666. (let ((case-fold-search t)
  2667. (errors ""))
  2668. (dolist (latex-error org-latex-known-errors)
  2669. (when (save-excursion (re-search-forward (car latex-error) nil t))
  2670. (setq errors (concat errors " " (cdr latex-error)))))
  2671. (and (org-string-nw-p errors) (org-trim errors)))))))
  2672. ;;;###autoload
  2673. (defun org-latex-publish-to-latex (plist filename pub-dir)
  2674. "Publish an Org file to LaTeX.
  2675. FILENAME is the filename of the Org file to be published. PLIST
  2676. is the property list for the given project. PUB-DIR is the
  2677. publishing directory.
  2678. Return output file name."
  2679. (org-publish-org-to 'latex filename ".tex" plist pub-dir))
  2680. ;;;###autoload
  2681. (defun org-latex-publish-to-pdf (plist filename pub-dir)
  2682. "Publish an Org file to PDF (via LaTeX).
  2683. FILENAME is the filename of the Org file to be published. PLIST
  2684. is the property list for the given project. PUB-DIR is the
  2685. publishing directory.
  2686. Return output file name."
  2687. ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
  2688. ;; in working directory and then moved to publishing directory.
  2689. (org-publish-attachment
  2690. plist
  2691. (org-latex-compile (org-publish-org-to 'latex filename ".tex" plist))
  2692. pub-dir))
  2693. (provide 'ox-latex)
  2694. ;; Local variables:
  2695. ;; generated-autoload-file: "org-loaddefs.el"
  2696. ;; End:
  2697. ;;; ox-latex.el ends here