ox-latex.el 104 KB

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