ox-latex.el 105 KB

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