ox-latex.el 106 KB

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