ox-latex.el 104 KB

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