org-e-latex.el 88 KB

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