org-e-latex.el 76 KB

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