ox-latex.el 103 KB

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