org-e-latex.el 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613
  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
  739. (append options (list language))
  740. ",")
  741. nil nil header 1))))))
  742. (defun org-e-latex--guess-inputenc (header)
  743. "Set the coding system in inputenc to what the buffer is.
  744. HEADER is the LaTeX header string. Return the new header."
  745. (let* ((cs (or (ignore-errors
  746. (latexenc-coding-system-to-inputenc
  747. buffer-file-coding-system))
  748. "utf8")))
  749. (if (not cs) header
  750. ;; First translate if that is requested.
  751. (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
  752. ;; Then find the \usepackage statement and replace the option.
  753. (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  754. cs header t nil 1))))
  755. (defun org-e-latex--find-verb-separator (s)
  756. "Return a character not used in string S.
  757. This is used to choose a separator for constructs like \\verb."
  758. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  759. (loop for c across ll
  760. when (not (string-match (regexp-quote (char-to-string c)) s))
  761. return (char-to-string c))))
  762. (defun org-e-latex--make-option-string (options)
  763. "Return a comma separated string of keywords and values.
  764. OPTIONS is an alist where the key is the options keyword as
  765. a string, and the value a list containing the keyword value, or
  766. nil."
  767. (mapconcat (lambda (pair)
  768. (concat (first pair)
  769. (when (> (length (second pair)) 0)
  770. (concat "=" (second pair)))))
  771. options
  772. ","))
  773. (defun org-e-latex--quotation-marks (text info)
  774. "Export quotation marks depending on language conventions.
  775. TEXT is a string containing quotation marks to be replaced. INFO
  776. is a plist used as a communication channel."
  777. (mapc (lambda(l)
  778. (let ((start 0))
  779. (while (setq start (string-match (car l) text start))
  780. (let ((new-quote (concat (match-string 1 text) (cdr l))))
  781. (setq text (replace-match new-quote t t text))))))
  782. (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
  783. ;; Falls back on English.
  784. (assoc "en" org-e-latex-quotes))))
  785. text)
  786. (defun org-e-latex--wrap-label (element output)
  787. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  788. This function shouldn't be used for floats. See
  789. `org-e-latex--caption/label-string'."
  790. (let ((label (org-element-property :name element)))
  791. (if (or (not output) (not label) (string= output "") (string= label ""))
  792. output
  793. (concat (format "\\label{%s}\n" label) output))))
  794. (defun org-e-latex--text-markup (text markup)
  795. "Format TEXT depending on MARKUP text markup.
  796. See `org-e-latex-text-markup-alist' for details."
  797. (let ((fmt (cdr (assq markup org-e-latex-text-markup-alist))))
  798. (cond
  799. ;; No format string: Return raw text.
  800. ((not fmt) text)
  801. ;; Handle the `verb' special case: Find and appropriate separator
  802. ;; and use "\\verb" command.
  803. ((eq 'verb fmt)
  804. (let ((separator (org-e-latex--find-verb-separator text)))
  805. (concat "\\verb" separator text separator)))
  806. ;; Handle the `protectedtexttt' special case: Protect some
  807. ;; special chars and use "\texttt{%s}" format string.
  808. ((eq 'protectedtexttt fmt)
  809. (let ((start 0)
  810. (trans '(("\\" . "\\textbackslash{}")
  811. ("~" . "\\textasciitilde{}")
  812. ("^" . "\\textasciicircum{}")))
  813. (rtn "")
  814. char)
  815. (while (string-match "[\\{}$%&_#~^]" text)
  816. (setq char (match-string 0 text))
  817. (if (> (match-beginning 0) 0)
  818. (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
  819. (setq text (substring text (1+ (match-beginning 0))))
  820. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  821. rtn (concat rtn char)))
  822. (setq text (concat rtn text)
  823. fmt "\\texttt{%s}")
  824. (while (string-match "--" text)
  825. (setq text (replace-match "-{}-" t t text)))
  826. (format fmt text)))
  827. ;; Else use format string.
  828. (t (format fmt text)))))
  829. (defun org-e-latex--delayed-footnotes-definitions (element info)
  830. "Return footnotes definitions in ELEMENT as a string.
  831. INFO is a plist used as a communication channel.
  832. Footnotes definitions are returned within \"\\footnotetxt{}\"
  833. commands.
  834. This functions is used within constructs that don't support
  835. \"\\footnote{}\" command (i.e. an item's tag). In that case,
  836. \"\\footnotemark\" is used within the construct and this function
  837. outside of it."
  838. (mapconcat
  839. (lambda (ref)
  840. (format
  841. "\\footnotetext[%s]{%s}"
  842. (org-export-get-footnote-number ref info)
  843. (org-trim
  844. (org-export-data
  845. (org-export-get-footnote-definition ref info) info))))
  846. ;; Find every footnote reference in ELEMENT.
  847. (let* (all-refs
  848. search-refs ; For byte-compiler.
  849. (search-refs
  850. (function
  851. (lambda (data)
  852. ;; Return a list of all footnote references never seen
  853. ;; before in DATA.
  854. (org-element-map
  855. data 'footnote-reference
  856. (lambda (ref)
  857. (when (org-export-footnote-first-reference-p ref info)
  858. (push ref all-refs)
  859. (when (eq (org-element-property :type ref) 'standard)
  860. (funcall search-refs
  861. (org-export-get-footnote-definition ref info)))))
  862. info)
  863. (reverse all-refs)))))
  864. (funcall search-refs element))
  865. ""))
  866. ;;; Template
  867. (defun org-e-latex-template (contents info)
  868. "Return complete document string after LaTeX conversion.
  869. CONTENTS is the transcoded contents string. INFO is a plist
  870. holding export options."
  871. (let ((title (org-export-data (plist-get info :title) info)))
  872. (concat
  873. ;; 1. Time-stamp.
  874. (and (plist-get info :time-stamp-file)
  875. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  876. ;; 2. Document class and packages.
  877. (let ((class (plist-get info :latex-class))
  878. (class-options (plist-get info :latex-class-options)))
  879. (org-element-normalize-string
  880. (let* ((header (nth 1 (assoc class org-e-latex-classes)))
  881. (document-class-string
  882. (and (stringp header)
  883. (if class-options
  884. (replace-regexp-in-string
  885. "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
  886. class-options header t nil 1)
  887. header))))
  888. (when document-class-string
  889. (org-e-latex--guess-babel-language
  890. (org-e-latex--guess-inputenc
  891. (org-splice-latex-header
  892. document-class-string
  893. org-export-latex-default-packages-alist ; defined in org.el
  894. org-export-latex-packages-alist nil ; defined in org.el
  895. (plist-get info :latex-header-extra)))
  896. info)))))
  897. ;; 3. Define alert if not yet defined.
  898. "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
  899. ;; 4. Possibly limit depth for headline numbering.
  900. (let ((sec-num (plist-get info :section-numbers)))
  901. (when (integerp sec-num)
  902. (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
  903. ;; 5. Author.
  904. (let ((author (and (plist-get info :with-author)
  905. (let ((auth (plist-get info :author)))
  906. (and auth (org-export-data auth info)))))
  907. (email (and (plist-get info :with-email)
  908. (org-export-data (plist-get info :email) info))))
  909. (cond ((and author email (not (string= "" email)))
  910. (format "\\author{%s\\thanks{%s}}\n" author email))
  911. (author (format "\\author{%s}\n" author))
  912. (t "\\author{}\n")))
  913. ;; 6. Date.
  914. (let ((date (org-export-data (plist-get info :date) info)))
  915. (and date (format "\\date{%s}\n" date)))
  916. ;; 7. Title
  917. (format "\\title{%s}\n" title)
  918. ;; 8. Hyperref options.
  919. (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
  920. (or (plist-get info :keywords) "")
  921. (or (plist-get info :description) "")
  922. (if (not (plist-get info :with-creator)) ""
  923. (plist-get info :creator)))
  924. ;; 9. Document start.
  925. "\\begin{document}\n\n"
  926. ;; 10. Title command.
  927. (org-element-normalize-string
  928. (cond ((string= "" title) nil)
  929. ((not (stringp org-e-latex-title-command)) nil)
  930. ((string-match "\\(?:[^%]\\|^\\)%s"
  931. org-e-latex-title-command)
  932. (format org-e-latex-title-command title))
  933. (t org-e-latex-title-command)))
  934. ;; 11. Table of contents.
  935. (let ((depth (plist-get info :with-toc)))
  936. (when depth
  937. (concat (when (wholenump depth)
  938. (format "\\setcounter{tocdepth}{%d}\n" depth))
  939. "\\tableofcontents\n\\vspace*{1cm}\n\n")))
  940. ;; 12. Document's body.
  941. contents
  942. ;; 13. Creator.
  943. (let ((creator-info (plist-get info :with-creator)))
  944. (cond
  945. ((not creator-info) "")
  946. ((eq creator-info 'comment)
  947. (format "%% %s\n" (plist-get info :creator)))
  948. (t (concat (plist-get info :creator) "\n"))))
  949. ;; 14. Document end.
  950. "\\end{document}")))
  951. ;;; Transcode Functions
  952. ;;;; Babel Call
  953. ;;
  954. ;; Babel Calls are ignored.
  955. ;;;; Bold
  956. (defun org-e-latex-bold (bold contents info)
  957. "Transcode BOLD from Org to LaTeX.
  958. CONTENTS is the text with bold markup. INFO is a plist holding
  959. contextual information."
  960. (org-e-latex--text-markup contents 'bold))
  961. ;;;; Center Block
  962. (defun org-e-latex-center-block (center-block contents info)
  963. "Transcode a CENTER-BLOCK element from Org to LaTeX.
  964. CONTENTS holds the contents of the center block. INFO is a plist
  965. holding contextual information."
  966. (org-e-latex--wrap-label
  967. center-block
  968. (format "\\begin{center}\n%s\\end{center}" contents)))
  969. ;;;; Clock
  970. (defun org-e-latex-clock (clock contents info)
  971. "Transcode a CLOCK element from Org to LaTeX.
  972. CONTENTS is nil. INFO is a plist holding contextual
  973. information."
  974. (concat
  975. "\\noindent"
  976. (format "\\textbf{%s} " org-clock-string)
  977. (format org-e-latex-inactive-timestamp-format
  978. (concat (org-translate-time (org-element-property :value clock))
  979. (let ((time (org-element-property :time clock)))
  980. (and time (format " (%s)" time)))))
  981. "\\\\"))
  982. ;;;; Code
  983. (defun org-e-latex-code (code contents info)
  984. "Transcode a CODE object from Org to LaTeX.
  985. CONTENTS is nil. INFO is a plist used as a communication
  986. channel."
  987. (org-e-latex--text-markup (org-element-property :value code) 'code))
  988. ;;;; Comment
  989. ;;
  990. ;; Comments are ignored.
  991. ;;;; Comment Block
  992. ;;
  993. ;; Comment Blocks are ignored.
  994. ;;;; Drawer
  995. (defun org-e-latex-drawer (drawer contents info)
  996. "Transcode a DRAWER element from Org to LaTeX.
  997. CONTENTS holds the contents of the block. INFO is a plist
  998. holding contextual information."
  999. (let* ((name (org-element-property :drawer-name drawer))
  1000. (output (if (functionp org-e-latex-format-drawer-function)
  1001. (funcall org-e-latex-format-drawer-function
  1002. name contents)
  1003. ;; If there's no user defined function: simply
  1004. ;; display contents of the drawer.
  1005. contents)))
  1006. (org-e-latex--wrap-label drawer output)))
  1007. ;;;; Dynamic Block
  1008. (defun org-e-latex-dynamic-block (dynamic-block contents info)
  1009. "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
  1010. CONTENTS holds the contents of the block. INFO is a plist
  1011. holding contextual information. See `org-export-data'."
  1012. (org-e-latex--wrap-label dynamic-block contents))
  1013. ;;;; Entity
  1014. (defun org-e-latex-entity (entity contents info)
  1015. "Transcode an ENTITY object from Org to LaTeX.
  1016. CONTENTS are the definition itself. INFO is a plist holding
  1017. contextual information."
  1018. (let ((ent (org-element-property :latex entity)))
  1019. (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
  1020. ;;;; Example Block
  1021. (defun org-e-latex-example-block (example-block contents info)
  1022. "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
  1023. CONTENTS is nil. INFO is a plist holding contextual
  1024. information."
  1025. (org-e-latex--wrap-label
  1026. example-block
  1027. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1028. (org-export-format-code-default example-block info))))
  1029. ;;;; Export Block
  1030. (defun org-e-latex-export-block (export-block contents info)
  1031. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  1032. CONTENTS is nil. INFO is a plist holding contextual information."
  1033. (when (string= (org-element-property :type export-block) "LATEX")
  1034. (org-remove-indentation (org-element-property :value export-block))))
  1035. ;;;; Export Snippet
  1036. (defun org-e-latex-export-snippet (export-snippet contents info)
  1037. "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
  1038. CONTENTS is nil. INFO is a plist holding contextual information."
  1039. (when (eq (org-export-snippet-backend export-snippet) 'e-latex)
  1040. (org-element-property :value export-snippet)))
  1041. ;;;; Fixed Width
  1042. (defun org-e-latex-fixed-width (fixed-width contents info)
  1043. "Transcode a FIXED-WIDTH element from Org to LaTeX.
  1044. CONTENTS is nil. INFO is a plist holding contextual information."
  1045. (org-e-latex--wrap-label
  1046. fixed-width
  1047. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1048. (org-remove-indentation
  1049. (org-element-property :value fixed-width)))))
  1050. ;;;; Footnote Definition
  1051. ;;
  1052. ;; Footnote Definitions are ignored.
  1053. ;;;; Footnote Reference
  1054. (defun org-e-latex-footnote-reference (footnote-reference contents info)
  1055. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  1056. CONTENTS is nil. INFO is a plist holding contextual information."
  1057. (concat
  1058. ;; Insert separator between two footnotes in a row.
  1059. (let ((prev (org-export-get-previous-element footnote-reference)))
  1060. (when (eq (org-element-type prev) 'footnote-reference)
  1061. org-e-latex-footnote-separator))
  1062. (cond
  1063. ;; Use \footnotemark if reference is within an item's tag. Since
  1064. ;; we can't specify footnote number as an optional argument within
  1065. ;; an item tag, juggle with footnote counter to achieve the same
  1066. ;; result.
  1067. ((eq (org-element-type (org-export-get-parent-element footnote-reference))
  1068. 'item)
  1069. (let ((num (org-export-get-footnote-number footnote-reference info)))
  1070. (format "\\setcounter{footnote}{%s}\\footnotemark\\setcounter{footnote}{%s}"
  1071. (1- num) num)))
  1072. ;; Use \footnotemark if the footnote has already been defined.
  1073. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1074. (format "\\footnotemark[%s]{}"
  1075. (org-export-get-footnote-number footnote-reference info)))
  1076. ;; Use \footnotemark if reference is within another footnote
  1077. ;; reference or footnote definition.
  1078. ((loop for parent in (org-export-get-genealogy footnote-reference)
  1079. thereis (memq (org-element-type parent)
  1080. '(footnote-reference footnote-definition)))
  1081. (let ((num (org-export-get-footnote-number footnote-reference info)))
  1082. (format "\\footnotemark[%s]{}\\setcounter{footnote}{%s}" num num)))
  1083. ;; Otherwise, define it with \footnote command.
  1084. (t
  1085. (let ((def (org-export-get-footnote-definition footnote-reference info)))
  1086. (unless (eq (org-element-type def) 'org-data)
  1087. (setq def (cons 'org-data (cons nil def))))
  1088. (concat
  1089. (format "\\footnote{%s}" (org-trim (org-export-data def info)))
  1090. ;; Retrieve all footnote references within the footnote and
  1091. ;; add their definition after it, since LaTeX doesn't support
  1092. ;; them inside.
  1093. (org-e-latex--delayed-footnotes-definitions def info)))))))
  1094. ;;;; Headline
  1095. (defun org-e-latex-headline (headline contents info)
  1096. "Transcode an HEADLINE element from Org to LaTeX.
  1097. CONTENTS holds the contents of the headline. INFO is a plist
  1098. holding contextual information."
  1099. (let* ((class (plist-get info :latex-class))
  1100. (level (org-export-get-relative-level headline info))
  1101. (numberedp (org-export-numbered-headline-p headline info))
  1102. (class-sectionning (assoc class org-e-latex-classes))
  1103. ;; Section formatting will set two placeholders: one for the
  1104. ;; title and the other for the contents.
  1105. (section-fmt
  1106. (let ((sec (if (and (symbolp (nth 2 class-sectionning))
  1107. (fboundp (nth 2 class-sectionning)))
  1108. (funcall (nth 2 class-sectionning) level numberedp)
  1109. (nth (1+ level) class-sectionning))))
  1110. (cond
  1111. ;; No section available for that LEVEL.
  1112. ((not sec) nil)
  1113. ;; Section format directly returned by a function.
  1114. ((stringp sec) sec)
  1115. ;; (numbered-section . unnumbered-section)
  1116. ((not (consp (cdr sec)))
  1117. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  1118. ;; (numbered-open numbered-close)
  1119. ((= (length sec) 2)
  1120. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  1121. ;; (num-in num-out no-num-in no-num-out)
  1122. ((= (length sec) 4)
  1123. (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
  1124. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  1125. (text (org-export-data (org-element-property :title headline) info))
  1126. (todo
  1127. (and (plist-get info :with-todo-keywords)
  1128. (let ((todo (org-element-property :todo-keyword headline)))
  1129. (and todo (org-export-data todo info)))))
  1130. (todo-type (and todo (org-element-property :todo-type headline)))
  1131. (tags (and (plist-get info :with-tags)
  1132. (org-export-get-tags headline info)))
  1133. (priority (and (plist-get info :with-priority)
  1134. (org-element-property :priority headline)))
  1135. ;; Create the headline text along with a no-tag version. The
  1136. ;; latter is required to remove tags from table of contents.
  1137. (full-text (if (functionp org-e-latex-format-headline-function)
  1138. ;; User-defined formatting function.
  1139. (funcall org-e-latex-format-headline-function
  1140. todo todo-type priority text tags)
  1141. ;; Default formatting.
  1142. (concat
  1143. (when todo
  1144. (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1145. (when priority (format "\\framebox{\\#%c} " priority))
  1146. text
  1147. (when tags
  1148. (format "\\hfill{}\\textsc{:%s:}"
  1149. (mapconcat 'identity tags ":"))))))
  1150. (full-text-no-tag
  1151. (if (functionp org-e-latex-format-headline-function)
  1152. ;; User-defined formatting function.
  1153. (funcall org-e-latex-format-headline-function
  1154. todo todo-type priority text nil)
  1155. ;; Default formatting.
  1156. (concat
  1157. (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1158. (when priority (format "\\framebox{\\#%c} " priority))
  1159. text)))
  1160. ;; Associate some \label to the headline for internal links.
  1161. (headline-label
  1162. (format "\\label{sec-%s}\n"
  1163. (mapconcat 'number-to-string
  1164. (org-export-get-headline-number headline info)
  1165. "-")))
  1166. (pre-blanks
  1167. (make-string (org-element-property :pre-blank headline) 10)))
  1168. (cond
  1169. ;; Case 1: This is a footnote section: ignore it.
  1170. ((org-element-property :footnote-section-p headline) nil)
  1171. ;; Case 2. This is a deep sub-tree: export it as a list item.
  1172. ;; Also export as items headlines for which no section
  1173. ;; format has been found.
  1174. ((or (not section-fmt) (org-export-low-level-p headline info))
  1175. ;; Build the real contents of the sub-tree.
  1176. (let ((low-level-body
  1177. (concat
  1178. ;; If the headline is the first sibling, start a list.
  1179. (when (org-export-first-sibling-p headline)
  1180. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  1181. ;; Itemize headline
  1182. "\\item " full-text "\n" headline-label pre-blanks contents)))
  1183. ;; If headline is not the last sibling simply return
  1184. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
  1185. ;; blank line.
  1186. (if (not (org-export-last-sibling-p headline)) low-level-body
  1187. (replace-regexp-in-string
  1188. "[ \t\n]*\\'"
  1189. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  1190. low-level-body))))
  1191. ;; Case 3. Standard headline. Export it as a section.
  1192. (t
  1193. (cond
  1194. ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
  1195. ;; Regular section. Use specified format string.
  1196. (format section-fmt full-text
  1197. (concat headline-label pre-blanks contents)))
  1198. ((string-match "\\`\\\\\\(.*?\\){" section-fmt)
  1199. ;; If tags should be removed from table of contents, insert
  1200. ;; title without tags as an alternative heading in sectioning
  1201. ;; command.
  1202. (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
  1203. nil nil section-fmt 1)
  1204. ;; Replace square brackets with parenthesis since
  1205. ;; square brackets are not supported in optional
  1206. ;; arguments.
  1207. (replace-regexp-in-string
  1208. "\\[" "("
  1209. (replace-regexp-in-string
  1210. "\\]" ")"
  1211. full-text-no-tag))
  1212. full-text
  1213. (concat headline-label pre-blanks contents)))
  1214. (t
  1215. ;; Impossible to add an alternative heading. Fallback to
  1216. ;; regular sectioning format string.
  1217. (format section-fmt full-text
  1218. (concat headline-label pre-blanks contents))))))))
  1219. ;;;; Horizontal Rule
  1220. (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
  1221. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  1222. CONTENTS is nil. INFO is a plist holding contextual information."
  1223. (let ((attr (mapconcat #'identity
  1224. (org-element-property :attr_latex horizontal-rule)
  1225. " ")))
  1226. (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
  1227. ;;;; Inline Babel Call
  1228. ;;
  1229. ;; Inline Babel Calls are ignored.
  1230. ;;;; Inline Src Block
  1231. (defun org-e-latex-inline-src-block (inline-src-block contents info)
  1232. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  1233. CONTENTS holds the contents of the item. INFO is a plist holding
  1234. contextual information."
  1235. (let* ((code (org-element-property :value inline-src-block))
  1236. (separator (org-e-latex--find-verb-separator code)))
  1237. (cond
  1238. ;; Do not use a special package: transcode it verbatim.
  1239. ((not org-e-latex-listings)
  1240. (concat "\\verb" separator code separator))
  1241. ;; Use minted package.
  1242. ((eq org-e-latex-listings 'minted)
  1243. (let* ((org-lang (org-element-property :language inline-src-block))
  1244. (mint-lang (or (cadr (assq (intern org-lang)
  1245. org-e-latex-minted-langs))
  1246. org-lang))
  1247. (options (org-e-latex--make-option-string
  1248. org-e-latex-minted-options)))
  1249. (concat (format "\\mint%s{%s}"
  1250. (if (string= options "") "" (format "[%s]" options))
  1251. mint-lang)
  1252. separator code separator)))
  1253. ;; Use listings package.
  1254. (t
  1255. ;; Maybe translate language's name.
  1256. (let* ((org-lang (org-element-property :language inline-src-block))
  1257. (lst-lang (or (cadr (assq (intern org-lang)
  1258. org-e-latex-listings-langs))
  1259. org-lang))
  1260. (options (org-e-latex--make-option-string
  1261. (append org-e-latex-listings-options
  1262. `(("language" ,lst-lang))))))
  1263. (concat (format "\\lstinline[%s]" options)
  1264. separator code separator))))))
  1265. ;;;; Inlinetask
  1266. (defun org-e-latex-inlinetask (inlinetask contents info)
  1267. "Transcode an INLINETASK element from Org to LaTeX.
  1268. CONTENTS holds the contents of the block. INFO is a plist
  1269. holding contextual information."
  1270. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  1271. (todo (and (plist-get info :with-todo-keywords)
  1272. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1273. (and todo (org-export-data todo info)))))
  1274. (todo-type (org-element-property :todo-type inlinetask))
  1275. (tags (and (plist-get info :with-tags)
  1276. (org-export-get-tags inlinetask info)))
  1277. (priority (and (plist-get info :with-priority)
  1278. (org-element-property :priority inlinetask))))
  1279. ;; If `org-e-latex-format-inlinetask-function' is provided, call it
  1280. ;; with appropriate arguments.
  1281. (if (functionp org-e-latex-format-inlinetask-function)
  1282. (funcall org-e-latex-format-inlinetask-function
  1283. todo todo-type priority title tags contents)
  1284. ;; Otherwise, use a default template.
  1285. (org-e-latex--wrap-label
  1286. inlinetask
  1287. (let ((full-title
  1288. (concat
  1289. (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1290. (when priority (format "\\framebox{\\#%c} " priority))
  1291. title
  1292. (when tags (format "\\hfill{}\\textsc{:%s:}"
  1293. (mapconcat 'identity tags ":"))))))
  1294. (format (concat "\\begin{center}\n"
  1295. "\\fbox{\n"
  1296. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1297. "%s\n\n"
  1298. "\\rule[.8em]{\\textwidth}{2pt}\n\n"
  1299. "%s"
  1300. "\\end{minipage}\n"
  1301. "}\n"
  1302. "\\end{center}")
  1303. full-title contents))))))
  1304. ;;;; Italic
  1305. (defun org-e-latex-italic (italic contents info)
  1306. "Transcode ITALIC from Org to LaTeX.
  1307. CONTENTS is the text with italic markup. INFO is a plist holding
  1308. contextual information."
  1309. (org-e-latex--text-markup contents 'italic))
  1310. ;;;; Item
  1311. (defun org-e-latex-item (item contents info)
  1312. "Transcode an ITEM element from Org to LaTeX.
  1313. CONTENTS holds the contents of the item. INFO is a plist holding
  1314. contextual information."
  1315. (let* ((counter
  1316. (let ((count (org-element-property :counter item))
  1317. (level
  1318. (loop for parent in (org-export-get-genealogy item)
  1319. count (eq (org-element-type parent) 'plain-list)
  1320. until (eq (org-element-type parent) 'headline))))
  1321. (and count
  1322. (< level 5)
  1323. (format "\\setcounter{enum%s}{%s}\n"
  1324. (nth (1- level) '("i" "ii" "iii" "iv"))
  1325. (1- count)))))
  1326. (checkbox (case (org-element-property :checkbox item)
  1327. (on "$\\boxtimes$ ")
  1328. (off "$\\Box$ ")
  1329. (trans "$\\boxminus$ ")))
  1330. (tag (let ((tag (org-element-property :tag item)))
  1331. ;; Check-boxes must belong to the tag.
  1332. (and tag (format "[%s] "
  1333. (concat checkbox
  1334. (org-export-data tag info)))))))
  1335. (concat counter "\\item" (or tag (concat " " checkbox))
  1336. (org-trim contents)
  1337. ;; If there are footnotes references in tag, be sure to
  1338. ;; add their definition at the end of the item. This
  1339. ;; workaround is necessary since "\footnote{}" command is
  1340. ;; not supported in tags.
  1341. (and tag
  1342. (org-e-latex--delayed-footnotes-definitions
  1343. (org-element-property :tag item) info)))))
  1344. ;;;; Keyword
  1345. (defun org-e-latex-keyword (keyword contents info)
  1346. "Transcode a KEYWORD element from Org to LaTeX.
  1347. CONTENTS is nil. INFO is a plist holding contextual information."
  1348. (let ((key (org-element-property :key keyword))
  1349. (value (org-element-property :value keyword)))
  1350. (cond
  1351. ((string= key "LATEX") value)
  1352. ((string= key "INDEX") (format "\\index{%s}" value))
  1353. ;; Invisible targets.
  1354. ((string= key "TARGET") nil)
  1355. ((string= key "TOC")
  1356. (let ((value (downcase value)))
  1357. (cond
  1358. ((string-match "\\<headlines\\>" value)
  1359. (let ((depth (or (and (string-match "[0-9]+" value)
  1360. (string-to-number (match-string 0 value)))
  1361. (plist-get info :with-toc))))
  1362. (concat
  1363. (when (wholenump depth)
  1364. (format "\\setcounter{tocdepth}{%s}\n" depth))
  1365. "\\tableofcontents")))
  1366. ((string= "tables" value) "\\listoftables")
  1367. ((string= "figures" value) "\\listoffigures")
  1368. ((string= "listings" value)
  1369. (cond
  1370. ((eq org-e-latex-listings 'minted) "\\listoflistings")
  1371. (org-e-latex-listings "\\lstlistoflistings")
  1372. ;; At the moment, src blocks with a caption are wrapped
  1373. ;; into a figure environment.
  1374. (t "\\listoffigures")))))))))
  1375. ;;;; Latex Environment
  1376. (defun org-e-latex-latex-environment (latex-environment contents info)
  1377. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1378. CONTENTS is nil. INFO is a plist holding contextual information."
  1379. (let ((label (org-element-property :name latex-environment))
  1380. (value (org-remove-indentation
  1381. (org-element-property :value latex-environment))))
  1382. (if (not (org-string-nw-p label)) value
  1383. ;; Environment is labelled: label must be within the environment
  1384. ;; (otherwise, a reference pointing to that element will count
  1385. ;; the section instead).
  1386. (with-temp-buffer
  1387. (insert value)
  1388. (goto-char (point-min))
  1389. (forward-line)
  1390. (insert (format "\\label{%s}\n" label))
  1391. (buffer-string)))))
  1392. ;;;; Latex Fragment
  1393. (defun org-e-latex-latex-fragment (latex-fragment contents info)
  1394. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1395. CONTENTS is nil. INFO is a plist holding contextual information."
  1396. (org-element-property :value latex-fragment))
  1397. ;;;; Line Break
  1398. (defun org-e-latex-line-break (line-break contents info)
  1399. "Transcode a LINE-BREAK object from Org to LaTeX.
  1400. CONTENTS is nil. INFO is a plist holding contextual information."
  1401. "\\\\")
  1402. ;;;; Link
  1403. (defun org-e-latex-link--inline-image (link info)
  1404. "Return LaTeX code for an inline image.
  1405. LINK is the link pointing to the inline image. INFO is a plist
  1406. used as a communication channel."
  1407. (let* ((parent (org-export-get-parent-element link))
  1408. (path (let ((raw-path (org-element-property :path link)))
  1409. (if (not (file-name-absolute-p raw-path)) raw-path
  1410. (expand-file-name raw-path))))
  1411. (caption (org-e-latex--caption/label-string
  1412. (org-element-property :caption parent)
  1413. (org-element-property :name parent)
  1414. info))
  1415. ;; Retrieve latex attributes from the element around.
  1416. (attr (let ((raw-attr
  1417. (mapconcat #'identity
  1418. (org-element-property :attr_latex parent)
  1419. " ")))
  1420. (unless (string= raw-attr "") raw-attr)))
  1421. (disposition
  1422. (cond
  1423. ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
  1424. ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
  1425. ((or (and attr (string-match "\\<float\\>" attr))
  1426. (not (string= caption "")))
  1427. 'float)))
  1428. (placement
  1429. (cond
  1430. ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
  1431. (org-match-string-no-properties 1 attr))
  1432. ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
  1433. ((eq disposition 'float)
  1434. (concat "[" org-e-latex-default-figure-position "]"))
  1435. (t ""))))
  1436. ;; Now clear ATTR from any special keyword and set a default
  1437. ;; value if nothing is left.
  1438. (setq attr
  1439. (if (not attr) ""
  1440. (org-trim
  1441. (replace-regexp-in-string
  1442. "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
  1443. (setq attr (cond ((not (string= attr "")) attr)
  1444. ((eq disposition 'float) "width=0.7\\textwidth")
  1445. ((eq disposition 'wrap) "width=0.48\\textwidth")
  1446. (t (or org-e-latex-image-default-option ""))))
  1447. ;; Return proper string, depending on DISPOSITION.
  1448. (case disposition
  1449. (wrap (format "\\begin{wrapfigure}%s
  1450. \\centering
  1451. \\includegraphics[%s]{%s}
  1452. %s\\end{wrapfigure}" placement attr path caption))
  1453. (multicolumn (format "\\begin{figure*}%s
  1454. \\centering
  1455. \\includegraphics[%s]{%s}
  1456. %s\\end{figure*}" placement attr path caption))
  1457. (float (format "\\begin{figure}%s
  1458. \\centering
  1459. \\includegraphics[%s]{%s}
  1460. %s\\end{figure}" placement attr path caption))
  1461. (t (format "\\includegraphics[%s]{%s}" attr path)))))
  1462. (defun org-e-latex-link (link desc info)
  1463. "Transcode a LINK object from Org to LaTeX.
  1464. DESC is the description part of the link, or the empty string.
  1465. INFO is a plist holding contextual information. See
  1466. `org-export-data'."
  1467. (let* ((type (org-element-property :type link))
  1468. (raw-path (org-element-property :path link))
  1469. ;; Ensure DESC really exists, or set it to nil.
  1470. (desc (and (not (string= desc "")) desc))
  1471. (imagep (org-export-inline-image-p
  1472. link org-e-latex-inline-image-rules))
  1473. (path (cond
  1474. ((member type '("http" "https" "ftp" "mailto"))
  1475. (concat type ":" raw-path))
  1476. ((string= type "file")
  1477. (when (string-match "\\(.+\\)::.+" raw-path)
  1478. (setq raw-path (match-string 1 raw-path)))
  1479. (if (file-name-absolute-p raw-path)
  1480. (concat "file://" (expand-file-name raw-path))
  1481. (concat "file://" raw-path)))
  1482. (t raw-path)))
  1483. protocol)
  1484. (cond
  1485. ;; Image file.
  1486. (imagep (org-e-latex-link--inline-image link info))
  1487. ;; Radio link: Transcode target's contents and use them as link's
  1488. ;; description.
  1489. ((string= type "radio")
  1490. (let ((destination (org-export-resolve-radio-link link info)))
  1491. (when destination
  1492. (format "\\hyperref[%s]{%s}"
  1493. (org-export-solidify-link-text path)
  1494. (org-export-data (org-element-contents destination) info)))))
  1495. ;; Links pointing to an headline: Find destination and build
  1496. ;; appropriate referencing command.
  1497. ((member type '("custom-id" "fuzzy" "id"))
  1498. (let ((destination (if (string= type "fuzzy")
  1499. (org-export-resolve-fuzzy-link link info)
  1500. (org-export-resolve-id-link link info))))
  1501. (case (org-element-type destination)
  1502. ;; Id link points to an external file.
  1503. (plain-text
  1504. (if desc (format "\\href{file://%s}{%s}" destination desc)
  1505. (format "\\url{file://%s}" destination)))
  1506. ;; Fuzzy link points nowhere.
  1507. ('nil
  1508. (format org-e-latex-link-with-unknown-path-format
  1509. (or desc
  1510. (org-export-data
  1511. (org-element-property :raw-link link) info))))
  1512. ;; Fuzzy link points to an invisible target.
  1513. (keyword nil)
  1514. ;; LINK points to an headline. If headlines are numbered
  1515. ;; and the link has no description, display headline's
  1516. ;; number. Otherwise, display description or headline's
  1517. ;; title.
  1518. (headline
  1519. (let ((label
  1520. (format "sec-%s"
  1521. (mapconcat
  1522. 'number-to-string
  1523. (org-export-get-headline-number destination info)
  1524. "-"))))
  1525. (if (and (plist-get info :section-numbers) (not desc))
  1526. (format "\\ref{%s}" label)
  1527. (format "\\hyperref[%s]{%s}" label
  1528. (or desc
  1529. (org-export-data
  1530. (org-element-property :title destination) info))))))
  1531. ;; Fuzzy link points to a target. Do as above.
  1532. (otherwise
  1533. (let ((path (org-export-solidify-link-text path)))
  1534. (if (not desc) (format "\\ref{%s}" path)
  1535. (format "\\hyperref[%s]{%s}" path desc)))))))
  1536. ;; Coderef: replace link with the reference name or the
  1537. ;; equivalent line number.
  1538. ((string= type "coderef")
  1539. (format (org-export-get-coderef-format path desc)
  1540. (org-export-resolve-coderef path info)))
  1541. ;; Link type is handled by a special function.
  1542. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  1543. (funcall protocol (org-link-unescape path) desc 'latex))
  1544. ;; External link with a description part.
  1545. ((and path desc) (format "\\href{%s}{%s}" path desc))
  1546. ;; External link without a description part.
  1547. (path (format "\\url{%s}" path))
  1548. ;; No path, only description. Try to do something useful.
  1549. (t (format org-e-latex-link-with-unknown-path-format desc)))))
  1550. ;;;; Macro
  1551. (defun org-e-latex-macro (macro contents info)
  1552. "Transcode a MACRO element from Org to LaTeX.
  1553. CONTENTS is nil. INFO is a plist holding contextual information."
  1554. ;; Use available tools.
  1555. (org-export-expand-macro macro info))
  1556. ;;;; Paragraph
  1557. (defun org-e-latex-paragraph (paragraph contents info)
  1558. "Transcode a PARAGRAPH element from Org to LaTeX.
  1559. CONTENTS is the contents of the paragraph, as a string. INFO is
  1560. the plist used as a communication channel."
  1561. contents)
  1562. ;;;; Plain List
  1563. (defun org-e-latex-plain-list (plain-list contents info)
  1564. "Transcode a PLAIN-LIST element from Org to LaTeX.
  1565. CONTENTS is the contents of the list. INFO is a plist holding
  1566. contextual information."
  1567. (let* ((type (org-element-property :type plain-list))
  1568. (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
  1569. "inparadesc" "asparadesc"))
  1570. (paralist-regexp (concat
  1571. "\\("
  1572. (mapconcat 'identity paralist-types "\\|")
  1573. "\\)"))
  1574. (attr (mapconcat #'identity
  1575. (org-element-property :attr_latex plain-list)
  1576. " "))
  1577. (latex-type (cond
  1578. ((and attr
  1579. (string-match
  1580. (format "\\<%s\\>" paralist-regexp) attr))
  1581. (match-string 1 attr))
  1582. ((eq type 'ordered) "enumerate")
  1583. ((eq type 'unordered) "itemize")
  1584. ((eq type 'descriptive) "description"))))
  1585. (org-e-latex--wrap-label
  1586. plain-list
  1587. (format "\\begin{%s}%s\n%s\\end{%s}"
  1588. latex-type
  1589. ;; Once special environment, if any, has been removed, the
  1590. ;; rest of the attributes will be optional arguments.
  1591. ;; They will be put inside square brackets if necessary.
  1592. (let ((opt (replace-regexp-in-string
  1593. (format " *%s *" paralist-regexp) "" attr)))
  1594. (cond ((string= opt "") "")
  1595. ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
  1596. (t (format "[%s]" opt))))
  1597. contents
  1598. latex-type))))
  1599. ;;;; Plain Text
  1600. (defun org-e-latex-plain-text (text info)
  1601. "Transcode a TEXT string from Org to LaTeX.
  1602. TEXT is the string to transcode. INFO is a plist holding
  1603. contextual information."
  1604. ;; Protect %, #, &, $, ~, ^, _, { and }.
  1605. (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
  1606. (setq text
  1607. (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
  1608. ;; Protect \
  1609. (setq text (replace-regexp-in-string
  1610. "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
  1611. "$\\backslash$" text nil t 1))
  1612. ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
  1613. (let ((case-fold-search nil)
  1614. (start 0))
  1615. (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
  1616. (setq text (replace-match
  1617. (format "\\%s{}" (match-string 1 text)) nil t text)
  1618. start (match-end 0))))
  1619. ;; Handle quotation marks
  1620. (setq text (org-e-latex--quotation-marks text info))
  1621. ;; Convert special strings.
  1622. (when (plist-get info :with-special-strings)
  1623. (while (string-match (regexp-quote "...") text)
  1624. (setq text (replace-match "\\ldots{}" nil t text))))
  1625. ;; Handle break preservation if required.
  1626. (when (plist-get info :preserve-breaks)
  1627. (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
  1628. text)))
  1629. ;; Return value.
  1630. text)
  1631. ;;;; Planning
  1632. (defun org-e-latex-planning (planning contents info)
  1633. "Transcode a PLANNING element from Org to LaTeX.
  1634. CONTENTS is nil. INFO is a plist holding contextual
  1635. information."
  1636. (concat
  1637. "\\noindent"
  1638. (mapconcat
  1639. 'identity
  1640. (delq nil
  1641. (list
  1642. (let ((closed (org-element-property :closed planning)))
  1643. (when closed
  1644. (concat
  1645. (format "\\textbf{%s} " org-closed-string)
  1646. (format org-e-latex-inactive-timestamp-format
  1647. (org-translate-time closed)))))
  1648. (let ((deadline (org-element-property :deadline planning)))
  1649. (when deadline
  1650. (concat
  1651. (format "\\textbf{%s} " org-deadline-string)
  1652. (format org-e-latex-active-timestamp-format
  1653. (org-translate-time deadline)))))
  1654. (let ((scheduled (org-element-property :scheduled planning)))
  1655. (when scheduled
  1656. (concat
  1657. (format "\\textbf{%s} " org-scheduled-string)
  1658. (format org-e-latex-active-timestamp-format
  1659. (org-translate-time scheduled)))))))
  1660. " ")
  1661. "\\\\"))
  1662. ;;;; Property Drawer
  1663. (defun org-e-latex-property-drawer (property-drawer contents info)
  1664. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  1665. CONTENTS is nil. INFO is a plist holding contextual
  1666. information."
  1667. ;; The property drawer isn't exported but we want separating blank
  1668. ;; lines nonetheless.
  1669. "")
  1670. ;;;; Quote Block
  1671. (defun org-e-latex-quote-block (quote-block contents info)
  1672. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  1673. CONTENTS holds the contents of the block. INFO is a plist
  1674. holding contextual information."
  1675. (org-e-latex--wrap-label
  1676. quote-block
  1677. (format "\\begin{quote}\n%s\\end{quote}" contents)))
  1678. ;;;; Quote Section
  1679. (defun org-e-latex-quote-section (quote-section contents info)
  1680. "Transcode a QUOTE-SECTION element from Org to LaTeX.
  1681. CONTENTS is nil. INFO is a plist holding contextual information."
  1682. (let ((value (org-remove-indentation
  1683. (org-element-property :value quote-section))))
  1684. (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  1685. ;;;; Radio Target
  1686. (defun org-e-latex-radio-target (radio-target text info)
  1687. "Transcode a RADIO-TARGET object from Org to LaTeX.
  1688. TEXT is the text of the target. INFO is a plist holding
  1689. contextual information."
  1690. (format "\\label{%s}%s"
  1691. (org-export-solidify-link-text
  1692. (org-element-property :value radio-target))
  1693. text))
  1694. ;;;; Section
  1695. (defun org-e-latex-section (section contents info)
  1696. "Transcode a SECTION element from Org to LaTeX.
  1697. CONTENTS holds the contents of the section. INFO is a plist
  1698. holding contextual information."
  1699. contents)
  1700. ;;;; Special Block
  1701. (defun org-e-latex-special-block (special-block contents info)
  1702. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  1703. CONTENTS holds the contents of the block. INFO is a plist
  1704. holding contextual information."
  1705. (let ((type (downcase (org-element-property :type special-block))))
  1706. (org-e-latex--wrap-label
  1707. special-block
  1708. (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
  1709. ;;;; Src Block
  1710. (defun org-e-latex-src-block (src-block contents info)
  1711. "Transcode a SRC-BLOCK element from Org to LaTeX.
  1712. CONTENTS holds the contents of the item. INFO is a plist holding
  1713. contextual information."
  1714. (let* ((lang (org-element-property :language src-block))
  1715. (caption (org-element-property :caption src-block))
  1716. (label (org-element-property :name src-block))
  1717. (custom-env (and lang
  1718. (cadr (assq (intern lang)
  1719. org-e-latex-custom-lang-environments))))
  1720. (num-start (case (org-element-property :number-lines src-block)
  1721. (continued (org-export-get-loc src-block info))
  1722. (new 0)))
  1723. (retain-labels (org-element-property :retain-labels src-block)))
  1724. (cond
  1725. ;; Case 1. No source fontification.
  1726. ((not org-e-latex-listings)
  1727. (let ((caption-str (org-e-latex--caption/label-string caption label info))
  1728. (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
  1729. (format
  1730. (or float-env "%s")
  1731. (concat caption-str
  1732. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1733. (org-export-format-code-default src-block info))))))
  1734. ;; Case 2. Custom environment.
  1735. (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
  1736. custom-env
  1737. (org-export-format-code-default src-block info)
  1738. custom-env))
  1739. ;; Case 3. Use minted package.
  1740. ((eq org-e-latex-listings 'minted)
  1741. (let ((float-env (when (or label caption)
  1742. (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
  1743. (org-e-latex--caption/label-string
  1744. caption label info))))
  1745. (body
  1746. (format
  1747. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  1748. ;; Options.
  1749. (org-e-latex--make-option-string
  1750. (if (not num-start) org-e-latex-minted-options
  1751. (append `(("linenos")
  1752. ("firstnumber" ,(number-to-string (1+ num-start))))
  1753. org-e-latex-minted-options)))
  1754. ;; Language.
  1755. (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
  1756. ;; Source code.
  1757. (let* ((code-info (org-export-unravel-code src-block))
  1758. (max-width
  1759. (apply 'max
  1760. (mapcar 'length
  1761. (org-split-string (car code-info) "\n")))))
  1762. (org-export-format-code
  1763. (car code-info)
  1764. (lambda (loc num ref)
  1765. (concat
  1766. loc
  1767. (when ref
  1768. ;; Ensure references are flushed to the right,
  1769. ;; separated with 6 spaces from the widest line
  1770. ;; of code.
  1771. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1772. (format "(%s)" ref)))))
  1773. nil (and retain-labels (cdr code-info)))))))
  1774. ;; Return value.
  1775. (if float-env (format float-env body) body)))
  1776. ;; Case 4. Use listings package.
  1777. (t
  1778. (let ((lst-lang
  1779. (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
  1780. (caption-str
  1781. (when caption
  1782. (let ((main (org-export-data (car caption) info)))
  1783. (if (not (cdr caption)) (format "{%s}" main)
  1784. (format "{[%s]%s}"
  1785. (org-export-data (cdr caption) info)
  1786. main))))))
  1787. (concat
  1788. ;; Options.
  1789. (format "\\lstset{%s}\n"
  1790. (org-e-latex--make-option-string
  1791. (append org-e-latex-listings-options
  1792. `(("language" ,lst-lang))
  1793. (when label `(("label" ,label)))
  1794. (when caption-str `(("caption" ,caption-str)))
  1795. (cond ((not num-start) '(("numbers" "none")))
  1796. ((zerop num-start) '(("numbers" "left")))
  1797. (t `(("numbers" "left")
  1798. ("firstnumber"
  1799. ,(number-to-string (1+ num-start)))))))))
  1800. ;; Source code.
  1801. (format
  1802. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  1803. (let* ((code-info (org-export-unravel-code src-block))
  1804. (max-width
  1805. (apply 'max
  1806. (mapcar 'length
  1807. (org-split-string (car code-info) "\n")))))
  1808. (org-export-format-code
  1809. (car code-info)
  1810. (lambda (loc num ref)
  1811. (concat
  1812. loc
  1813. (when ref
  1814. ;; Ensure references are flushed to the right,
  1815. ;; separated with 6 spaces from the widest line of
  1816. ;; code
  1817. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1818. (format "(%s)" ref)))))
  1819. nil (and retain-labels (cdr code-info)))))))))))
  1820. ;;;; Statistics Cookie
  1821. (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
  1822. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  1823. CONTENTS is nil. INFO is a plist holding contextual information."
  1824. (org-element-property :value statistics-cookie))
  1825. ;;;; Strike-Through
  1826. (defun org-e-latex-strike-through (strike-through contents info)
  1827. "Transcode STRIKE-THROUGH from Org to LaTeX.
  1828. CONTENTS is the text with strike-through markup. INFO is a plist
  1829. holding contextual information."
  1830. (org-e-latex--text-markup contents 'strike-through))
  1831. ;;;; Subscript
  1832. (defun org-e-latex-subscript (subscript contents info)
  1833. "Transcode a SUBSCRIPT object from Org to LaTeX.
  1834. CONTENTS is the contents of the object. INFO is a plist holding
  1835. contextual information."
  1836. (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
  1837. ;;;; Superscript
  1838. (defun org-e-latex-superscript (superscript contents info)
  1839. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  1840. CONTENTS is the contents of the object. INFO is a plist holding
  1841. contextual information."
  1842. (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
  1843. ;;;; Table
  1844. ;;
  1845. ;; `org-e-latex-table' is the entry point for table transcoding. It
  1846. ;; takes care of tables with a "verbatim" attribute. Otherwise, it
  1847. ;; delegates the job to either `org-e-latex-table--table.el-table' or
  1848. ;; `org-e-latex-table--org-table' functions, depending of the type of
  1849. ;; the table.
  1850. ;;
  1851. ;; `org-e-latex-table--align-string' is a subroutine used to build
  1852. ;; alignment string for Org tables.
  1853. (defun org-e-latex-table (table contents info)
  1854. "Transcode a TABLE element from Org to LaTeX.
  1855. CONTENTS is the contents of the table. INFO is a plist holding
  1856. contextual information."
  1857. (cond
  1858. ;; Case 1: verbatim table.
  1859. ((or org-e-latex-tables-verbatim
  1860. (let ((attr (mapconcat 'identity
  1861. (org-element-property :attr_latex table)
  1862. " ")))
  1863. (and attr (string-match "\\<verbatim\\>" attr))))
  1864. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  1865. ;; Re-create table, without affiliated keywords.
  1866. (org-trim
  1867. (org-element-interpret-data
  1868. `(table nil ,@(org-element-contents table))))))
  1869. ;; Case 2: table.el table. Convert it using appropriate tools.
  1870. ((eq (org-element-property :type table) 'table.el)
  1871. (org-e-latex-table--table.el-table table contents info))
  1872. ;; Case 3: Standard table.
  1873. (t (org-e-latex-table--org-table table contents info))))
  1874. (defun org-e-latex-table--align-string (table info)
  1875. "Return an appropriate LaTeX alignment string.
  1876. TABLE is the considered table. INFO is a plist used as
  1877. a communication channel."
  1878. (let ((attr (mapconcat 'identity
  1879. (org-element-property :attr_latex table)
  1880. " ")))
  1881. (if (string-match "\\<align=\\(\\S-+\\)" attr) (match-string 1 attr)
  1882. (let (alignment)
  1883. ;; Extract column groups and alignment from first (non-rule)
  1884. ;; row.
  1885. (org-element-map
  1886. (org-element-map
  1887. table 'table-row
  1888. (lambda (row)
  1889. (and (eq (org-element-property :type row) 'standard) row))
  1890. info 'first-match)
  1891. 'table-cell
  1892. (lambda (cell)
  1893. (let ((borders (org-export-table-cell-borders cell info)))
  1894. ;; Check left border for the first cell only.
  1895. (when (and (memq 'left borders) (not alignment))
  1896. (push "|" alignment))
  1897. (push (case (org-export-table-cell-alignment cell info)
  1898. (left "l")
  1899. (right "r")
  1900. (center "c"))
  1901. alignment)
  1902. (when (memq 'right borders) (push "|" alignment))))
  1903. info)
  1904. (apply 'concat (reverse alignment))))))
  1905. (defun org-e-latex-table--org-table (table contents info)
  1906. "Return appropriate LaTeX code for an Org table.
  1907. TABLE is the table type element to transcode. CONTENTS is its
  1908. contents, as a string. INFO is a plist used as a communication
  1909. channel.
  1910. This function assumes TABLE has `org' as its `:type' attribute."
  1911. (let* ((label (org-element-property :name table))
  1912. (caption (org-e-latex--caption/label-string
  1913. (org-element-property :caption table) label info))
  1914. (attr (mapconcat 'identity
  1915. (org-element-property :attr_latex table)
  1916. " "))
  1917. ;; Determine alignment string.
  1918. (alignment (org-e-latex-table--align-string table info))
  1919. ;; Determine environment for the table: longtable, tabular...
  1920. (table-env (cond
  1921. ((not attr) org-e-latex-default-table-environment)
  1922. ((string-match "\\<longtable\\>" attr) "longtable")
  1923. ((string-match "\\<tabular.?\\>" attr)
  1924. (org-match-string-no-properties 0 attr))
  1925. (t org-e-latex-default-table-environment)))
  1926. ;; If table is a float, determine environment: table, table*
  1927. ;; or sidewaystable.
  1928. (float-env (cond
  1929. ((string= "longtable" table-env) nil)
  1930. ((and attr (string-match "\\<sidewaystable\\>" attr))
  1931. "sidewaystables")
  1932. ((and attr
  1933. (or (string-match (regexp-quote "table*") attr)
  1934. (string-match "\\<multicolumn\\>" attr)))
  1935. "table*")
  1936. ((or (not (string= caption "")) label) "table")))
  1937. ;; Extract others display options.
  1938. (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
  1939. (org-match-string-no-properties 1 attr)))
  1940. (placement
  1941. (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
  1942. (org-match-string-no-properties 1 attr)
  1943. (format "[%s]" org-e-latex-default-figure-position))))
  1944. ;; Prepare the final format string for the table.
  1945. (cond
  1946. ;; Longtable.
  1947. ((string= "longtable" table-env)
  1948. (format
  1949. "\\begin{longtable}{%s}\n%s%s%s\\end{longtable}"
  1950. alignment
  1951. (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
  1952. (concat (org-trim caption) "\\\\\n"))
  1953. contents
  1954. (if (or org-e-latex-table-caption-above (string= "" caption)) ""
  1955. (concat (org-trim caption) "\\\\\n"))))
  1956. ;; Others.
  1957. (t (concat (when float-env
  1958. (concat
  1959. (format "\\begin{%s}%s\n" float-env placement)
  1960. (if org-e-latex-table-caption-above caption "")))
  1961. (when org-e-latex-tables-centered "\\begin{center}\n")
  1962. (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  1963. table-env
  1964. (if width (format "{%s}" width) "")
  1965. alignment
  1966. contents
  1967. table-env)
  1968. (when org-e-latex-tables-centered "\n\\end{center}")
  1969. (when float-env
  1970. (concat (if org-e-latex-table-caption-above "" caption)
  1971. (format "\n\\end{%s}" float-env))))))))
  1972. (defun org-e-latex-table--table.el-table (table contents info)
  1973. "Return appropriate LaTeX code for a table.el table.
  1974. TABLE is the table type element to transcode. CONTENTS is its
  1975. contents, as a string. INFO is a plist used as a communication
  1976. channel.
  1977. This function assumes TABLE has `table.el' as its `:type'
  1978. attribute."
  1979. (require 'table)
  1980. ;; Ensure "*org-export-table*" buffer is empty.
  1981. (with-current-buffer (get-buffer-create "*org-export-table*")
  1982. (erase-buffer))
  1983. (let ((output (with-temp-buffer
  1984. (insert (org-element-property :value table))
  1985. (goto-char 1)
  1986. (re-search-forward "^[ \t]*|[^|]" nil t)
  1987. (table-generate-source 'latex "*org-export-table*")
  1988. (with-current-buffer "*org-export-table*"
  1989. (org-trim (buffer-string))))))
  1990. (kill-buffer (get-buffer "*org-export-table*"))
  1991. ;; Remove left out comments.
  1992. (while (string-match "^%.*\n" output)
  1993. (setq output (replace-match "" t t output)))
  1994. ;; When the "rmlines" attribute is provided, remove all hlines but
  1995. ;; the the one separating heading from the table body.
  1996. (let ((attr (mapconcat 'identity
  1997. (org-element-property :attr_latex table)
  1998. " ")))
  1999. (when (and attr (string-match "\\<rmlines\\>" attr))
  2000. (let ((n 0) (pos 0))
  2001. (while (and (< (length output) pos)
  2002. (setq pos (string-match "^\\\\hline\n?" output pos)))
  2003. (incf n)
  2004. (unless (= n 2)
  2005. (setq output (replace-match "" nil nil output)))))))
  2006. (if (not org-e-latex-tables-centered) output
  2007. (format "\\begin{center}\n%s\n\\end{center}" output))))
  2008. ;;;; Table Cell
  2009. (defun org-e-latex-table-cell (table-cell contents info)
  2010. "Transcode a TABLE-CELL element from Org to LaTeX.
  2011. CONTENTS is the cell contents. INFO is a plist used as
  2012. a communication channel."
  2013. (concat (if (and contents
  2014. org-e-latex-table-scientific-notation
  2015. (string-match orgtbl-exp-regexp contents))
  2016. ;; Use appropriate format string for scientific
  2017. ;; notation.
  2018. (format org-e-latex-table-scientific-notation
  2019. (match-string 1 contents)
  2020. (match-string 2 contents))
  2021. contents)
  2022. (when (org-export-get-next-element table-cell) " & ")))
  2023. ;;;; Table Row
  2024. (defun org-e-latex-table-row (table-row contents info)
  2025. "Transcode a TABLE-ROW element from Org to LaTeX.
  2026. CONTENTS is the contents of the row. INFO is a plist used as
  2027. a communication channel."
  2028. ;; Rules are ignored since table separators are deduced from
  2029. ;; borders of the current row.
  2030. (when (eq (org-element-property :type table-row) 'standard)
  2031. (let* ((attr (mapconcat 'identity
  2032. (org-element-property
  2033. :attr_latex (org-export-get-parent table-row))
  2034. " "))
  2035. (longtablep (and attr (string-match "\\<longtable\\>" attr)))
  2036. (booktabsp
  2037. (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
  2038. org-e-latex-tables-booktabs))
  2039. ;; TABLE-ROW's borders are extracted from its first cell.
  2040. (borders
  2041. (org-export-table-cell-borders
  2042. (car (org-element-contents table-row)) info)))
  2043. (concat
  2044. ;; When BOOKTABS are activated enforce top-rule even when no
  2045. ;; hline was specifically marked.
  2046. (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
  2047. ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
  2048. contents "\\\\\n"
  2049. (cond
  2050. ;; Special case for long tables. Define header and footers.
  2051. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  2052. (format "%s
  2053. \\endhead
  2054. %s\\multicolumn{%d}{r}{Continued on next page} \\\\
  2055. \\endfoot
  2056. \\endlastfoot"
  2057. (if booktabsp "\\midrule" "\\hline")
  2058. (if booktabsp "\\midrule" "\\hline")
  2059. ;; Number of columns.
  2060. (cdr (org-export-table-dimensions
  2061. (org-export-get-parent-table table-row) info))))
  2062. ;; When BOOKTABS are activated enforce bottom rule even when
  2063. ;; no hline was specifically marked.
  2064. ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
  2065. ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
  2066. ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
  2067. ;;;; Target
  2068. (defun org-e-latex-target (target contents info)
  2069. "Transcode a TARGET object from Org to LaTeX.
  2070. CONTENTS is nil. INFO is a plist holding contextual
  2071. information."
  2072. (format "\\label{%s}"
  2073. (org-export-solidify-link-text (org-element-property :value target))))
  2074. ;;;; Timestamp
  2075. (defun org-e-latex-timestamp (timestamp contents info)
  2076. "Transcode a TIMESTAMP object from Org to LaTeX.
  2077. CONTENTS is nil. INFO is a plist holding contextual
  2078. information."
  2079. (let ((value (org-translate-time (org-element-property :value timestamp)))
  2080. (type (org-element-property :type timestamp)))
  2081. (cond ((memq type '(active active-range))
  2082. (format org-e-latex-active-timestamp-format value))
  2083. ((memq type '(inactive inactive-range))
  2084. (format org-e-latex-inactive-timestamp-format value))
  2085. (t (format org-e-latex-diary-timestamp-format value)))))
  2086. ;;;; Underline
  2087. (defun org-e-latex-underline (underline contents info)
  2088. "Transcode UNDERLINE from Org to LaTeX.
  2089. CONTENTS is the text with underline markup. INFO is a plist
  2090. holding contextual information."
  2091. (org-e-latex--text-markup contents 'underline))
  2092. ;;;; Verbatim
  2093. (defun org-e-latex-verbatim (verbatim contents info)
  2094. "Transcode a VERBATIM object from Org to LaTeX.
  2095. CONTENTS is nil. INFO is a plist used as a communication
  2096. channel."
  2097. (org-e-latex--text-markup (org-element-property :value verbatim) 'verbatim))
  2098. ;;;; Verse Block
  2099. (defun org-e-latex-verse-block (verse-block contents info)
  2100. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  2101. CONTENTS is verse block contents. INFO is a plist holding
  2102. contextual information."
  2103. (org-e-latex--wrap-label
  2104. verse-block
  2105. ;; In a verse environment, add a line break to each newline
  2106. ;; character and change each white space at beginning of a line
  2107. ;; into a space of 1 em. Also change each blank line with
  2108. ;; a vertical space of 1 em.
  2109. (progn
  2110. (setq contents (replace-regexp-in-string
  2111. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  2112. (replace-regexp-in-string
  2113. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
  2114. (while (string-match "^[ \t]+" contents)
  2115. (let ((new-str (format "\\hspace*{%dem}"
  2116. (length (match-string 0 contents)))))
  2117. (setq contents (replace-match new-str nil t contents))))
  2118. (format "\\begin{verse}\n%s\\end{verse}" contents))))
  2119. ;;; Interactive functions
  2120. (defun org-e-latex-export-to-latex
  2121. (&optional subtreep visible-only body-only ext-plist pub-dir)
  2122. "Export current buffer to a LaTeX file.
  2123. If narrowing is active in the current buffer, only export its
  2124. narrowed part.
  2125. If a region is active, export that region.
  2126. When optional argument SUBTREEP is non-nil, export the sub-tree
  2127. at point, extracting information from the headline properties
  2128. first.
  2129. When optional argument VISIBLE-ONLY is non-nil, don't export
  2130. contents of hidden elements.
  2131. When optional argument BODY-ONLY is non-nil, only write code
  2132. between \"\\begin{document}\" and \"\\end{document}\".
  2133. EXT-PLIST, when provided, is a property list with external
  2134. parameters overriding Org default settings, but still inferior to
  2135. file-local settings.
  2136. When optional argument PUB-DIR is set, use it as the publishing
  2137. directory.
  2138. Return output file's name."
  2139. (interactive)
  2140. (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
  2141. (org-export-to-file
  2142. 'e-latex outfile subtreep visible-only body-only ext-plist)))
  2143. (defun org-e-latex-export-to-pdf
  2144. (&optional subtreep visible-only body-only ext-plist pub-dir)
  2145. "Export current buffer to LaTeX then process through to PDF.
  2146. If narrowing is active in the current buffer, only export its
  2147. narrowed part.
  2148. If a region is active, export that region.
  2149. When optional argument SUBTREEP is non-nil, export the sub-tree
  2150. at point, extracting information from the headline properties
  2151. first.
  2152. When optional argument VISIBLE-ONLY is non-nil, don't export
  2153. contents of hidden elements.
  2154. When optional argument BODY-ONLY is non-nil, only write code
  2155. between \"\\begin{document}\" and \"\\end{document}\".
  2156. EXT-PLIST, when provided, is a property list with external
  2157. parameters overriding Org default settings, but still inferior to
  2158. file-local settings.
  2159. When optional argument PUB-DIR is set, use it as the publishing
  2160. directory.
  2161. Return PDF file's name."
  2162. (interactive)
  2163. (org-e-latex-compile
  2164. (org-e-latex-export-to-latex
  2165. subtreep visible-only body-only ext-plist pub-dir)))
  2166. (defun org-e-latex-compile (texfile)
  2167. "Compile a TeX file.
  2168. TEXFILE is the name of the file being compiled. Processing is
  2169. done through the command specified in `org-e-latex-pdf-process'.
  2170. Return PDF file name or an error if it couldn't be produced."
  2171. (let* ((wconfig (current-window-configuration))
  2172. (texfile (file-truename texfile))
  2173. (base (file-name-sans-extension texfile))
  2174. errors)
  2175. (message (format "Processing LaTeX file %s ..." texfile))
  2176. (unwind-protect
  2177. (progn
  2178. (cond
  2179. ;; A function is provided: Apply it.
  2180. ((functionp org-e-latex-pdf-process)
  2181. (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
  2182. ;; A list is provided: Replace %b, %f and %o with appropriate
  2183. ;; values in each command before applying it. Output is
  2184. ;; redirected to "*Org PDF LaTeX Output*" buffer.
  2185. ((consp org-e-latex-pdf-process)
  2186. (let* ((out-dir (or (file-name-directory texfile) "./"))
  2187. (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
  2188. (mapc
  2189. (lambda (command)
  2190. (shell-command
  2191. (replace-regexp-in-string
  2192. "%b" (shell-quote-argument base)
  2193. (replace-regexp-in-string
  2194. "%f" (shell-quote-argument texfile)
  2195. (replace-regexp-in-string
  2196. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  2197. outbuf))
  2198. org-e-latex-pdf-process)
  2199. ;; Collect standard errors from output buffer.
  2200. (setq errors (org-e-latex-collect-errors outbuf))))
  2201. (t (error "No valid command to process to PDF")))
  2202. (let ((pdffile (concat base ".pdf")))
  2203. ;; Check for process failure. Provide collected errors if
  2204. ;; possible.
  2205. (if (not (file-exists-p pdffile))
  2206. (error (concat (format "PDF file %s wasn't produced" pdffile)
  2207. (when errors (concat ": " errors))))
  2208. ;; Else remove log files, when specified, and signal end of
  2209. ;; process to user, along with any error encountered.
  2210. (when org-e-latex-remove-logfiles
  2211. (dolist (ext org-e-latex-logfiles-extensions)
  2212. (let ((file (concat base "." ext)))
  2213. (when (file-exists-p file) (delete-file file)))))
  2214. (message (concat "Process completed"
  2215. (if (not errors) "."
  2216. (concat " with errors: " errors)))))
  2217. ;; Return output file name.
  2218. pdffile))
  2219. (set-window-configuration wconfig))))
  2220. (defun org-e-latex-collect-errors (buffer)
  2221. "Collect some kind of errors from \"pdflatex\" command output.
  2222. BUFFER is the buffer containing output.
  2223. Return collected error types as a string, or nil if there was
  2224. none."
  2225. (with-current-buffer buffer
  2226. (save-excursion
  2227. (goto-char (point-max))
  2228. ;; Find final "pdflatex" run.
  2229. (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
  2230. (let ((case-fold-search t)
  2231. (errors ""))
  2232. (when (save-excursion
  2233. (re-search-forward "Reference.*?undefined" nil t))
  2234. (setq errors (concat errors " [undefined reference]")))
  2235. (when (save-excursion
  2236. (re-search-forward "Citation.*?undefined" nil t))
  2237. (setq errors (concat errors " [undefined citation]")))
  2238. (when (save-excursion
  2239. (re-search-forward "Undefined control sequence" nil t))
  2240. (setq errors (concat errors " [undefined control sequence]")))
  2241. (when (save-excursion
  2242. (re-search-forward "^! LaTeX.*?Error" nil t))
  2243. (setq errors (concat errors " [LaTeX error]")))
  2244. (when (save-excursion
  2245. (re-search-forward "^! Package.*?Error" nil t))
  2246. (setq errors (concat errors " [package error]")))
  2247. (and (org-string-nw-p errors) (org-trim errors)))))))
  2248. (provide 'org-e-latex)
  2249. ;;; org-e-latex.el ends here