org-e-latex.el 75 KB

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