org-e-latex.el 83 KB

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