org-e-latex.el 83 KB

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