ox-latex.el 108 KB

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