ox-latex.el 104 KB

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