ox-latex.el 108 KB

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