org-e-latex.el 82 KB

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