org-e-latex.el 94 KB

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