ox-latex.el 106 KB

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