ox-latex.el 103 KB

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