org-e-latex.el 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667
  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. ;;
  1055. ;; Footnote reference export is handled by
  1056. ;; `org-e-latex-footnote-reference'.
  1057. ;;
  1058. ;; Internally, `org-e-latex--get-footnote-counter' is used to restore
  1059. ;; the value of the LaTeX "footnote" counter after a jump due to
  1060. ;; a reference to an already defined footnote. It is only needed in
  1061. ;; item tags since the optional argument to \footnotemark is not
  1062. ;; allowed there.
  1063. (defun org-e-latex--get-footnote-counter (footnote-reference info)
  1064. "Return \"footnote\" counter before FOOTNOTE-REFERENCE is encountered.
  1065. INFO is a plist used as a communication channel."
  1066. ;; Find original counter value by counting number of footnote
  1067. ;; references appearing for the first time before the current
  1068. ;; footnote reference.
  1069. (let* ((label (org-element-property :label footnote-reference))
  1070. seen-refs
  1071. search-ref ; For byte-compiler.
  1072. (search-ref
  1073. (function
  1074. (lambda (data)
  1075. ;; Search footnote references through DATA, filling
  1076. ;; SEEN-REFS along the way.
  1077. (org-element-map
  1078. data 'footnote-reference
  1079. (lambda (fn)
  1080. (let ((fn-lbl (org-element-property :label fn)))
  1081. (cond
  1082. ;; Anonymous footnote match: return number.
  1083. ((equal fn footnote-reference) (length seen-refs))
  1084. ;; Anonymous footnote: it's always a new one.
  1085. ;; Also, be sure to return nil from the `cond' so
  1086. ;; `first-match' doesn't get us out of the loop.
  1087. ((not fn-lbl) (push 'inline seen-refs) nil)
  1088. ;; Label not seen so far: add it so SEEN-REFS.
  1089. ;;
  1090. ;; Also search for subsequent references in
  1091. ;; footnote definition so numbering follows reading
  1092. ;; logic. Note that we don't have to care about
  1093. ;; inline definitions, since `org-element-map'
  1094. ;; already traverse them at the right time.
  1095. ((not (member fn-lbl seen-refs))
  1096. (push fn-lbl seen-refs)
  1097. (funcall search-ref
  1098. (org-export-get-footnote-definition fn info))))))
  1099. ;; Don't enter footnote definitions since it will happen
  1100. ;; when their first reference is found.
  1101. info 'first-match 'footnote-definition)))))
  1102. (funcall search-ref (plist-get info :parse-tree))))
  1103. (defun org-e-latex-footnote-reference (footnote-reference contents info)
  1104. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  1105. CONTENTS is nil. INFO is a plist holding contextual information."
  1106. (concat
  1107. ;; Insert separator between two footnotes in a row.
  1108. (let ((prev (org-export-get-previous-element footnote-reference)))
  1109. (when (eq (org-element-type prev) 'footnote-reference)
  1110. org-e-latex-footnote-separator))
  1111. (cond
  1112. ;; Use \footnotemark if reference is within an item's tag.
  1113. ((eq (org-element-type (org-export-get-parent-element footnote-reference))
  1114. 'item)
  1115. (if (org-export-footnote-first-reference-p footnote-reference info)
  1116. "\\footnotemark"
  1117. ;; Since we can't specify footnote number as an optional
  1118. ;; argument within an item tag, some extra work has to be done
  1119. ;; when the footnote has already been referenced. In that
  1120. ;; case, set footnote counter to the desired number, use the
  1121. ;; footnotemark, then set counter back to its original value.
  1122. (format
  1123. "\\setcounter{footnote}{%s}\\footnotemark\\setcounter{footnote}{%s}"
  1124. (1- (org-export-get-footnote-number footnote-reference info))
  1125. (org-e-latex--get-footnote-counter footnote-reference info))))
  1126. ;; Use \footnotemark if the footnote has already been defined.
  1127. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1128. (format "\\footnotemark[%s]{}"
  1129. (org-export-get-footnote-number footnote-reference info)))
  1130. ;; Use \footnotemark if reference is within another footnote
  1131. ;; reference or footnote definition.
  1132. ((loop for parent in (org-export-get-genealogy footnote-reference)
  1133. thereis (memq (org-element-type parent)
  1134. '(footnote-reference footnote-definition)))
  1135. "\\footnotemark")
  1136. ;; Otherwise, define it with \footnote command.
  1137. (t
  1138. (let ((def (org-export-get-footnote-definition footnote-reference info)))
  1139. (unless (eq (org-element-type def) 'org-data)
  1140. (setq def (cons 'org-data (cons nil def))))
  1141. (concat
  1142. (format "\\footnote{%s}" (org-trim (org-export-data def info)))
  1143. ;; Retrieve all footnote references within the footnote and
  1144. ;; add their definition after it, since LaTeX doesn't support
  1145. ;; them inside.
  1146. (org-e-latex--delayed-footnotes-definitions def info)))))))
  1147. ;;;; Headline
  1148. (defun org-e-latex-headline (headline contents info)
  1149. "Transcode an HEADLINE element from Org to LaTeX.
  1150. CONTENTS holds the contents of the headline. INFO is a plist
  1151. holding contextual information."
  1152. (let* ((class (plist-get info :latex-class))
  1153. (level (org-export-get-relative-level headline info))
  1154. (numberedp (org-export-numbered-headline-p headline info))
  1155. (class-sectionning (assoc class org-e-latex-classes))
  1156. ;; Section formatting will set two placeholders: one for the
  1157. ;; title and the other for the contents.
  1158. (section-fmt
  1159. (let ((sec (if (and (symbolp (nth 2 class-sectionning))
  1160. (fboundp (nth 2 class-sectionning)))
  1161. (funcall (nth 2 class-sectionning) level numberedp)
  1162. (nth (1+ level) class-sectionning))))
  1163. (cond
  1164. ;; No section available for that LEVEL.
  1165. ((not sec) nil)
  1166. ;; Section format directly returned by a function.
  1167. ((stringp sec) sec)
  1168. ;; (numbered-section . unnumbered-section)
  1169. ((not (consp (cdr sec)))
  1170. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  1171. ;; (numbered-open numbered-close)
  1172. ((= (length sec) 2)
  1173. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  1174. ;; (num-in num-out no-num-in no-num-out)
  1175. ((= (length sec) 4)
  1176. (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
  1177. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  1178. (text (org-export-data (org-element-property :title headline) info))
  1179. (todo
  1180. (and (plist-get info :with-todo-keywords)
  1181. (let ((todo (org-element-property :todo-keyword headline)))
  1182. (and todo (org-export-data todo info)))))
  1183. (todo-type (and todo (org-element-property :todo-type headline)))
  1184. (tags (and (plist-get info :with-tags)
  1185. (org-export-get-tags headline info)))
  1186. (priority (and (plist-get info :with-priority)
  1187. (org-element-property :priority headline)))
  1188. ;; Create the headline text along with a no-tag version. The
  1189. ;; latter is required to remove tags from table of contents.
  1190. (full-text (if (functionp org-e-latex-format-headline-function)
  1191. ;; User-defined formatting function.
  1192. (funcall org-e-latex-format-headline-function
  1193. todo todo-type priority text tags)
  1194. ;; Default formatting.
  1195. (concat
  1196. (when todo
  1197. (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1198. (when priority (format "\\framebox{\\#%c} " priority))
  1199. text
  1200. (when tags
  1201. (format "\\hfill{}\\textsc{:%s:}"
  1202. (mapconcat 'identity tags ":"))))))
  1203. (full-text-no-tag
  1204. (if (functionp org-e-latex-format-headline-function)
  1205. ;; User-defined formatting function.
  1206. (funcall org-e-latex-format-headline-function
  1207. todo todo-type priority text nil)
  1208. ;; Default formatting.
  1209. (concat
  1210. (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1211. (when priority (format "\\framebox{\\#%c} " priority))
  1212. text)))
  1213. ;; Associate some \label to the headline for internal links.
  1214. (headline-label
  1215. (format "\\label{sec-%s}\n"
  1216. (mapconcat 'number-to-string
  1217. (org-export-get-headline-number headline info)
  1218. "-")))
  1219. (pre-blanks
  1220. (make-string (org-element-property :pre-blank headline) 10)))
  1221. (cond
  1222. ;; Case 1: This is a footnote section: ignore it.
  1223. ((org-element-property :footnote-section-p headline) nil)
  1224. ;; Case 2. This is a deep sub-tree: export it as a list item.
  1225. ;; Also export as items headlines for which no section
  1226. ;; format has been found.
  1227. ((or (not section-fmt) (org-export-low-level-p headline info))
  1228. ;; Build the real contents of the sub-tree.
  1229. (let ((low-level-body
  1230. (concat
  1231. ;; If the headline is the first sibling, start a list.
  1232. (when (org-export-first-sibling-p headline)
  1233. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  1234. ;; Itemize headline
  1235. "\\item " full-text "\n" headline-label pre-blanks contents)))
  1236. ;; If headline is not the last sibling simply return
  1237. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
  1238. ;; blank line.
  1239. (if (not (org-export-last-sibling-p headline)) low-level-body
  1240. (replace-regexp-in-string
  1241. "[ \t\n]*\\'"
  1242. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  1243. low-level-body))))
  1244. ;; Case 3. Standard headline. Export it as a section.
  1245. (t
  1246. (cond
  1247. ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
  1248. ;; Regular section. Use specified format string.
  1249. (format section-fmt full-text
  1250. (concat headline-label pre-blanks contents)))
  1251. ((string-match "\\`\\\\\\(.*?\\){" section-fmt)
  1252. ;; If tags should be removed from table of contents, insert
  1253. ;; title without tags as an alternative heading in sectioning
  1254. ;; command.
  1255. (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
  1256. nil nil section-fmt 1)
  1257. ;; Replace square brackets with parenthesis since
  1258. ;; square brackets are not supported in optional
  1259. ;; arguments.
  1260. (replace-regexp-in-string
  1261. "\\[" "("
  1262. (replace-regexp-in-string
  1263. "\\]" ")"
  1264. full-text-no-tag))
  1265. full-text
  1266. (concat headline-label pre-blanks contents)))
  1267. (t
  1268. ;; Impossible to add an alternative heading. Fallback to
  1269. ;; regular sectioning format string.
  1270. (format section-fmt full-text
  1271. (concat headline-label pre-blanks contents))))))))
  1272. ;;;; Horizontal Rule
  1273. (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
  1274. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  1275. CONTENTS is nil. INFO is a plist holding contextual information."
  1276. (let ((attr (mapconcat #'identity
  1277. (org-element-property :attr_latex horizontal-rule)
  1278. " ")))
  1279. (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
  1280. ;;;; Inline Babel Call
  1281. ;;
  1282. ;; Inline Babel Calls are ignored.
  1283. ;;;; Inline Src Block
  1284. (defun org-e-latex-inline-src-block (inline-src-block contents info)
  1285. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  1286. CONTENTS holds the contents of the item. INFO is a plist holding
  1287. contextual information."
  1288. (let* ((code (org-element-property :value inline-src-block))
  1289. (separator (org-e-latex--find-verb-separator code)))
  1290. (cond
  1291. ;; Do not use a special package: transcode it verbatim.
  1292. ((not org-e-latex-listings)
  1293. (concat "\\verb" separator code separator))
  1294. ;; Use minted package.
  1295. ((eq org-e-latex-listings 'minted)
  1296. (let* ((org-lang (org-element-property :language inline-src-block))
  1297. (mint-lang (or (cadr (assq (intern org-lang)
  1298. org-e-latex-minted-langs))
  1299. org-lang))
  1300. (options (org-e-latex--make-option-string
  1301. org-e-latex-minted-options)))
  1302. (concat (format "\\mint%s{%s}"
  1303. (if (string= options "") "" (format "[%s]" options))
  1304. mint-lang)
  1305. separator code separator)))
  1306. ;; Use listings package.
  1307. (t
  1308. ;; Maybe translate language's name.
  1309. (let* ((org-lang (org-element-property :language inline-src-block))
  1310. (lst-lang (or (cadr (assq (intern org-lang)
  1311. org-e-latex-listings-langs))
  1312. org-lang))
  1313. (options (org-e-latex--make-option-string
  1314. (append org-e-latex-listings-options
  1315. `(("language" ,lst-lang))))))
  1316. (concat (format "\\lstinline[%s]" options)
  1317. separator code separator))))))
  1318. ;;;; Inlinetask
  1319. (defun org-e-latex-inlinetask (inlinetask contents info)
  1320. "Transcode an INLINETASK element from Org to LaTeX.
  1321. CONTENTS holds the contents of the block. INFO is a plist
  1322. holding contextual information."
  1323. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  1324. (todo (and (plist-get info :with-todo-keywords)
  1325. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1326. (and todo (org-export-data todo info)))))
  1327. (todo-type (org-element-property :todo-type inlinetask))
  1328. (tags (and (plist-get info :with-tags)
  1329. (org-export-get-tags inlinetask info)))
  1330. (priority (and (plist-get info :with-priority)
  1331. (org-element-property :priority inlinetask))))
  1332. ;; If `org-e-latex-format-inlinetask-function' is provided, call it
  1333. ;; with appropriate arguments.
  1334. (if (functionp org-e-latex-format-inlinetask-function)
  1335. (funcall org-e-latex-format-inlinetask-function
  1336. todo todo-type priority title tags contents)
  1337. ;; Otherwise, use a default template.
  1338. (org-e-latex--wrap-label
  1339. inlinetask
  1340. (let ((full-title
  1341. (concat
  1342. (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1343. (when priority (format "\\framebox{\\#%c} " priority))
  1344. title
  1345. (when tags (format "\\hfill{}\\textsc{:%s:}"
  1346. (mapconcat 'identity tags ":"))))))
  1347. (format (concat "\\begin{center}\n"
  1348. "\\fbox{\n"
  1349. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1350. "%s\n\n"
  1351. "\\rule[.8em]{\\textwidth}{2pt}\n\n"
  1352. "%s"
  1353. "\\end{minipage}\n"
  1354. "}\n"
  1355. "\\end{center}")
  1356. full-title contents))))))
  1357. ;;;; Italic
  1358. (defun org-e-latex-italic (italic contents info)
  1359. "Transcode ITALIC from Org to LaTeX.
  1360. CONTENTS is the text with italic markup. INFO is a plist holding
  1361. contextual information."
  1362. (org-e-latex--text-markup contents 'italic))
  1363. ;;;; Item
  1364. (defun org-e-latex-item (item contents info)
  1365. "Transcode an ITEM element from Org to LaTeX.
  1366. CONTENTS holds the contents of the item. INFO is a plist holding
  1367. contextual information."
  1368. (let* ((counter
  1369. (let ((count (org-element-property :counter item))
  1370. (level
  1371. (loop for parent in (org-export-get-genealogy item)
  1372. count (eq (org-element-type parent) 'plain-list)
  1373. until (eq (org-element-type parent) 'headline))))
  1374. (and count
  1375. (< level 5)
  1376. (format "\\setcounter{enum%s}{%s}\n"
  1377. (nth (1- level) '("i" "ii" "iii" "iv"))
  1378. (1- count)))))
  1379. (checkbox (case (org-element-property :checkbox item)
  1380. (on "$\\boxtimes$ ")
  1381. (off "$\\Box$ ")
  1382. (trans "$\\boxminus$ ")))
  1383. (tag (let ((tag (org-element-property :tag item)))
  1384. ;; Check-boxes must belong to the tag.
  1385. (and tag (format "[%s] "
  1386. (concat checkbox
  1387. (org-export-data tag info)))))))
  1388. (concat counter "\\item" (or tag (concat " " checkbox))
  1389. (org-trim contents)
  1390. ;; If there are footnotes references in tag, be sure to
  1391. ;; add their definition at the end of the item. This
  1392. ;; workaround is necessary since "\footnote{}" command is
  1393. ;; not supported in tags.
  1394. (and tag
  1395. (org-e-latex--delayed-footnotes-definitions
  1396. (org-element-property :tag item) info)))))
  1397. ;;;; Keyword
  1398. (defun org-e-latex-keyword (keyword contents info)
  1399. "Transcode a KEYWORD element from Org to LaTeX.
  1400. CONTENTS is nil. INFO is a plist holding contextual information."
  1401. (let ((key (org-element-property :key keyword))
  1402. (value (org-element-property :value keyword)))
  1403. (cond
  1404. ((string= key "LATEX") value)
  1405. ((string= key "INDEX") (format "\\index{%s}" value))
  1406. ;; Invisible targets.
  1407. ((string= key "TARGET") nil)
  1408. ((string= key "TOC")
  1409. (let ((value (downcase value)))
  1410. (cond
  1411. ((string-match "\\<headlines\\>" value)
  1412. (let ((depth (or (and (string-match "[0-9]+" value)
  1413. (string-to-number (match-string 0 value)))
  1414. (plist-get info :with-toc))))
  1415. (concat
  1416. (when (wholenump depth)
  1417. (format "\\setcounter{tocdepth}{%s}\n" depth))
  1418. "\\tableofcontents")))
  1419. ((string= "tables" value) "\\listoftables")
  1420. ((string= "figures" value) "\\listoffigures")
  1421. ((string= "listings" value)
  1422. (cond
  1423. ((eq org-e-latex-listings 'minted) "\\listoflistings")
  1424. (org-e-latex-listings "\\lstlistoflistings")
  1425. ;; At the moment, src blocks with a caption are wrapped
  1426. ;; into a figure environment.
  1427. (t "\\listoffigures")))))))))
  1428. ;;;; Latex Environment
  1429. (defun org-e-latex-latex-environment (latex-environment contents info)
  1430. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1431. CONTENTS is nil. INFO is a plist holding contextual information."
  1432. (let ((label (org-element-property :name latex-environment))
  1433. (value (org-remove-indentation
  1434. (org-element-property :value latex-environment))))
  1435. (if (not (org-string-nw-p label)) value
  1436. ;; Environment is labelled: label must be within the environment
  1437. ;; (otherwise, a reference pointing to that element will count
  1438. ;; the section instead).
  1439. (with-temp-buffer
  1440. (insert value)
  1441. (goto-char (point-min))
  1442. (forward-line)
  1443. (insert (format "\\label{%s}\n" label))
  1444. (buffer-string)))))
  1445. ;;;; Latex Fragment
  1446. (defun org-e-latex-latex-fragment (latex-fragment contents info)
  1447. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1448. CONTENTS is nil. INFO is a plist holding contextual information."
  1449. (org-element-property :value latex-fragment))
  1450. ;;;; Line Break
  1451. (defun org-e-latex-line-break (line-break contents info)
  1452. "Transcode a LINE-BREAK object from Org to LaTeX.
  1453. CONTENTS is nil. INFO is a plist holding contextual information."
  1454. "\\\\")
  1455. ;;;; Link
  1456. (defun org-e-latex-link--inline-image (link info)
  1457. "Return LaTeX code for an inline image.
  1458. LINK is the link pointing to the inline image. INFO is a plist
  1459. used as a communication channel."
  1460. (let* ((parent (org-export-get-parent-element link))
  1461. (path (let ((raw-path (org-element-property :path link)))
  1462. (if (not (file-name-absolute-p raw-path)) raw-path
  1463. (expand-file-name raw-path))))
  1464. (caption (org-e-latex--caption/label-string
  1465. (org-element-property :caption parent)
  1466. (org-element-property :name parent)
  1467. info))
  1468. ;; Retrieve latex attributes from the element around.
  1469. (attr (let ((raw-attr
  1470. (mapconcat #'identity
  1471. (org-element-property :attr_latex parent)
  1472. " ")))
  1473. (unless (string= raw-attr "") raw-attr)))
  1474. (disposition
  1475. (cond
  1476. ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
  1477. ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
  1478. ((or (and attr (string-match "\\<float\\>" attr))
  1479. (not (string= caption "")))
  1480. 'float)))
  1481. (placement
  1482. (cond
  1483. ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
  1484. (org-match-string-no-properties 1 attr))
  1485. ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
  1486. ((eq disposition 'float)
  1487. (concat "[" org-e-latex-default-figure-position "]"))
  1488. (t ""))))
  1489. ;; Now clear ATTR from any special keyword and set a default
  1490. ;; value if nothing is left.
  1491. (setq attr
  1492. (if (not attr) ""
  1493. (org-trim
  1494. (replace-regexp-in-string
  1495. "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr))))
  1496. (setq attr (cond ((not (string= attr "")) attr)
  1497. ((eq disposition 'float) "width=0.7\\textwidth")
  1498. ((eq disposition 'wrap) "width=0.48\\textwidth")
  1499. (t (or org-e-latex-image-default-option ""))))
  1500. ;; Return proper string, depending on DISPOSITION.
  1501. (case disposition
  1502. (wrap (format "\\begin{wrapfigure}%s
  1503. \\centering
  1504. \\includegraphics[%s]{%s}
  1505. %s\\end{wrapfigure}" placement attr path caption))
  1506. (multicolumn (format "\\begin{figure*}%s
  1507. \\centering
  1508. \\includegraphics[%s]{%s}
  1509. %s\\end{figure*}" placement attr path caption))
  1510. (float (format "\\begin{figure}%s
  1511. \\centering
  1512. \\includegraphics[%s]{%s}
  1513. %s\\end{figure}" placement attr path caption))
  1514. (t (format "\\includegraphics[%s]{%s}" attr path)))))
  1515. (defun org-e-latex-link (link desc info)
  1516. "Transcode a LINK object from Org to LaTeX.
  1517. DESC is the description part of the link, or the empty string.
  1518. INFO is a plist holding contextual information. See
  1519. `org-export-data'."
  1520. (let* ((type (org-element-property :type link))
  1521. (raw-path (org-element-property :path link))
  1522. ;; Ensure DESC really exists, or set it to nil.
  1523. (desc (and (not (string= desc "")) desc))
  1524. (imagep (org-export-inline-image-p
  1525. link org-e-latex-inline-image-rules))
  1526. (path (cond
  1527. ((member type '("http" "https" "ftp" "mailto"))
  1528. (concat type ":" raw-path))
  1529. ((string= type "file")
  1530. (when (string-match "\\(.+\\)::.+" raw-path)
  1531. (setq raw-path (match-string 1 raw-path)))
  1532. (if (file-name-absolute-p raw-path)
  1533. (concat "file://" (expand-file-name raw-path))
  1534. (concat "file://" raw-path)))
  1535. (t raw-path)))
  1536. protocol)
  1537. (cond
  1538. ;; Image file.
  1539. (imagep (org-e-latex-link--inline-image link info))
  1540. ;; Radio link: Transcode target's contents and use them as link's
  1541. ;; description.
  1542. ((string= type "radio")
  1543. (let ((destination (org-export-resolve-radio-link link info)))
  1544. (when destination
  1545. (format "\\hyperref[%s]{%s}"
  1546. (org-export-solidify-link-text path)
  1547. (org-export-data (org-element-contents destination) info)))))
  1548. ;; Links pointing to an headline: Find destination and build
  1549. ;; appropriate referencing command.
  1550. ((member type '("custom-id" "fuzzy" "id"))
  1551. (let ((destination (if (string= type "fuzzy")
  1552. (org-export-resolve-fuzzy-link link info)
  1553. (org-export-resolve-id-link link info))))
  1554. (case (org-element-type destination)
  1555. ;; Id link points to an external file.
  1556. (plain-text
  1557. (if desc (format "\\href{file://%s}{%s}" destination desc)
  1558. (format "\\url{file://%s}" destination)))
  1559. ;; Fuzzy link points nowhere.
  1560. ('nil
  1561. (format org-e-latex-link-with-unknown-path-format
  1562. (or desc
  1563. (org-export-data
  1564. (org-element-property :raw-link link) info))))
  1565. ;; Fuzzy link points to an invisible target.
  1566. (keyword nil)
  1567. ;; LINK points to an headline. If headlines are numbered
  1568. ;; and the link has no description, display headline's
  1569. ;; number. Otherwise, display description or headline's
  1570. ;; title.
  1571. (headline
  1572. (let ((label
  1573. (format "sec-%s"
  1574. (mapconcat
  1575. 'number-to-string
  1576. (org-export-get-headline-number destination info)
  1577. "-"))))
  1578. (if (and (plist-get info :section-numbers) (not desc))
  1579. (format "\\ref{%s}" label)
  1580. (format "\\hyperref[%s]{%s}" label
  1581. (or desc
  1582. (org-export-data
  1583. (org-element-property :title destination) info))))))
  1584. ;; Fuzzy link points to a target. Do as above.
  1585. (otherwise
  1586. (let ((path (org-export-solidify-link-text path)))
  1587. (if (not desc) (format "\\ref{%s}" path)
  1588. (format "\\hyperref[%s]{%s}" path desc)))))))
  1589. ;; Coderef: replace link with the reference name or the
  1590. ;; equivalent line number.
  1591. ((string= type "coderef")
  1592. (format (org-export-get-coderef-format path desc)
  1593. (org-export-resolve-coderef path info)))
  1594. ;; Link type is handled by a special function.
  1595. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  1596. (funcall protocol (org-link-unescape path) desc 'latex))
  1597. ;; External link with a description part.
  1598. ((and path desc) (format "\\href{%s}{%s}" path desc))
  1599. ;; External link without a description part.
  1600. (path (format "\\url{%s}" path))
  1601. ;; No path, only description. Try to do something useful.
  1602. (t (format org-e-latex-link-with-unknown-path-format desc)))))
  1603. ;;;; Macro
  1604. (defun org-e-latex-macro (macro contents info)
  1605. "Transcode a MACRO element from Org to LaTeX.
  1606. CONTENTS is nil. INFO is a plist holding contextual information."
  1607. ;; Use available tools.
  1608. (org-export-expand-macro macro info))
  1609. ;;;; Paragraph
  1610. (defun org-e-latex-paragraph (paragraph contents info)
  1611. "Transcode a PARAGRAPH element from Org to LaTeX.
  1612. CONTENTS is the contents of the paragraph, as a string. INFO is
  1613. the plist used as a communication channel."
  1614. contents)
  1615. ;;;; Plain List
  1616. (defun org-e-latex-plain-list (plain-list contents info)
  1617. "Transcode a PLAIN-LIST element from Org to LaTeX.
  1618. CONTENTS is the contents of the list. INFO is a plist holding
  1619. contextual information."
  1620. (let* ((type (org-element-property :type plain-list))
  1621. (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
  1622. "inparadesc" "asparadesc"))
  1623. (paralist-regexp (concat
  1624. "\\("
  1625. (mapconcat 'identity paralist-types "\\|")
  1626. "\\)"))
  1627. (attr (mapconcat #'identity
  1628. (org-element-property :attr_latex plain-list)
  1629. " "))
  1630. (latex-type (cond
  1631. ((and attr
  1632. (string-match
  1633. (format "\\<%s\\>" paralist-regexp) attr))
  1634. (match-string 1 attr))
  1635. ((eq type 'ordered) "enumerate")
  1636. ((eq type 'unordered) "itemize")
  1637. ((eq type 'descriptive) "description"))))
  1638. (org-e-latex--wrap-label
  1639. plain-list
  1640. (format "\\begin{%s}%s\n%s\\end{%s}"
  1641. latex-type
  1642. ;; Once special environment, if any, has been removed, the
  1643. ;; rest of the attributes will be optional arguments.
  1644. ;; They will be put inside square brackets if necessary.
  1645. (let ((opt (replace-regexp-in-string
  1646. (format " *%s *" paralist-regexp) "" attr)))
  1647. (cond ((string= opt "") "")
  1648. ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
  1649. (t (format "[%s]" opt))))
  1650. contents
  1651. latex-type))))
  1652. ;;;; Plain Text
  1653. (defun org-e-latex-plain-text (text info)
  1654. "Transcode a TEXT string from Org to LaTeX.
  1655. TEXT is the string to transcode. INFO is a plist holding
  1656. contextual information."
  1657. ;; Protect %, #, &, $, ~, ^, _, { and }.
  1658. (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
  1659. (setq text
  1660. (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
  1661. ;; Protect \
  1662. (setq text (replace-regexp-in-string
  1663. "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
  1664. "$\\backslash$" text nil t 1))
  1665. ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
  1666. (let ((case-fold-search nil)
  1667. (start 0))
  1668. (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
  1669. (setq text (replace-match
  1670. (format "\\%s{}" (match-string 1 text)) nil t text)
  1671. start (match-end 0))))
  1672. ;; Handle quotation marks
  1673. (setq text (org-e-latex--quotation-marks text info))
  1674. ;; Convert special strings.
  1675. (when (plist-get info :with-special-strings)
  1676. (while (string-match (regexp-quote "...") text)
  1677. (setq text (replace-match "\\ldots{}" nil t text))))
  1678. ;; Handle break preservation if required.
  1679. (when (plist-get info :preserve-breaks)
  1680. (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
  1681. text)))
  1682. ;; Return value.
  1683. text)
  1684. ;;;; Planning
  1685. (defun org-e-latex-planning (planning contents info)
  1686. "Transcode a PLANNING element from Org to LaTeX.
  1687. CONTENTS is nil. INFO is a plist holding contextual
  1688. information."
  1689. (concat
  1690. "\\noindent"
  1691. (mapconcat
  1692. 'identity
  1693. (delq nil
  1694. (list
  1695. (let ((closed (org-element-property :closed planning)))
  1696. (when closed
  1697. (concat
  1698. (format "\\textbf{%s} " org-closed-string)
  1699. (format org-e-latex-inactive-timestamp-format
  1700. (org-translate-time closed)))))
  1701. (let ((deadline (org-element-property :deadline planning)))
  1702. (when deadline
  1703. (concat
  1704. (format "\\textbf{%s} " org-deadline-string)
  1705. (format org-e-latex-active-timestamp-format
  1706. (org-translate-time deadline)))))
  1707. (let ((scheduled (org-element-property :scheduled planning)))
  1708. (when scheduled
  1709. (concat
  1710. (format "\\textbf{%s} " org-scheduled-string)
  1711. (format org-e-latex-active-timestamp-format
  1712. (org-translate-time scheduled)))))))
  1713. " ")
  1714. "\\\\"))
  1715. ;;;; Property Drawer
  1716. (defun org-e-latex-property-drawer (property-drawer contents info)
  1717. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  1718. CONTENTS is nil. INFO is a plist holding contextual
  1719. information."
  1720. ;; The property drawer isn't exported but we want separating blank
  1721. ;; lines nonetheless.
  1722. "")
  1723. ;;;; Quote Block
  1724. (defun org-e-latex-quote-block (quote-block contents info)
  1725. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  1726. CONTENTS holds the contents of the block. INFO is a plist
  1727. holding contextual information."
  1728. (org-e-latex--wrap-label
  1729. quote-block
  1730. (format "\\begin{quote}\n%s\\end{quote}" contents)))
  1731. ;;;; Quote Section
  1732. (defun org-e-latex-quote-section (quote-section contents info)
  1733. "Transcode a QUOTE-SECTION element from Org to LaTeX.
  1734. CONTENTS is nil. INFO is a plist holding contextual information."
  1735. (let ((value (org-remove-indentation
  1736. (org-element-property :value quote-section))))
  1737. (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  1738. ;;;; Radio Target
  1739. (defun org-e-latex-radio-target (radio-target text info)
  1740. "Transcode a RADIO-TARGET object from Org to LaTeX.
  1741. TEXT is the text of the target. INFO is a plist holding
  1742. contextual information."
  1743. (format "\\label{%s}%s"
  1744. (org-export-solidify-link-text
  1745. (org-element-property :value radio-target))
  1746. text))
  1747. ;;;; Section
  1748. (defun org-e-latex-section (section contents info)
  1749. "Transcode a SECTION element from Org to LaTeX.
  1750. CONTENTS holds the contents of the section. INFO is a plist
  1751. holding contextual information."
  1752. contents)
  1753. ;;;; Special Block
  1754. (defun org-e-latex-special-block (special-block contents info)
  1755. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  1756. CONTENTS holds the contents of the block. INFO is a plist
  1757. holding contextual information."
  1758. (let ((type (downcase (org-element-property :type special-block))))
  1759. (org-e-latex--wrap-label
  1760. special-block
  1761. (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
  1762. ;;;; Src Block
  1763. (defun org-e-latex-src-block (src-block contents info)
  1764. "Transcode a SRC-BLOCK element from Org to LaTeX.
  1765. CONTENTS holds the contents of the item. INFO is a plist holding
  1766. contextual information."
  1767. (let* ((lang (org-element-property :language src-block))
  1768. (caption (org-element-property :caption src-block))
  1769. (label (org-element-property :name src-block))
  1770. (custom-env (and lang
  1771. (cadr (assq (intern lang)
  1772. org-e-latex-custom-lang-environments))))
  1773. (num-start (case (org-element-property :number-lines src-block)
  1774. (continued (org-export-get-loc src-block info))
  1775. (new 0)))
  1776. (retain-labels (org-element-property :retain-labels src-block)))
  1777. (cond
  1778. ;; Case 1. No source fontification.
  1779. ((not org-e-latex-listings)
  1780. (let ((caption-str (org-e-latex--caption/label-string caption label info))
  1781. (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
  1782. (format
  1783. (or float-env "%s")
  1784. (concat caption-str
  1785. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1786. (org-export-format-code-default src-block info))))))
  1787. ;; Case 2. Custom environment.
  1788. (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
  1789. custom-env
  1790. (org-export-format-code-default src-block info)
  1791. custom-env))
  1792. ;; Case 3. Use minted package.
  1793. ((eq org-e-latex-listings 'minted)
  1794. (let ((float-env (when (or label caption)
  1795. (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
  1796. (org-e-latex--caption/label-string
  1797. caption label info))))
  1798. (body
  1799. (format
  1800. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  1801. ;; Options.
  1802. (org-e-latex--make-option-string
  1803. (if (not num-start) org-e-latex-minted-options
  1804. (append `(("linenos")
  1805. ("firstnumber" ,(number-to-string (1+ num-start))))
  1806. org-e-latex-minted-options)))
  1807. ;; Language.
  1808. (or (cadr (assq (intern lang) org-e-latex-minted-langs)) lang)
  1809. ;; Source code.
  1810. (let* ((code-info (org-export-unravel-code src-block))
  1811. (max-width
  1812. (apply 'max
  1813. (mapcar 'length
  1814. (org-split-string (car code-info) "\n")))))
  1815. (org-export-format-code
  1816. (car code-info)
  1817. (lambda (loc num ref)
  1818. (concat
  1819. loc
  1820. (when ref
  1821. ;; Ensure references are flushed to the right,
  1822. ;; separated with 6 spaces from the widest line
  1823. ;; of code.
  1824. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1825. (format "(%s)" ref)))))
  1826. nil (and retain-labels (cdr code-info)))))))
  1827. ;; Return value.
  1828. (if float-env (format float-env body) body)))
  1829. ;; Case 4. Use listings package.
  1830. (t
  1831. (let ((lst-lang
  1832. (or (cadr (assq (intern lang) org-e-latex-listings-langs)) lang))
  1833. (caption-str
  1834. (when caption
  1835. (let ((main (org-export-data (car caption) info)))
  1836. (if (not (cdr caption)) (format "{%s}" main)
  1837. (format "{[%s]%s}"
  1838. (org-export-data (cdr caption) info)
  1839. main))))))
  1840. (concat
  1841. ;; Options.
  1842. (format "\\lstset{%s}\n"
  1843. (org-e-latex--make-option-string
  1844. (append org-e-latex-listings-options
  1845. `(("language" ,lst-lang))
  1846. (when label `(("label" ,label)))
  1847. (when caption-str `(("caption" ,caption-str)))
  1848. (cond ((not num-start) '(("numbers" "none")))
  1849. ((zerop num-start) '(("numbers" "left")))
  1850. (t `(("numbers" "left")
  1851. ("firstnumber"
  1852. ,(number-to-string (1+ num-start)))))))))
  1853. ;; Source code.
  1854. (format
  1855. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  1856. (let* ((code-info (org-export-unravel-code src-block))
  1857. (max-width
  1858. (apply 'max
  1859. (mapcar 'length
  1860. (org-split-string (car code-info) "\n")))))
  1861. (org-export-format-code
  1862. (car code-info)
  1863. (lambda (loc num ref)
  1864. (concat
  1865. loc
  1866. (when ref
  1867. ;; Ensure references are flushed to the right,
  1868. ;; separated with 6 spaces from the widest line of
  1869. ;; code
  1870. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1871. (format "(%s)" ref)))))
  1872. nil (and retain-labels (cdr code-info)))))))))))
  1873. ;;;; Statistics Cookie
  1874. (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
  1875. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  1876. CONTENTS is nil. INFO is a plist holding contextual information."
  1877. (org-element-property :value statistics-cookie))
  1878. ;;;; Strike-Through
  1879. (defun org-e-latex-strike-through (strike-through contents info)
  1880. "Transcode STRIKE-THROUGH from Org to LaTeX.
  1881. CONTENTS is the text with strike-through markup. INFO is a plist
  1882. holding contextual information."
  1883. (org-e-latex--text-markup contents 'strike-through))
  1884. ;;;; Subscript
  1885. (defun org-e-latex-subscript (subscript contents info)
  1886. "Transcode a SUBSCRIPT object from Org to LaTeX.
  1887. CONTENTS is the contents of the object. INFO is a plist holding
  1888. contextual information."
  1889. (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
  1890. ;;;; Superscript
  1891. (defun org-e-latex-superscript (superscript contents info)
  1892. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  1893. CONTENTS is the contents of the object. INFO is a plist holding
  1894. contextual information."
  1895. (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
  1896. ;;;; Table
  1897. ;;
  1898. ;; `org-e-latex-table' is the entry point for table transcoding. It
  1899. ;; takes care of tables with a "verbatim" attribute. Otherwise, it
  1900. ;; delegates the job to either `org-e-latex-table--table.el-table' or
  1901. ;; `org-e-latex-table--org-table' functions, depending of the type of
  1902. ;; the table.
  1903. ;;
  1904. ;; `org-e-latex-table--align-string' is a subroutine used to build
  1905. ;; alignment string for Org tables.
  1906. (defun org-e-latex-table (table contents info)
  1907. "Transcode a TABLE element from Org to LaTeX.
  1908. CONTENTS is the contents of the table. INFO is a plist holding
  1909. contextual information."
  1910. (cond
  1911. ;; Case 1: verbatim table.
  1912. ((or org-e-latex-tables-verbatim
  1913. (let ((attr (mapconcat 'identity
  1914. (org-element-property :attr_latex table)
  1915. " ")))
  1916. (and attr (string-match "\\<verbatim\\>" attr))))
  1917. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  1918. ;; Re-create table, without affiliated keywords.
  1919. (org-trim
  1920. (org-element-interpret-data
  1921. `(table nil ,@(org-element-contents table))))))
  1922. ;; Case 2: table.el table. Convert it using appropriate tools.
  1923. ((eq (org-element-property :type table) 'table.el)
  1924. (org-e-latex-table--table.el-table table contents info))
  1925. ;; Case 3: Standard table.
  1926. (t (org-e-latex-table--org-table table contents info))))
  1927. (defun org-e-latex-table--align-string (table info)
  1928. "Return an appropriate LaTeX alignment string.
  1929. TABLE is the considered table. INFO is a plist used as
  1930. a communication channel."
  1931. (let ((attr (mapconcat 'identity
  1932. (org-element-property :attr_latex table)
  1933. " ")))
  1934. (if (string-match "\\<align=\\(\\S-+\\)" attr) (match-string 1 attr)
  1935. (let (alignment)
  1936. ;; Extract column groups and alignment from first (non-rule)
  1937. ;; row.
  1938. (org-element-map
  1939. (org-element-map
  1940. table 'table-row
  1941. (lambda (row)
  1942. (and (eq (org-element-property :type row) 'standard) row))
  1943. info 'first-match)
  1944. 'table-cell
  1945. (lambda (cell)
  1946. (let ((borders (org-export-table-cell-borders cell info)))
  1947. ;; Check left border for the first cell only.
  1948. (when (and (memq 'left borders) (not alignment))
  1949. (push "|" alignment))
  1950. (push (case (org-export-table-cell-alignment cell info)
  1951. (left "l")
  1952. (right "r")
  1953. (center "c"))
  1954. alignment)
  1955. (when (memq 'right borders) (push "|" alignment))))
  1956. info)
  1957. (apply 'concat (reverse alignment))))))
  1958. (defun org-e-latex-table--org-table (table contents info)
  1959. "Return appropriate LaTeX code for an Org table.
  1960. TABLE is the table type element to transcode. CONTENTS is its
  1961. contents, as a string. INFO is a plist used as a communication
  1962. channel.
  1963. This function assumes TABLE has `org' as its `:type' attribute."
  1964. (let* ((label (org-element-property :name table))
  1965. (caption (org-e-latex--caption/label-string
  1966. (org-element-property :caption table) label info))
  1967. (attr (mapconcat 'identity
  1968. (org-element-property :attr_latex table)
  1969. " "))
  1970. ;; Determine alignment string.
  1971. (alignment (org-e-latex-table--align-string table info))
  1972. ;; Determine environment for the table: longtable, tabular...
  1973. (table-env (cond
  1974. ((not attr) org-e-latex-default-table-environment)
  1975. ((string-match "\\<longtable\\>" attr) "longtable")
  1976. ((string-match "\\<tabular.?\\>" attr)
  1977. (org-match-string-no-properties 0 attr))
  1978. (t org-e-latex-default-table-environment)))
  1979. ;; If table is a float, determine environment: table, table*
  1980. ;; or sidewaystable.
  1981. (float-env (cond
  1982. ((string= "longtable" table-env) nil)
  1983. ((and attr (string-match "\\<sidewaystable\\>" attr))
  1984. "sidewaystables")
  1985. ((and attr
  1986. (or (string-match (regexp-quote "table*") attr)
  1987. (string-match "\\<multicolumn\\>" attr)))
  1988. "table*")
  1989. ((or (not (string= caption "")) label) "table")))
  1990. ;; Extract others display options.
  1991. (width (and attr (string-match "\\<width=\\(\\S-+\\)" attr)
  1992. (org-match-string-no-properties 1 attr)))
  1993. (placement
  1994. (if (and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
  1995. (org-match-string-no-properties 1 attr)
  1996. (format "[%s]" org-e-latex-default-figure-position))))
  1997. ;; Prepare the final format string for the table.
  1998. (cond
  1999. ;; Longtable.
  2000. ((string= "longtable" table-env)
  2001. (format
  2002. "\\begin{longtable}{%s}\n%s%s%s\\end{longtable}"
  2003. alignment
  2004. (if (or (not org-e-latex-table-caption-above) (string= "" caption)) ""
  2005. (concat (org-trim caption) "\\\\\n"))
  2006. contents
  2007. (if (or org-e-latex-table-caption-above (string= "" caption)) ""
  2008. (concat (org-trim caption) "\\\\\n"))))
  2009. ;; Others.
  2010. (t (concat (when float-env
  2011. (concat
  2012. (format "\\begin{%s}%s\n" float-env placement)
  2013. (if org-e-latex-table-caption-above caption "")))
  2014. (when org-e-latex-tables-centered "\\begin{center}\n")
  2015. (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  2016. table-env
  2017. (if width (format "{%s}" width) "")
  2018. alignment
  2019. contents
  2020. table-env)
  2021. (when org-e-latex-tables-centered "\n\\end{center}")
  2022. (when float-env
  2023. (concat (if org-e-latex-table-caption-above "" caption)
  2024. (format "\n\\end{%s}" float-env))))))))
  2025. (defun org-e-latex-table--table.el-table (table contents info)
  2026. "Return appropriate LaTeX code for a table.el table.
  2027. TABLE is the table type element to transcode. CONTENTS is its
  2028. contents, as a string. INFO is a plist used as a communication
  2029. channel.
  2030. This function assumes TABLE has `table.el' as its `:type'
  2031. attribute."
  2032. (require 'table)
  2033. ;; Ensure "*org-export-table*" buffer is empty.
  2034. (with-current-buffer (get-buffer-create "*org-export-table*")
  2035. (erase-buffer))
  2036. (let ((output (with-temp-buffer
  2037. (insert (org-element-property :value table))
  2038. (goto-char 1)
  2039. (re-search-forward "^[ \t]*|[^|]" nil t)
  2040. (table-generate-source 'latex "*org-export-table*")
  2041. (with-current-buffer "*org-export-table*"
  2042. (org-trim (buffer-string))))))
  2043. (kill-buffer (get-buffer "*org-export-table*"))
  2044. ;; Remove left out comments.
  2045. (while (string-match "^%.*\n" output)
  2046. (setq output (replace-match "" t t output)))
  2047. ;; When the "rmlines" attribute is provided, remove all hlines but
  2048. ;; the the one separating heading from the table body.
  2049. (let ((attr (mapconcat 'identity
  2050. (org-element-property :attr_latex table)
  2051. " ")))
  2052. (when (and attr (string-match "\\<rmlines\\>" attr))
  2053. (let ((n 0) (pos 0))
  2054. (while (and (< (length output) pos)
  2055. (setq pos (string-match "^\\\\hline\n?" output pos)))
  2056. (incf n)
  2057. (unless (= n 2)
  2058. (setq output (replace-match "" nil nil output)))))))
  2059. (if (not org-e-latex-tables-centered) output
  2060. (format "\\begin{center}\n%s\n\\end{center}" output))))
  2061. ;;;; Table Cell
  2062. (defun org-e-latex-table-cell (table-cell contents info)
  2063. "Transcode a TABLE-CELL element from Org to LaTeX.
  2064. CONTENTS is the cell contents. INFO is a plist used as
  2065. a communication channel."
  2066. (concat (if (and contents
  2067. org-e-latex-table-scientific-notation
  2068. (string-match orgtbl-exp-regexp contents))
  2069. ;; Use appropriate format string for scientific
  2070. ;; notation.
  2071. (format org-e-latex-table-scientific-notation
  2072. (match-string 1 contents)
  2073. (match-string 2 contents))
  2074. contents)
  2075. (when (org-export-get-next-element table-cell) " & ")))
  2076. ;;;; Table Row
  2077. (defun org-e-latex-table-row (table-row contents info)
  2078. "Transcode a TABLE-ROW element from Org to LaTeX.
  2079. CONTENTS is the contents of the row. INFO is a plist used as
  2080. a communication channel."
  2081. ;; Rules are ignored since table separators are deduced from
  2082. ;; borders of the current row.
  2083. (when (eq (org-element-property :type table-row) 'standard)
  2084. (let* ((attr (mapconcat 'identity
  2085. (org-element-property
  2086. :attr_latex (org-export-get-parent table-row))
  2087. " "))
  2088. (longtablep (and attr (string-match "\\<longtable\\>" attr)))
  2089. (booktabsp
  2090. (or (and attr (string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr))
  2091. org-e-latex-tables-booktabs))
  2092. ;; TABLE-ROW's borders are extracted from its first cell.
  2093. (borders
  2094. (org-export-table-cell-borders
  2095. (car (org-element-contents table-row)) info)))
  2096. (concat
  2097. ;; When BOOKTABS are activated enforce top-rule even when no
  2098. ;; hline was specifically marked.
  2099. (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
  2100. ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
  2101. contents "\\\\\n"
  2102. (cond
  2103. ;; Special case for long tables. Define header and footers.
  2104. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  2105. (format "%s
  2106. \\endhead
  2107. %s\\multicolumn{%d}{r}{Continued on next page} \\\\
  2108. \\endfoot
  2109. \\endlastfoot"
  2110. (if booktabsp "\\midrule" "\\hline")
  2111. (if booktabsp "\\midrule" "\\hline")
  2112. ;; Number of columns.
  2113. (cdr (org-export-table-dimensions
  2114. (org-export-get-parent-table table-row) info))))
  2115. ;; When BOOKTABS are activated enforce bottom rule even when
  2116. ;; no hline was specifically marked.
  2117. ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
  2118. ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
  2119. ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
  2120. ;;;; Target
  2121. (defun org-e-latex-target (target contents info)
  2122. "Transcode a TARGET object from Org to LaTeX.
  2123. CONTENTS is nil. INFO is a plist holding contextual
  2124. information."
  2125. (format "\\label{%s}"
  2126. (org-export-solidify-link-text (org-element-property :value target))))
  2127. ;;;; Timestamp
  2128. (defun org-e-latex-timestamp (timestamp contents info)
  2129. "Transcode a TIMESTAMP object from Org to LaTeX.
  2130. CONTENTS is nil. INFO is a plist holding contextual
  2131. information."
  2132. (let ((value (org-translate-time (org-element-property :value timestamp)))
  2133. (type (org-element-property :type timestamp)))
  2134. (cond ((memq type '(active active-range))
  2135. (format org-e-latex-active-timestamp-format value))
  2136. ((memq type '(inactive inactive-range))
  2137. (format org-e-latex-inactive-timestamp-format value))
  2138. (t (format org-e-latex-diary-timestamp-format value)))))
  2139. ;;;; Underline
  2140. (defun org-e-latex-underline (underline contents info)
  2141. "Transcode UNDERLINE from Org to LaTeX.
  2142. CONTENTS is the text with underline markup. INFO is a plist
  2143. holding contextual information."
  2144. (org-e-latex--text-markup contents 'underline))
  2145. ;;;; Verbatim
  2146. (defun org-e-latex-verbatim (verbatim contents info)
  2147. "Transcode a VERBATIM object from Org to LaTeX.
  2148. CONTENTS is nil. INFO is a plist used as a communication
  2149. channel."
  2150. (org-e-latex--text-markup (org-element-property :value verbatim) 'verbatim))
  2151. ;;;; Verse Block
  2152. (defun org-e-latex-verse-block (verse-block contents info)
  2153. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  2154. CONTENTS is verse block contents. INFO is a plist holding
  2155. contextual information."
  2156. (org-e-latex--wrap-label
  2157. verse-block
  2158. ;; In a verse environment, add a line break to each newline
  2159. ;; character and change each white space at beginning of a line
  2160. ;; into a space of 1 em. Also change each blank line with
  2161. ;; a vertical space of 1 em.
  2162. (progn
  2163. (setq contents (replace-regexp-in-string
  2164. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  2165. (replace-regexp-in-string
  2166. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
  2167. (while (string-match "^[ \t]+" contents)
  2168. (let ((new-str (format "\\hspace*{%dem}"
  2169. (length (match-string 0 contents)))))
  2170. (setq contents (replace-match new-str nil t contents))))
  2171. (format "\\begin{verse}\n%s\\end{verse}" contents))))
  2172. ;;; Interactive functions
  2173. (defun org-e-latex-export-to-latex
  2174. (&optional subtreep visible-only body-only ext-plist pub-dir)
  2175. "Export current buffer to a LaTeX file.
  2176. If narrowing is active in the current buffer, only export its
  2177. narrowed part.
  2178. If a region is active, export that region.
  2179. When optional argument SUBTREEP is non-nil, export the sub-tree
  2180. at point, extracting information from the headline properties
  2181. first.
  2182. When optional argument VISIBLE-ONLY is non-nil, don't export
  2183. contents of hidden elements.
  2184. When optional argument BODY-ONLY is non-nil, only write code
  2185. between \"\\begin{document}\" and \"\\end{document}\".
  2186. EXT-PLIST, when provided, is a property list with external
  2187. parameters overriding Org default settings, but still inferior to
  2188. file-local settings.
  2189. When optional argument PUB-DIR is set, use it as the publishing
  2190. directory.
  2191. Return output file's name."
  2192. (interactive)
  2193. (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
  2194. (org-export-to-file
  2195. 'e-latex outfile subtreep visible-only body-only ext-plist)))
  2196. (defun org-e-latex-export-to-pdf
  2197. (&optional subtreep visible-only body-only ext-plist pub-dir)
  2198. "Export current buffer to LaTeX then process through to PDF.
  2199. If narrowing is active in the current buffer, only export its
  2200. narrowed part.
  2201. If a region is active, export that region.
  2202. When optional argument SUBTREEP is non-nil, export the sub-tree
  2203. at point, extracting information from the headline properties
  2204. first.
  2205. When optional argument VISIBLE-ONLY is non-nil, don't export
  2206. contents of hidden elements.
  2207. When optional argument BODY-ONLY is non-nil, only write code
  2208. between \"\\begin{document}\" and \"\\end{document}\".
  2209. EXT-PLIST, when provided, is a property list with external
  2210. parameters overriding Org default settings, but still inferior to
  2211. file-local settings.
  2212. When optional argument PUB-DIR is set, use it as the publishing
  2213. directory.
  2214. Return PDF file's name."
  2215. (interactive)
  2216. (org-e-latex-compile
  2217. (org-e-latex-export-to-latex
  2218. subtreep visible-only body-only ext-plist pub-dir)))
  2219. (defun org-e-latex-compile (texfile)
  2220. "Compile a TeX file.
  2221. TEXFILE is the name of the file being compiled. Processing is
  2222. done through the command specified in `org-e-latex-pdf-process'.
  2223. Return PDF file name or an error if it couldn't be produced."
  2224. (let* ((wconfig (current-window-configuration))
  2225. (texfile (file-truename texfile))
  2226. (base (file-name-sans-extension texfile))
  2227. errors)
  2228. (message (format "Processing LaTeX file %s ..." texfile))
  2229. (unwind-protect
  2230. (progn
  2231. (cond
  2232. ;; A function is provided: Apply it.
  2233. ((functionp org-e-latex-pdf-process)
  2234. (funcall org-e-latex-pdf-process (shell-quote-argument texfile)))
  2235. ;; A list is provided: Replace %b, %f and %o with appropriate
  2236. ;; values in each command before applying it. Output is
  2237. ;; redirected to "*Org PDF LaTeX Output*" buffer.
  2238. ((consp org-e-latex-pdf-process)
  2239. (let* ((out-dir (or (file-name-directory texfile) "./"))
  2240. (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
  2241. (mapc
  2242. (lambda (command)
  2243. (shell-command
  2244. (replace-regexp-in-string
  2245. "%b" (shell-quote-argument base)
  2246. (replace-regexp-in-string
  2247. "%f" (shell-quote-argument texfile)
  2248. (replace-regexp-in-string
  2249. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  2250. outbuf))
  2251. org-e-latex-pdf-process)
  2252. ;; Collect standard errors from output buffer.
  2253. (setq errors (org-e-latex-collect-errors outbuf))))
  2254. (t (error "No valid command to process to PDF")))
  2255. (let ((pdffile (concat base ".pdf")))
  2256. ;; Check for process failure. Provide collected errors if
  2257. ;; possible.
  2258. (if (not (file-exists-p pdffile))
  2259. (error (concat (format "PDF file %s wasn't produced" pdffile)
  2260. (when errors (concat ": " errors))))
  2261. ;; Else remove log files, when specified, and signal end of
  2262. ;; process to user, along with any error encountered.
  2263. (when org-e-latex-remove-logfiles
  2264. (dolist (ext org-e-latex-logfiles-extensions)
  2265. (let ((file (concat base "." ext)))
  2266. (when (file-exists-p file) (delete-file file)))))
  2267. (message (concat "Process completed"
  2268. (if (not errors) "."
  2269. (concat " with errors: " errors)))))
  2270. ;; Return output file name.
  2271. pdffile))
  2272. (set-window-configuration wconfig))))
  2273. (defun org-e-latex-collect-errors (buffer)
  2274. "Collect some kind of errors from \"pdflatex\" command output.
  2275. BUFFER is the buffer containing output.
  2276. Return collected error types as a string, or nil if there was
  2277. none."
  2278. (with-current-buffer buffer
  2279. (save-excursion
  2280. (goto-char (point-max))
  2281. ;; Find final "pdflatex" run.
  2282. (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
  2283. (let ((case-fold-search t)
  2284. (errors ""))
  2285. (when (save-excursion
  2286. (re-search-forward "Reference.*?undefined" nil t))
  2287. (setq errors (concat errors " [undefined reference]")))
  2288. (when (save-excursion
  2289. (re-search-forward "Citation.*?undefined" nil t))
  2290. (setq errors (concat errors " [undefined citation]")))
  2291. (when (save-excursion
  2292. (re-search-forward "Undefined control sequence" nil t))
  2293. (setq errors (concat errors " [undefined control sequence]")))
  2294. (when (save-excursion
  2295. (re-search-forward "^! LaTeX.*?Error" nil t))
  2296. (setq errors (concat errors " [LaTeX error]")))
  2297. (when (save-excursion
  2298. (re-search-forward "^! Package.*?Error" nil t))
  2299. (setq errors (concat errors " [package error]")))
  2300. (and (org-string-nw-p errors) (org-trim errors)))))))
  2301. (provide 'org-e-latex)
  2302. ;;; org-e-latex.el ends here