org-e-latex.el 104 KB

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