org-e-latex.el 77 KB

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