ox-latex.el 102 KB

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