org-e-latex.el 86 KB

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