org-e-latex.el 75 KB

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