org-e-latex.el 95 KB

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