org-e-latex.el 94 KB

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