org-e-latex.el 96 KB

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