org-e-latex.el 104 KB

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