ox-latex.el 104 KB

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