ox-latex.el 104 KB

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