ox-latex.el 104 KB

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