ox-latex.el 104 KB

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