ox-latex.el 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886
  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. (org-export-get-optional-title headline info) info)
  1265. (and (eq (plist-get info :with-tags) t) tags))))
  1266. (if (and opt-title (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
  1267. (format (replace-match "\\1[%s]" nil nil section-fmt 1)
  1268. ;; Replace square brackets with parenthesis
  1269. ;; since square brackets are not supported in
  1270. ;; optional arguments.
  1271. (replace-regexp-in-string
  1272. "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
  1273. full-text
  1274. (concat headline-label pre-blanks contents))
  1275. ;; Impossible to add an alternative heading. Fallback to
  1276. ;; regular sectioning format string.
  1277. (format section-fmt full-text
  1278. (concat headline-label pre-blanks contents))))))))
  1279. (defun org-latex-format-headline-default-function
  1280. (todo todo-type priority text tags)
  1281. "Default format function for a headline.
  1282. See `org-latex-format-headline-function' for details."
  1283. (concat
  1284. (and todo (format "\\textbf{\\textsc{\\textsf{%s}}} " todo))
  1285. (and priority (format "\\framebox{\\#%c} " priority))
  1286. text
  1287. (and tags
  1288. (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags ":")))))
  1289. ;;;; Horizontal Rule
  1290. (defun org-latex-horizontal-rule (horizontal-rule contents info)
  1291. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  1292. CONTENTS is nil. INFO is a plist holding contextual information."
  1293. (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
  1294. (prev (org-export-get-previous-element horizontal-rule info)))
  1295. (concat
  1296. ;; Make sure the rule doesn't start at the end of the current
  1297. ;; line by separating it with a blank line from previous element.
  1298. (when (and prev
  1299. (let ((prev-blank (org-element-property :post-blank prev)))
  1300. (or (not prev-blank) (zerop prev-blank))))
  1301. "\n")
  1302. (org-latex--wrap-label
  1303. horizontal-rule
  1304. (format "\\rule{%s}{%s}"
  1305. (or (plist-get attr :width) "\\linewidth")
  1306. (or (plist-get attr :thickness) "0.5pt"))))))
  1307. ;;;; Inline Src Block
  1308. (defun org-latex-inline-src-block (inline-src-block contents info)
  1309. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  1310. CONTENTS holds the contents of the item. INFO is a plist holding
  1311. contextual information."
  1312. (let* ((code (org-element-property :value inline-src-block))
  1313. (separator (org-latex--find-verb-separator code)))
  1314. (cond
  1315. ;; Do not use a special package: transcode it verbatim.
  1316. ((not org-latex-listings)
  1317. (concat "\\verb" separator code separator))
  1318. ;; Use minted package.
  1319. ((eq org-latex-listings 'minted)
  1320. (let* ((org-lang (org-element-property :language inline-src-block))
  1321. (mint-lang (or (cadr (assq (intern org-lang)
  1322. org-latex-minted-langs))
  1323. org-lang))
  1324. (options (org-latex--make-option-string
  1325. org-latex-minted-options)))
  1326. (concat (format "\\mint%s{%s}"
  1327. (if (string= options "") "" (format "[%s]" options))
  1328. mint-lang)
  1329. separator code separator)))
  1330. ;; Use listings package.
  1331. (t
  1332. ;; Maybe translate language's name.
  1333. (let* ((org-lang (org-element-property :language inline-src-block))
  1334. (lst-lang (or (cadr (assq (intern org-lang)
  1335. org-latex-listings-langs))
  1336. org-lang))
  1337. (options (org-latex--make-option-string
  1338. (append org-latex-listings-options
  1339. `(("language" ,lst-lang))))))
  1340. (concat (format "\\lstinline[%s]" options)
  1341. separator code separator))))))
  1342. ;;;; Inlinetask
  1343. (defun org-latex-inlinetask (inlinetask contents info)
  1344. "Transcode an INLINETASK element from Org to LaTeX.
  1345. CONTENTS holds the contents of the block. INFO is a plist
  1346. holding contextual information."
  1347. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  1348. (todo (and (plist-get info :with-todo-keywords)
  1349. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1350. (and todo (org-export-data todo info)))))
  1351. (todo-type (org-element-property :todo-type inlinetask))
  1352. (tags (and (plist-get info :with-tags)
  1353. (org-export-get-tags inlinetask info)))
  1354. (priority (and (plist-get info :with-priority)
  1355. (org-element-property :priority inlinetask))))
  1356. ;; If `org-latex-format-inlinetask-function' is provided, call it
  1357. ;; with appropriate arguments.
  1358. (if (functionp org-latex-format-inlinetask-function)
  1359. (funcall org-latex-format-inlinetask-function
  1360. todo todo-type priority title tags contents)
  1361. ;; Otherwise, use a default template.
  1362. (org-latex--wrap-label
  1363. inlinetask
  1364. (let ((full-title
  1365. (concat
  1366. (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1367. (when priority (format "\\framebox{\\#%c} " priority))
  1368. title
  1369. (when tags (format "\\hfill{}\\textsc{:%s:}"
  1370. (mapconcat 'identity tags ":"))))))
  1371. (format (concat "\\begin{center}\n"
  1372. "\\fbox{\n"
  1373. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1374. "%s\n\n"
  1375. "\\rule[.8em]{\\textwidth}{2pt}\n\n"
  1376. "%s"
  1377. "\\end{minipage}\n"
  1378. "}\n"
  1379. "\\end{center}")
  1380. full-title contents))))))
  1381. ;;;; Italic
  1382. (defun org-latex-italic (italic contents info)
  1383. "Transcode ITALIC from Org to LaTeX.
  1384. CONTENTS is the text with italic markup. INFO is a plist holding
  1385. contextual information."
  1386. (org-latex--text-markup contents 'italic))
  1387. ;;;; Item
  1388. (defun org-latex-item (item contents info)
  1389. "Transcode an ITEM element from Org to LaTeX.
  1390. CONTENTS holds the contents of the item. INFO is a plist holding
  1391. contextual information."
  1392. (let* ((counter
  1393. (let ((count (org-element-property :counter item))
  1394. (level
  1395. (loop for parent in (org-export-get-genealogy item)
  1396. count (eq (org-element-type parent) 'plain-list)
  1397. until (eq (org-element-type parent) 'headline))))
  1398. (and count
  1399. (< level 5)
  1400. (format "\\setcounter{enum%s}{%s}\n"
  1401. (nth (1- level) '("i" "ii" "iii" "iv"))
  1402. (1- count)))))
  1403. (checkbox (case (org-element-property :checkbox item)
  1404. (on "$\\boxtimes$ ")
  1405. (off "$\\Box$ ")
  1406. (trans "$\\boxminus$ ")))
  1407. (tag (let ((tag (org-element-property :tag item)))
  1408. ;; Check-boxes must belong to the tag.
  1409. (and tag (format "[%s] "
  1410. (concat checkbox
  1411. (org-export-data tag info)))))))
  1412. (concat counter "\\item" (or tag (concat " " checkbox))
  1413. (and contents (org-trim contents))
  1414. ;; If there are footnotes references in tag, be sure to
  1415. ;; add their definition at the end of the item. This
  1416. ;; workaround is necessary since "\footnote{}" command is
  1417. ;; not supported in tags.
  1418. (and tag
  1419. (org-latex--delayed-footnotes-definitions
  1420. (org-element-property :tag item) info)))))
  1421. ;;;; Keyword
  1422. (defun org-latex-keyword (keyword contents info)
  1423. "Transcode a KEYWORD element from Org to LaTeX.
  1424. CONTENTS is nil. INFO is a plist holding contextual information."
  1425. (let ((key (org-element-property :key keyword))
  1426. (value (org-element-property :value keyword)))
  1427. (cond
  1428. ((string= key "LATEX") value)
  1429. ((string= key "INDEX") (format "\\index{%s}" value))
  1430. ;; Invisible targets.
  1431. ((string= key "TARGET") nil)
  1432. ((string= key "TOC")
  1433. (let ((value (downcase value)))
  1434. (cond
  1435. ((string-match "\\<headlines\\>" value)
  1436. (let ((depth (or (and (string-match "[0-9]+" value)
  1437. (string-to-number (match-string 0 value)))
  1438. (plist-get info :with-toc))))
  1439. (concat
  1440. (when (wholenump depth)
  1441. (format "\\setcounter{tocdepth}{%s}\n" depth))
  1442. "\\tableofcontents")))
  1443. ((string= "tables" value) "\\listoftables")
  1444. ((string= "listings" value)
  1445. (cond
  1446. ((eq org-latex-listings 'minted) "\\listoflistings")
  1447. (org-latex-listings "\\lstlistoflistings")
  1448. ;; At the moment, src blocks with a caption are wrapped
  1449. ;; into a figure environment.
  1450. (t "\\listoffigures")))))))))
  1451. ;;;; Latex Environment
  1452. (defun org-latex-latex-environment (latex-environment contents info)
  1453. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1454. CONTENTS is nil. INFO is a plist holding contextual information."
  1455. (when (plist-get info :with-latex)
  1456. (let ((label (org-element-property :name latex-environment))
  1457. (value (org-remove-indentation
  1458. (org-element-property :value latex-environment))))
  1459. (if (not (org-string-nw-p label)) value
  1460. ;; Environment is labelled: label must be within the environment
  1461. ;; (otherwise, a reference pointing to that element will count
  1462. ;; the section instead).
  1463. (with-temp-buffer
  1464. (insert value)
  1465. (goto-char (point-min))
  1466. (forward-line)
  1467. (insert
  1468. (format "\\label{%s}\n" (org-export-solidify-link-text label)))
  1469. (buffer-string))))))
  1470. ;;;; Latex Fragment
  1471. (defun org-latex-latex-fragment (latex-fragment contents info)
  1472. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1473. CONTENTS is nil. INFO is a plist holding contextual information."
  1474. (when (plist-get info :with-latex)
  1475. (org-element-property :value latex-fragment)))
  1476. ;;;; Line Break
  1477. (defun org-latex-line-break (line-break contents info)
  1478. "Transcode a LINE-BREAK object from Org to LaTeX.
  1479. CONTENTS is nil. INFO is a plist holding contextual information."
  1480. "\\\\\n")
  1481. ;;;; Link
  1482. (defun org-latex--inline-image (link info)
  1483. "Return LaTeX code for an inline image.
  1484. LINK is the link pointing to the inline image. INFO is a plist
  1485. used as a communication channel."
  1486. (let* ((parent (org-export-get-parent-element link))
  1487. (path (let ((raw-path (org-element-property :path link)))
  1488. (if (not (file-name-absolute-p raw-path)) raw-path
  1489. (expand-file-name raw-path))))
  1490. (caption (org-latex--caption/label-string parent info))
  1491. ;; Retrieve latex attributes from the element around.
  1492. (attr (org-export-read-attribute :attr_latex parent))
  1493. (float (let ((float (plist-get attr :float)))
  1494. (cond ((string= float "wrap") 'wrap)
  1495. ((string= float "multicolumn") 'multicolumn)
  1496. ((or (string= float "figure")
  1497. (org-element-property :caption parent))
  1498. 'figure))))
  1499. (placement
  1500. (let ((place (plist-get attr :placement)))
  1501. (cond (place (format "%s" place))
  1502. ((eq float 'wrap) "{l}{0.5\\textwidth}")
  1503. ((eq float 'figure)
  1504. (format "[%s]" org-latex-default-figure-position))
  1505. (t ""))))
  1506. (comment-include (if (plist-get attr :comment-include) "%" ""))
  1507. ;; Options for "includegraphics" macro. Make sure it is
  1508. ;; a string with square brackets when non empty. Default to
  1509. ;; `org-latex-image-default-option' when possible.
  1510. (options (let ((opt (format "%s"
  1511. (or (plist-get attr :options)
  1512. org-latex-image-default-option))))
  1513. (cond ((string-match "\\`\\[.*\\]" opt) opt)
  1514. ((org-string-nw-p opt) (format "[%s]" opt))
  1515. ((eq float 'float) "[width=0.7\\textwidth]")
  1516. ((eq float 'wrap) "[width=0.48\\textwidth]")
  1517. (t "")))))
  1518. ;; Return proper string, depending on FLOAT.
  1519. (case float
  1520. (wrap (format "\\begin{wrapfigure}%s
  1521. \\centering
  1522. %s\\includegraphics%s{%s}
  1523. %s\\end{wrapfigure}" placement comment-include options path caption))
  1524. (multicolumn (format "\\begin{figure*}%s
  1525. \\centering
  1526. %s\\includegraphics%s{%s}
  1527. %s\\end{figure*}" placement comment-include options path caption))
  1528. (figure (format "\\begin{figure}%s
  1529. \\centering
  1530. %s\\includegraphics%s{%s}
  1531. %s\\end{figure}" placement comment-include options path caption))
  1532. (t (format "\\includegraphics%s{%s}" options path)))))
  1533. (defun org-latex-link (link desc info)
  1534. "Transcode a LINK object from Org to LaTeX.
  1535. DESC is the description part of the link, or the empty string.
  1536. INFO is a plist holding contextual information. See
  1537. `org-export-data'."
  1538. (let* ((type (org-element-property :type link))
  1539. (raw-path (org-element-property :path link))
  1540. ;; Ensure DESC really exists, or set it to nil.
  1541. (desc (and (not (string= desc "")) desc))
  1542. (imagep (org-export-inline-image-p
  1543. link org-latex-inline-image-rules))
  1544. (path (cond
  1545. ((member type '("http" "https" "ftp" "mailto"))
  1546. (concat type ":" raw-path))
  1547. ((string= type "file")
  1548. (if (file-name-absolute-p raw-path)
  1549. (concat "file://" (expand-file-name raw-path))
  1550. (concat "file://" raw-path)))
  1551. (t raw-path)))
  1552. protocol)
  1553. (cond
  1554. ;; Image file.
  1555. (imagep (org-latex--inline-image link info))
  1556. ;; Radio link: Transcode target's contents and use them as link's
  1557. ;; description.
  1558. ((string= type "radio")
  1559. (let ((destination (org-export-resolve-radio-link link info)))
  1560. (when destination
  1561. (format "\\hyperref[%s]{%s}"
  1562. (org-export-solidify-link-text path)
  1563. (org-export-data (org-element-contents destination) info)))))
  1564. ;; Links pointing to a headline: Find destination and build
  1565. ;; appropriate referencing command.
  1566. ((member type '("custom-id" "fuzzy" "id"))
  1567. (let ((destination (if (string= type "fuzzy")
  1568. (org-export-resolve-fuzzy-link link info)
  1569. (org-export-resolve-id-link link info))))
  1570. (case (org-element-type destination)
  1571. ;; Id link points to an external file.
  1572. (plain-text
  1573. (if desc (format "\\href{file://%s}{%s}" destination desc)
  1574. (format "\\url{file://%s}" destination)))
  1575. ;; Fuzzy link points nowhere.
  1576. ('nil
  1577. (format org-latex-link-with-unknown-path-format
  1578. (or desc
  1579. (org-export-data
  1580. (org-element-property :raw-link link) info))))
  1581. ;; Fuzzy link points to an invisible target.
  1582. (keyword nil)
  1583. ;; LINK points to a headline. If headlines are numbered
  1584. ;; and the link has no description, display headline's
  1585. ;; number. Otherwise, display description or headline's
  1586. ;; title.
  1587. (headline
  1588. (let ((label
  1589. (format "sec-%s"
  1590. (mapconcat
  1591. 'number-to-string
  1592. (org-export-get-headline-number destination info)
  1593. "-"))))
  1594. (if (and (plist-get info :section-numbers) (not desc))
  1595. (format "\\ref{%s}" label)
  1596. (format "\\hyperref[%s]{%s}" label
  1597. (or desc
  1598. (org-export-data
  1599. (org-element-property :title destination) info))))))
  1600. ;; Fuzzy link points to a target. Do as above.
  1601. (otherwise
  1602. (let ((path (org-export-solidify-link-text path)))
  1603. (if (not desc) (format "\\ref{%s}" path)
  1604. (format "\\hyperref[%s]{%s}" path desc)))))))
  1605. ;; Coderef: replace link with the reference name or the
  1606. ;; equivalent line number.
  1607. ((string= type "coderef")
  1608. (format (org-export-get-coderef-format path desc)
  1609. (org-export-resolve-coderef path info)))
  1610. ;; Link type is handled by a special function.
  1611. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  1612. (funcall protocol (org-link-unescape path) desc 'latex))
  1613. ;; External link with a description part.
  1614. ((and path desc) (format "\\href{%s}{%s}" path desc))
  1615. ;; External link without a description part.
  1616. (path (format "\\url{%s}" path))
  1617. ;; No path, only description. Try to do something useful.
  1618. (t (format org-latex-link-with-unknown-path-format desc)))))
  1619. ;;;; Paragraph
  1620. (defun org-latex-paragraph (paragraph contents info)
  1621. "Transcode a PARAGRAPH element from Org to LaTeX.
  1622. CONTENTS is the contents of the paragraph, as a string. INFO is
  1623. the plist used as a communication channel."
  1624. contents)
  1625. ;;;; Plain List
  1626. (defun org-latex-plain-list (plain-list contents info)
  1627. "Transcode a PLAIN-LIST element from Org to LaTeX.
  1628. CONTENTS is the contents of the list. INFO is a plist holding
  1629. contextual information."
  1630. (let* ((type (org-element-property :type plain-list))
  1631. (attr (org-export-read-attribute :attr_latex plain-list))
  1632. (latex-type (let ((env (plist-get attr :environment)))
  1633. (cond (env (format "%s" env))
  1634. ((eq type 'ordered) "enumerate")
  1635. ((eq type 'unordered) "itemize")
  1636. ((eq type 'descriptive) "description")))))
  1637. (org-latex--wrap-label
  1638. plain-list
  1639. (format "\\begin{%s}%s\n%s\\end{%s}"
  1640. latex-type
  1641. ;; Put optional arguments, if any inside square brackets
  1642. ;; when necessary.
  1643. (let ((options (format "%s" (or (plist-get attr :options) ""))))
  1644. (cond ((equal options "") "")
  1645. ((string-match "\\`\\[.*\\]\\'" options) options)
  1646. (t (format "[%s]" options))))
  1647. contents
  1648. latex-type))))
  1649. ;;;; Plain Text
  1650. (defun org-latex-plain-text (text info)
  1651. "Transcode a TEXT string from Org to LaTeX.
  1652. TEXT is the string to transcode. INFO is a plist holding
  1653. contextual information."
  1654. (let ((specialp (plist-get info :with-special-strings))
  1655. (output text))
  1656. ;; Protect %, #, &, $, ~, ^, _, { and }.
  1657. (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" output)
  1658. (setq output
  1659. (replace-match
  1660. (format "\\%s" (match-string 2 output)) nil t output 2)))
  1661. ;; Protect \. If special strings are used, be careful not to
  1662. ;; protect "\" in "\-" constructs.
  1663. (let ((symbols (if specialp "-%$#&{}~^_\\" "%$#&{}~^_\\")))
  1664. (setq output
  1665. (replace-regexp-in-string
  1666. (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
  1667. "$\\backslash$" output nil t 1)))
  1668. ;; Activate smart quotes. Be sure to provide original TEXT string
  1669. ;; since OUTPUT may have been modified.
  1670. (when (plist-get info :with-smart-quotes)
  1671. (setq output (org-export-activate-smart-quotes output :latex info text)))
  1672. ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
  1673. (let ((case-fold-search nil)
  1674. (start 0))
  1675. (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
  1676. (setq output (replace-match
  1677. (format "\\%s{}" (match-string 1 output)) nil t output)
  1678. start (match-end 0))))
  1679. ;; Convert special strings.
  1680. (when specialp
  1681. (setq output
  1682. (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
  1683. ;; Handle break preservation if required.
  1684. (when (plist-get info :preserve-breaks)
  1685. (setq output (replace-regexp-in-string
  1686. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
  1687. ;; Return value.
  1688. output))
  1689. ;;;; Planning
  1690. (defun org-latex-planning (planning contents info)
  1691. "Transcode a PLANNING element from Org to LaTeX.
  1692. CONTENTS is nil. INFO is a plist holding contextual
  1693. information."
  1694. (concat
  1695. "\\noindent"
  1696. (mapconcat
  1697. 'identity
  1698. (delq nil
  1699. (list
  1700. (let ((closed (org-element-property :closed planning)))
  1701. (when closed
  1702. (concat
  1703. (format "\\textbf{%s} " org-closed-string)
  1704. (format org-latex-inactive-timestamp-format
  1705. (org-translate-time
  1706. (org-element-property :raw-value closed))))))
  1707. (let ((deadline (org-element-property :deadline planning)))
  1708. (when deadline
  1709. (concat
  1710. (format "\\textbf{%s} " org-deadline-string)
  1711. (format org-latex-active-timestamp-format
  1712. (org-translate-time
  1713. (org-element-property :raw-value deadline))))))
  1714. (let ((scheduled (org-element-property :scheduled planning)))
  1715. (when scheduled
  1716. (concat
  1717. (format "\\textbf{%s} " org-scheduled-string)
  1718. (format org-latex-active-timestamp-format
  1719. (org-translate-time
  1720. (org-element-property :raw-value scheduled))))))))
  1721. " ")
  1722. "\\\\"))
  1723. ;;;; Quote Block
  1724. (defun org-latex-quote-block (quote-block contents info)
  1725. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  1726. CONTENTS holds the contents of the block. INFO is a plist
  1727. holding contextual information."
  1728. (org-latex--wrap-label
  1729. quote-block
  1730. (format "\\begin{quote}\n%s\\end{quote}" contents)))
  1731. ;;;; Quote Section
  1732. (defun org-latex-quote-section (quote-section contents info)
  1733. "Transcode a QUOTE-SECTION element from Org to LaTeX.
  1734. CONTENTS is nil. INFO is a plist holding contextual information."
  1735. (let ((value (org-remove-indentation
  1736. (org-element-property :value quote-section))))
  1737. (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  1738. ;;;; Radio Target
  1739. (defun org-latex-radio-target (radio-target text info)
  1740. "Transcode a RADIO-TARGET object from Org to LaTeX.
  1741. TEXT is the text of the target. INFO is a plist holding
  1742. contextual information."
  1743. (format "\\label{%s}%s"
  1744. (org-export-solidify-link-text
  1745. (org-element-property :value radio-target))
  1746. text))
  1747. ;;;; Section
  1748. (defun org-latex-section (section contents info)
  1749. "Transcode a SECTION element from Org to LaTeX.
  1750. CONTENTS holds the contents of the section. INFO is a plist
  1751. holding contextual information."
  1752. contents)
  1753. ;;;; Special Block
  1754. (defun org-latex-special-block (special-block contents info)
  1755. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  1756. CONTENTS holds the contents of the block. INFO is a plist
  1757. holding contextual information."
  1758. (let ((type (downcase (org-element-property :type special-block)))
  1759. (opt (org-export-read-attribute :attr_latex special-block :options)))
  1760. (concat (format "\\begin{%s}%s\n" type (or opt ""))
  1761. ;; Insert any label or caption within the block
  1762. ;; (otherwise, a reference pointing to that element will
  1763. ;; count the section instead).
  1764. (org-latex--caption/label-string special-block info)
  1765. contents
  1766. (format "\\end{%s}" type))))
  1767. ;;;; Src Block
  1768. (defun org-latex-src-block (src-block contents info)
  1769. "Transcode a SRC-BLOCK element from Org to LaTeX.
  1770. CONTENTS holds the contents of the item. INFO is a plist holding
  1771. contextual information."
  1772. (when (org-string-nw-p (org-element-property :value src-block))
  1773. (let* ((lang (org-element-property :language src-block))
  1774. (caption (org-element-property :caption src-block))
  1775. (label (org-element-property :name src-block))
  1776. (custom-env (and lang
  1777. (cadr (assq (intern lang)
  1778. org-latex-custom-lang-environments))))
  1779. (num-start (case (org-element-property :number-lines src-block)
  1780. (continued (org-export-get-loc src-block info))
  1781. (new 0)))
  1782. (retain-labels (org-element-property :retain-labels src-block)))
  1783. (cond
  1784. ;; Case 1. No source fontification.
  1785. ((not org-latex-listings)
  1786. (let ((caption-str (org-latex--caption/label-string src-block info))
  1787. (float-env (and caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
  1788. (format
  1789. (or float-env "%s")
  1790. (concat caption-str
  1791. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1792. (org-export-format-code-default src-block info))))))
  1793. ;; Case 2. Custom environment.
  1794. (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
  1795. custom-env
  1796. (org-export-format-code-default src-block info)
  1797. custom-env))
  1798. ;; Case 3. Use minted package.
  1799. ((eq org-latex-listings 'minted)
  1800. (let ((float-env
  1801. (when (or label caption)
  1802. (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
  1803. (org-latex--caption/label-string src-block info))))
  1804. (body
  1805. (format
  1806. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  1807. ;; Options.
  1808. (org-latex--make-option-string
  1809. (if (not num-start) org-latex-minted-options
  1810. (append `(("linenos")
  1811. ("firstnumber" ,(number-to-string (1+ num-start))))
  1812. org-latex-minted-options)))
  1813. ;; Language.
  1814. (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
  1815. ;; Source code.
  1816. (let* ((code-info (org-export-unravel-code src-block))
  1817. (max-width
  1818. (apply 'max
  1819. (mapcar 'length
  1820. (org-split-string (car code-info)
  1821. "\n")))))
  1822. (org-export-format-code
  1823. (car code-info)
  1824. (lambda (loc num ref)
  1825. (concat
  1826. loc
  1827. (when ref
  1828. ;; Ensure references are flushed to the right,
  1829. ;; separated with 6 spaces from the widest line
  1830. ;; of code.
  1831. (concat (make-string (+ (- max-width (length loc)) 6)
  1832. ?\s)
  1833. (format "(%s)" ref)))))
  1834. nil (and retain-labels (cdr code-info)))))))
  1835. ;; Return value.
  1836. (if float-env (format float-env body) body)))
  1837. ;; Case 4. Use listings package.
  1838. (t
  1839. (let ((lst-lang
  1840. (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
  1841. (caption-str
  1842. (when caption
  1843. (let ((main (org-export-get-caption src-block))
  1844. (secondary (org-export-get-caption src-block t)))
  1845. (if (not secondary)
  1846. (format "{%s}" (org-export-data main info))
  1847. (format "{[%s]%s}"
  1848. (org-export-data secondary info)
  1849. (org-export-data main info)))))))
  1850. (concat
  1851. ;; Options.
  1852. (format "\\lstset{%s}\n"
  1853. (org-latex--make-option-string
  1854. (append org-latex-listings-options
  1855. `(("language" ,lst-lang))
  1856. (when label `(("label" ,label)))
  1857. (when caption-str `(("caption" ,caption-str)))
  1858. (cond ((not num-start) '(("numbers" "none")))
  1859. ((zerop num-start) '(("numbers" "left")))
  1860. (t `(("numbers" "left")
  1861. ("firstnumber"
  1862. ,(number-to-string (1+ num-start)))))))))
  1863. ;; Source code.
  1864. (format
  1865. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  1866. (let* ((code-info (org-export-unravel-code src-block))
  1867. (max-width
  1868. (apply 'max
  1869. (mapcar 'length
  1870. (org-split-string (car code-info) "\n")))))
  1871. (org-export-format-code
  1872. (car code-info)
  1873. (lambda (loc num ref)
  1874. (concat
  1875. loc
  1876. (when ref
  1877. ;; Ensure references are flushed to the right,
  1878. ;; separated with 6 spaces from the widest line of
  1879. ;; code
  1880. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1881. (format "(%s)" ref)))))
  1882. nil (and retain-labels (cdr code-info))))))))))))
  1883. ;;;; Statistics Cookie
  1884. (defun org-latex-statistics-cookie (statistics-cookie contents info)
  1885. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  1886. CONTENTS is nil. INFO is a plist holding contextual information."
  1887. (replace-regexp-in-string
  1888. "%" "\\%" (org-element-property :value statistics-cookie) nil t))
  1889. ;;;; Strike-Through
  1890. (defun org-latex-strike-through (strike-through contents info)
  1891. "Transcode STRIKE-THROUGH from Org to LaTeX.
  1892. CONTENTS is the text with strike-through markup. INFO is a plist
  1893. holding contextual information."
  1894. (org-latex--text-markup contents 'strike-through))
  1895. ;;;; Subscript
  1896. (defun org-latex-subscript (subscript contents info)
  1897. "Transcode a SUBSCRIPT object from Org to LaTeX.
  1898. CONTENTS is the contents of the object. INFO is a plist holding
  1899. contextual information."
  1900. (if (= (length contents) 1) (format "$_%s$" contents)
  1901. ;; Handle multiple objects in SUBSCRIPT by creating a subscript
  1902. ;; command for each of them.
  1903. (let ((prev-blanks 0))
  1904. (mapconcat
  1905. (lambda (obj)
  1906. (case (org-element-type obj)
  1907. ((entity latex-fragment)
  1908. (setq prev-blanks (org-element-property :post-blank obj))
  1909. (let ((data (org-trim (org-export-data obj info))))
  1910. (string-match
  1911. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  1912. data)
  1913. (format "$_{%s}$" (match-string 1 data))))
  1914. (plain-text
  1915. (format "$_\\mathrm{%s}$"
  1916. (concat (make-string prev-blanks ? )
  1917. ;; mathrm command doesn't handle spaces,
  1918. ;; so we have to enforce them.
  1919. (replace-regexp-in-string
  1920. " " "\\\\ " (org-export-data obj info)))))
  1921. (otherwise
  1922. (setq prev-blanks (org-element-property :post-blank obj))
  1923. (format "$_{%s}$" (org-export-data obj info)))))
  1924. (org-element-contents subscript) ""))))
  1925. ;;;; Superscript
  1926. (defun org-latex-superscript (superscript contents info)
  1927. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  1928. CONTENTS is the contents of the object. INFO is a plist holding
  1929. contextual information."
  1930. (if (= (length contents) 1) (format "$^%s$" contents)
  1931. ;; Handle multiple objects in SUPERSCRIPT by creating
  1932. ;; a superscript command for each of them.
  1933. (let ((prev-blanks 0))
  1934. (mapconcat
  1935. (lambda (obj)
  1936. (case (org-element-type obj)
  1937. ((entity latex-fragment)
  1938. (setq prev-blanks (org-element-property :post-blank obj))
  1939. (let ((data (org-trim (org-export-data obj info))))
  1940. (string-match
  1941. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  1942. data)
  1943. (format "$^{%s}$" (match-string 1 data))))
  1944. (plain-text
  1945. (format "$^\\mathrm{%s}$"
  1946. (concat (make-string prev-blanks ? )
  1947. ;; mathrm command doesn't handle spaces,
  1948. ;; so we have to enforce them.
  1949. (replace-regexp-in-string
  1950. " " "\\\\ " (org-export-data obj info)))))
  1951. (otherwise
  1952. (setq prev-blanks (org-element-property :post-blank obj))
  1953. (format "$^{%s}$" (org-export-data obj info)))))
  1954. (org-element-contents superscript) ""))))
  1955. ;;;; Table
  1956. ;;
  1957. ;; `org-latex-table' is the entry point for table transcoding. It
  1958. ;; takes care of tables with a "verbatim" mode. Otherwise, it
  1959. ;; delegates the job to either `org-latex--table.el-table',
  1960. ;; `org-latex--org-table' or `org-latex--math-table' functions,
  1961. ;; depending of the type of the table and the mode requested.
  1962. ;;
  1963. ;; `org-latex--align-string' is a subroutine used to build alignment
  1964. ;; string for Org tables.
  1965. (defun org-latex-table (table contents info)
  1966. "Transcode a TABLE element from Org to LaTeX.
  1967. CONTENTS is the contents of the table. INFO is a plist holding
  1968. contextual information."
  1969. (if (eq (org-element-property :type table) 'table.el)
  1970. ;; "table.el" table. Convert it using appropriate tools.
  1971. (org-latex--table.el-table table info)
  1972. (let ((type (or (org-export-read-attribute :attr_latex table :mode)
  1973. org-latex-default-table-mode)))
  1974. (cond
  1975. ;; Case 1: Verbatim table.
  1976. ((string= type "verbatim")
  1977. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  1978. ;; Re-create table, without affiliated keywords.
  1979. (org-trim (org-element-interpret-data
  1980. `(table nil ,@(org-element-contents table))))))
  1981. ;; Case 2: Matrix.
  1982. ((or (string= type "math") (string= type "inline-math"))
  1983. (org-latex--math-table table info))
  1984. ;; Case 3: Standard table.
  1985. (t (concat (org-latex--org-table table contents info)
  1986. ;; When there are footnote references within the
  1987. ;; table, insert their definition just after it.
  1988. (org-latex--delayed-footnotes-definitions table info)))))))
  1989. (defun org-latex--align-string (table info)
  1990. "Return an appropriate LaTeX alignment string.
  1991. TABLE is the considered table. INFO is a plist used as
  1992. a communication channel."
  1993. (or (org-export-read-attribute :attr_latex table :align)
  1994. (let (align)
  1995. ;; Extract column groups and alignment from first (non-rule)
  1996. ;; row.
  1997. (org-element-map
  1998. (org-element-map table 'table-row
  1999. (lambda (row)
  2000. (and (eq (org-element-property :type row) 'standard) row))
  2001. info 'first-match)
  2002. 'table-cell
  2003. (lambda (cell)
  2004. (let ((borders (org-export-table-cell-borders cell info)))
  2005. ;; Check left border for the first cell only.
  2006. (when (and (memq 'left borders) (not align))
  2007. (push "|" align))
  2008. (push (case (org-export-table-cell-alignment cell info)
  2009. (left "l")
  2010. (right "r")
  2011. (center "c"))
  2012. align)
  2013. (when (memq 'right borders) (push "|" align))))
  2014. info)
  2015. (apply 'concat (nreverse align)))))
  2016. (defun org-latex--org-table (table contents info)
  2017. "Return appropriate LaTeX code for an Org table.
  2018. TABLE is the table type element to transcode. CONTENTS is its
  2019. contents, as a string. INFO is a plist used as a communication
  2020. channel.
  2021. This function assumes TABLE has `org' as its `:type' property and
  2022. `table' as its `:mode' attribute."
  2023. (let* ((caption (org-latex--caption/label-string table info))
  2024. (attr (org-export-read-attribute :attr_latex table))
  2025. ;; Determine alignment string.
  2026. (alignment (org-latex--align-string table info))
  2027. ;; Determine environment for the table: longtable, tabular...
  2028. (table-env (let ((env (plist-get attr :environment)))
  2029. (if env (format "%s" env)
  2030. org-latex-default-table-environment)))
  2031. ;; If table is a float, determine environment: table, table*
  2032. ;; or sidewaystable.
  2033. (float-env (unless (equal "longtable" table-env)
  2034. (let ((float (plist-get attr :float)))
  2035. (cond
  2036. ((string= float "sidewaystable") "sidewaystable")
  2037. ((string= float "multicolumn") "table*")
  2038. ((or (string= float "table")
  2039. (org-element-property :caption table))
  2040. "table")))))
  2041. ;; Extract others display options.
  2042. (fontsize (let ((font (plist-get attr :font)))
  2043. (and font (concat (org-trim (format "%s" font)) "\n"))))
  2044. (width (plist-get attr :width))
  2045. (placement (or (plist-get attr :placement)
  2046. (format "[%s]" org-latex-default-figure-position)))
  2047. (centerp (if (plist-member attr :center) (plist-get attr :center)
  2048. org-latex-tables-centered)))
  2049. ;; Prepare the final format string for the table.
  2050. (cond
  2051. ;; Longtable.
  2052. ((equal "longtable" table-env)
  2053. (concat (and fontsize (concat "{" fontsize))
  2054. (format "\\begin{longtable}{%s}\n" alignment)
  2055. (and org-latex-table-caption-above
  2056. (org-string-nw-p caption)
  2057. (concat caption "\\\\\n"))
  2058. contents
  2059. (and (not org-latex-table-caption-above)
  2060. (org-string-nw-p caption)
  2061. (concat caption "\\\\\n"))
  2062. "\\end{longtable}\n"
  2063. (and fontsize "}")))
  2064. ;; Others.
  2065. (t (concat (cond
  2066. (float-env
  2067. (concat (format "\\begin{%s}%s\n" float-env placement)
  2068. (if org-latex-table-caption-above caption "")
  2069. (when centerp "\\centering\n")
  2070. fontsize))
  2071. (centerp (concat "\\begin{center}\n" fontsize))
  2072. (fontsize (concat "{" fontsize)))
  2073. (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  2074. table-env
  2075. (if width (format "{%s}" width) "")
  2076. alignment
  2077. contents
  2078. table-env)
  2079. (cond
  2080. (float-env
  2081. (concat (if org-latex-table-caption-above "" caption)
  2082. (format "\n\\end{%s}" float-env)))
  2083. (centerp "\n\\end{center}")
  2084. (fontsize "}")))))))
  2085. (defun org-latex--table.el-table (table info)
  2086. "Return appropriate LaTeX code for a table.el table.
  2087. TABLE is the table type element to transcode. INFO is a plist
  2088. used as a communication channel.
  2089. This function assumes TABLE has `table.el' as its `:type'
  2090. property."
  2091. (require 'table)
  2092. ;; Ensure "*org-export-table*" buffer is empty.
  2093. (with-current-buffer (get-buffer-create "*org-export-table*")
  2094. (erase-buffer))
  2095. (let ((output (with-temp-buffer
  2096. (insert (org-element-property :value table))
  2097. (goto-char 1)
  2098. (re-search-forward "^[ \t]*|[^|]" nil t)
  2099. (table-generate-source 'latex "*org-export-table*")
  2100. (with-current-buffer "*org-export-table*"
  2101. (org-trim (buffer-string))))))
  2102. (kill-buffer (get-buffer "*org-export-table*"))
  2103. ;; Remove left out comments.
  2104. (while (string-match "^%.*\n" output)
  2105. (setq output (replace-match "" t t output)))
  2106. (let ((attr (org-export-read-attribute :attr_latex table)))
  2107. (when (plist-get attr :rmlines)
  2108. ;; When the "rmlines" attribute is provided, remove all hlines
  2109. ;; but the the one separating heading from the table body.
  2110. (let ((n 0) (pos 0))
  2111. (while (and (< (length output) pos)
  2112. (setq pos (string-match "^\\\\hline\n?" output pos)))
  2113. (incf n)
  2114. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  2115. (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
  2116. org-latex-tables-centered)))
  2117. (if (not centerp) output
  2118. (format "\\begin{center}\n%s\n\\end{center}" output))))))
  2119. (defun org-latex--math-table (table info)
  2120. "Return appropriate LaTeX code for a matrix.
  2121. TABLE is the table type element to transcode. INFO is a plist
  2122. used as a communication channel.
  2123. This function assumes TABLE has `org' as its `:type' property and
  2124. `inline-math' or `math' as its `:mode' attribute.."
  2125. (let* ((caption (org-latex--caption/label-string table info))
  2126. (attr (org-export-read-attribute :attr_latex table))
  2127. (inlinep (eq (plist-get attr :mode) 'inline-math))
  2128. (env (let ((env (plist-get attr :environment)))
  2129. (if env (format "%s" env)
  2130. org-latex-default-table-environment)))
  2131. (contents
  2132. (mapconcat
  2133. (lambda (row)
  2134. ;; Ignore horizontal rules.
  2135. (when (eq (org-element-property :type row) 'standard)
  2136. ;; Return each cell unmodified.
  2137. (concat
  2138. (mapconcat
  2139. (lambda (cell)
  2140. (substring (org-element-interpret-data cell) 0 -1))
  2141. (org-element-map row 'table-cell 'identity info) "&")
  2142. (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
  2143. "\n")))
  2144. (org-element-map table 'table-row 'identity info) ""))
  2145. ;; Variables related to math clusters (contiguous math tables
  2146. ;; of the same type).
  2147. (mode (org-export-read-attribute :attr_latex table :mode))
  2148. (prev (org-export-get-previous-element table info))
  2149. (next (org-export-get-next-element table info))
  2150. (same-mode-p
  2151. (lambda (table)
  2152. ;; Non-nil when TABLE has the same mode as current table.
  2153. (string= (or (org-export-read-attribute :attr_latex table :mode)
  2154. org-latex-default-table-mode)
  2155. mode))))
  2156. (concat
  2157. ;; Opening string. If TABLE is in the middle of a table cluster,
  2158. ;; do not insert any.
  2159. (cond ((and prev
  2160. (eq (org-element-type prev) 'table)
  2161. (memq (org-element-property :post-blank prev) '(0 nil))
  2162. (funcall same-mode-p prev))
  2163. nil)
  2164. (inlinep "\\(")
  2165. ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
  2166. (t "\\["))
  2167. ;; Prefix (make sure it is a string).
  2168. (format "%s" (or (plist-get attr :math-prefix) ""))
  2169. ;; Environment. Also treat special cases.
  2170. (cond ((equal env "array")
  2171. (let ((align (org-latex--align-string table info)))
  2172. (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
  2173. ((assoc env org-latex-table-matrix-macros)
  2174. (format "\\%s%s{\n%s}" env
  2175. (format "%s" (or (plist-get attr :math-arguments) ""))
  2176. contents))
  2177. (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
  2178. ;; Suffix (make sure it is a string).
  2179. (format "%s" (or (plist-get attr :math-suffix) ""))
  2180. ;; Closing string. If TABLE is in the middle of a table cluster,
  2181. ;; do not insert any. If it closes such a cluster, be sure to
  2182. ;; close the cluster with a string matching the opening string.
  2183. (cond ((and next
  2184. (eq (org-element-type next) 'table)
  2185. (memq (org-element-property :post-blank table) '(0 nil))
  2186. (funcall same-mode-p next))
  2187. nil)
  2188. (inlinep "\\)")
  2189. ;; Find cluster beginning to know which environment to use.
  2190. ((let ((cluster-beg table) prev)
  2191. (while (and (setq prev (org-export-get-previous-element
  2192. cluster-beg info))
  2193. (memq (org-element-property :post-blank prev)
  2194. '(0 nil))
  2195. (funcall same-mode-p prev))
  2196. (setq cluster-beg prev))
  2197. (and (or (org-element-property :caption cluster-beg)
  2198. (org-element-property :name cluster-beg))
  2199. "\n\\end{equation}")))
  2200. (t "\\]")))))
  2201. ;;;; Table Cell
  2202. (defun org-latex-table-cell (table-cell contents info)
  2203. "Transcode a TABLE-CELL element from Org to LaTeX.
  2204. CONTENTS is the cell contents. INFO is a plist used as
  2205. a communication channel."
  2206. (concat (if (and contents
  2207. org-latex-table-scientific-notation
  2208. (string-match orgtbl-exp-regexp contents))
  2209. ;; Use appropriate format string for scientific
  2210. ;; notation.
  2211. (format org-latex-table-scientific-notation
  2212. (match-string 1 contents)
  2213. (match-string 2 contents))
  2214. contents)
  2215. (when (org-export-get-next-element table-cell info) " & ")))
  2216. ;;;; Table Row
  2217. (defun org-latex-table-row (table-row contents info)
  2218. "Transcode a TABLE-ROW element from Org to LaTeX.
  2219. CONTENTS is the contents of the row. INFO is a plist used as
  2220. a communication channel."
  2221. ;; Rules are ignored since table separators are deduced from
  2222. ;; borders of the current row.
  2223. (when (eq (org-element-property :type table-row) 'standard)
  2224. (let* ((attr (org-export-read-attribute :attr_latex
  2225. (org-export-get-parent table-row)))
  2226. (longtablep (string= (or (plist-get attr :environment)
  2227. org-latex-default-table-environment)
  2228. "longtable"))
  2229. (booktabsp (if (plist-member attr :booktabs)
  2230. (plist-get attr :booktabs)
  2231. org-latex-tables-booktabs))
  2232. ;; TABLE-ROW's borders are extracted from its first cell.
  2233. (borders (org-export-table-cell-borders
  2234. (car (org-element-contents table-row)) info)))
  2235. (concat
  2236. ;; When BOOKTABS are activated enforce top-rule even when no
  2237. ;; hline was specifically marked.
  2238. (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
  2239. ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
  2240. contents "\\\\\n"
  2241. (cond
  2242. ;; Special case for long tables. Define header and footers.
  2243. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  2244. (format "%s
  2245. \\endhead
  2246. %s\\multicolumn{%d}{r}{Continued on next page} \\\\
  2247. \\endfoot
  2248. \\endlastfoot"
  2249. (if booktabsp "\\midrule" "\\hline")
  2250. (if booktabsp "\\midrule" "\\hline")
  2251. ;; Number of columns.
  2252. (cdr (org-export-table-dimensions
  2253. (org-export-get-parent-table table-row) info))))
  2254. ;; When BOOKTABS are activated enforce bottom rule even when
  2255. ;; no hline was specifically marked.
  2256. ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
  2257. ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
  2258. ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
  2259. ;;;; Target
  2260. (defun org-latex-target (target contents info)
  2261. "Transcode a TARGET object from Org to LaTeX.
  2262. CONTENTS is nil. INFO is a plist holding contextual
  2263. information."
  2264. (format "\\label{%s}"
  2265. (org-export-solidify-link-text (org-element-property :value target))))
  2266. ;;;; Timestamp
  2267. (defun org-latex-timestamp (timestamp contents info)
  2268. "Transcode a TIMESTAMP object from Org to LaTeX.
  2269. CONTENTS is nil. INFO is a plist holding contextual
  2270. information."
  2271. (let ((value (org-latex-plain-text
  2272. (org-timestamp-translate timestamp) info)))
  2273. (case (org-element-property :type timestamp)
  2274. ((active active-range) (format org-latex-active-timestamp-format value))
  2275. ((inactive inactive-range)
  2276. (format org-latex-inactive-timestamp-format value))
  2277. (otherwise (format org-latex-diary-timestamp-format value)))))
  2278. ;;;; Underline
  2279. (defun org-latex-underline (underline contents info)
  2280. "Transcode UNDERLINE from Org to LaTeX.
  2281. CONTENTS is the text with underline markup. INFO is a plist
  2282. holding contextual information."
  2283. (org-latex--text-markup contents 'underline))
  2284. ;;;; Verbatim
  2285. (defun org-latex-verbatim (verbatim contents info)
  2286. "Transcode a VERBATIM object from Org to LaTeX.
  2287. CONTENTS is nil. INFO is a plist used as a communication
  2288. channel."
  2289. (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
  2290. ;;;; Verse Block
  2291. (defun org-latex-verse-block (verse-block contents info)
  2292. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  2293. CONTENTS is verse block contents. INFO is a plist holding
  2294. contextual information."
  2295. (org-latex--wrap-label
  2296. verse-block
  2297. ;; In a verse environment, add a line break to each newline
  2298. ;; character and change each white space at beginning of a line
  2299. ;; into a space of 1 em. Also change each blank line with
  2300. ;; a vertical space of 1 em.
  2301. (progn
  2302. (setq contents (replace-regexp-in-string
  2303. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  2304. (replace-regexp-in-string
  2305. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
  2306. (while (string-match "^[ \t]+" contents)
  2307. (let ((new-str (format "\\hspace*{%dem}"
  2308. (length (match-string 0 contents)))))
  2309. (setq contents (replace-match new-str nil t contents))))
  2310. (format "\\begin{verse}\n%s\\end{verse}" contents))))
  2311. ;;; End-user functions
  2312. ;;;###autoload
  2313. (defun org-latex-export-as-latex
  2314. (&optional async subtreep visible-only body-only ext-plist)
  2315. "Export current buffer as a LaTeX buffer.
  2316. If narrowing is active in the current buffer, only export its
  2317. narrowed part.
  2318. If a region is active, export that region.
  2319. A non-nil optional argument ASYNC means the process should happen
  2320. asynchronously. The resulting buffer should be accessible
  2321. through the `org-export-stack' interface.
  2322. When optional argument SUBTREEP is non-nil, export the sub-tree
  2323. at point, extracting information from the headline properties
  2324. first.
  2325. When optional argument VISIBLE-ONLY is non-nil, don't export
  2326. contents of hidden elements.
  2327. When optional argument BODY-ONLY is non-nil, only write code
  2328. between \"\\begin{document}\" and \"\\end{document}\".
  2329. EXT-PLIST, when provided, is a property list with external
  2330. parameters overriding Org default settings, but still inferior to
  2331. file-local settings.
  2332. Export is done in a buffer named \"*Org LATEX Export*\", which
  2333. will be displayed when `org-export-show-temporary-export-buffer'
  2334. is non-nil."
  2335. (interactive)
  2336. (if async
  2337. (org-export-async-start
  2338. (lambda (output)
  2339. (with-current-buffer (get-buffer-create "*Org LATEX Export*")
  2340. (erase-buffer)
  2341. (insert output)
  2342. (goto-char (point-min))
  2343. (LaTeX-mode)
  2344. (org-export-add-to-stack (current-buffer) 'latex)))
  2345. `(org-export-as 'latex ,subtreep ,visible-only ,body-only
  2346. ',ext-plist))
  2347. (let ((outbuf
  2348. (org-export-to-buffer 'latex "*Org LATEX Export*"
  2349. subtreep visible-only body-only ext-plist)))
  2350. (with-current-buffer outbuf (LaTeX-mode))
  2351. (when org-export-show-temporary-export-buffer
  2352. (switch-to-buffer-other-window outbuf)))))
  2353. ;;;###autoload
  2354. (defun org-latex-export-to-latex
  2355. (&optional async subtreep visible-only body-only ext-plist)
  2356. "Export current buffer to a LaTeX file.
  2357. If narrowing is active in the current buffer, only export its
  2358. narrowed part.
  2359. If a region is active, export that region.
  2360. A non-nil optional argument ASYNC means the process should happen
  2361. asynchronously. The resulting file should be accessible through
  2362. the `org-export-stack' interface.
  2363. When optional argument SUBTREEP is non-nil, export the sub-tree
  2364. at point, extracting information from the headline properties
  2365. first.
  2366. When optional argument VISIBLE-ONLY is non-nil, don't export
  2367. contents of hidden elements.
  2368. When optional argument BODY-ONLY is non-nil, only write code
  2369. between \"\\begin{document}\" and \"\\end{document}\".
  2370. EXT-PLIST, when provided, is a property list with external
  2371. parameters overriding Org default settings, but still inferior to
  2372. file-local settings.
  2373. Return output file's name."
  2374. (interactive)
  2375. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  2376. (if async
  2377. (org-export-async-start
  2378. (lambda (f) (org-export-add-to-stack f 'latex))
  2379. `(expand-file-name
  2380. (org-export-to-file
  2381. 'latex ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))
  2382. (org-export-to-file
  2383. 'latex outfile subtreep visible-only body-only ext-plist))))
  2384. ;;;###autoload
  2385. (defun org-latex-export-to-pdf
  2386. (&optional async subtreep visible-only body-only ext-plist)
  2387. "Export current buffer to LaTeX then process through to PDF.
  2388. If narrowing is active in the current buffer, only export its
  2389. narrowed part.
  2390. If a region is active, export that region.
  2391. A non-nil optional argument ASYNC means the process should happen
  2392. asynchronously. The resulting file should be accessible through
  2393. the `org-export-stack' interface.
  2394. When optional argument SUBTREEP is non-nil, export the sub-tree
  2395. at point, extracting information from the headline properties
  2396. first.
  2397. When optional argument VISIBLE-ONLY is non-nil, don't export
  2398. contents of hidden elements.
  2399. When optional argument BODY-ONLY is non-nil, only write code
  2400. between \"\\begin{document}\" and \"\\end{document}\".
  2401. EXT-PLIST, when provided, is a property list with external
  2402. parameters overriding Org default settings, but still inferior to
  2403. file-local settings.
  2404. Return PDF file's name."
  2405. (interactive)
  2406. (if async
  2407. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  2408. (org-export-async-start
  2409. (lambda (f) (org-export-add-to-stack f 'latex))
  2410. `(expand-file-name
  2411. (org-latex-compile
  2412. (org-export-to-file
  2413. 'latex ,outfile ,subtreep ,visible-only ,body-only
  2414. ',ext-plist)))))
  2415. (org-latex-compile
  2416. (org-latex-export-to-latex
  2417. nil subtreep visible-only body-only ext-plist))))
  2418. (defun org-latex-compile (texfile)
  2419. "Compile a TeX file.
  2420. TEXFILE is the name of the file being compiled. Processing is
  2421. done through the command specified in `org-latex-pdf-process'.
  2422. Return PDF file name or an error if it couldn't be produced."
  2423. (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
  2424. (full-name (file-truename texfile))
  2425. (out-dir (file-name-directory texfile))
  2426. ;; Make sure `default-directory' is set to TEXFILE directory,
  2427. ;; not to whatever value the current buffer may have.
  2428. (default-directory (file-name-directory full-name))
  2429. errors)
  2430. (message (format "Processing LaTeX file %s ..." texfile))
  2431. (save-window-excursion
  2432. (cond
  2433. ;; A function is provided: Apply it.
  2434. ((functionp org-latex-pdf-process)
  2435. (funcall org-latex-pdf-process (shell-quote-argument texfile)))
  2436. ;; A list is provided: Replace %b, %f and %o with appropriate
  2437. ;; values in each command before applying it. Output is
  2438. ;; redirected to "*Org PDF LaTeX Output*" buffer.
  2439. ((consp org-latex-pdf-process)
  2440. (let ((outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
  2441. (mapc
  2442. (lambda (command)
  2443. (shell-command
  2444. (replace-regexp-in-string
  2445. "%b" (shell-quote-argument base-name)
  2446. (replace-regexp-in-string
  2447. "%f" (shell-quote-argument full-name)
  2448. (replace-regexp-in-string
  2449. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  2450. outbuf))
  2451. org-latex-pdf-process)
  2452. ;; Collect standard errors from output buffer.
  2453. (setq errors (org-latex--collect-errors outbuf))))
  2454. (t (error "No valid command to process to PDF")))
  2455. (let ((pdffile (concat out-dir base-name ".pdf")))
  2456. ;; Check for process failure. Provide collected errors if
  2457. ;; possible.
  2458. (if (not (file-exists-p pdffile))
  2459. (error (concat (format "PDF file %s wasn't produced" pdffile)
  2460. (when errors (concat ": " errors))))
  2461. ;; Else remove log files, when specified, and signal end of
  2462. ;; process to user, along with any error encountered.
  2463. (when org-latex-remove-logfiles
  2464. (dolist (ext org-latex-logfiles-extensions)
  2465. (let ((file (concat out-dir base-name "." ext)))
  2466. (when (file-exists-p file) (delete-file file)))))
  2467. (message (concat "Process completed"
  2468. (if (not errors) "."
  2469. (concat " with errors: " errors)))))
  2470. ;; Return output file name.
  2471. pdffile))))
  2472. (defun org-latex--collect-errors (buffer)
  2473. "Collect some kind of errors from \"pdflatex\" command output.
  2474. BUFFER is the buffer containing output.
  2475. Return collected error types as a string, or nil if there was
  2476. none."
  2477. (with-current-buffer buffer
  2478. (save-excursion
  2479. (goto-char (point-max))
  2480. (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
  2481. (let ((case-fold-search t)
  2482. (errors ""))
  2483. (dolist (latex-error org-latex-known-errors)
  2484. (when (save-excursion (re-search-forward (car latex-error) nil t))
  2485. (setq errors (concat errors " " (cdr latex-error)))))
  2486. (and (org-string-nw-p errors) (org-trim errors)))))))
  2487. ;;;###autoload
  2488. (defun org-latex-publish-to-latex (plist filename pub-dir)
  2489. "Publish an Org file to LaTeX.
  2490. FILENAME is the filename of the Org file to be published. PLIST
  2491. is the property list for the given project. PUB-DIR is the
  2492. publishing directory.
  2493. Return output file name."
  2494. (org-publish-org-to 'latex filename ".tex" plist pub-dir))
  2495. ;;;###autoload
  2496. (defun org-latex-publish-to-pdf (plist filename pub-dir)
  2497. "Publish an Org file to PDF (via LaTeX).
  2498. FILENAME is the filename of the Org file to be published. PLIST
  2499. is the property list for the given project. PUB-DIR is the
  2500. publishing directory.
  2501. Return output file name."
  2502. ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
  2503. ;; in working directory and then moved to publishing directory.
  2504. (org-publish-attachment
  2505. plist
  2506. (org-latex-compile (org-publish-org-to 'latex filename ".tex" plist))
  2507. pub-dir))
  2508. (provide 'ox-latex)
  2509. ;; Local variables:
  2510. ;; generated-autoload-file: "org-loaddefs.el"
  2511. ;; End:
  2512. ;;; ox-latex.el ends here