org-e-latex.el 93 KB

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