org-e-latex.el 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737
  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. (defcustom org-e-latex-known-errors
  678. '(("Reference.*?undefined" . "[undefined reference]")
  679. ("Citation.*?undefined" . "[undefined citation]")
  680. ("Undefined control sequence" . "[undefined control sequence]")
  681. ("^! LaTeX.*?Error" . "[LaTeX error]")
  682. ("^! Package.*?Error" . "[package error]")
  683. ("Runaway argument" . "Runaway argument"))
  684. "Alist of regular expressions and associated messages for the user.
  685. The regular expressions are used to find possible errors in the
  686. log of a latex-run."
  687. :group 'org-export-e-latex
  688. :type '(repeat
  689. (cons
  690. (string :tag "Regexp")
  691. (string :tag "Message"))))
  692. ;;; Internal Functions
  693. (defun org-e-latex--caption/label-string (element info)
  694. "Return caption and label LaTeX string for ELEMENT.
  695. INFO is a plist holding contextual information. If there's no
  696. caption nor label, return the empty string.
  697. For non-floats, see `org-e-latex--wrap-label'."
  698. (let* ((label (org-element-property :name element))
  699. (label-str (if label (format "\\label{%s}" label) ""))
  700. (main (org-export-get-caption element))
  701. (short (org-export-get-caption element t)))
  702. (cond
  703. ((and (not main) (not label)) "")
  704. ((not main) (format "\\label{%s}\n" label))
  705. ;; Option caption format with short name.
  706. (short (format "\\caption[%s]{%s%s}\n"
  707. (org-export-data short info)
  708. label-str
  709. (org-export-data main info)))
  710. ;; Standard caption format.
  711. (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
  712. (defun org-e-latex--guess-babel-language (header info)
  713. "Set Babel's language according to LANGUAGE keyword.
  714. HEADER is the LaTeX header string. INFO is the plist used as
  715. a communication channel.
  716. Insertion of guessed language only happens when Babel package has
  717. explicitly been loaded. Then it is added to the rest of
  718. package's options.
  719. Return the new header."
  720. (let ((language-code (plist-get info :language)))
  721. ;; If no language is set or Babel package is not loaded, return
  722. ;; HEADER as-is.
  723. (if (or (not (stringp language-code))
  724. (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
  725. header
  726. (let ((options (save-match-data
  727. (org-split-string (match-string 1 header) ",")))
  728. (language (cdr (assoc language-code
  729. org-e-latex-babel-language-alist))))
  730. ;; If LANGUAGE is already loaded, return header. Otherwise,
  731. ;; append LANGUAGE to other options.
  732. (if (member language options) header
  733. (replace-match (mapconcat 'identity
  734. (append options (list language))
  735. ",")
  736. nil nil header 1))))))
  737. (defun org-e-latex--guess-inputenc (header)
  738. "Set the coding system in inputenc to what the buffer is.
  739. HEADER is the LaTeX header string. Return the new header."
  740. (let* ((cs (or (ignore-errors
  741. (latexenc-coding-system-to-inputenc
  742. buffer-file-coding-system))
  743. "utf8")))
  744. (if (not cs) header
  745. ;; First translate if that is requested.
  746. (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
  747. ;; Then find the \usepackage statement and replace the option.
  748. (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  749. cs header t nil 1))))
  750. (defun org-e-latex--find-verb-separator (s)
  751. "Return a character not used in string S.
  752. This is used to choose a separator for constructs like \\verb."
  753. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  754. (loop for c across ll
  755. when (not (string-match (regexp-quote (char-to-string c)) s))
  756. return (char-to-string c))))
  757. (defun org-e-latex--make-option-string (options)
  758. "Return a comma separated string of keywords and values.
  759. OPTIONS is an alist where the key is the options keyword as
  760. a string, and the value a list containing the keyword value, or
  761. nil."
  762. (mapconcat (lambda (pair)
  763. (concat (first pair)
  764. (when (> (length (second pair)) 0)
  765. (concat "=" (second pair)))))
  766. options
  767. ","))
  768. (defun org-e-latex--quotation-marks (text info)
  769. "Export quotation marks depending on language conventions.
  770. TEXT is a string containing quotation marks to be replaced. INFO
  771. is a plist used as a communication channel."
  772. (mapc (lambda(l)
  773. (let ((start 0))
  774. (while (setq start (string-match (car l) text start))
  775. (let ((new-quote (concat (match-string 1 text) (cdr l))))
  776. (setq text (replace-match new-quote t t text))))))
  777. (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
  778. ;; Falls back on English.
  779. (assoc "en" org-e-latex-quotes))))
  780. text)
  781. (defun org-e-latex--wrap-label (element output)
  782. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  783. This function shouldn't be used for floats. See
  784. `org-e-latex--caption/label-string'."
  785. (let ((label (org-element-property :name element)))
  786. (if (or (not output) (not label) (string= output "") (string= label ""))
  787. output
  788. (concat (format "\\label{%s}\n" label) output))))
  789. (defun org-e-latex--text-markup (text markup)
  790. "Format TEXT depending on MARKUP text markup.
  791. See `org-e-latex-text-markup-alist' for details."
  792. (let ((fmt (cdr (assq markup org-e-latex-text-markup-alist))))
  793. (cond
  794. ;; No format string: Return raw text.
  795. ((not fmt) text)
  796. ;; Handle the `verb' special case: Find and appropriate separator
  797. ;; and use "\\verb" command.
  798. ((eq 'verb fmt)
  799. (let ((separator (org-e-latex--find-verb-separator text)))
  800. (concat "\\verb" separator text separator)))
  801. ;; Handle the `protectedtexttt' special case: Protect some
  802. ;; special chars and use "\texttt{%s}" format string.
  803. ((eq 'protectedtexttt fmt)
  804. (let ((start 0)
  805. (trans '(("\\" . "\\textbackslash{}")
  806. ("~" . "\\textasciitilde{}")
  807. ("^" . "\\textasciicircum{}")))
  808. (rtn "")
  809. char)
  810. (while (string-match "[\\{}$%&_#~^]" text)
  811. (setq char (match-string 0 text))
  812. (if (> (match-beginning 0) 0)
  813. (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
  814. (setq text (substring text (1+ (match-beginning 0))))
  815. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  816. rtn (concat rtn char)))
  817. (setq text (concat rtn text)
  818. fmt "\\texttt{%s}")
  819. (while (string-match "--" text)
  820. (setq text (replace-match "-{}-" t t text)))
  821. (format fmt text)))
  822. ;; Else use format string.
  823. (t (format fmt text)))))
  824. (defun org-e-latex--delayed-footnotes-definitions (element info)
  825. "Return footnotes definitions in ELEMENT as a string.
  826. INFO is a plist used as a communication channel.
  827. Footnotes definitions are returned within \"\\footnotetxt{}\"
  828. commands.
  829. This function is used within constructs that don't support
  830. \"\\footnote{}\" command (i.e. an item's tag). In that case,
  831. \"\\footnotemark\" is used within the construct and the function
  832. just outside of it."
  833. (mapconcat
  834. (lambda (ref)
  835. (format
  836. "\\footnotetext[%s]{%s}"
  837. (org-export-get-footnote-number ref info)
  838. (org-trim
  839. (org-export-data
  840. (org-export-get-footnote-definition ref info) info))))
  841. ;; Find every footnote reference in ELEMENT.
  842. (let* (all-refs
  843. search-refs ; For byte-compiler.
  844. (search-refs
  845. (function
  846. (lambda (data)
  847. ;; Return a list of all footnote references never seen
  848. ;; before in DATA.
  849. (org-element-map
  850. data 'footnote-reference
  851. (lambda (ref)
  852. (when (org-export-footnote-first-reference-p ref info)
  853. (push ref all-refs)
  854. (when (eq (org-element-property :type ref) 'standard)
  855. (funcall search-refs
  856. (org-export-get-footnote-definition ref info)))))
  857. info)
  858. (reverse all-refs)))))
  859. (funcall search-refs element))
  860. ""))
  861. ;;; Template
  862. (defun org-e-latex-template (contents info)
  863. "Return complete document string after LaTeX conversion.
  864. CONTENTS is the transcoded contents string. INFO is a plist
  865. holding export options."
  866. (let ((title (org-export-data (plist-get info :title) info)))
  867. (concat
  868. ;; Time-stamp.
  869. (and (plist-get info :time-stamp-file)
  870. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  871. ;; Document class and packages.
  872. (let ((class (plist-get info :latex-class))
  873. (class-options (plist-get info :latex-class-options)))
  874. (org-element-normalize-string
  875. (let* ((header (nth 1 (assoc class org-e-latex-classes)))
  876. (document-class-string
  877. (and (stringp header)
  878. (if (not class-options) header
  879. (replace-regexp-in-string
  880. "^[ \t]*\\\\documentclass\\(\\(\\[.*\\]\\)?\\)"
  881. class-options header t nil 1)))))
  882. (when document-class-string
  883. (org-e-latex--guess-babel-language
  884. (org-e-latex--guess-inputenc
  885. (org-splice-latex-header
  886. document-class-string
  887. org-export-latex-default-packages-alist ; defined in org.el
  888. org-export-latex-packages-alist nil ; defined in org.el
  889. (plist-get info :latex-header-extra)))
  890. info)))))
  891. ;; Possibly limit depth for headline numbering.
  892. (let ((sec-num (plist-get info :section-numbers)))
  893. (when (integerp sec-num)
  894. (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
  895. ;; Author.
  896. (let ((author (and (plist-get info :with-author)
  897. (let ((auth (plist-get info :author)))
  898. (and auth (org-export-data auth info)))))
  899. (email (and (plist-get info :with-email)
  900. (org-export-data (plist-get info :email) info))))
  901. (cond ((and author email (not (string= "" email)))
  902. (format "\\author{%s\\thanks{%s}}\n" author email))
  903. (author (format "\\author{%s}\n" author))
  904. (t "\\author{}\n")))
  905. ;; Date.
  906. (let ((date (org-export-data (plist-get info :date) info)))
  907. (and date (format "\\date{%s}\n" date)))
  908. ;; Title
  909. (format "\\title{%s}\n" title)
  910. ;; Hyperref options.
  911. (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
  912. (or (plist-get info :keywords) "")
  913. (or (plist-get info :description) "")
  914. (if (not (plist-get info :with-creator)) ""
  915. (plist-get info :creator)))
  916. ;; Document start.
  917. "\\begin{document}\n\n"
  918. ;; Title command.
  919. (org-element-normalize-string
  920. (cond ((string= "" title) nil)
  921. ((not (stringp org-e-latex-title-command)) nil)
  922. ((string-match "\\(?:[^%]\\|^\\)%s"
  923. org-e-latex-title-command)
  924. (format org-e-latex-title-command title))
  925. (t org-e-latex-title-command)))
  926. ;; Table of contents.
  927. (let ((depth (plist-get info :with-toc)))
  928. (when depth
  929. (concat (when (wholenump depth)
  930. (format "\\setcounter{tocdepth}{%d}\n" depth))
  931. org-e-latex-toc-command)))
  932. ;; Document's body.
  933. contents
  934. ;; Creator.
  935. (let ((creator-info (plist-get info :with-creator)))
  936. (cond
  937. ((not creator-info) "")
  938. ((eq creator-info 'comment)
  939. (format "%% %s\n" (plist-get info :creator)))
  940. (t (concat (plist-get info :creator) "\n"))))
  941. ;; Document end.
  942. "\\end{document}")))
  943. ;;; Transcode Functions
  944. ;;;; Babel Call
  945. ;;
  946. ;; Babel Calls are ignored.
  947. ;;;; Bold
  948. (defun org-e-latex-bold (bold contents info)
  949. "Transcode BOLD from Org to LaTeX.
  950. CONTENTS is the text with bold markup. INFO is a plist holding
  951. contextual information."
  952. (org-e-latex--text-markup contents 'bold))
  953. ;;;; Center Block
  954. (defun org-e-latex-center-block (center-block contents info)
  955. "Transcode a CENTER-BLOCK element from Org to LaTeX.
  956. CONTENTS holds the contents of the center block. INFO is a plist
  957. holding contextual information."
  958. (org-e-latex--wrap-label
  959. center-block
  960. (format "\\begin{center}\n%s\\end{center}" contents)))
  961. ;;;; Clock
  962. (defun org-e-latex-clock (clock contents info)
  963. "Transcode a CLOCK element from Org to LaTeX.
  964. CONTENTS is nil. INFO is a plist holding contextual
  965. information."
  966. (concat
  967. "\\noindent"
  968. (format "\\textbf{%s} " org-clock-string)
  969. (format org-e-latex-inactive-timestamp-format
  970. (concat (org-translate-time (org-element-property :value clock))
  971. (let ((time (org-element-property :time clock)))
  972. (and time (format " (%s)" time)))))
  973. "\\\\"))
  974. ;;;; Code
  975. (defun org-e-latex-code (code contents info)
  976. "Transcode a CODE object from Org to LaTeX.
  977. CONTENTS is nil. INFO is a plist used as a communication
  978. channel."
  979. (org-e-latex--text-markup (org-element-property :value code) 'code))
  980. ;;;; Comment
  981. ;;
  982. ;; Comments are ignored.
  983. ;;;; Comment Block
  984. ;;
  985. ;; Comment Blocks are ignored.
  986. ;;;; Drawer
  987. (defun org-e-latex-drawer (drawer contents info)
  988. "Transcode a DRAWER element from Org to LaTeX.
  989. CONTENTS holds the contents of the block. INFO is a plist
  990. holding contextual information."
  991. (let* ((name (org-element-property :drawer-name drawer))
  992. (output (if (functionp org-e-latex-format-drawer-function)
  993. (funcall org-e-latex-format-drawer-function
  994. name contents)
  995. ;; If there's no user defined function: simply
  996. ;; display contents of the drawer.
  997. contents)))
  998. (org-e-latex--wrap-label drawer output)))
  999. ;;;; Dynamic Block
  1000. (defun org-e-latex-dynamic-block (dynamic-block contents info)
  1001. "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
  1002. CONTENTS holds the contents of the block. INFO is a plist
  1003. holding contextual information. See `org-export-data'."
  1004. (org-e-latex--wrap-label dynamic-block contents))
  1005. ;;;; Entity
  1006. (defun org-e-latex-entity (entity contents info)
  1007. "Transcode an ENTITY object from Org to LaTeX.
  1008. CONTENTS are the definition itself. INFO is a plist holding
  1009. contextual information."
  1010. (let ((ent (org-element-property :latex entity)))
  1011. (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
  1012. ;;;; Example Block
  1013. (defun org-e-latex-example-block (example-block contents info)
  1014. "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
  1015. CONTENTS is nil. INFO is a plist holding contextual
  1016. information."
  1017. (org-e-latex--wrap-label
  1018. example-block
  1019. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1020. (org-export-format-code-default example-block info))))
  1021. ;;;; Export Block
  1022. (defun org-e-latex-export-block (export-block contents info)
  1023. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  1024. CONTENTS is nil. INFO is a plist holding contextual information."
  1025. (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
  1026. (org-remove-indentation (org-element-property :value export-block))))
  1027. ;;;; Export Snippet
  1028. (defun org-e-latex-export-snippet (export-snippet contents info)
  1029. "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
  1030. CONTENTS is nil. INFO is a plist holding contextual information."
  1031. (when (eq (org-export-snippet-backend export-snippet) 'e-latex)
  1032. (org-element-property :value export-snippet)))
  1033. ;;;; Fixed Width
  1034. (defun org-e-latex-fixed-width (fixed-width contents info)
  1035. "Transcode a FIXED-WIDTH element from Org to LaTeX.
  1036. CONTENTS is nil. INFO is a plist holding contextual information."
  1037. (org-e-latex--wrap-label
  1038. fixed-width
  1039. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1040. (org-remove-indentation
  1041. (org-element-property :value fixed-width)))))
  1042. ;;;; Footnote Definition
  1043. ;;
  1044. ;; Footnote Definitions are ignored.
  1045. ;;;; Footnote Reference
  1046. ;;
  1047. ;; Footnote reference export is handled by
  1048. ;; `org-e-latex-footnote-reference'.
  1049. ;;
  1050. ;; Internally, `org-e-latex--get-footnote-counter' is used to restore
  1051. ;; the value of the LaTeX "footnote" counter after a jump due to
  1052. ;; a reference to an already defined footnote. It is only needed in
  1053. ;; item tags since the optional argument to \footnotemark is not
  1054. ;; allowed there.
  1055. (defun org-e-latex--get-footnote-counter (footnote-reference info)
  1056. "Return \"footnote\" counter before FOOTNOTE-REFERENCE is encountered.
  1057. INFO is a plist used as a communication channel."
  1058. ;; Find original counter value by counting number of footnote
  1059. ;; references appearing for the first time before the current
  1060. ;; footnote reference.
  1061. (let* ((label (org-element-property :label footnote-reference))
  1062. seen-refs
  1063. search-ref ; For byte-compiler.
  1064. (search-ref
  1065. (function
  1066. (lambda (data)
  1067. ;; Search footnote references through DATA, filling
  1068. ;; SEEN-REFS along the way.
  1069. (org-element-map
  1070. data 'footnote-reference
  1071. (lambda (fn)
  1072. (let ((fn-lbl (org-element-property :label fn)))
  1073. (cond
  1074. ;; Anonymous footnote match: return number.
  1075. ((eq fn footnote-reference) (length seen-refs))
  1076. ;; Anonymous footnote: it's always a new one.
  1077. ;; Also, be sure to return nil from the `cond' so
  1078. ;; `first-match' doesn't get us out of the loop.
  1079. ((not fn-lbl) (push 'inline seen-refs) nil)
  1080. ;; Label not seen so far: add it so SEEN-REFS.
  1081. ;;
  1082. ;; Also search for subsequent references in
  1083. ;; footnote definition so numbering follows reading
  1084. ;; logic. Note that we don't have to care about
  1085. ;; inline definitions, since `org-element-map'
  1086. ;; already traverse them at the right time.
  1087. ((not (member fn-lbl seen-refs))
  1088. (push fn-lbl seen-refs)
  1089. (funcall search-ref
  1090. (org-export-get-footnote-definition fn info))))))
  1091. ;; Don't enter footnote definitions since it will happen
  1092. ;; when their first reference is found.
  1093. info 'first-match 'footnote-definition)))))
  1094. (funcall search-ref (plist-get info :parse-tree))))
  1095. (defun org-e-latex-footnote-reference (footnote-reference contents info)
  1096. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  1097. CONTENTS is nil. INFO is a plist holding contextual information."
  1098. (concat
  1099. ;; Insert separator between two footnotes in a row.
  1100. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1101. (when (eq (org-element-type prev) 'footnote-reference)
  1102. org-e-latex-footnote-separator))
  1103. (cond
  1104. ;; Use \footnotemark if reference is within an item's tag.
  1105. ((eq (org-element-type (org-export-get-parent-element footnote-reference))
  1106. 'item)
  1107. (if (org-export-footnote-first-reference-p footnote-reference info)
  1108. "\\footnotemark"
  1109. ;; Since we can't specify footnote number as an optional
  1110. ;; argument within an item tag, some extra work has to be done
  1111. ;; when the footnote has already been referenced. In that
  1112. ;; case, set footnote counter to the desired number, use the
  1113. ;; footnotemark, then set counter back to its original value.
  1114. (format
  1115. "\\setcounter{footnote}{%s}\\footnotemark\\setcounter{footnote}{%s}"
  1116. (1- (org-export-get-footnote-number footnote-reference info))
  1117. (org-e-latex--get-footnote-counter footnote-reference info))))
  1118. ;; Use \footnotemark if the footnote has already been defined.
  1119. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1120. (format "\\footnotemark[%s]{}"
  1121. (org-export-get-footnote-number footnote-reference info)))
  1122. ;; Use \footnotemark if reference is within another footnote
  1123. ;; reference or footnote definition.
  1124. ((loop for parent in (org-export-get-genealogy footnote-reference)
  1125. thereis (memq (org-element-type parent)
  1126. '(footnote-reference footnote-definition)))
  1127. "\\footnotemark")
  1128. ;; Otherwise, define it with \footnote command.
  1129. (t
  1130. (let ((def (org-export-get-footnote-definition footnote-reference info)))
  1131. (unless (eq (org-element-type def) 'org-data)
  1132. (setq def (cons 'org-data (cons nil def))))
  1133. (concat
  1134. (format "\\footnote{%s}" (org-trim (org-export-data def info)))
  1135. ;; Retrieve all footnote references within the footnote and
  1136. ;; add their definition after it, since LaTeX doesn't support
  1137. ;; them inside.
  1138. (org-e-latex--delayed-footnotes-definitions def info)))))))
  1139. ;;;; Headline
  1140. (defun org-e-latex-headline (headline contents info)
  1141. "Transcode an HEADLINE element from Org to LaTeX.
  1142. CONTENTS holds the contents of the headline. INFO is a plist
  1143. holding contextual information."
  1144. (let* ((class (plist-get info :latex-class))
  1145. (level (org-export-get-relative-level headline info))
  1146. (numberedp (org-export-numbered-headline-p headline info))
  1147. (class-sectionning (assoc class org-e-latex-classes))
  1148. ;; Section formatting will set two placeholders: one for the
  1149. ;; title and the other for the contents.
  1150. (section-fmt
  1151. (let ((sec (if (and (symbolp (nth 2 class-sectionning))
  1152. (fboundp (nth 2 class-sectionning)))
  1153. (funcall (nth 2 class-sectionning) level numberedp)
  1154. (nth (1+ level) class-sectionning))))
  1155. (cond
  1156. ;; No section available for that LEVEL.
  1157. ((not sec) nil)
  1158. ;; Section format directly returned by a function.
  1159. ((stringp sec) sec)
  1160. ;; (numbered-section . unnumbered-section)
  1161. ((not (consp (cdr sec)))
  1162. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  1163. ;; (numbered-open numbered-close)
  1164. ((= (length sec) 2)
  1165. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  1166. ;; (num-in num-out no-num-in no-num-out)
  1167. ((= (length sec) 4)
  1168. (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
  1169. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  1170. (text (org-export-data (org-element-property :title headline) info))
  1171. (todo
  1172. (and (plist-get info :with-todo-keywords)
  1173. (let ((todo (org-element-property :todo-keyword headline)))
  1174. (and todo (org-export-data todo info)))))
  1175. (todo-type (and todo (org-element-property :todo-type headline)))
  1176. (tags (and (plist-get info :with-tags)
  1177. (org-export-get-tags headline info)))
  1178. (priority (and (plist-get info :with-priority)
  1179. (org-element-property :priority headline)))
  1180. ;; Create the headline text along with a no-tag version. The
  1181. ;; latter is required to remove tags from table of contents.
  1182. (full-text (if (functionp org-e-latex-format-headline-function)
  1183. ;; User-defined formatting function.
  1184. (funcall org-e-latex-format-headline-function
  1185. todo todo-type priority text tags)
  1186. ;; Default formatting.
  1187. (concat
  1188. (when todo
  1189. (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1190. (when priority (format "\\framebox{\\#%c} " priority))
  1191. text
  1192. (when tags
  1193. (format "\\hfill{}\\textsc{:%s:}"
  1194. (mapconcat 'identity tags ":"))))))
  1195. (full-text-no-tag
  1196. (if (functionp org-e-latex-format-headline-function)
  1197. ;; User-defined formatting function.
  1198. (funcall org-e-latex-format-headline-function
  1199. todo todo-type priority text nil)
  1200. ;; Default formatting.
  1201. (concat
  1202. (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1203. (when priority (format "\\framebox{\\#%c} " priority))
  1204. text)))
  1205. ;; Associate some \label to the headline for internal links.
  1206. (headline-label
  1207. (format "\\label{sec-%s}\n"
  1208. (mapconcat 'number-to-string
  1209. (org-export-get-headline-number headline info)
  1210. "-")))
  1211. (pre-blanks
  1212. (make-string (org-element-property :pre-blank headline) 10)))
  1213. (cond
  1214. ;; Case 1: This is a footnote section: ignore it.
  1215. ((org-element-property :footnote-section-p headline) nil)
  1216. ;; Case 2. This is a deep sub-tree: export it as a list item.
  1217. ;; Also export as items headlines for which no section
  1218. ;; format has been found.
  1219. ((or (not section-fmt) (org-export-low-level-p headline info))
  1220. ;; Build the real contents of the sub-tree.
  1221. (let ((low-level-body
  1222. (concat
  1223. ;; If the headline is the first sibling, start a list.
  1224. (when (org-export-first-sibling-p headline info)
  1225. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  1226. ;; Itemize headline
  1227. "\\item " full-text "\n" headline-label pre-blanks contents)))
  1228. ;; If headline is not the last sibling simply return
  1229. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
  1230. ;; blank line.
  1231. (if (not (org-export-last-sibling-p headline info)) low-level-body
  1232. (replace-regexp-in-string
  1233. "[ \t\n]*\\'"
  1234. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  1235. low-level-body))))
  1236. ;; Case 3. Standard headline. Export it as a section.
  1237. (t
  1238. (cond
  1239. ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
  1240. ;; Regular section. Use specified format string.
  1241. (format section-fmt full-text
  1242. (concat headline-label pre-blanks contents)))
  1243. ((string-match "\\`\\\\\\(.*?\\){" section-fmt)
  1244. ;; If tags should be removed from table of contents, insert
  1245. ;; title without tags as an alternative heading in sectioning
  1246. ;; command.
  1247. (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
  1248. nil nil section-fmt 1)
  1249. ;; Replace square brackets with parenthesis since
  1250. ;; square brackets are not supported in optional
  1251. ;; arguments.
  1252. (replace-regexp-in-string
  1253. "\\[" "("
  1254. (replace-regexp-in-string
  1255. "\\]" ")"
  1256. full-text-no-tag))
  1257. full-text
  1258. (concat headline-label pre-blanks contents)))
  1259. (t
  1260. ;; Impossible to add an alternative heading. Fallback to
  1261. ;; regular sectioning format string.
  1262. (format section-fmt full-text
  1263. (concat headline-label pre-blanks contents))))))))
  1264. ;;;; Horizontal Rule
  1265. (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
  1266. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  1267. CONTENTS is nil. INFO is a plist holding contextual information."
  1268. (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
  1269. (prev (org-export-get-previous-element horizontal-rule info)))
  1270. (concat
  1271. ;; Make sure the rule doesn't start at the end of the current
  1272. ;; line by separating it with a blank line from previous element.
  1273. (when (and prev
  1274. (let ((prev-blank (org-element-property :post-blank prev)))
  1275. (or (not prev-blank) (zerop prev-blank))))
  1276. "\n")
  1277. (org-e-latex--wrap-label
  1278. horizontal-rule
  1279. (format "\\rule{%s}{%s}"
  1280. (or (plist-get attr :width) "\\linewidth")
  1281. (or (plist-get attr :thickness) "0.5pt"))))))
  1282. ;;;; Inline Babel Call
  1283. ;;
  1284. ;; Inline Babel Calls are ignored.
  1285. ;;;; Inline Src Block
  1286. (defun org-e-latex-inline-src-block (inline-src-block contents info)
  1287. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  1288. CONTENTS holds the contents of the item. INFO is a plist holding
  1289. contextual information."
  1290. (let* ((code (org-element-property :value inline-src-block))
  1291. (separator (org-e-latex--find-verb-separator code)))
  1292. (cond
  1293. ;; Do not use a special package: transcode it verbatim.
  1294. ((not org-e-latex-listings)
  1295. (concat "\\verb" separator code separator))
  1296. ;; Use minted package.
  1297. ((eq org-e-latex-listings 'minted)
  1298. (let* ((org-lang (org-element-property :language inline-src-block))
  1299. (mint-lang (or (cadr (assq (intern org-lang)
  1300. org-e-latex-minted-langs))
  1301. org-lang))
  1302. (options (org-e-latex--make-option-string
  1303. org-e-latex-minted-options)))
  1304. (concat (format "\\mint%s{%s}"
  1305. (if (string= options "") "" (format "[%s]" options))
  1306. mint-lang)
  1307. separator code separator)))
  1308. ;; Use listings package.
  1309. (t
  1310. ;; Maybe translate language's name.
  1311. (let* ((org-lang (org-element-property :language inline-src-block))
  1312. (lst-lang (or (cadr (assq (intern org-lang)
  1313. org-e-latex-listings-langs))
  1314. org-lang))
  1315. (options (org-e-latex--make-option-string
  1316. (append org-e-latex-listings-options
  1317. `(("language" ,lst-lang))))))
  1318. (concat (format "\\lstinline[%s]" options)
  1319. separator code separator))))))
  1320. ;;;; Inlinetask
  1321. (defun org-e-latex-inlinetask (inlinetask contents info)
  1322. "Transcode an INLINETASK element from Org to LaTeX.
  1323. CONTENTS holds the contents of the block. INFO is a plist
  1324. holding contextual information."
  1325. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  1326. (todo (and (plist-get info :with-todo-keywords)
  1327. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1328. (and todo (org-export-data todo info)))))
  1329. (todo-type (org-element-property :todo-type inlinetask))
  1330. (tags (and (plist-get info :with-tags)
  1331. (org-export-get-tags inlinetask info)))
  1332. (priority (and (plist-get info :with-priority)
  1333. (org-element-property :priority inlinetask))))
  1334. ;; If `org-e-latex-format-inlinetask-function' is provided, call it
  1335. ;; with appropriate arguments.
  1336. (if (functionp org-e-latex-format-inlinetask-function)
  1337. (funcall org-e-latex-format-inlinetask-function
  1338. todo todo-type priority title tags contents)
  1339. ;; Otherwise, use a default template.
  1340. (org-e-latex--wrap-label
  1341. inlinetask
  1342. (let ((full-title
  1343. (concat
  1344. (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1345. (when priority (format "\\framebox{\\#%c} " priority))
  1346. title
  1347. (when tags (format "\\hfill{}\\textsc{:%s:}"
  1348. (mapconcat 'identity tags ":"))))))
  1349. (format (concat "\\begin{center}\n"
  1350. "\\fbox{\n"
  1351. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1352. "%s\n\n"
  1353. "\\rule[.8em]{\\textwidth}{2pt}\n\n"
  1354. "%s"
  1355. "\\end{minipage}\n"
  1356. "}\n"
  1357. "\\end{center}")
  1358. full-title contents))))))
  1359. ;;;; Italic
  1360. (defun org-e-latex-italic (italic contents info)
  1361. "Transcode ITALIC from Org to LaTeX.
  1362. CONTENTS is the text with italic markup. INFO is a plist holding
  1363. contextual information."
  1364. (org-e-latex--text-markup contents 'italic))
  1365. ;;;; Item
  1366. (defun org-e-latex-item (item contents info)
  1367. "Transcode an ITEM element from Org to LaTeX.
  1368. CONTENTS holds the contents of the item. INFO is a plist holding
  1369. contextual information."
  1370. (let* ((counter
  1371. (let ((count (org-element-property :counter item))
  1372. (level
  1373. (loop for parent in (org-export-get-genealogy item)
  1374. count (eq (org-element-type parent) 'plain-list)
  1375. until (eq (org-element-type parent) 'headline))))
  1376. (and count
  1377. (< level 5)
  1378. (format "\\setcounter{enum%s}{%s}\n"
  1379. (nth (1- level) '("i" "ii" "iii" "iv"))
  1380. (1- count)))))
  1381. (checkbox (case (org-element-property :checkbox item)
  1382. (on "$\\boxtimes$ ")
  1383. (off "$\\Box$ ")
  1384. (trans "$\\boxminus$ ")))
  1385. (tag (let ((tag (org-element-property :tag item)))
  1386. ;; Check-boxes must belong to the tag.
  1387. (and tag (format "[%s] "
  1388. (concat checkbox
  1389. (org-export-data tag info)))))))
  1390. (concat counter "\\item" (or tag (concat " " checkbox))
  1391. (and contents (org-trim contents))
  1392. ;; If there are footnotes references in tag, be sure to
  1393. ;; add their definition at the end of the item. This
  1394. ;; workaround is necessary since "\footnote{}" command is
  1395. ;; not supported in tags.
  1396. (and tag
  1397. (org-e-latex--delayed-footnotes-definitions
  1398. (org-element-property :tag item) info)))))
  1399. ;;;; Keyword
  1400. (defun org-e-latex-keyword (keyword contents info)
  1401. "Transcode a KEYWORD element from Org to LaTeX.
  1402. CONTENTS is nil. INFO is a plist holding contextual information."
  1403. (let ((key (org-element-property :key keyword))
  1404. (value (org-element-property :value keyword)))
  1405. (cond
  1406. ((string= key "LATEX") value)
  1407. ((string= key "INDEX") (format "\\index{%s}" value))
  1408. ;; Invisible targets.
  1409. ((string= key "TARGET") nil)
  1410. ((string= key "TOC")
  1411. (let ((value (downcase value)))
  1412. (cond
  1413. ((string-match "\\<headlines\\>" value)
  1414. (let ((depth (or (and (string-match "[0-9]+" value)
  1415. (string-to-number (match-string 0 value)))
  1416. (plist-get info :with-toc))))
  1417. (concat
  1418. (when (wholenump depth)
  1419. (format "\\setcounter{tocdepth}{%s}\n" depth))
  1420. "\\tableofcontents")))
  1421. ((string= "tables" value) "\\listoftables")
  1422. ((string= "figures" value) "\\listoffigures")
  1423. ((string= "listings" value)
  1424. (cond
  1425. ((eq org-e-latex-listings 'minted) "\\listoflistings")
  1426. (org-e-latex-listings "\\lstlistoflistings")
  1427. ;; At the moment, src blocks with a caption are wrapped
  1428. ;; into a figure environment.
  1429. (t "\\listoffigures")))))))))
  1430. ;;;; Latex Environment
  1431. (defun org-e-latex-latex-environment (latex-environment contents info)
  1432. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1433. CONTENTS is nil. INFO is a plist holding contextual information."
  1434. (let ((label (org-element-property :name latex-environment))
  1435. (value (org-remove-indentation
  1436. (org-element-property :value latex-environment))))
  1437. (if (not (org-string-nw-p label)) value
  1438. ;; Environment is labelled: label must be within the environment
  1439. ;; (otherwise, a reference pointing to that element will count
  1440. ;; the section instead).
  1441. (with-temp-buffer
  1442. (insert value)
  1443. (goto-char (point-min))
  1444. (forward-line)
  1445. (insert (format "\\label{%s}\n" label))
  1446. (buffer-string)))))
  1447. ;;;; Latex Fragment
  1448. (defun org-e-latex-latex-fragment (latex-fragment contents info)
  1449. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1450. CONTENTS is nil. INFO is a plist holding contextual information."
  1451. (org-element-property :value latex-fragment))
  1452. ;;;; Line Break
  1453. (defun org-e-latex-line-break (line-break contents info)
  1454. "Transcode a LINE-BREAK object from Org to LaTeX.
  1455. CONTENTS is nil. INFO is a plist holding contextual information."
  1456. "\\\\")
  1457. ;;;; Link
  1458. (defun org-e-latex-link--inline-image (link info)
  1459. "Return LaTeX code for an inline image.
  1460. LINK is the link pointing to the inline image. INFO is a plist
  1461. used as a communication channel."
  1462. (let* ((parent (org-export-get-parent-element link))
  1463. (path (let ((raw-path (org-element-property :path link)))
  1464. (if (not (file-name-absolute-p raw-path)) raw-path
  1465. (expand-file-name raw-path))))
  1466. (caption (org-e-latex--caption/label-string parent info))
  1467. ;; Retrieve latex attributes from the element around.
  1468. (attr (let ((raw-attr
  1469. (mapconcat #'identity
  1470. (org-element-property :attr_latex parent)
  1471. " ")))
  1472. (unless (string= raw-attr "") raw-attr)))
  1473. (disposition
  1474. (cond
  1475. ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
  1476. ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
  1477. ((or (and attr (string-match "\\<float\\>" attr))
  1478. (not (string= caption "")))
  1479. 'float)))
  1480. (placement
  1481. (cond
  1482. ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
  1483. (org-match-string-no-properties 1 attr))
  1484. ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
  1485. ((eq disposition 'float)
  1486. (concat "[" org-e-latex-default-figure-position "]"))
  1487. (t ""))))
  1488. ;; Now clear ATTR from any special keyword and set a default
  1489. ;; value if nothing is left.
  1490. (setq attr
  1491. (if (not attr) ""
  1492. (org-trim
  1493. (replace-regexp-in-string
  1494. "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
  1495. (setq attr (cond ((not (string= attr "")) attr)
  1496. ((eq disposition 'float) "width=0.7\\textwidth")
  1497. ((eq disposition 'wrap) "width=0.48\\textwidth")
  1498. (t (or org-e-latex-image-default-option ""))))
  1499. ;; Return proper string, depending on DISPOSITION.
  1500. (case disposition
  1501. (wrap (format "\\begin{wrapfigure}%s
  1502. \\centering
  1503. \\includegraphics[%s]{%s}
  1504. %s\\end{wrapfigure}" placement attr path caption))
  1505. (multicolumn (format "\\begin{figure*}%s
  1506. \\centering
  1507. \\includegraphics[%s]{%s}
  1508. %s\\end{figure*}" placement attr path caption))
  1509. (float (format "\\begin{figure}%s
  1510. \\centering
  1511. \\includegraphics[%s]{%s}
  1512. %s\\end{figure}" placement attr path caption))
  1513. (t (format "\\includegraphics[%s]{%s}" attr path)))))
  1514. (defun org-e-latex-link (link desc info)
  1515. "Transcode a LINK object from Org to LaTeX.
  1516. DESC is the description part of the link, or the empty string.
  1517. INFO is a plist holding contextual information. See
  1518. `org-export-data'."
  1519. (let* ((type (org-element-property :type link))
  1520. (raw-path (org-element-property :path link))
  1521. ;; Ensure DESC really exists, or set it to nil.
  1522. (desc (and (not (string= desc "")) desc))
  1523. (imagep (org-export-inline-image-p
  1524. link org-e-latex-inline-image-rules))
  1525. (path (cond
  1526. ((member type '("http" "https" "ftp" "mailto"))
  1527. (concat type ":" raw-path))
  1528. ((string= type "file")
  1529. (if (file-name-absolute-p raw-path)
  1530. (concat "file://" (expand-file-name raw-path))
  1531. (concat "file://" raw-path)))
  1532. (t raw-path)))
  1533. protocol)
  1534. (cond
  1535. ;; Image file.
  1536. (imagep (org-e-latex-link--inline-image link info))
  1537. ;; Radio link: Transcode target's contents and use them as link's
  1538. ;; description.
  1539. ((string= type "radio")
  1540. (let ((destination (org-export-resolve-radio-link link info)))
  1541. (when destination
  1542. (format "\\hyperref[%s]{%s}"
  1543. (org-export-solidify-link-text path)
  1544. (org-export-data (org-element-contents destination) info)))))
  1545. ;; Links pointing to an headline: Find destination and build
  1546. ;; appropriate referencing command.
  1547. ((member type '("custom-id" "fuzzy" "id"))
  1548. (let ((destination (if (string= type "fuzzy")
  1549. (org-export-resolve-fuzzy-link link info)
  1550. (org-export-resolve-id-link link info))))
  1551. (case (org-element-type destination)
  1552. ;; Id link points to an external file.
  1553. (plain-text
  1554. (if desc (format "\\href{file://%s}{%s}" destination desc)
  1555. (format "\\url{file://%s}" destination)))
  1556. ;; Fuzzy link points nowhere.
  1557. ('nil
  1558. (format org-e-latex-link-with-unknown-path-format
  1559. (or desc
  1560. (org-export-data
  1561. (org-element-property :raw-link link) info))))
  1562. ;; Fuzzy link points to an invisible target.
  1563. (keyword nil)
  1564. ;; LINK points to an headline. If headlines are numbered
  1565. ;; and the link has no description, display headline's
  1566. ;; number. Otherwise, display description or headline's
  1567. ;; title.
  1568. (headline
  1569. (let ((label
  1570. (format "sec-%s"
  1571. (mapconcat
  1572. 'number-to-string
  1573. (org-export-get-headline-number destination info)
  1574. "-"))))
  1575. (if (and (plist-get info :section-numbers) (not desc))
  1576. (format "\\ref{%s}" label)
  1577. (format "\\hyperref[%s]{%s}" label
  1578. (or desc
  1579. (org-export-data
  1580. (org-element-property :title destination) info))))))
  1581. ;; Fuzzy link points to a target. Do as above.
  1582. (otherwise
  1583. (let ((path (org-export-solidify-link-text path)))
  1584. (if (not desc) (format "\\ref{%s}" path)
  1585. (format "\\hyperref[%s]{%s}" path desc)))))))
  1586. ;; Coderef: replace link with the reference name or the
  1587. ;; equivalent line number.
  1588. ((string= type "coderef")
  1589. (format (org-export-get-coderef-format path desc)
  1590. (org-export-resolve-coderef path info)))
  1591. ;; Link type is handled by a special function.
  1592. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  1593. (funcall protocol (org-link-unescape path) desc 'latex))
  1594. ;; External link with a description part.
  1595. ((and path desc) (format "\\href{%s}{%s}" path desc))
  1596. ;; External link without a description part.
  1597. (path (format "\\url{%s}" path))
  1598. ;; No path, only description. Try to do something useful.
  1599. (t (format org-e-latex-link-with-unknown-path-format desc)))))
  1600. ;;;; Paragraph
  1601. (defun org-e-latex-paragraph (paragraph contents info)
  1602. "Transcode a PARAGRAPH element from Org to LaTeX.
  1603. CONTENTS is the contents of the paragraph, as a string. INFO is
  1604. the plist used as a communication channel."
  1605. contents)
  1606. ;;;; Plain List
  1607. (defun org-e-latex-plain-list (plain-list contents info)
  1608. "Transcode a PLAIN-LIST element from Org to LaTeX.
  1609. CONTENTS is the contents of the list. INFO is a plist holding
  1610. contextual information."
  1611. (let* ((type (org-element-property :type plain-list))
  1612. (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
  1613. "inparadesc" "asparadesc"))
  1614. (paralist-regexp (concat
  1615. "\\("
  1616. (mapconcat 'identity paralist-types "\\|")
  1617. "\\)"))
  1618. (attr (mapconcat #'identity
  1619. (org-element-property :attr_latex plain-list)
  1620. " "))
  1621. (latex-type (cond
  1622. ((and attr
  1623. (string-match
  1624. (format "\\<%s\\>" paralist-regexp) attr))
  1625. (match-string 1 attr))
  1626. ((eq type 'ordered) "enumerate")
  1627. ((eq type 'unordered) "itemize")
  1628. ((eq type 'descriptive) "description"))))
  1629. (org-e-latex--wrap-label
  1630. plain-list
  1631. (format "\\begin{%s}%s\n%s\\end{%s}"
  1632. latex-type
  1633. ;; Once special environment, if any, has been removed, the
  1634. ;; rest of the attributes will be optional arguments.
  1635. ;; They will be put inside square brackets if necessary.
  1636. (let ((opt (replace-regexp-in-string
  1637. (format " *%s *" paralist-regexp) "" attr)))
  1638. (cond ((string= opt "") "")
  1639. ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
  1640. (t (format "[%s]" opt))))
  1641. contents
  1642. latex-type))))
  1643. ;;;; Plain Text
  1644. (defun org-e-latex-plain-text (text info)
  1645. "Transcode a TEXT string from Org to LaTeX.
  1646. TEXT is the string to transcode. INFO is a plist holding
  1647. contextual information."
  1648. (let ((specialp (plist-get info :with-special-strings)))
  1649. ;; Protect %, #, &, $, ~, ^, _, { and }.
  1650. (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
  1651. (setq text
  1652. (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
  1653. ;; Protect \. If special strings are used, be careful not to
  1654. ;; protect "\" in "\-" constructs.
  1655. (let ((symbols (if specialp "-%$#&{}~^_\\" "%$#&{}~^_\\")))
  1656. (setq text
  1657. (replace-regexp-in-string
  1658. (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
  1659. "$\\backslash$" text nil t 1)))
  1660. ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
  1661. (let ((case-fold-search nil)
  1662. (start 0))
  1663. (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
  1664. (setq text (replace-match
  1665. (format "\\%s{}" (match-string 1 text)) nil t text)
  1666. start (match-end 0))))
  1667. ;; Handle quotation marks.
  1668. (setq text (org-e-latex--quotation-marks text info))
  1669. ;; Convert special strings.
  1670. (when specialp
  1671. (setq text (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" text nil t)))
  1672. ;; Handle break preservation if required.
  1673. (when (plist-get info :preserve-breaks)
  1674. (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
  1675. text)))
  1676. ;; Return value.
  1677. text))
  1678. ;;;; Planning
  1679. (defun org-e-latex-planning (planning contents info)
  1680. "Transcode a PLANNING element from Org to LaTeX.
  1681. CONTENTS is nil. INFO is a plist holding contextual
  1682. information."
  1683. (concat
  1684. "\\noindent"
  1685. (mapconcat
  1686. 'identity
  1687. (delq nil
  1688. (list
  1689. (let ((closed (org-element-property :closed planning)))
  1690. (when closed
  1691. (concat
  1692. (format "\\textbf{%s} " org-closed-string)
  1693. (format org-e-latex-inactive-timestamp-format
  1694. (org-translate-time closed)))))
  1695. (let ((deadline (org-element-property :deadline planning)))
  1696. (when deadline
  1697. (concat
  1698. (format "\\textbf{%s} " org-deadline-string)
  1699. (format org-e-latex-active-timestamp-format
  1700. (org-translate-time deadline)))))
  1701. (let ((scheduled (org-element-property :scheduled planning)))
  1702. (when scheduled
  1703. (concat
  1704. (format "\\textbf{%s} " org-scheduled-string)
  1705. (format org-e-latex-active-timestamp-format
  1706. (org-translate-time scheduled)))))))
  1707. " ")
  1708. "\\\\"))
  1709. ;;;; Property Drawer
  1710. (defun org-e-latex-property-drawer (property-drawer contents info)
  1711. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  1712. CONTENTS is nil. INFO is a plist holding contextual
  1713. information."
  1714. ;; The property drawer isn't exported but we want separating blank
  1715. ;; lines nonetheless.
  1716. "")
  1717. ;;;; Quote Block
  1718. (defun org-e-latex-quote-block (quote-block contents info)
  1719. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  1720. CONTENTS holds the contents of the block. INFO is a plist
  1721. holding contextual information."
  1722. (org-e-latex--wrap-label
  1723. quote-block
  1724. (format "\\begin{quote}\n%s\\end{quote}" contents)))
  1725. ;;;; Quote Section
  1726. (defun org-e-latex-quote-section (quote-section contents info)
  1727. "Transcode a QUOTE-SECTION element from Org to LaTeX.
  1728. CONTENTS is nil. INFO is a plist holding contextual information."
  1729. (let ((value (org-remove-indentation
  1730. (org-element-property :value quote-section))))
  1731. (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  1732. ;;;; Radio Target
  1733. (defun org-e-latex-radio-target (radio-target text info)
  1734. "Transcode a RADIO-TARGET object from Org to LaTeX.
  1735. TEXT is the text of the target. INFO is a plist holding
  1736. contextual information."
  1737. (format "\\label{%s}%s"
  1738. (org-export-solidify-link-text
  1739. (org-element-property :value radio-target))
  1740. text))
  1741. ;;;; Section
  1742. (defun org-e-latex-section (section contents info)
  1743. "Transcode a SECTION element from Org to LaTeX.
  1744. CONTENTS holds the contents of the section. INFO is a plist
  1745. holding contextual information."
  1746. contents)
  1747. ;;;; Special Block
  1748. (defun org-e-latex-special-block (special-block contents info)
  1749. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  1750. CONTENTS holds the contents of the block. INFO is a plist
  1751. holding contextual information."
  1752. (let ((type (downcase (org-element-property :type special-block))))
  1753. (org-e-latex--wrap-label
  1754. special-block
  1755. (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
  1756. ;;;; Src Block
  1757. (defun org-e-latex-src-block (src-block contents info)
  1758. "Transcode a SRC-BLOCK element from Org to LaTeX.
  1759. CONTENTS holds the contents of the item. INFO is a plist holding
  1760. contextual information."
  1761. (let* ((lang (org-element-property :language src-block))
  1762. (caption (org-element-property :caption src-block))
  1763. (label (org-element-property :name src-block))
  1764. (custom-env (and lang
  1765. (cadr (assq (intern lang)
  1766. org-e-latex-custom-lang-environments))))
  1767. (num-start (case (org-element-property :number-lines src-block)
  1768. (continued (org-export-get-loc src-block info))
  1769. (new 0)))
  1770. (retain-labels (org-element-property :retain-labels src-block)))
  1771. (cond
  1772. ;; Case 1. No source fontification.
  1773. ((not org-e-latex-listings)
  1774. (let ((caption-str (org-e-latex--caption/label-string src-block info))
  1775. (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
  1776. (format
  1777. (or float-env "%s")
  1778. (concat caption-str
  1779. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1780. (org-export-format-code-default src-block info))))))
  1781. ;; Case 2. Custom environment.
  1782. (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
  1783. custom-env
  1784. (org-export-format-code-default src-block info)
  1785. custom-env))
  1786. ;; Case 3. Use minted package.
  1787. ((eq org-e-latex-listings 'minted)
  1788. (let ((float-env
  1789. (when (or label caption)
  1790. (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
  1791. (org-e-latex--caption/label-string src-block info))))
  1792. (body
  1793. (format
  1794. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  1795. ;; Options.
  1796. (org-e-latex--make-option-string
  1797. (if (not num-start) org-e-latex-minted-options
  1798. (append `(("linenos")
  1799. ("firstnumber" ,(number-to-string (1+ num-start))))
  1800. org-e-latex-minted-options)))
  1801. ;; Language.
  1802. (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
  1803. ;; Source code.
  1804. (let* ((code-info (org-export-unravel-code src-block))
  1805. (max-width
  1806. (apply 'max
  1807. (mapcar 'length
  1808. (org-split-string (car code-info) "\n")))))
  1809. (org-export-format-code
  1810. (car code-info)
  1811. (lambda (loc num ref)
  1812. (concat
  1813. loc
  1814. (when ref
  1815. ;; Ensure references are flushed to the right,
  1816. ;; separated with 6 spaces from the widest line
  1817. ;; of code.
  1818. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1819. (format "(%s)" ref)))))
  1820. nil (and retain-labels (cdr code-info)))))))
  1821. ;; Return value.
  1822. (if float-env (format float-env body) body)))
  1823. ;; Case 4. Use listings package.
  1824. (t
  1825. (let ((lst-lang
  1826. (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
  1827. (caption-str
  1828. (when caption
  1829. (let ((main (org-export-get-caption src-block))
  1830. (secondary (org-export-get-caption src-block t)))
  1831. (if (not secondary) (format "{%s}" (org-export-data main info))
  1832. (format "{[%s]%s}"
  1833. (org-export-data secondary info)
  1834. (org-export-data main info)))))))
  1835. (concat
  1836. ;; Options.
  1837. (format "\\lstset{%s}\n"
  1838. (org-e-latex--make-option-string
  1839. (append org-e-latex-listings-options
  1840. `(("language" ,lst-lang))
  1841. (when label `(("label" ,label)))
  1842. (when caption-str `(("caption" ,caption-str)))
  1843. (cond ((not num-start) '(("numbers" "none")))
  1844. ((zerop num-start) '(("numbers" "left")))
  1845. (t `(("numbers" "left")
  1846. ("firstnumber"
  1847. ,(number-to-string (1+ num-start)))))))))
  1848. ;; Source code.
  1849. (format
  1850. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  1851. (let* ((code-info (org-export-unravel-code src-block))
  1852. (max-width
  1853. (apply 'max
  1854. (mapcar 'length
  1855. (org-split-string (car code-info) "\n")))))
  1856. (org-export-format-code
  1857. (car code-info)
  1858. (lambda (loc num ref)
  1859. (concat
  1860. loc
  1861. (when ref
  1862. ;; Ensure references are flushed to the right,
  1863. ;; separated with 6 spaces from the widest line of
  1864. ;; code
  1865. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1866. (format "(%s)" ref)))))
  1867. nil (and retain-labels (cdr code-info)))))))))))
  1868. ;;;; Statistics Cookie
  1869. (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
  1870. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  1871. CONTENTS is nil. INFO is a plist holding contextual information."
  1872. (replace-regexp-in-string
  1873. "%" "\\%" (org-element-property :value statistics-cookie) nil t))
  1874. ;;;; Strike-Through
  1875. (defun org-e-latex-strike-through (strike-through contents info)
  1876. "Transcode STRIKE-THROUGH from Org to LaTeX.
  1877. CONTENTS is the text with strike-through markup. INFO is a plist
  1878. holding contextual information."
  1879. (org-e-latex--text-markup contents 'strike-through))
  1880. ;;;; Subscript
  1881. (defun org-e-latex-subscript (subscript contents info)
  1882. "Transcode a SUBSCRIPT object from Org to LaTeX.
  1883. CONTENTS is the contents of the object. INFO is a plist holding
  1884. contextual information."
  1885. (if (= (length contents) 1) (format "$_%s$" contents)
  1886. ;; Handle multiple objects in SUBSCRIPT by creating a subscript
  1887. ;; command for each of them.
  1888. (let ((prev-blanks 0))
  1889. (mapconcat
  1890. (lambda (obj)
  1891. (case (org-element-type obj)
  1892. ((entity latex-fragment)
  1893. (setq prev-blanks (org-element-property :post-blank obj))
  1894. (let ((data (org-trim (org-export-data obj info))))
  1895. (string-match
  1896. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  1897. data)
  1898. (format "$_{%s}$" (match-string 1 data))))
  1899. (plain-text
  1900. (format "$_\\mathrm{%s}$"
  1901. (concat (make-string prev-blanks ? )
  1902. ;; mathrm command doesn't handle spaces,
  1903. ;; so we have to enforce them.
  1904. (replace-regexp-in-string
  1905. " " "\\\\ " (org-export-data obj info)))))
  1906. (otherwise
  1907. (setq prev-blanks (org-element-property :post-blank obj))
  1908. (format "$_{%s}$" (org-export-data obj info)))))
  1909. (org-element-contents subscript) ""))))
  1910. ;;;; Superscript
  1911. (defun org-e-latex-superscript (superscript contents info)
  1912. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  1913. CONTENTS is the contents of the object. INFO is a plist holding
  1914. contextual information."
  1915. (if (= (length contents) 1) (format "$^%s$" contents)
  1916. ;; Handle multiple objects in SUPERSCRIPT by creating
  1917. ;; a superscript command for each of them.
  1918. (let ((prev-blanks 0))
  1919. (mapconcat
  1920. (lambda (obj)
  1921. (case (org-element-type obj)
  1922. ((entity latex-fragment)
  1923. (setq prev-blanks (org-element-property :post-blank obj))
  1924. (let ((data (org-trim (org-export-data obj info))))
  1925. (string-match
  1926. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  1927. data)
  1928. (format "$^{%s}$" (match-string 1 data))))
  1929. (plain-text
  1930. (format "$^\\mathrm{%s}$"
  1931. (concat (make-string prev-blanks ? )
  1932. ;; mathrm command doesn't handle spaces,
  1933. ;; so we have to enforce them.
  1934. (replace-regexp-in-string
  1935. " " "\\\\ " (org-export-data obj info)))))
  1936. (otherwise
  1937. (setq prev-blanks (org-element-property :post-blank obj))
  1938. (format "$^{%s}$" (org-export-data obj info)))))
  1939. (org-element-contents superscript) ""))))
  1940. ;;;; Table
  1941. ;;
  1942. ;; `org-e-latex-table' is the entry point for table transcoding. It
  1943. ;; takes care of tables with a "verbatim" attribute. Otherwise, it
  1944. ;; delegates the job to either `org-e-latex-table--table.el-table' or
  1945. ;; `org-e-latex-table--org-table' functions, depending of the type of
  1946. ;; the table.
  1947. ;;
  1948. ;; `org-e-latex-table--align-string' is a subroutine used to build
  1949. ;; alignment string for Org tables.
  1950. (defun org-e-latex-table (table contents info)
  1951. "Transcode a TABLE element from Org to LaTeX.
  1952. CONTENTS is the contents of the table. INFO is a plist holding
  1953. contextual information."
  1954. (cond
  1955. ;; Case 1: verbatim table.
  1956. ((or org-e-latex-tables-verbatim
  1957. (let ((attr (mapconcat 'identity
  1958. (org-element-property :attr_latex table)
  1959. " ")))
  1960. (and attr (string-match "\\<verbatim\\>" attr))))
  1961. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  1962. ;; Re-create table, without affiliated keywords.
  1963. (org-trim
  1964. (org-element-interpret-data
  1965. `(table nil ,@(org-element-contents table))))))
  1966. ;; Case 2: table.el table. Convert it using appropriate tools.
  1967. ((eq (org-element-property :type table) 'table.el)
  1968. (org-e-latex-table--table.el-table table contents info))
  1969. ;; Case 3: Standard table.
  1970. (t (org-e-latex-table--org-table table contents info))))
  1971. (defun org-e-latex-table--align-string (table info)
  1972. "Return an appropriate LaTeX alignment string.
  1973. TABLE is the considered table. INFO is a plist used as
  1974. a communication channel."
  1975. (let ((attr (mapconcat 'identity
  1976. (org-element-property :attr_latex table)
  1977. " ")))
  1978. (if (string-match "\\<align=\\(\\S-+\\)" attr) (match-string 1 attr)
  1979. (let (alignment)
  1980. ;; Extract column groups and alignment from first (non-rule)
  1981. ;; row.
  1982. (org-element-map
  1983. (org-element-map
  1984. table 'table-row
  1985. (lambda (row)
  1986. (and (eq (org-element-property :type row) 'standard) row))
  1987. info 'first-match)
  1988. 'table-cell
  1989. (lambda (cell)
  1990. (let ((borders (org-export-table-cell-borders cell info)))
  1991. ;; Check left border for the first cell only.
  1992. (when (and (memq 'left borders) (not alignment))
  1993. (push "|" alignment))
  1994. (push (case (org-export-table-cell-alignment cell info)
  1995. (left "l")
  1996. (right "r")
  1997. (center "c"))
  1998. alignment)
  1999. (when (memq 'right borders) (push "|" alignment))))
  2000. info)
  2001. (apply 'concat (reverse alignment))))))
  2002. (defun org-e-latex-table--org-table (table contents info)
  2003. "Return appropriate LaTeX code for an Org table.
  2004. TABLE is the table type element to transcode. CONTENTS is its
  2005. contents, as a string. INFO is a plist used as a communication
  2006. channel.
  2007. This function assumes TABLE has `org' as its `:type' attribute."
  2008. (let* ((label (org-element-property :name table))
  2009. (caption (org-e-latex--caption/label-string table info))
  2010. (attr (mapconcat 'identity
  2011. (org-element-property :attr_latex table)
  2012. " "))
  2013. ;; Determine alignment string.
  2014. (alignment (org-e-latex-table--align-string table info))
  2015. ;; Determine environment for the table: longtable, tabular...
  2016. (table-env (cond
  2017. ((not attr) org-e-latex-default-table-environment)
  2018. ((string-match "\\<longtable\\>" attr) "longtable")
  2019. ((string-match "\\<tabular.?\\>" attr)
  2020. (org-match-string-no-properties 0 attr))
  2021. (t org-e-latex-default-table-environment)))
  2022. ;; If table is a float, determine environment: table, table*
  2023. ;; or sidewaystable.
  2024. (float-env (cond
  2025. ((string= "longtable" table-env) nil)
  2026. ((and attr (string-match "\\<sidewaystable\\>" attr))
  2027. "sidewaystable")
  2028. ((and attr
  2029. (or (string-match (regexp-quote "table*") attr)
  2030. (string-match "\\<multicolumn\\>" attr)))
  2031. "table*")
  2032. ((or (not (string= caption "")) label) "table")))
  2033. ;; Extract others display options.
  2034. (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
  2035. (org-match-string-no-properties 1 attr)))
  2036. (placement
  2037. (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
  2038. (org-match-string-no-properties 1 attr)
  2039. (format "[%s]" org-e-latex-default-figure-position))))
  2040. ;; Prepare the final format string for the table.
  2041. (cond
  2042. ;; Longtable.
  2043. ((string= "longtable" table-env)
  2044. (format
  2045. "\\begin{longtable}{%s}\n%s%s%s\\end{longtable}"
  2046. alignment
  2047. (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
  2048. (concat (org-trim caption) "\\\\\n"))
  2049. contents
  2050. (if (or org-e-latex-table-caption-above (string= "" caption)) ""
  2051. (concat (org-trim caption) "\\\\\n"))))
  2052. ;; Others.
  2053. (t (concat (when float-env
  2054. (concat
  2055. (format "\\begin{%s}%s\n" float-env placement)
  2056. (if org-e-latex-table-caption-above caption "")))
  2057. (when org-e-latex-tables-centered "\\begin{center}\n")
  2058. (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  2059. table-env
  2060. (if width (format "{%s}" width) "")
  2061. alignment
  2062. contents
  2063. table-env)
  2064. (when org-e-latex-tables-centered "\n\\end{center}")
  2065. (when float-env
  2066. (concat (if org-e-latex-table-caption-above "" caption)
  2067. (format "\n\\end{%s}" float-env))))))))
  2068. (defun org-e-latex-table--table.el-table (table contents info)
  2069. "Return appropriate LaTeX code for a table.el table.
  2070. TABLE is the table type element to transcode. CONTENTS is its
  2071. contents, as a string. INFO is a plist used as a communication
  2072. channel.
  2073. This function assumes TABLE has `table.el' as its `:type'
  2074. attribute."
  2075. (require 'table)
  2076. ;; Ensure "*org-export-table*" buffer is empty.
  2077. (with-current-buffer (get-buffer-create "*org-export-table*")
  2078. (erase-buffer))
  2079. (let ((output (with-temp-buffer
  2080. (insert (org-element-property :value table))
  2081. (goto-char 1)
  2082. (re-search-forward "^[ \t]*|[^|]" nil t)
  2083. (table-generate-source 'latex "*org-export-table*")
  2084. (with-current-buffer "*org-export-table*"
  2085. (org-trim (buffer-string))))))
  2086. (kill-buffer (get-buffer "*org-export-table*"))
  2087. ;; Remove left out comments.
  2088. (while (string-match "^%.*\n" output)
  2089. (setq output (replace-match "" t t output)))
  2090. ;; When the "rmlines" attribute is provided, remove all hlines but
  2091. ;; the the one separating heading from the table body.
  2092. (let ((attr (mapconcat 'identity
  2093. (org-element-property :attr_latex table)
  2094. " ")))
  2095. (when (and attr (string-match "\\<rmlines\\>" attr))
  2096. (let ((n 0) (pos 0))
  2097. (while (and (< (length output) pos)
  2098. (setq pos (string-match "^\\\\hline\n?" output pos)))
  2099. (incf n)
  2100. (unless (= n 2)
  2101. (setq output (replace-match "" nil nil output)))))))
  2102. (if (not org-e-latex-tables-centered) output
  2103. (format "\\begin{center}\n%s\n\\end{center}" output))))
  2104. ;;;; Table Cell
  2105. (defun org-e-latex-table-cell (table-cell contents info)
  2106. "Transcode a TABLE-CELL element from Org to LaTeX.
  2107. CONTENTS is the cell contents. INFO is a plist used as
  2108. a communication channel."
  2109. (concat (if (and contents
  2110. org-e-latex-table-scientific-notation
  2111. (string-match orgtbl-exp-regexp contents))
  2112. ;; Use appropriate format string for scientific
  2113. ;; notation.
  2114. (format org-e-latex-table-scientific-notation
  2115. (match-string 1 contents)
  2116. (match-string 2 contents))
  2117. contents)
  2118. (when (org-export-get-next-element table-cell info) " & ")))
  2119. ;;;; Table Row
  2120. (defun org-e-latex-table-row (table-row contents info)
  2121. "Transcode a TABLE-ROW element from Org to LaTeX.
  2122. CONTENTS is the contents of the row. INFO is a plist used as
  2123. a communication channel."
  2124. ;; Rules are ignored since table separators are deduced from
  2125. ;; borders of the current row.
  2126. (when (eq (org-element-property :type table-row) 'standard)
  2127. (let* ((attr (mapconcat 'identity
  2128. (org-element-property
  2129. :attr_latex (org-export-get-parent table-row))
  2130. " "))
  2131. (longtablep (and attr (string-match "\\<longtable\\>" attr)))
  2132. (booktabsp
  2133. (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
  2134. org-e-latex-tables-booktabs))
  2135. ;; TABLE-ROW's borders are extracted from its first cell.
  2136. (borders
  2137. (org-export-table-cell-borders
  2138. (car (org-element-contents table-row)) info)))
  2139. (concat
  2140. ;; When BOOKTABS are activated enforce top-rule even when no
  2141. ;; hline was specifically marked.
  2142. (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
  2143. ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
  2144. contents "\\\\\n"
  2145. (cond
  2146. ;; Special case for long tables. Define header and footers.
  2147. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  2148. (format "%s
  2149. \\endhead
  2150. %s\\multicolumn{%d}{r}{Continued on next page} \\\\
  2151. \\endfoot
  2152. \\endlastfoot"
  2153. (if booktabsp "\\midrule" "\\hline")
  2154. (if booktabsp "\\midrule" "\\hline")
  2155. ;; Number of columns.
  2156. (cdr (org-export-table-dimensions
  2157. (org-export-get-parent-table table-row) info))))
  2158. ;; When BOOKTABS are activated enforce bottom rule even when
  2159. ;; no hline was specifically marked.
  2160. ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
  2161. ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
  2162. ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
  2163. ;;;; Target
  2164. (defun org-e-latex-target (target contents info)
  2165. "Transcode a TARGET object from Org to LaTeX.
  2166. CONTENTS is nil. INFO is a plist holding contextual
  2167. information."
  2168. (format "\\label{%s}"
  2169. (org-export-solidify-link-text (org-element-property :value target))))
  2170. ;;;; Timestamp
  2171. (defun org-e-latex-timestamp (timestamp contents info)
  2172. "Transcode a TIMESTAMP object from Org to LaTeX.
  2173. CONTENTS is nil. INFO is a plist holding contextual
  2174. information."
  2175. (let ((value (org-translate-time (org-element-property :value timestamp)))
  2176. (range-end (org-element-property :range-end timestamp)))
  2177. (case (org-element-property :type timestamp)
  2178. (active (format org-e-latex-active-timestamp-format value))
  2179. (active-range
  2180. (concat (format org-e-latex-active-timestamp-format value)
  2181. "--"
  2182. (format org-e-latex-active-timestamp-format
  2183. (org-translate-time range-end))))
  2184. (inactive (format org-e-latex-inactive-timestamp-format value))
  2185. (inactive-range
  2186. (concat (format org-e-latex-inactive-timestamp-format value)
  2187. "--"
  2188. (format org-e-latex-inactive-timestamp-format
  2189. (org-translate-time range-end))))
  2190. (otherwise (format org-e-latex-diary-timestamp-format value)))))
  2191. ;;;; Underline
  2192. (defun org-e-latex-underline (underline contents info)
  2193. "Transcode UNDERLINE from Org to LaTeX.
  2194. CONTENTS is the text with underline markup. INFO is a plist
  2195. holding contextual information."
  2196. (org-e-latex--text-markup contents 'underline))
  2197. ;;;; Verbatim
  2198. (defun org-e-latex-verbatim (verbatim contents info)
  2199. "Transcode a VERBATIM object from Org to LaTeX.
  2200. CONTENTS is nil. INFO is a plist used as a communication
  2201. channel."
  2202. (org-e-latex--text-markup (org-element-property :value verbatim) 'verbatim))
  2203. ;;;; Verse Block
  2204. (defun org-e-latex-verse-block (verse-block contents info)
  2205. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  2206. CONTENTS is verse block contents. INFO is a plist holding
  2207. contextual information."
  2208. (org-e-latex--wrap-label
  2209. verse-block
  2210. ;; In a verse environment, add a line break to each newline
  2211. ;; character and change each white space at beginning of a line
  2212. ;; into a space of 1 em. Also change each blank line with
  2213. ;; a vertical space of 1 em.
  2214. (progn
  2215. (setq contents (replace-regexp-in-string
  2216. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  2217. (replace-regexp-in-string
  2218. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
  2219. (while (string-match "^[ \t]+" contents)
  2220. (let ((new-str (format "\\hspace*{%dem}"
  2221. (length (match-string 0 contents)))))
  2222. (setq contents (replace-match new-str nil t contents))))
  2223. (format "\\begin{verse}\n%s\\end{verse}" contents))))
  2224. ;;; Interactive functions
  2225. ;;;###autoload
  2226. (defun org-e-latex-export-as-latex
  2227. (&optional subtreep visible-only body-only ext-plist)
  2228. "Export current buffer as a LaTeX buffer.
  2229. If narrowing is active in the current buffer, only export its
  2230. narrowed part.
  2231. If a region is active, export that region.
  2232. When optional argument SUBTREEP is non-nil, export the sub-tree
  2233. at point, extracting information from the headline properties
  2234. first.
  2235. When optional argument VISIBLE-ONLY is non-nil, don't export
  2236. contents of hidden elements.
  2237. When optional argument BODY-ONLY is non-nil, only write code
  2238. between \"\\begin{document}\" and \"\\end{document}\".
  2239. EXT-PLIST, when provided, is a property list with external
  2240. parameters overriding Org default settings, but still inferior to
  2241. file-local settings.
  2242. Export is done in a buffer named \"*Org E-LATEX Export*\", which
  2243. will be displayed when `org-export-show-temporary-export-buffer'
  2244. is non-nil."
  2245. (interactive)
  2246. (let ((outbuf (org-export-to-buffer
  2247. 'e-latex "*Org E-LATEX Export*"
  2248. subtreep visible-only body-only ext-plist)))
  2249. (with-current-buffer outbuf (LaTeX-mode))
  2250. (when org-export-show-temporary-export-buffer
  2251. (switch-to-buffer-other-window outbuf))))
  2252. ;;;###autoload
  2253. (defun org-e-latex-export-to-latex
  2254. (&optional subtreep visible-only body-only ext-plist pub-dir)
  2255. "Export current buffer to a LaTeX file.
  2256. If narrowing is active in the current buffer, only export its
  2257. narrowed part.
  2258. If a region is active, export that region.
  2259. When optional argument SUBTREEP is non-nil, export the sub-tree
  2260. at point, extracting information from the headline properties
  2261. first.
  2262. When optional argument VISIBLE-ONLY is non-nil, don't export
  2263. contents of hidden elements.
  2264. When optional argument BODY-ONLY is non-nil, only write code
  2265. between \"\\begin{document}\" and \"\\end{document}\".
  2266. EXT-PLIST, when provided, is a property list with external
  2267. parameters overriding Org default settings, but still inferior to
  2268. file-local settings.
  2269. When optional argument PUB-DIR is set, use it as the publishing
  2270. directory.
  2271. Return output file's name."
  2272. (interactive)
  2273. (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
  2274. (org-export-to-file
  2275. 'e-latex outfile subtreep visible-only body-only ext-plist)))
  2276. ;;;###autoload
  2277. (defun org-e-latex-export-to-pdf
  2278. (&optional subtreep visible-only body-only ext-plist pub-dir)
  2279. "Export current buffer to LaTeX then process through to PDF.
  2280. If narrowing is active in the current buffer, only export its
  2281. narrowed part.
  2282. If a region is active, export that region.
  2283. When optional argument SUBTREEP is non-nil, export the sub-tree
  2284. at point, extracting information from the headline properties
  2285. first.
  2286. When optional argument VISIBLE-ONLY is non-nil, don't export
  2287. contents of hidden elements.
  2288. When optional argument BODY-ONLY is non-nil, only write code
  2289. between \"\\begin{document}\" and \"\\end{document}\".
  2290. EXT-PLIST, when provided, is a property list with external
  2291. parameters overriding Org default settings, but still inferior to
  2292. file-local settings.
  2293. When optional argument PUB-DIR is set, use it as the publishing
  2294. directory.
  2295. Return PDF file's name."
  2296. (interactive)
  2297. (org-e-latex-compile
  2298. (org-e-latex-export-to-latex
  2299. subtreep visible-only body-only ext-plist pub-dir)))
  2300. (defun org-e-latex-compile (texfile)
  2301. "Compile a TeX file.
  2302. TEXFILE is the name of the file being compiled. Processing is
  2303. done through the command specified in `org-e-latex-pdf-process'.
  2304. Return PDF file name or an error if it couldn't be produced."
  2305. (let* ((wconfig (current-window-configuration))
  2306. (texfile (file-truename texfile))
  2307. (base (file-name-sans-extension texfile))
  2308. errors)
  2309. (message (format "Processing LaTeX file %s ..." texfile))
  2310. (unwind-protect
  2311. (progn
  2312. (cond
  2313. ;; A function is provided: Apply it.
  2314. ((functionp org-e-latex-pdf-process)
  2315. (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
  2316. ;; A list is provided: Replace %b, %f and %o with appropriate
  2317. ;; values in each command before applying it. Output is
  2318. ;; redirected to "*Org PDF LaTeX Output*" buffer.
  2319. ((consp org-e-latex-pdf-process)
  2320. (let* ((out-dir (or (file-name-directory texfile) "./"))
  2321. (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
  2322. (mapc
  2323. (lambda (command)
  2324. (shell-command
  2325. (replace-regexp-in-string
  2326. "%b" (shell-quote-argument base)
  2327. (replace-regexp-in-string
  2328. "%f" (shell-quote-argument texfile)
  2329. (replace-regexp-in-string
  2330. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  2331. outbuf))
  2332. org-e-latex-pdf-process)
  2333. ;; Collect standard errors from output buffer.
  2334. (setq errors (org-e-latex--collect-errors outbuf))))
  2335. (t (error "No valid command to process to PDF")))
  2336. (let ((pdffile (concat base ".pdf")))
  2337. ;; Check for process failure. Provide collected errors if
  2338. ;; possible.
  2339. (if (not (file-exists-p pdffile))
  2340. (error (concat (format "PDF file %s wasn't produced" pdffile)
  2341. (when errors (concat ": " errors))))
  2342. ;; Else remove log files, when specified, and signal end of
  2343. ;; process to user, along with any error encountered.
  2344. (when org-e-latex-remove-logfiles
  2345. (dolist (ext org-e-latex-logfiles-extensions)
  2346. (let ((file (concat base "." ext)))
  2347. (when (file-exists-p file) (delete-file file)))))
  2348. (message (concat "Process completed"
  2349. (if (not errors) "."
  2350. (concat " with errors: " errors)))))
  2351. ;; Return output file name.
  2352. pdffile))
  2353. (set-window-configuration wconfig))))
  2354. (defun org-e-latex--collect-errors (buffer)
  2355. "Collect some kind of errors from \"pdflatex\" command output.
  2356. BUFFER is the buffer containing output.
  2357. Return collected error types as a string, or nil if there was
  2358. none."
  2359. (with-current-buffer buffer
  2360. (save-excursion
  2361. (goto-char (point-max))
  2362. (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
  2363. (let ((case-fold-search t)
  2364. (errors ""))
  2365. (dolist (latex-error org-e-latex-known-errors)
  2366. (when (save-excursion (re-search-forward (car latex-error) nil t))
  2367. (setq errors (concat errors " " (cdr latex-error)))))
  2368. (and (org-string-nw-p errors) (org-trim errors)))))))
  2369. (provide 'org-e-latex)
  2370. ;;; org-e-latex.el ends here