ox-latex.el 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967
  1. ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
  2. ;; Copyright (C) 2011-2013 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; See Org manual for details.
  18. ;;; Code:
  19. (eval-when-compile (require 'cl))
  20. (require 'ox)
  21. (require 'ox-publish)
  22. (defvar org-latex-default-packages-alist)
  23. (defvar org-latex-packages-alist)
  24. (defvar orgtbl-exp-regexp)
  25. ;;; Define Back-End
  26. (org-export-define-backend 'latex
  27. '((bold . org-latex-bold)
  28. (center-block . org-latex-center-block)
  29. (clock . org-latex-clock)
  30. (code . org-latex-code)
  31. (comment . (lambda (&rest args) ""))
  32. (comment-block . (lambda (&rest args) ""))
  33. (drawer . org-latex-drawer)
  34. (dynamic-block . org-latex-dynamic-block)
  35. (entity . org-latex-entity)
  36. (example-block . org-latex-example-block)
  37. (export-block . org-latex-export-block)
  38. (export-snippet . org-latex-export-snippet)
  39. (fixed-width . org-latex-fixed-width)
  40. (footnote-definition . org-latex-footnote-definition)
  41. (footnote-reference . org-latex-footnote-reference)
  42. (headline . org-latex-headline)
  43. (horizontal-rule . org-latex-horizontal-rule)
  44. (inline-src-block . org-latex-inline-src-block)
  45. (inlinetask . org-latex-inlinetask)
  46. (italic . org-latex-italic)
  47. (item . org-latex-item)
  48. (keyword . org-latex-keyword)
  49. (latex-environment . org-latex-latex-environment)
  50. (latex-fragment . org-latex-latex-fragment)
  51. (line-break . org-latex-line-break)
  52. (link . org-latex-link)
  53. (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\\|pgf\\)\\'"))
  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 "[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. (defvar org-latex-custom-lang-environments nil
  606. "Alist mapping languages to language-specific LaTeX environments.
  607. It is used during export of src blocks by the listings and minted
  608. latex packages. For example,
  609. \(setq org-latex-custom-lang-environments
  610. '\(\(python \"pythoncode\"\)\)\)
  611. would have the effect that if org encounters begin_src python
  612. during latex export it will output
  613. \\begin{pythoncode}
  614. <src block body>
  615. \\end{pythoncode}")
  616. ;;;; Compilation
  617. (defcustom org-latex-pdf-process
  618. '("pdflatex -interaction nonstopmode -output-directory %o %f"
  619. "pdflatex -interaction nonstopmode -output-directory %o %f"
  620. "pdflatex -interaction nonstopmode -output-directory %o %f")
  621. "Commands to process a LaTeX file to a PDF file.
  622. This is a list of strings, each of them will be given to the
  623. shell as a command. %f in the command will be replaced by the
  624. full file name, %b by the file base name (i.e. without directory
  625. and extension parts) and %o by the base directory of the file.
  626. The reason why this is a list is that it usually takes several
  627. runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
  628. does not have a clever mechanism to detect which of these
  629. commands have to be run to get to a stable result, and it also
  630. does not do any error checking.
  631. By default, Org uses 3 runs of `pdflatex' to do the processing.
  632. If you have texi2dvi on your system and if that does not cause
  633. the infamous egrep/locale bug:
  634. http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
  635. then `texi2dvi' is the superior choice. Org does offer it as one
  636. of the customize options.
  637. Alternatively, this may be a Lisp function that does the
  638. processing, so you could use this to apply the machinery of
  639. AUCTeX or the Emacs LaTeX mode. This function should accept the
  640. file name as its single argument."
  641. :group 'org-export-pdf
  642. :type '(choice
  643. (repeat :tag "Shell command sequence"
  644. (string :tag "Shell command"))
  645. (const :tag "2 runs of pdflatex"
  646. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  647. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  648. (const :tag "3 runs of pdflatex"
  649. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  650. "pdflatex -interaction nonstopmode -output-directory %o %f"
  651. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  652. (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
  653. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  654. "bibtex %b"
  655. "pdflatex -interaction nonstopmode -output-directory %o %f"
  656. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  657. (const :tag "2 runs of xelatex"
  658. ("xelatex -interaction nonstopmode -output-directory %o %f"
  659. "xelatex -interaction nonstopmode -output-directory %o %f"))
  660. (const :tag "3 runs of xelatex"
  661. ("xelatex -interaction nonstopmode -output-directory %o %f"
  662. "xelatex -interaction nonstopmode -output-directory %o %f"
  663. "xelatex -interaction nonstopmode -output-directory %o %f"))
  664. (const :tag "xelatex,bibtex,xelatex,xelatex"
  665. ("xelatex -interaction nonstopmode -output-directory %o %f"
  666. "bibtex %b"
  667. "xelatex -interaction nonstopmode -output-directory %o %f"
  668. "xelatex -interaction nonstopmode -output-directory %o %f"))
  669. (const :tag "texi2dvi"
  670. ("texi2dvi -p -b -c -V %f"))
  671. (const :tag "rubber"
  672. ("rubber -d --into %o %f"))
  673. (function)))
  674. (defcustom org-latex-logfiles-extensions
  675. '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
  676. "The list of file extensions to consider as LaTeX logfiles.
  677. The logfiles will be remove if `org-latex-remove-logfiles' is
  678. non-nil."
  679. :group 'org-export-latex
  680. :type '(repeat (string :tag "Extension")))
  681. (defcustom org-latex-remove-logfiles t
  682. "Non-nil means remove the logfiles produced by PDF production.
  683. By default, logfiles are files with these extensions: .aux, .idx,
  684. .log, .out, .toc, .nav, .snm and .vrb. To define the set of
  685. logfiles to remove, set `org-latex-logfiles-extensions'."
  686. :group 'org-export-latex
  687. :type 'boolean)
  688. (defcustom org-latex-known-errors
  689. '(("Reference.*?undefined" . "[undefined reference]")
  690. ("Citation.*?undefined" . "[undefined citation]")
  691. ("Undefined control sequence" . "[undefined control sequence]")
  692. ("^! LaTeX.*?Error" . "[LaTeX error]")
  693. ("^! Package.*?Error" . "[package error]")
  694. ("Runaway argument" . "Runaway argument"))
  695. "Alist of regular expressions and associated messages for the user.
  696. The regular expressions are used to find possible errors in the
  697. log of a latex-run."
  698. :group 'org-export-latex
  699. :version "24.4"
  700. :package-version '(Org . "8.0")
  701. :type '(repeat
  702. (cons
  703. (string :tag "Regexp")
  704. (string :tag "Message"))))
  705. ;;; Internal Functions
  706. (defun org-latex--caption/label-string (element info)
  707. "Return caption and label LaTeX string for ELEMENT.
  708. INFO is a plist holding contextual information. If there's no
  709. caption nor label, return the empty string.
  710. For non-floats, see `org-latex--wrap-label'."
  711. (let* ((label (org-element-property :name element))
  712. (label-str (if (not (org-string-nw-p label)) ""
  713. (format "\\label{%s}"
  714. (org-export-solidify-link-text label))))
  715. (main (org-export-get-caption element))
  716. (short (org-export-get-caption element t)))
  717. (cond
  718. ((and (not main) (equal label-str "")) "")
  719. ((not main) (concat label-str "\n"))
  720. ;; Option caption format with short name.
  721. (short (format "\\caption[%s]{%s%s}\n"
  722. (org-export-data short info)
  723. label-str
  724. (org-export-data main info)))
  725. ;; Standard caption format.
  726. (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
  727. (defun org-latex-guess-inputenc (header)
  728. "Set the coding system in inputenc to what the buffer is.
  729. HEADER is the LaTeX header string. This function only applies
  730. when specified inputenc option is \"AUTO\".
  731. Return the new header, as a string."
  732. (let* ((cs (or (ignore-errors
  733. (latexenc-coding-system-to-inputenc
  734. (or org-export-coding-system buffer-file-coding-system)))
  735. "utf8")))
  736. (if (not cs) header
  737. ;; First translate if that is requested.
  738. (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
  739. ;; Then find the \usepackage statement and replace the option.
  740. (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  741. cs header t nil 1))))
  742. (defun org-latex-guess-babel-language (header info)
  743. "Set Babel's language according to LANGUAGE keyword.
  744. HEADER is the LaTeX header string. INFO is the plist used as
  745. a communication channel.
  746. Insertion of guessed language only happens when Babel package has
  747. explicitly been loaded. Then it is added to the rest of
  748. package's options.
  749. Return the new header."
  750. (let ((language-code (plist-get info :language)))
  751. ;; If no language is set or Babel package is not loaded, return
  752. ;; HEADER as-is.
  753. (if (or (not (stringp language-code))
  754. (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header)))
  755. header
  756. (let ((options (save-match-data
  757. (org-split-string (match-string 1 header) ",")))
  758. (language (cdr (assoc language-code
  759. org-latex-babel-language-alist))))
  760. ;; If LANGUAGE is already loaded, return header. Otherwise,
  761. ;; append LANGUAGE to other options.
  762. (if (member language options) header
  763. (replace-match (mapconcat 'identity
  764. (append options (list language))
  765. ",")
  766. nil nil header 1))))))
  767. (defun org-latex--find-verb-separator (s)
  768. "Return a character not used in string S.
  769. This is used to choose a separator for constructs like \\verb."
  770. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  771. (loop for c across ll
  772. when (not (string-match (regexp-quote (char-to-string c)) s))
  773. return (char-to-string c))))
  774. (defun org-latex--make-option-string (options)
  775. "Return a comma separated string of keywords and values.
  776. OPTIONS is an alist where the key is the options keyword as
  777. a string, and the value a list containing the keyword value, or
  778. nil."
  779. (mapconcat (lambda (pair)
  780. (concat (first pair)
  781. (when (> (length (second pair)) 0)
  782. (concat "=" (second pair)))))
  783. options
  784. ","))
  785. (defun org-latex--wrap-label (element output)
  786. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  787. This function shouldn't be used for floats. See
  788. `org-latex--caption/label-string'."
  789. (let ((label (org-element-property :name element)))
  790. (if (not (and (org-string-nw-p output) (org-string-nw-p label))) output
  791. (concat (format "\\label{%s}\n" (org-export-solidify-link-text label))
  792. output))))
  793. (defun org-latex--text-markup (text markup)
  794. "Format TEXT depending on MARKUP text markup.
  795. See `org-latex-text-markup-alist' for details."
  796. (let ((fmt (cdr (assq markup org-latex-text-markup-alist))))
  797. (cond
  798. ;; No format string: Return raw text.
  799. ((not fmt) text)
  800. ;; Handle the `verb' special case: Find and appropriate separator
  801. ;; and use "\\verb" command.
  802. ((eq 'verb fmt)
  803. (let ((separator (org-latex--find-verb-separator text)))
  804. (concat "\\verb" separator text separator)))
  805. ;; Handle the `protectedtexttt' special case: Protect some
  806. ;; special chars and use "\texttt{%s}" format string.
  807. ((eq 'protectedtexttt fmt)
  808. (let ((start 0)
  809. (trans '(("\\" . "\\textbackslash{}")
  810. ("~" . "\\textasciitilde{}")
  811. ("^" . "\\textasciicircum{}")))
  812. (rtn "")
  813. char)
  814. (while (string-match "[\\{}$%&_#~^]" text)
  815. (setq char (match-string 0 text))
  816. (if (> (match-beginning 0) 0)
  817. (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
  818. (setq text (substring text (1+ (match-beginning 0))))
  819. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  820. rtn (concat rtn char)))
  821. (setq text (concat rtn text)
  822. fmt "\\texttt{%s}")
  823. (while (string-match "--" text)
  824. (setq text (replace-match "-{}-" t t text)))
  825. (format fmt text)))
  826. ;; Else use format string.
  827. (t (format fmt text)))))
  828. (defun org-latex--delayed-footnotes-definitions (element info)
  829. "Return footnotes definitions in ELEMENT as a string.
  830. INFO is a plist used as a communication channel.
  831. Footnotes definitions are returned within \"\\footnotetxt{}\"
  832. commands.
  833. This function is used within constructs that don't support
  834. \"\\footnote{}\" command (i.e. an item's tag). In that case,
  835. \"\\footnotemark\" is used within the construct and the function
  836. just outside of it."
  837. (mapconcat
  838. (lambda (ref)
  839. (format
  840. "\\footnotetext[%s]{%s}"
  841. (org-export-get-footnote-number ref info)
  842. (org-trim
  843. (org-export-data
  844. (org-export-get-footnote-definition ref info) info))))
  845. ;; Find every footnote reference in ELEMENT.
  846. (let* (all-refs
  847. search-refs ; For byte-compiler.
  848. (search-refs
  849. (function
  850. (lambda (data)
  851. ;; Return a list of all footnote references never seen
  852. ;; before in DATA.
  853. (org-element-map data 'footnote-reference
  854. (lambda (ref)
  855. (when (org-export-footnote-first-reference-p ref info)
  856. (push ref all-refs)
  857. (when (eq (org-element-property :type ref) 'standard)
  858. (funcall search-refs
  859. (org-export-get-footnote-definition ref info)))))
  860. info)
  861. (reverse all-refs)))))
  862. (funcall search-refs element))
  863. ""))
  864. ;;; Template
  865. (defun org-latex-template (contents info)
  866. "Return complete document string after LaTeX conversion.
  867. CONTENTS is the transcoded contents string. INFO is a plist
  868. holding export options."
  869. (let ((title (org-export-data (plist-get info :title) info)))
  870. (concat
  871. ;; Time-stamp.
  872. (and (plist-get info :time-stamp-file)
  873. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  874. ;; Document class and packages.
  875. (let ((class (plist-get info :latex-class))
  876. (class-options (plist-get info :latex-class-options)))
  877. (org-element-normalize-string
  878. (let* ((header (nth 1 (assoc class org-latex-classes)))
  879. (document-class-string
  880. (and (stringp header)
  881. (if (not class-options) header
  882. (replace-regexp-in-string
  883. "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
  884. class-options header t nil 1)))))
  885. (if (not document-class-string)
  886. (user-error "Unknown LaTeX class `%s'" class)
  887. (org-latex-guess-babel-language
  888. (org-latex-guess-inputenc
  889. (org-splice-latex-header
  890. document-class-string
  891. org-latex-default-packages-alist
  892. org-latex-packages-alist nil
  893. (concat (plist-get info :latex-header)
  894. (plist-get info :latex-header-extra))))
  895. info)))))
  896. ;; Possibly limit depth for headline numbering.
  897. (let ((sec-num (plist-get info :section-numbers)))
  898. (when (integerp sec-num)
  899. (format "\\setcounter{secnumdepth}{%d}\n" sec-num)))
  900. ;; Author.
  901. (let ((author (and (plist-get info :with-author)
  902. (let ((auth (plist-get info :author)))
  903. (and auth (org-export-data auth info)))))
  904. (email (and (plist-get info :with-email)
  905. (org-export-data (plist-get info :email) info))))
  906. (cond ((and author email (not (string= "" email)))
  907. (format "\\author{%s\\thanks{%s}}\n" author email))
  908. ((or author email) (format "\\author{%s}\n" (or author email)))))
  909. ;; Date.
  910. (let ((date (and (plist-get info :with-date) (org-export-get-date info))))
  911. (format "\\date{%s}\n" (org-export-data date info)))
  912. ;; Title
  913. (format "\\title{%s}\n" title)
  914. ;; Hyperref options.
  915. (when (plist-get info :latex-hyperref-p)
  916. (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
  917. (or (plist-get info :keywords) "")
  918. (or (plist-get info :description) "")
  919. (if (not (plist-get info :with-creator)) ""
  920. (plist-get info :creator))))
  921. ;; Document start.
  922. "\\begin{document}\n\n"
  923. ;; Title command.
  924. (org-element-normalize-string
  925. (cond ((string= "" title) nil)
  926. ((not (stringp org-latex-title-command)) nil)
  927. ((string-match "\\(?:[^%]\\|^\\)%s"
  928. org-latex-title-command)
  929. (format org-latex-title-command title))
  930. (t org-latex-title-command)))
  931. ;; Table of contents.
  932. (let ((depth (plist-get info :with-toc)))
  933. (when depth
  934. (concat (when (wholenump depth)
  935. (format "\\setcounter{tocdepth}{%d}\n" depth))
  936. org-latex-toc-command)))
  937. ;; Document's body.
  938. contents
  939. ;; Creator.
  940. (let ((creator-info (plist-get info :with-creator)))
  941. (cond
  942. ((not creator-info) "")
  943. ((eq creator-info 'comment)
  944. (format "%% %s\n" (plist-get info :creator)))
  945. (t (concat (plist-get info :creator) "\n"))))
  946. ;; Document end.
  947. "\\end{document}")))
  948. ;;; Transcode Functions
  949. ;;;; Bold
  950. (defun org-latex-bold (bold contents info)
  951. "Transcode BOLD from Org to LaTeX.
  952. CONTENTS is the text with bold markup. INFO is a plist holding
  953. contextual information."
  954. (org-latex--text-markup contents 'bold))
  955. ;;;; Center Block
  956. (defun org-latex-center-block (center-block contents info)
  957. "Transcode a CENTER-BLOCK element from Org to LaTeX.
  958. CONTENTS holds the contents of the center block. INFO is a plist
  959. holding contextual information."
  960. (org-latex--wrap-label
  961. center-block
  962. (format "\\begin{center}\n%s\\end{center}" contents)))
  963. ;;;; Clock
  964. (defun org-latex-clock (clock contents info)
  965. "Transcode a CLOCK element from Org to LaTeX.
  966. CONTENTS is nil. INFO is a plist holding contextual
  967. information."
  968. (concat
  969. "\\noindent"
  970. (format "\\textbf{%s} " org-clock-string)
  971. (format org-latex-inactive-timestamp-format
  972. (concat (org-translate-time
  973. (org-element-property :raw-value
  974. (org-element-property :value clock)))
  975. (let ((time (org-element-property :duration clock)))
  976. (and time (format " (%s)" time)))))
  977. "\\\\"))
  978. ;;;; Code
  979. (defun org-latex-code (code contents info)
  980. "Transcode a CODE object from Org to LaTeX.
  981. CONTENTS is nil. INFO is a plist used as a communication
  982. channel."
  983. (org-latex--text-markup (org-element-property :value code) 'code))
  984. ;;;; Drawer
  985. (defun org-latex-drawer (drawer contents info)
  986. "Transcode a DRAWER element from Org to LaTeX.
  987. CONTENTS holds the contents of the block. INFO is a plist
  988. holding contextual information."
  989. (let* ((name (org-element-property :drawer-name drawer))
  990. (output (if (functionp org-latex-format-drawer-function)
  991. (funcall org-latex-format-drawer-function
  992. name contents)
  993. ;; If there's no user defined function: simply
  994. ;; display contents of the drawer.
  995. contents)))
  996. (org-latex--wrap-label drawer output)))
  997. ;;;; Dynamic Block
  998. (defun org-latex-dynamic-block (dynamic-block contents info)
  999. "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
  1000. CONTENTS holds the contents of the block. INFO is a plist
  1001. holding contextual information. See `org-export-data'."
  1002. (org-latex--wrap-label dynamic-block contents))
  1003. ;;;; Entity
  1004. (defun org-latex-entity (entity contents info)
  1005. "Transcode an ENTITY object from Org to LaTeX.
  1006. CONTENTS are the definition itself. INFO is a plist holding
  1007. contextual information."
  1008. (let ((ent (org-element-property :latex entity)))
  1009. (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent)))
  1010. ;;;; Example Block
  1011. (defun org-latex-example-block (example-block contents info)
  1012. "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
  1013. CONTENTS is nil. INFO is a plist holding contextual
  1014. information."
  1015. (when (org-string-nw-p (org-element-property :value example-block))
  1016. (org-latex--wrap-label
  1017. example-block
  1018. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1019. (org-export-format-code-default example-block info)))))
  1020. ;;;; Export Block
  1021. (defun org-latex-export-block (export-block contents info)
  1022. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  1023. CONTENTS is nil. INFO is a plist holding contextual information."
  1024. (when (member (org-element-property :type export-block) '("LATEX" "TEX"))
  1025. (org-remove-indentation (org-element-property :value export-block))))
  1026. ;;;; Export Snippet
  1027. (defun org-latex-export-snippet (export-snippet contents info)
  1028. "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
  1029. CONTENTS is nil. INFO is a plist holding contextual information."
  1030. (when (eq (org-export-snippet-backend export-snippet) 'latex)
  1031. (org-element-property :value export-snippet)))
  1032. ;;;; Fixed Width
  1033. (defun org-latex-fixed-width (fixed-width contents info)
  1034. "Transcode a FIXED-WIDTH element from Org to LaTeX.
  1035. CONTENTS is nil. INFO is a plist holding contextual information."
  1036. (org-latex--wrap-label
  1037. fixed-width
  1038. (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1039. (org-remove-indentation
  1040. (org-element-property :value fixed-width)))))
  1041. ;;;; Footnote Reference
  1042. ;;
  1043. ;; Footnote reference export is handled by
  1044. ;; `org-latex-footnote-reference'.
  1045. ;;
  1046. ;; Internally, `org-latex--get-footnote-counter' is used to restore
  1047. ;; the value of the LaTeX "footnote" counter after a jump due to
  1048. ;; a reference to an already defined footnote. It is only needed in
  1049. ;; item tags since the optional argument to \footnotemark is not
  1050. ;; allowed there.
  1051. (defun org-latex--get-footnote-counter (footnote-reference info)
  1052. "Return \"footnote\" counter before FOOTNOTE-REFERENCE is encountered.
  1053. INFO is a plist used as a communication channel."
  1054. ;; Find original counter value by counting number of footnote
  1055. ;; references appearing for the first time before the current
  1056. ;; footnote reference.
  1057. (let* ((label (org-element-property :label footnote-reference))
  1058. seen-refs
  1059. search-ref ; For byte-compiler.
  1060. (search-ref
  1061. (function
  1062. (lambda (data)
  1063. ;; Search footnote references through DATA, filling
  1064. ;; SEEN-REFS along the way.
  1065. (org-element-map data 'footnote-reference
  1066. (lambda (fn)
  1067. (let ((fn-lbl (org-element-property :label fn)))
  1068. (cond
  1069. ;; Anonymous footnote match: return number.
  1070. ((eq fn footnote-reference) (length seen-refs))
  1071. ;; Anonymous footnote: it's always a new one.
  1072. ;; Also, be sure to return nil from the `cond' so
  1073. ;; `first-match' doesn't get us out of the loop.
  1074. ((not fn-lbl) (push 'inline seen-refs) nil)
  1075. ;; Label not seen so far: add it so SEEN-REFS.
  1076. ;;
  1077. ;; Also search for subsequent references in
  1078. ;; footnote definition so numbering follows
  1079. ;; reading logic. Note that we don't care about
  1080. ;; inline definitions, since `org-element-map'
  1081. ;; already traverses them at the right time.
  1082. ((not (member fn-lbl seen-refs))
  1083. (push fn-lbl seen-refs)
  1084. (funcall search-ref
  1085. (org-export-get-footnote-definition fn info))))))
  1086. ;; Don't enter footnote definitions since it will
  1087. ;; happen when their first reference is found.
  1088. info 'first-match 'footnote-definition)))))
  1089. (funcall search-ref (plist-get info :parse-tree))))
  1090. (defun org-latex-footnote-reference (footnote-reference contents info)
  1091. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  1092. CONTENTS is nil. INFO is a plist holding contextual information."
  1093. (concat
  1094. ;; Insert separator between two footnotes in a row.
  1095. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1096. (when (eq (org-element-type prev) 'footnote-reference)
  1097. org-latex-footnote-separator))
  1098. (cond
  1099. ;; Use \footnotemark if reference is within an item's tag.
  1100. ((eq (org-element-type (org-export-get-parent-element footnote-reference))
  1101. 'item)
  1102. (if (org-export-footnote-first-reference-p footnote-reference info)
  1103. "\\footnotemark"
  1104. ;; Since we can't specify footnote number as an optional
  1105. ;; argument within an item tag, some extra work has to be done
  1106. ;; when the footnote has already been referenced. In that
  1107. ;; case, set footnote counter to the desired number, use the
  1108. ;; footnotemark, then set counter back to its original value.
  1109. (format
  1110. "\\setcounter{footnote}{%s}\\footnotemark\\setcounter{footnote}{%s}"
  1111. (1- (org-export-get-footnote-number footnote-reference info))
  1112. (org-latex--get-footnote-counter footnote-reference info))))
  1113. ;; Use \footnotemark if the footnote has already been defined.
  1114. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1115. (format "\\footnotemark[%s]{}"
  1116. (org-export-get-footnote-number footnote-reference info)))
  1117. ;; Use \footnotemark if reference is within another footnote
  1118. ;; reference, footnote definition or table cell.
  1119. ((loop for parent in (org-export-get-genealogy footnote-reference)
  1120. thereis (memq (org-element-type parent)
  1121. '(footnote-reference footnote-definition table-cell)))
  1122. "\\footnotemark")
  1123. ;; Otherwise, define it with \footnote command.
  1124. (t
  1125. (let ((def (org-export-get-footnote-definition footnote-reference info)))
  1126. (concat
  1127. (format "\\footnote{%s}" (org-trim (org-export-data def info)))
  1128. ;; Retrieve all footnote references within the footnote and
  1129. ;; add their definition after it, since LaTeX doesn't support
  1130. ;; them inside.
  1131. (org-latex--delayed-footnotes-definitions def info)))))))
  1132. ;;;; Headline
  1133. (defun org-latex-headline (headline contents info)
  1134. "Transcode a HEADLINE element from Org to LaTeX.
  1135. CONTENTS holds the contents of the headline. INFO is a plist
  1136. holding contextual information."
  1137. (unless (org-element-property :footnote-section-p headline)
  1138. (let* ((class (plist-get info :latex-class))
  1139. (level (org-export-get-relative-level headline info))
  1140. (numberedp (org-export-numbered-headline-p headline info))
  1141. (class-sectionning (assoc class org-latex-classes))
  1142. ;; Section formatting will set two placeholders: one for
  1143. ;; the title and the other for the contents.
  1144. (section-fmt
  1145. (let ((sec (if (functionp (nth 2 class-sectionning))
  1146. (funcall (nth 2 class-sectionning) level numberedp)
  1147. (nth (1+ level) class-sectionning))))
  1148. (cond
  1149. ;; No section available for that LEVEL.
  1150. ((not sec) nil)
  1151. ;; Section format directly returned by a function. Add
  1152. ;; placeholder for contents.
  1153. ((stringp sec) (concat sec "\n%s"))
  1154. ;; (numbered-section . unnumbered-section)
  1155. ((not (consp (cdr sec)))
  1156. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  1157. ;; (numbered-open numbered-close)
  1158. ((= (length sec) 2)
  1159. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  1160. ;; (num-in num-out no-num-in no-num-out)
  1161. ((= (length sec) 4)
  1162. (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
  1163. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  1164. (text (org-export-data (org-element-property :title headline) info))
  1165. (todo
  1166. (and (plist-get info :with-todo-keywords)
  1167. (let ((todo (org-element-property :todo-keyword headline)))
  1168. (and todo (org-export-data todo info)))))
  1169. (todo-type (and todo (org-element-property :todo-type headline)))
  1170. (tags (and (plist-get info :with-tags)
  1171. (org-export-get-tags headline info)))
  1172. (priority (and (plist-get info :with-priority)
  1173. (org-element-property :priority headline)))
  1174. ;; Create the headline text along with a no-tag version.
  1175. ;; The latter is required to remove tags from toc.
  1176. (full-text (funcall org-latex-format-headline-function
  1177. todo todo-type priority text tags))
  1178. ;; Associate \label to the headline for internal links.
  1179. (headline-label
  1180. (format "\\label{sec-%s}\n"
  1181. (mapconcat 'number-to-string
  1182. (org-export-get-headline-number headline info)
  1183. "-")))
  1184. (pre-blanks
  1185. (make-string (org-element-property :pre-blank headline) 10)))
  1186. (if (or (not section-fmt) (org-export-low-level-p headline info))
  1187. ;; This is a deep sub-tree: export it as a list item. Also
  1188. ;; export as items headlines for which no section format has
  1189. ;; been found.
  1190. (let ((low-level-body
  1191. (concat
  1192. ;; If headline is the first sibling, start a list.
  1193. (when (org-export-first-sibling-p headline info)
  1194. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  1195. ;; Itemize headline
  1196. "\\item " full-text "\n" headline-label pre-blanks contents)))
  1197. ;; If headline is not the last sibling simply return
  1198. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before
  1199. ;; any blank line.
  1200. (if (not (org-export-last-sibling-p headline info)) low-level-body
  1201. (replace-regexp-in-string
  1202. "[ \t\n]*\\'"
  1203. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  1204. low-level-body)))
  1205. ;; This is a standard headline. Export it as a section. Add
  1206. ;; an alternative heading when possible, and when this is not
  1207. ;; identical to the usual heading.
  1208. (let ((opt-title
  1209. (funcall org-latex-format-headline-function
  1210. todo todo-type priority
  1211. (org-export-data
  1212. (org-export-get-alt-title headline info) info)
  1213. (and (eq (plist-get info :with-tags) t) tags))))
  1214. (if (and numberedp opt-title
  1215. (not (equal opt-title full-text))
  1216. (string-match "\\`\\\\\\(.*?[^*]\\){" section-fmt))
  1217. (format (replace-match "\\1[%s]" nil nil section-fmt 1)
  1218. ;; Replace square brackets with parenthesis
  1219. ;; since square brackets are not supported in
  1220. ;; optional arguments.
  1221. (replace-regexp-in-string
  1222. "\\[" "(" (replace-regexp-in-string "\\]" ")" opt-title))
  1223. full-text
  1224. (concat headline-label pre-blanks contents))
  1225. ;; Impossible to add an alternative heading. Fallback to
  1226. ;; regular sectioning format string.
  1227. (format section-fmt full-text
  1228. (concat headline-label pre-blanks contents))))))))
  1229. (defun org-latex-format-headline-default-function
  1230. (todo todo-type priority text tags)
  1231. "Default format function for a headline.
  1232. See `org-latex-format-headline-function' for details."
  1233. (concat
  1234. (and todo (format "{\\bfseries\\sffamily %s} " todo))
  1235. (and priority (format "\\framebox{\\#%c} " priority))
  1236. text
  1237. (and tags
  1238. (format "\\hfill{}\\textsc{%s}" (mapconcat 'identity tags ":")))))
  1239. ;;;; Horizontal Rule
  1240. (defun org-latex-horizontal-rule (horizontal-rule contents info)
  1241. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  1242. CONTENTS is nil. INFO is a plist holding contextual information."
  1243. (let ((attr (org-export-read-attribute :attr_latex horizontal-rule))
  1244. (prev (org-export-get-previous-element horizontal-rule info)))
  1245. (concat
  1246. ;; Make sure the rule doesn't start at the end of the current
  1247. ;; line by separating it with a blank line from previous element.
  1248. (when (and prev
  1249. (let ((prev-blank (org-element-property :post-blank prev)))
  1250. (or (not prev-blank) (zerop prev-blank))))
  1251. "\n")
  1252. (org-latex--wrap-label
  1253. horizontal-rule
  1254. (format "\\rule{%s}{%s}"
  1255. (or (plist-get attr :width) "\\linewidth")
  1256. (or (plist-get attr :thickness) "0.5pt"))))))
  1257. ;;;; Inline Src Block
  1258. (defun org-latex-inline-src-block (inline-src-block contents info)
  1259. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  1260. CONTENTS holds the contents of the item. INFO is a plist holding
  1261. contextual information."
  1262. (let* ((code (org-element-property :value inline-src-block))
  1263. (separator (org-latex--find-verb-separator code)))
  1264. (cond
  1265. ;; Do not use a special package: transcode it verbatim.
  1266. ((not org-latex-listings)
  1267. (concat "\\verb" separator code separator))
  1268. ;; Use minted package.
  1269. ((eq org-latex-listings 'minted)
  1270. (let* ((org-lang (org-element-property :language inline-src-block))
  1271. (mint-lang (or (cadr (assq (intern org-lang)
  1272. org-latex-minted-langs))
  1273. org-lang))
  1274. (options (org-latex--make-option-string
  1275. org-latex-minted-options)))
  1276. (concat (format "\\mint%s{%s}"
  1277. (if (string= options "") "" (format "[%s]" options))
  1278. mint-lang)
  1279. separator code separator)))
  1280. ;; Use listings package.
  1281. (t
  1282. ;; Maybe translate language's name.
  1283. (let* ((org-lang (org-element-property :language inline-src-block))
  1284. (lst-lang (or (cadr (assq (intern org-lang)
  1285. org-latex-listings-langs))
  1286. org-lang))
  1287. (options (org-latex--make-option-string
  1288. (append org-latex-listings-options
  1289. `(("language" ,lst-lang))))))
  1290. (concat (format "\\lstinline[%s]" options)
  1291. separator code separator))))))
  1292. ;;;; Inlinetask
  1293. (defun org-latex-inlinetask (inlinetask contents info)
  1294. "Transcode an INLINETASK element from Org to LaTeX.
  1295. CONTENTS holds the contents of the block. INFO is a plist
  1296. holding contextual information."
  1297. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  1298. (todo (and (plist-get info :with-todo-keywords)
  1299. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1300. (and todo (org-export-data todo info)))))
  1301. (todo-type (org-element-property :todo-type inlinetask))
  1302. (tags (and (plist-get info :with-tags)
  1303. (org-export-get-tags inlinetask info)))
  1304. (priority (and (plist-get info :with-priority)
  1305. (org-element-property :priority inlinetask))))
  1306. ;; If `org-latex-format-inlinetask-function' is provided, call it
  1307. ;; with appropriate arguments.
  1308. (if (functionp org-latex-format-inlinetask-function)
  1309. (funcall org-latex-format-inlinetask-function
  1310. todo todo-type priority title tags contents)
  1311. ;; Otherwise, use a default template.
  1312. (org-latex--wrap-label
  1313. inlinetask
  1314. (let ((full-title
  1315. (concat
  1316. (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  1317. (when priority (format "\\framebox{\\#%c} " priority))
  1318. title
  1319. (when tags (format "\\hfill{}\\textsc{:%s:}"
  1320. (mapconcat 'identity tags ":"))))))
  1321. (format (concat "\\begin{center}\n"
  1322. "\\fbox{\n"
  1323. "\\begin{minipage}[c]{.6\\textwidth}\n"
  1324. "%s\n\n"
  1325. "\\rule[.8em]{\\textwidth}{2pt}\n\n"
  1326. "%s"
  1327. "\\end{minipage}\n"
  1328. "}\n"
  1329. "\\end{center}")
  1330. full-title contents))))))
  1331. ;;;; Italic
  1332. (defun org-latex-italic (italic contents info)
  1333. "Transcode ITALIC from Org to LaTeX.
  1334. CONTENTS is the text with italic markup. INFO is a plist holding
  1335. contextual information."
  1336. (org-latex--text-markup contents 'italic))
  1337. ;;;; Item
  1338. (defun org-latex-item (item contents info)
  1339. "Transcode an ITEM element from Org to LaTeX.
  1340. CONTENTS holds the contents of the item. INFO is a plist holding
  1341. contextual information."
  1342. (let* ((counter
  1343. (let ((count (org-element-property :counter item))
  1344. (level
  1345. ;; Determine level of current item to determine the
  1346. ;; correct LaTeX counter to use (enumi, enumii...).
  1347. (let ((parent item) (level 0))
  1348. (while (memq (org-element-type
  1349. (setq parent (org-export-get-parent parent)))
  1350. '(plain-list item))
  1351. (when (and (eq (org-element-type parent) 'plain-list)
  1352. (eq (org-element-property :type parent)
  1353. 'ordered))
  1354. (incf level)))
  1355. level)))
  1356. (and count
  1357. (< level 5)
  1358. (format "\\setcounter{enum%s}{%s}\n"
  1359. (nth (1- level) '("i" "ii" "iii" "iv"))
  1360. (1- count)))))
  1361. (checkbox (case (org-element-property :checkbox item)
  1362. (on "$\\boxtimes$ ")
  1363. (off "$\\Box$ ")
  1364. (trans "$\\boxminus$ ")))
  1365. (tag (let ((tag (org-element-property :tag item)))
  1366. ;; Check-boxes must belong to the tag.
  1367. (and tag (format "[%s] "
  1368. (concat checkbox
  1369. (org-export-data tag info)))))))
  1370. (concat counter "\\item" (or tag (concat " " checkbox))
  1371. (and contents (org-trim contents))
  1372. ;; If there are footnotes references in tag, be sure to
  1373. ;; add their definition at the end of the item. This
  1374. ;; workaround is necessary since "\footnote{}" command is
  1375. ;; not supported in tags.
  1376. (and tag
  1377. (org-latex--delayed-footnotes-definitions
  1378. (org-element-property :tag item) info)))))
  1379. ;;;; Keyword
  1380. (defun org-latex-keyword (keyword contents info)
  1381. "Transcode a KEYWORD element from Org to LaTeX.
  1382. CONTENTS is nil. INFO is a plist holding contextual information."
  1383. (let ((key (org-element-property :key keyword))
  1384. (value (org-element-property :value keyword)))
  1385. (cond
  1386. ((string= key "LATEX") value)
  1387. ((string= key "INDEX") (format "\\index{%s}" value))
  1388. ((string= key "TOC")
  1389. (let ((value (downcase value)))
  1390. (cond
  1391. ((string-match "\\<headlines\\>" value)
  1392. (let ((depth (or (and (string-match "[0-9]+" value)
  1393. (string-to-number (match-string 0 value)))
  1394. (plist-get info :with-toc))))
  1395. (concat
  1396. (when (wholenump depth)
  1397. (format "\\setcounter{tocdepth}{%s}\n" depth))
  1398. "\\tableofcontents")))
  1399. ((string= "tables" value) "\\listoftables")
  1400. ((string= "listings" value)
  1401. (cond
  1402. ((eq org-latex-listings 'minted) "\\listoflistings")
  1403. (org-latex-listings "\\lstlistoflistings")
  1404. ;; At the moment, src blocks with a caption are wrapped
  1405. ;; into a figure environment.
  1406. (t "\\listoffigures")))))))))
  1407. ;;;; Latex Environment
  1408. (defun org-latex-latex-environment (latex-environment contents info)
  1409. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1410. CONTENTS is nil. INFO is a plist holding contextual information."
  1411. (when (plist-get info :with-latex)
  1412. (let ((label (org-element-property :name latex-environment))
  1413. (value (org-remove-indentation
  1414. (org-element-property :value latex-environment))))
  1415. (if (not (org-string-nw-p label)) value
  1416. ;; Environment is labelled: label must be within the environment
  1417. ;; (otherwise, a reference pointing to that element will count
  1418. ;; the section instead).
  1419. (with-temp-buffer
  1420. (insert value)
  1421. (goto-char (point-min))
  1422. (forward-line)
  1423. (insert
  1424. (format "\\label{%s}\n" (org-export-solidify-link-text label)))
  1425. (buffer-string))))))
  1426. ;;;; Latex Fragment
  1427. (defun org-latex-latex-fragment (latex-fragment contents info)
  1428. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1429. CONTENTS is nil. INFO is a plist holding contextual information."
  1430. (when (plist-get info :with-latex)
  1431. (org-element-property :value latex-fragment)))
  1432. ;;;; Line Break
  1433. (defun org-latex-line-break (line-break contents info)
  1434. "Transcode a LINE-BREAK object from Org to LaTeX.
  1435. CONTENTS is nil. INFO is a plist holding contextual information."
  1436. "\\\\\n")
  1437. ;;;; Link
  1438. (defun org-latex--inline-image (link info)
  1439. "Return LaTeX code for an inline image.
  1440. LINK is the link pointing to the inline image. INFO is a plist
  1441. used as a communication channel."
  1442. (let* ((parent (org-export-get-parent-element link))
  1443. (path (let ((raw-path (org-element-property :path link)))
  1444. (if (not (file-name-absolute-p raw-path)) raw-path
  1445. (expand-file-name raw-path))))
  1446. (filetype (file-name-extension path))
  1447. (caption (org-latex--caption/label-string parent info))
  1448. ;; Retrieve latex attributes from the element around.
  1449. (attr (org-export-read-attribute :attr_latex parent))
  1450. (float (let ((float (plist-get attr :float)))
  1451. (cond ((and (not float) (plist-member attr :float)) nil)
  1452. ((string= float "wrap") 'wrap)
  1453. ((string= float "multicolumn") 'multicolumn)
  1454. ((or float (org-element-property :caption parent))
  1455. 'figure))))
  1456. (placement
  1457. (let ((place (plist-get attr :placement)))
  1458. (cond (place (format "%s" place))
  1459. ((eq float 'wrap) "{l}{0.5\\textwidth}")
  1460. ((eq float 'figure)
  1461. (format "[%s]" org-latex-default-figure-position))
  1462. (t ""))))
  1463. (comment-include (if (plist-get attr :comment-include) "%" ""))
  1464. ;; It is possible to specify width and height in the
  1465. ;; ATTR_LATEX line, and also via default variables.
  1466. (width (cond ((plist-get attr :width))
  1467. ((plist-get attr :height) "")
  1468. ((eq float 'wrap) "0.48\\textwidth")
  1469. (t org-latex-image-default-width)))
  1470. (height (cond ((plist-get attr :height))
  1471. ((or (plist-get attr :width)
  1472. (memq float '(figure wrap))) "")
  1473. (t org-latex-image-default-height)))
  1474. (options (let ((opt (or (plist-get attr :options)
  1475. org-latex-image-default-option)))
  1476. (if (not (string-match "\\`\\[\\(.*\\)\\]\\'" opt)) opt
  1477. (match-string 1 opt))))
  1478. image-code)
  1479. (if (member filetype '("tikz" "pgf"))
  1480. ;; For tikz images:
  1481. ;; - use \input to read in image file.
  1482. ;; - if options are present, wrap in a tikzpicture environment.
  1483. ;; - if width or height are present, use \resizebox to change
  1484. ;; the image size.
  1485. (progn
  1486. (setq image-code (format "\\input{%s}" path))
  1487. (when (org-string-nw-p options)
  1488. (setq image-code
  1489. (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
  1490. options
  1491. image-code)))
  1492. (when (or (org-string-nw-p width) (org-string-nw-p height))
  1493. (setq image-code (format "\\resizebox{%s}{%s}{%s}"
  1494. (if (org-string-nw-p width) width "!")
  1495. (if (org-string-nw-p height) height "!")
  1496. image-code))))
  1497. ;; For other images:
  1498. ;; - add width and height to options.
  1499. ;; - include the image with \includegraphics.
  1500. (when (org-string-nw-p width)
  1501. (setq options (concat options ",width=" width)))
  1502. (when (org-string-nw-p height)
  1503. (setq options (concat options ",height=" height)))
  1504. (setq image-code
  1505. (format "\\includegraphics%s{%s}"
  1506. (cond ((not (org-string-nw-p options)) "")
  1507. ((= (aref options 0) ?,)
  1508. (format "[%s]"(substring options 1)))
  1509. (t (format "[%s]" options)))
  1510. path)))
  1511. ;; Return proper string, depending on FLOAT.
  1512. (case float
  1513. (wrap (format "\\begin{wrapfigure}%s
  1514. \\centering
  1515. %s%s
  1516. %s\\end{wrapfigure}" placement comment-include image-code caption))
  1517. (multicolumn (format "\\begin{figure*}%s
  1518. \\centering
  1519. %s%s
  1520. %s\\end{figure*}" placement comment-include image-code caption))
  1521. (figure (format "\\begin{figure}%s
  1522. \\centering
  1523. %s%s
  1524. %s\\end{figure}" placement comment-include image-code caption))
  1525. (otherwise image-code))))
  1526. (defun org-latex-link (link desc info)
  1527. "Transcode a LINK object from Org to LaTeX.
  1528. DESC is the description part of the link, or the empty string.
  1529. INFO is a plist holding contextual information. See
  1530. `org-export-data'."
  1531. (let* ((type (org-element-property :type link))
  1532. (raw-path (org-element-property :path link))
  1533. ;; Ensure DESC really exists, or set it to nil.
  1534. (desc (and (not (string= desc "")) desc))
  1535. (imagep (org-export-inline-image-p
  1536. link org-latex-inline-image-rules))
  1537. (path (cond
  1538. ((member type '("http" "https" "ftp" "mailto"))
  1539. (concat type ":" raw-path))
  1540. ((string= type "file")
  1541. (if (not (file-name-absolute-p raw-path)) raw-path
  1542. (concat "file://" (expand-file-name raw-path))))
  1543. (t raw-path)))
  1544. protocol)
  1545. (cond
  1546. ;; Image file.
  1547. (imagep (org-latex--inline-image link info))
  1548. ;; Radio link: Transcode target's contents and use them as link's
  1549. ;; description.
  1550. ((string= type "radio")
  1551. (let ((destination (org-export-resolve-radio-link link info)))
  1552. (when destination
  1553. (format "\\hyperref[%s]{%s}"
  1554. (org-export-solidify-link-text path)
  1555. (org-export-data (org-element-contents destination) info)))))
  1556. ;; Links pointing to a headline: Find destination and build
  1557. ;; appropriate referencing command.
  1558. ((member type '("custom-id" "fuzzy" "id"))
  1559. (let ((destination (if (string= type "fuzzy")
  1560. (org-export-resolve-fuzzy-link link info)
  1561. (org-export-resolve-id-link link info))))
  1562. (case (org-element-type destination)
  1563. ;; Id link points to an external file.
  1564. (plain-text
  1565. (if desc (format "\\href{%s}{%s}" destination desc)
  1566. (format "\\url{%s}" destination)))
  1567. ;; Fuzzy link points nowhere.
  1568. ('nil
  1569. (format org-latex-link-with-unknown-path-format
  1570. (or desc
  1571. (org-export-data
  1572. (org-element-property :raw-link link) info))))
  1573. ;; LINK points to a headline. If headlines are numbered
  1574. ;; and the link has no description, display headline's
  1575. ;; number. Otherwise, display description or headline's
  1576. ;; title.
  1577. (headline
  1578. (let ((label
  1579. (format "sec-%s"
  1580. (mapconcat
  1581. 'number-to-string
  1582. (org-export-get-headline-number destination info)
  1583. "-"))))
  1584. (if (and (plist-get info :section-numbers) (not desc))
  1585. (format "\\ref{%s}" label)
  1586. (format "\\hyperref[%s]{%s}" label
  1587. (or desc
  1588. (org-export-data
  1589. (org-element-property :title destination) info))))))
  1590. ;; Fuzzy link points to a target. Do as above.
  1591. (otherwise
  1592. (let ((path (org-export-solidify-link-text path)))
  1593. (if (not desc) (format "\\ref{%s}" path)
  1594. (format "\\hyperref[%s]{%s}" path desc)))))))
  1595. ;; Coderef: replace link with the reference name or the
  1596. ;; equivalent line number.
  1597. ((string= type "coderef")
  1598. (format (org-export-get-coderef-format path desc)
  1599. (org-export-resolve-coderef path info)))
  1600. ;; Link type is handled by a special function.
  1601. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  1602. (funcall protocol (org-link-unescape path) desc 'latex))
  1603. ;; External link with a description part.
  1604. ((and path desc) (format "\\href{%s}{%s}" path desc))
  1605. ;; External link without a description part.
  1606. (path (format "\\url{%s}" path))
  1607. ;; No path, only description. Try to do something useful.
  1608. (t (format org-latex-link-with-unknown-path-format desc)))))
  1609. ;;;; Paragraph
  1610. (defun org-latex-paragraph (paragraph contents info)
  1611. "Transcode a PARAGRAPH element from Org to LaTeX.
  1612. CONTENTS is the contents of the paragraph, as a string. INFO is
  1613. the plist used as a communication channel."
  1614. contents)
  1615. ;;;; Plain List
  1616. (defun org-latex-plain-list (plain-list contents info)
  1617. "Transcode a PLAIN-LIST element from Org to LaTeX.
  1618. CONTENTS is the contents of the list. INFO is a plist holding
  1619. contextual information."
  1620. (let* ((type (org-element-property :type plain-list))
  1621. (attr (org-export-read-attribute :attr_latex plain-list))
  1622. (latex-type (let ((env (plist-get attr :environment)))
  1623. (cond (env (format "%s" env))
  1624. ((eq type 'ordered) "enumerate")
  1625. ((eq type 'unordered) "itemize")
  1626. ((eq type 'descriptive) "description")))))
  1627. (org-latex--wrap-label
  1628. plain-list
  1629. (format "\\begin{%s}%s\n%s\\end{%s}"
  1630. latex-type
  1631. ;; Put optional arguments, if any inside square brackets
  1632. ;; when necessary.
  1633. (let ((options (format "%s" (or (plist-get attr :options) ""))))
  1634. (cond ((equal options "") "")
  1635. ((string-match "\\`\\[.*\\]\\'" options) options)
  1636. (t (format "[%s]" options))))
  1637. contents
  1638. latex-type))))
  1639. ;;;; Plain Text
  1640. (defun org-latex-plain-text (text info)
  1641. "Transcode a TEXT string from Org to LaTeX.
  1642. TEXT is the string to transcode. INFO is a plist holding
  1643. contextual information."
  1644. (let ((specialp (plist-get info :with-special-strings))
  1645. (output text))
  1646. ;; Protect %, #, &, $, _, { and }.
  1647. (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}_]\\)" output)
  1648. (setq output
  1649. (replace-match
  1650. (format "\\%s" (match-string 2 output)) nil t output 2)))
  1651. ;; Protect ^.
  1652. (setq output
  1653. (replace-regexp-in-string
  1654. "\\([^\\]\\|^\\)\\(\\^\\)" "\\\\^{}" output nil nil 2))
  1655. ;; Protect \. If special strings are used, be careful not to
  1656. ;; protect "\" in "\-" constructs.
  1657. (let ((symbols (if specialp "-%$#&{}^_\\" "%$#&{}^_\\")))
  1658. (setq output
  1659. (replace-regexp-in-string
  1660. (format "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%s]\\|$\\)" symbols)
  1661. "$\\backslash$" output nil t 1)))
  1662. ;; Protect ~.
  1663. (setq output
  1664. (replace-regexp-in-string
  1665. "\\([^\\]\\|^\\)\\(~\\)" "\\textasciitilde{}" output nil t 2))
  1666. ;; Activate smart quotes. Be sure to provide original TEXT string
  1667. ;; since OUTPUT may have been modified.
  1668. (when (plist-get info :with-smart-quotes)
  1669. (setq output (org-export-activate-smart-quotes output :latex info text)))
  1670. ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
  1671. (let ((case-fold-search nil)
  1672. (start 0))
  1673. (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" output start)
  1674. (setq output (replace-match
  1675. (format "\\%s{}" (match-string 1 output)) nil t output)
  1676. start (match-end 0))))
  1677. ;; Convert special strings.
  1678. (when specialp
  1679. (setq output
  1680. (replace-regexp-in-string "\\.\\.\\." "\\ldots{}" output nil t)))
  1681. ;; Handle break preservation if required.
  1682. (when (plist-get info :preserve-breaks)
  1683. (setq output (replace-regexp-in-string
  1684. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" output)))
  1685. ;; Return value.
  1686. output))
  1687. ;;;; Planning
  1688. (defun org-latex-planning (planning contents info)
  1689. "Transcode a PLANNING element from Org to LaTeX.
  1690. CONTENTS is nil. INFO is a plist holding contextual
  1691. information."
  1692. (concat
  1693. "\\noindent"
  1694. (mapconcat
  1695. 'identity
  1696. (delq nil
  1697. (list
  1698. (let ((closed (org-element-property :closed planning)))
  1699. (when closed
  1700. (concat
  1701. (format "\\textbf{%s} " org-closed-string)
  1702. (format org-latex-inactive-timestamp-format
  1703. (org-translate-time
  1704. (org-element-property :raw-value closed))))))
  1705. (let ((deadline (org-element-property :deadline planning)))
  1706. (when deadline
  1707. (concat
  1708. (format "\\textbf{%s} " org-deadline-string)
  1709. (format org-latex-active-timestamp-format
  1710. (org-translate-time
  1711. (org-element-property :raw-value deadline))))))
  1712. (let ((scheduled (org-element-property :scheduled planning)))
  1713. (when scheduled
  1714. (concat
  1715. (format "\\textbf{%s} " org-scheduled-string)
  1716. (format org-latex-active-timestamp-format
  1717. (org-translate-time
  1718. (org-element-property :raw-value scheduled))))))))
  1719. " ")
  1720. "\\\\"))
  1721. ;;;; Quote Block
  1722. (defun org-latex-quote-block (quote-block contents info)
  1723. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  1724. CONTENTS holds the contents of the block. INFO is a plist
  1725. holding contextual information."
  1726. (org-latex--wrap-label
  1727. quote-block
  1728. (format "\\begin{quote}\n%s\\end{quote}" contents)))
  1729. ;;;; Quote Section
  1730. (defun org-latex-quote-section (quote-section contents info)
  1731. "Transcode a QUOTE-SECTION element from Org to LaTeX.
  1732. CONTENTS is nil. INFO is a plist holding contextual information."
  1733. (let ((value (org-remove-indentation
  1734. (org-element-property :value quote-section))))
  1735. (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  1736. ;;;; Radio Target
  1737. (defun org-latex-radio-target (radio-target text info)
  1738. "Transcode a RADIO-TARGET object from Org to LaTeX.
  1739. TEXT is the text of the target. INFO is a plist holding
  1740. contextual information."
  1741. (format "\\label{%s}%s"
  1742. (org-export-solidify-link-text
  1743. (org-element-property :value radio-target))
  1744. text))
  1745. ;;;; Section
  1746. (defun org-latex-section (section contents info)
  1747. "Transcode a SECTION element from Org to LaTeX.
  1748. CONTENTS holds the contents of the section. INFO is a plist
  1749. holding contextual information."
  1750. contents)
  1751. ;;;; Special Block
  1752. (defun org-latex-special-block (special-block contents info)
  1753. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  1754. CONTENTS holds the contents of the block. INFO is a plist
  1755. holding contextual information."
  1756. (let ((type (downcase (org-element-property :type special-block)))
  1757. (opt (org-export-read-attribute :attr_latex special-block :options)))
  1758. (concat (format "\\begin{%s}%s\n" type (or opt ""))
  1759. ;; Insert any label or caption within the block
  1760. ;; (otherwise, a reference pointing to that element will
  1761. ;; count the section instead).
  1762. (org-latex--caption/label-string special-block info)
  1763. contents
  1764. (format "\\end{%s}" type))))
  1765. ;;;; Src Block
  1766. (defun org-latex-src-block (src-block contents info)
  1767. "Transcode a SRC-BLOCK element from Org to LaTeX.
  1768. CONTENTS holds the contents of the item. INFO is a plist holding
  1769. contextual information."
  1770. (when (org-string-nw-p (org-element-property :value src-block))
  1771. (let* ((lang (org-element-property :language src-block))
  1772. (caption (org-element-property :caption src-block))
  1773. (label (org-element-property :name src-block))
  1774. (custom-env (and lang
  1775. (cadr (assq (intern lang)
  1776. org-latex-custom-lang-environments))))
  1777. (num-start (case (org-element-property :number-lines src-block)
  1778. (continued (org-export-get-loc src-block info))
  1779. (new 0)))
  1780. (retain-labels (org-element-property :retain-labels src-block))
  1781. (attributes (org-export-read-attribute :attr_latex src-block))
  1782. (float (plist-get attributes :float)))
  1783. (cond
  1784. ;; Case 1. No source fontification.
  1785. ((not org-latex-listings)
  1786. (let* ((caption-str (org-latex--caption/label-string src-block info))
  1787. (float-env
  1788. (cond ((and (not float) (plist-member attributes :float)) "%s")
  1789. ((string= "multicolumn" float)
  1790. (format "\\begin{figure*}[%s]\n%s%%s\n\\end{figure*}"
  1791. org-latex-default-figure-position
  1792. caption-str))
  1793. ((or caption float)
  1794. (format "\\begin{figure}[H]\n%s%%s\n\\end{figure}"
  1795. caption-str))
  1796. (t "%s"))))
  1797. (format
  1798. float-env
  1799. (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
  1800. (org-export-format-code-default src-block info))))))
  1801. ;; Case 2. Custom environment.
  1802. (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
  1803. custom-env
  1804. (org-export-format-code-default src-block info)
  1805. custom-env))
  1806. ;; Case 3. Use minted package.
  1807. ((eq org-latex-listings 'minted)
  1808. (let* ((caption-str (org-latex--caption/label-string src-block info))
  1809. (float-env
  1810. (cond ((and (not float) (plist-member attributes :float)) "%s")
  1811. ((string= "multicolumn" float)
  1812. (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
  1813. caption-str))
  1814. ((or caption float)
  1815. (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
  1816. caption-str))
  1817. (t "%s")))
  1818. (body
  1819. (format
  1820. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  1821. ;; Options.
  1822. (org-latex--make-option-string
  1823. (if (or (not num-start)
  1824. (assoc "linenos" org-latex-minted-options))
  1825. org-latex-minted-options
  1826. (append
  1827. `(("linenos")
  1828. ("firstnumber" ,(number-to-string (1+ num-start))))
  1829. org-latex-minted-options)))
  1830. ;; Language.
  1831. (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
  1832. ;; Source code.
  1833. (let* ((code-info (org-export-unravel-code src-block))
  1834. (max-width
  1835. (apply 'max
  1836. (mapcar 'length
  1837. (org-split-string (car code-info)
  1838. "\n")))))
  1839. (org-export-format-code
  1840. (car code-info)
  1841. (lambda (loc num ref)
  1842. (concat
  1843. loc
  1844. (when ref
  1845. ;; Ensure references are flushed to the right,
  1846. ;; separated with 6 spaces from the widest line
  1847. ;; of code.
  1848. (concat (make-string (+ (- max-width (length loc)) 6)
  1849. ?\s)
  1850. (format "(%s)" ref)))))
  1851. nil (and retain-labels (cdr code-info)))))))
  1852. ;; Return value.
  1853. (format float-env body)))
  1854. ;; Case 4. Use listings package.
  1855. (t
  1856. (let ((lst-lang
  1857. (or (cadr (assq (intern lang) org-latex-listings-langs)) lang))
  1858. (caption-str
  1859. (when caption
  1860. (let ((main (org-export-get-caption src-block))
  1861. (secondary (org-export-get-caption src-block t)))
  1862. (if (not secondary)
  1863. (format "{%s}" (org-export-data main info))
  1864. (format "{[%s]%s}"
  1865. (org-export-data secondary info)
  1866. (org-export-data main info)))))))
  1867. (concat
  1868. ;; Options.
  1869. (format
  1870. "\\lstset{%s}\n"
  1871. (org-latex--make-option-string
  1872. (append
  1873. org-latex-listings-options
  1874. (cond
  1875. ((and (not float) (plist-member attributes :float)) nil)
  1876. ((string= "multicolumn" float) '(("float" "*")))
  1877. ((and float (not (assoc "float" org-latex-listings-options)))
  1878. `(("float" ,org-latex-default-figure-position))))
  1879. `(("language" ,lst-lang))
  1880. (when label `(("label" ,label)))
  1881. (when caption-str `(("caption" ,caption-str)))
  1882. (cond ((assoc "numbers" org-latex-listings-options) nil)
  1883. ((not num-start) '(("numbers" "none")))
  1884. ((zerop num-start) '(("numbers" "left")))
  1885. (t `(("numbers" "left")
  1886. ("firstnumber"
  1887. ,(number-to-string (1+ num-start)))))))))
  1888. ;; Source code.
  1889. (format
  1890. "\\begin{lstlisting}\n%s\\end{lstlisting}"
  1891. (let* ((code-info (org-export-unravel-code src-block))
  1892. (max-width
  1893. (apply 'max
  1894. (mapcar 'length
  1895. (org-split-string (car code-info) "\n")))))
  1896. (org-export-format-code
  1897. (car code-info)
  1898. (lambda (loc num ref)
  1899. (concat
  1900. loc
  1901. (when ref
  1902. ;; Ensure references are flushed to the right,
  1903. ;; separated with 6 spaces from the widest line of
  1904. ;; code
  1905. (concat (make-string (+ (- max-width (length loc)) 6) ? )
  1906. (format "(%s)" ref)))))
  1907. nil (and retain-labels (cdr code-info))))))))))))
  1908. ;;;; Statistics Cookie
  1909. (defun org-latex-statistics-cookie (statistics-cookie contents info)
  1910. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  1911. CONTENTS is nil. INFO is a plist holding contextual information."
  1912. (replace-regexp-in-string
  1913. "%" "\\%" (org-element-property :value statistics-cookie) nil t))
  1914. ;;;; Strike-Through
  1915. (defun org-latex-strike-through (strike-through contents info)
  1916. "Transcode STRIKE-THROUGH from Org to LaTeX.
  1917. CONTENTS is the text with strike-through markup. INFO is a plist
  1918. holding contextual information."
  1919. (org-latex--text-markup contents 'strike-through))
  1920. ;;;; Subscript
  1921. (defun org-latex--script-size (object info)
  1922. "Transcode a subscript or superscript object.
  1923. OBJECT is an Org object. INFO is a plist used as a communication
  1924. channel."
  1925. (let ((in-script-p
  1926. ;; Non-nil if object is already in a sub/superscript.
  1927. (let ((parent object))
  1928. (catch 'exit
  1929. (while (setq parent (org-export-get-parent parent))
  1930. (let ((type (org-element-type parent)))
  1931. (cond ((memq type '(subscript superscript))
  1932. (throw 'exit t))
  1933. ((memq type org-element-all-elements)
  1934. (throw 'exit nil))))))))
  1935. (type (org-element-type object))
  1936. (output ""))
  1937. (org-element-map (org-element-contents object)
  1938. (cons 'plain-text org-element-all-objects)
  1939. (lambda (obj)
  1940. (case (org-element-type obj)
  1941. ((entity latex-fragment)
  1942. (let ((data (org-trim (org-export-data obj info))))
  1943. (string-match
  1944. "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
  1945. data)
  1946. (setq output
  1947. (concat output
  1948. (match-string 1 data)
  1949. (let ((blank (org-element-property :post-blank obj)))
  1950. (and blank (> blank 0) "\\ "))))))
  1951. (plain-text
  1952. (setq output
  1953. (format "%s\\text{%s}" output (org-export-data obj info))))
  1954. (otherwise
  1955. (setq output
  1956. (concat output
  1957. (org-export-data obj info)
  1958. (let ((blank (org-element-property :post-blank obj)))
  1959. (and blank (> blank 0) "\\ ")))))))
  1960. info nil org-element-recursive-objects)
  1961. ;; Result. Do not wrap into math mode if already in a subscript
  1962. ;; or superscript. Do not wrap into curly brackets if OUTPUT is
  1963. ;; a single character. Also merge consecutive subscript and
  1964. ;; superscript into the same math snippet.
  1965. (concat (and (not in-script-p)
  1966. (let ((prev (org-export-get-previous-element object info)))
  1967. (or (not prev)
  1968. (not (eq (org-element-type prev)
  1969. (if (eq type 'subscript) 'superscript
  1970. 'subscript)))
  1971. (let ((blank (org-element-property :post-blank prev)))
  1972. (and blank (> blank 0)))))
  1973. "$")
  1974. (if (eq (org-element-type object) 'subscript) "_" "^")
  1975. (and (> (length output) 1) "{")
  1976. output
  1977. (and (> (length output) 1) "}")
  1978. (and (not in-script-p)
  1979. (or (let ((blank (org-element-property :post-blank object)))
  1980. (and blank (> blank 0)))
  1981. (not (eq (org-element-type
  1982. (org-export-get-next-element object info))
  1983. (if (eq type 'subscript) 'superscript
  1984. 'subscript))))
  1985. "$"))))
  1986. (defun org-latex-subscript (subscript contents info)
  1987. "Transcode a SUBSCRIPT object from Org to LaTeX.
  1988. CONTENTS is the contents of the object. INFO is a plist holding
  1989. contextual information."
  1990. (org-latex--script-size subscript info))
  1991. ;;;; Superscript
  1992. (defun org-latex-superscript (superscript contents info)
  1993. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  1994. CONTENTS is the contents of the object. INFO is a plist holding
  1995. contextual information."
  1996. (org-latex--script-size superscript info))
  1997. ;;;; Table
  1998. ;;
  1999. ;; `org-latex-table' is the entry point for table transcoding. It
  2000. ;; takes care of tables with a "verbatim" mode. Otherwise, it
  2001. ;; delegates the job to either `org-latex--table.el-table',
  2002. ;; `org-latex--org-table' or `org-latex--math-table' functions,
  2003. ;; depending of the type of the table and the mode requested.
  2004. ;;
  2005. ;; `org-latex--align-string' is a subroutine used to build alignment
  2006. ;; string for Org tables.
  2007. (defun org-latex-table (table contents info)
  2008. "Transcode a TABLE element from Org to LaTeX.
  2009. CONTENTS is the contents of the table. INFO is a plist holding
  2010. contextual information."
  2011. (if (eq (org-element-property :type table) 'table.el)
  2012. ;; "table.el" table. Convert it using appropriate tools.
  2013. (org-latex--table.el-table table info)
  2014. (let ((type (or (org-export-read-attribute :attr_latex table :mode)
  2015. org-latex-default-table-mode)))
  2016. (cond
  2017. ;; Case 1: Verbatim table.
  2018. ((string= type "verbatim")
  2019. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  2020. ;; Re-create table, without affiliated keywords.
  2021. (org-trim (org-element-interpret-data
  2022. `(table nil ,@(org-element-contents table))))))
  2023. ;; Case 2: Matrix.
  2024. ((or (string= type "math") (string= type "inline-math"))
  2025. (org-latex--math-table table info))
  2026. ;; Case 3: Standard table.
  2027. (t (concat (org-latex--org-table table contents info)
  2028. ;; When there are footnote references within the
  2029. ;; table, insert their definition just after it.
  2030. (org-latex--delayed-footnotes-definitions table info)))))))
  2031. (defun org-latex--align-string (table info)
  2032. "Return an appropriate LaTeX alignment string.
  2033. TABLE is the considered table. INFO is a plist used as
  2034. a communication channel."
  2035. (or (org-export-read-attribute :attr_latex table :align)
  2036. (let (align)
  2037. ;; Extract column groups and alignment from first (non-rule)
  2038. ;; row.
  2039. (org-element-map
  2040. (org-element-map table 'table-row
  2041. (lambda (row)
  2042. (and (eq (org-element-property :type row) 'standard) row))
  2043. info 'first-match)
  2044. 'table-cell
  2045. (lambda (cell)
  2046. (let ((borders (org-export-table-cell-borders cell info)))
  2047. ;; Check left border for the first cell only.
  2048. (when (and (memq 'left borders) (not align))
  2049. (push "|" align))
  2050. (push (case (org-export-table-cell-alignment cell info)
  2051. (left "l")
  2052. (right "r")
  2053. (center "c"))
  2054. align)
  2055. (when (memq 'right borders) (push "|" align))))
  2056. info)
  2057. (apply 'concat (nreverse align)))))
  2058. (defun org-latex--org-table (table contents info)
  2059. "Return appropriate LaTeX code for an Org table.
  2060. TABLE is the table type element to transcode. CONTENTS is its
  2061. contents, as a string. INFO is a plist used as a communication
  2062. channel.
  2063. This function assumes TABLE has `org' as its `:type' property and
  2064. `table' as its `:mode' attribute."
  2065. (let* ((caption (org-latex--caption/label-string table info))
  2066. (attr (org-export-read-attribute :attr_latex table))
  2067. ;; Determine alignment string.
  2068. (alignment (org-latex--align-string table info))
  2069. ;; Determine environment for the table: longtable, tabular...
  2070. (table-env (or (plist-get attr :environment)
  2071. org-latex-default-table-environment))
  2072. ;; If table is a float, determine environment: table, table*
  2073. ;; or sidewaystable.
  2074. (float-env (unless (member table-env '("longtable" "longtabu"))
  2075. (let ((float (plist-get attr :float)))
  2076. (cond
  2077. ((and (not float) (plist-member attr :float)) nil)
  2078. ((string= float "sidewaystable") "sidewaystable")
  2079. ((string= float "multicolumn") "table*")
  2080. ((or float (org-element-property :caption table))
  2081. "table")))))
  2082. ;; Extract others display options.
  2083. (fontsize (let ((font (plist-get attr :font)))
  2084. (and font (concat font "\n"))))
  2085. (width (plist-get attr :width))
  2086. (spreadp (plist-get attr :spread))
  2087. (placement (or (plist-get attr :placement)
  2088. (format "[%s]" org-latex-default-figure-position)))
  2089. (centerp (if (plist-member attr :center) (plist-get attr :center)
  2090. org-latex-tables-centered)))
  2091. ;; Prepare the final format string for the table.
  2092. (cond
  2093. ;; Longtable.
  2094. ((equal "longtable" table-env)
  2095. (concat (and fontsize (concat "{" fontsize))
  2096. (format "\\begin{longtable}{%s}\n" alignment)
  2097. (and org-latex-table-caption-above
  2098. (org-string-nw-p caption)
  2099. (concat caption "\\\\\n"))
  2100. contents
  2101. (and (not org-latex-table-caption-above)
  2102. (org-string-nw-p caption)
  2103. (concat caption "\\\\\n"))
  2104. "\\end{longtable}\n"
  2105. (and fontsize "}")))
  2106. ;; Longtabu
  2107. ((equal "longtabu" table-env)
  2108. (concat (and fontsize (concat "{" fontsize))
  2109. (format "\\begin{longtabu}%s{%s}\n"
  2110. (if width
  2111. (format " %s %s "
  2112. (if spreadp "spread" "to") width) "")
  2113. alignment)
  2114. (and org-latex-table-caption-above
  2115. (org-string-nw-p caption)
  2116. (concat caption "\\\\\n"))
  2117. contents
  2118. (and (not org-latex-table-caption-above)
  2119. (org-string-nw-p caption)
  2120. (concat caption "\\\\\n"))
  2121. "\\end{longtabu}\n"
  2122. (and fontsize "}")))
  2123. ;; Others.
  2124. (t (concat (cond
  2125. (float-env
  2126. (concat (format "\\begin{%s}%s\n" float-env placement)
  2127. (if org-latex-table-caption-above caption "")
  2128. (when centerp "\\centering\n")
  2129. fontsize))
  2130. (centerp (concat "\\begin{center}\n" fontsize))
  2131. (fontsize (concat "{" fontsize)))
  2132. (cond ((equal "tabu" table-env)
  2133. (format "\\begin{tabu}%s{%s}\n%s\\end{tabu}"
  2134. (if width (format
  2135. (if spreadp " spread %s " " to %s ")
  2136. width) "")
  2137. alignment
  2138. contents))
  2139. (t (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
  2140. table-env
  2141. (if width (format "{%s}" width) "")
  2142. alignment
  2143. contents
  2144. table-env)))
  2145. (cond
  2146. (float-env
  2147. (concat (if org-latex-table-caption-above "" caption)
  2148. (format "\n\\end{%s}" float-env)))
  2149. (centerp "\n\\end{center}")
  2150. (fontsize "}")))))))
  2151. (defun org-latex--table.el-table (table info)
  2152. "Return appropriate LaTeX code for a table.el table.
  2153. TABLE is the table type element to transcode. INFO is a plist
  2154. used as a communication channel.
  2155. This function assumes TABLE has `table.el' as its `:type'
  2156. property."
  2157. (require 'table)
  2158. ;; Ensure "*org-export-table*" buffer is empty.
  2159. (with-current-buffer (get-buffer-create "*org-export-table*")
  2160. (erase-buffer))
  2161. (let ((output (with-temp-buffer
  2162. (insert (org-element-property :value table))
  2163. (goto-char 1)
  2164. (re-search-forward "^[ \t]*|[^|]" nil t)
  2165. (table-generate-source 'latex "*org-export-table*")
  2166. (with-current-buffer "*org-export-table*"
  2167. (org-trim (buffer-string))))))
  2168. (kill-buffer (get-buffer "*org-export-table*"))
  2169. ;; Remove left out comments.
  2170. (while (string-match "^%.*\n" output)
  2171. (setq output (replace-match "" t t output)))
  2172. (let ((attr (org-export-read-attribute :attr_latex table)))
  2173. (when (plist-get attr :rmlines)
  2174. ;; When the "rmlines" attribute is provided, remove all hlines
  2175. ;; but the the one separating heading from the table body.
  2176. (let ((n 0) (pos 0))
  2177. (while (and (< (length output) pos)
  2178. (setq pos (string-match "^\\\\hline\n?" output pos)))
  2179. (incf n)
  2180. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  2181. (let ((centerp (if (plist-member attr :center) (plist-get attr :center)
  2182. org-latex-tables-centered)))
  2183. (if (not centerp) output
  2184. (format "\\begin{center}\n%s\n\\end{center}" output))))))
  2185. (defun org-latex--math-table (table info)
  2186. "Return appropriate LaTeX code for a matrix.
  2187. TABLE is the table type element to transcode. INFO is a plist
  2188. used as a communication channel.
  2189. This function assumes TABLE has `org' as its `:type' property and
  2190. `inline-math' or `math' as its `:mode' attribute.."
  2191. (let* ((caption (org-latex--caption/label-string table info))
  2192. (attr (org-export-read-attribute :attr_latex table))
  2193. (inlinep (equal (plist-get attr :mode) "inline-math"))
  2194. (env (or (plist-get attr :environment)
  2195. org-latex-default-table-environment))
  2196. (contents
  2197. (mapconcat
  2198. (lambda (row)
  2199. ;; Ignore horizontal rules.
  2200. (when (eq (org-element-property :type row) 'standard)
  2201. ;; Return each cell unmodified.
  2202. (concat
  2203. (mapconcat
  2204. (lambda (cell)
  2205. (substring (org-element-interpret-data cell) 0 -1))
  2206. (org-element-map row 'table-cell 'identity info) "&")
  2207. (or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
  2208. "\n")))
  2209. (org-element-map table 'table-row 'identity info) ""))
  2210. ;; Variables related to math clusters (contiguous math tables
  2211. ;; of the same type).
  2212. (mode (org-export-read-attribute :attr_latex table :mode))
  2213. (prev (org-export-get-previous-element table info))
  2214. (next (org-export-get-next-element table info))
  2215. (same-mode-p
  2216. (lambda (table)
  2217. ;; Non-nil when TABLE has the same mode as current table.
  2218. (string= (or (org-export-read-attribute :attr_latex table :mode)
  2219. org-latex-default-table-mode)
  2220. mode))))
  2221. (concat
  2222. ;; Opening string. If TABLE is in the middle of a table cluster,
  2223. ;; do not insert any.
  2224. (cond ((and prev
  2225. (eq (org-element-type prev) 'table)
  2226. (memq (org-element-property :post-blank prev) '(0 nil))
  2227. (funcall same-mode-p prev))
  2228. nil)
  2229. (inlinep "\\(")
  2230. ((org-string-nw-p caption) (concat "\\begin{equation}\n" caption))
  2231. (t "\\["))
  2232. ;; Prefix.
  2233. (or (plist-get attr :math-prefix) "")
  2234. ;; Environment. Also treat special cases.
  2235. (cond ((equal env "array")
  2236. (let ((align (org-latex--align-string table info)))
  2237. (format "\\begin{array}{%s}\n%s\\end{array}" align contents)))
  2238. ((assoc env org-latex-table-matrix-macros)
  2239. (format "\\%s%s{\n%s}"
  2240. env
  2241. (or (plist-get attr :math-arguments) "")
  2242. contents))
  2243. (t (format "\\begin{%s}\n%s\\end{%s}" env contents env)))
  2244. ;; Suffix.
  2245. (or (plist-get attr :math-suffix) "")
  2246. ;; Closing string. If TABLE is in the middle of a table cluster,
  2247. ;; do not insert any. If it closes such a cluster, be sure to
  2248. ;; close the cluster with a string matching the opening string.
  2249. (cond ((and next
  2250. (eq (org-element-type next) 'table)
  2251. (memq (org-element-property :post-blank table) '(0 nil))
  2252. (funcall same-mode-p next))
  2253. nil)
  2254. (inlinep "\\)")
  2255. ;; Find cluster beginning to know which environment to use.
  2256. ((let ((cluster-beg table) prev)
  2257. (while (and (setq prev (org-export-get-previous-element
  2258. cluster-beg info))
  2259. (memq (org-element-property :post-blank prev)
  2260. '(0 nil))
  2261. (funcall same-mode-p prev))
  2262. (setq cluster-beg prev))
  2263. (and (or (org-element-property :caption cluster-beg)
  2264. (org-element-property :name cluster-beg))
  2265. "\n\\end{equation}")))
  2266. (t "\\]")))))
  2267. ;;;; Table Cell
  2268. (defun org-latex-table-cell (table-cell contents info)
  2269. "Transcode a TABLE-CELL element from Org to LaTeX.
  2270. CONTENTS is the cell contents. INFO is a plist used as
  2271. a communication channel."
  2272. (concat (if (and contents
  2273. org-latex-table-scientific-notation
  2274. (string-match orgtbl-exp-regexp contents))
  2275. ;; Use appropriate format string for scientific
  2276. ;; notation.
  2277. (format org-latex-table-scientific-notation
  2278. (match-string 1 contents)
  2279. (match-string 2 contents))
  2280. contents)
  2281. (when (org-export-get-next-element table-cell info) " & ")))
  2282. ;;;; Table Row
  2283. (defun org-latex-table-row (table-row contents info)
  2284. "Transcode a TABLE-ROW element from Org to LaTeX.
  2285. CONTENTS is the contents of the row. INFO is a plist used as
  2286. a communication channel."
  2287. ;; Rules are ignored since table separators are deduced from
  2288. ;; borders of the current row.
  2289. (when (eq (org-element-property :type table-row) 'standard)
  2290. (let* ((attr (org-export-read-attribute :attr_latex
  2291. (org-export-get-parent table-row)))
  2292. (longtablep (member (or (plist-get attr :environment)
  2293. org-latex-default-table-environment)
  2294. '("longtable" "longtabu")))
  2295. (booktabsp (if (plist-member attr :booktabs)
  2296. (plist-get attr :booktabs)
  2297. org-latex-tables-booktabs))
  2298. ;; TABLE-ROW's borders are extracted from its first cell.
  2299. (borders (org-export-table-cell-borders
  2300. (car (org-element-contents table-row)) info)))
  2301. (concat
  2302. ;; When BOOKTABS are activated enforce top-rule even when no
  2303. ;; hline was specifically marked.
  2304. (cond ((and booktabsp (memq 'top borders)) "\\toprule\n")
  2305. ((and (memq 'top borders) (memq 'above borders)) "\\hline\n"))
  2306. contents "\\\\\n"
  2307. (cond
  2308. ;; Special case for long tables. Define header and footers.
  2309. ((and longtablep (org-export-table-row-ends-header-p table-row info))
  2310. (format "%s
  2311. \\endhead
  2312. %s\\multicolumn{%d}{r}{Continued on next page} \\\\
  2313. \\endfoot
  2314. \\endlastfoot"
  2315. (if booktabsp "\\midrule" "\\hline")
  2316. (if booktabsp "\\midrule" "\\hline")
  2317. ;; Number of columns.
  2318. (cdr (org-export-table-dimensions
  2319. (org-export-get-parent-table table-row) info))))
  2320. ;; When BOOKTABS are activated enforce bottom rule even when
  2321. ;; no hline was specifically marked.
  2322. ((and booktabsp (memq 'bottom borders)) "\\bottomrule")
  2323. ((and (memq 'bottom borders) (memq 'below borders)) "\\hline")
  2324. ((memq 'below borders) (if booktabsp "\\midrule" "\\hline")))))))
  2325. ;;;; Target
  2326. (defun org-latex-target (target contents info)
  2327. "Transcode a TARGET object from Org to LaTeX.
  2328. CONTENTS is nil. INFO is a plist holding contextual
  2329. information."
  2330. (format "\\label{%s}"
  2331. (org-export-solidify-link-text (org-element-property :value target))))
  2332. ;;;; Timestamp
  2333. (defun org-latex-timestamp (timestamp contents info)
  2334. "Transcode a TIMESTAMP object from Org to LaTeX.
  2335. CONTENTS is nil. INFO is a plist holding contextual
  2336. information."
  2337. (let ((value (org-latex-plain-text
  2338. (org-timestamp-translate timestamp) info)))
  2339. (case (org-element-property :type timestamp)
  2340. ((active active-range) (format org-latex-active-timestamp-format value))
  2341. ((inactive inactive-range)
  2342. (format org-latex-inactive-timestamp-format value))
  2343. (otherwise (format org-latex-diary-timestamp-format value)))))
  2344. ;;;; Underline
  2345. (defun org-latex-underline (underline contents info)
  2346. "Transcode UNDERLINE from Org to LaTeX.
  2347. CONTENTS is the text with underline markup. INFO is a plist
  2348. holding contextual information."
  2349. (org-latex--text-markup contents 'underline))
  2350. ;;;; Verbatim
  2351. (defun org-latex-verbatim (verbatim contents info)
  2352. "Transcode a VERBATIM object from Org to LaTeX.
  2353. CONTENTS is nil. INFO is a plist used as a communication
  2354. channel."
  2355. (org-latex--text-markup (org-element-property :value verbatim) 'verbatim))
  2356. ;;;; Verse Block
  2357. (defun org-latex-verse-block (verse-block contents info)
  2358. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  2359. CONTENTS is verse block contents. INFO is a plist holding
  2360. contextual information."
  2361. (org-latex--wrap-label
  2362. verse-block
  2363. ;; In a verse environment, add a line break to each newline
  2364. ;; character and change each white space at beginning of a line
  2365. ;; into a space of 1 em. Also change each blank line with
  2366. ;; a vertical space of 1 em.
  2367. (progn
  2368. (setq contents (replace-regexp-in-string
  2369. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  2370. (replace-regexp-in-string
  2371. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
  2372. (while (string-match "^[ \t]+" contents)
  2373. (let ((new-str (format "\\hspace*{%dem}"
  2374. (length (match-string 0 contents)))))
  2375. (setq contents (replace-match new-str nil t contents))))
  2376. (format "\\begin{verse}\n%s\\end{verse}" contents))))
  2377. ;;; End-user functions
  2378. ;;;###autoload
  2379. (defun org-latex-export-as-latex
  2380. (&optional async subtreep visible-only body-only ext-plist)
  2381. "Export current buffer as a LaTeX buffer.
  2382. If narrowing is active in the current buffer, only export its
  2383. narrowed part.
  2384. If a region is active, export that region.
  2385. A non-nil optional argument ASYNC means the process should happen
  2386. asynchronously. The resulting buffer should be accessible
  2387. through the `org-export-stack' interface.
  2388. When optional argument SUBTREEP is non-nil, export the sub-tree
  2389. at point, extracting information from the headline properties
  2390. first.
  2391. When optional argument VISIBLE-ONLY is non-nil, don't export
  2392. contents of hidden elements.
  2393. When optional argument BODY-ONLY is non-nil, only write code
  2394. between \"\\begin{document}\" and \"\\end{document}\".
  2395. EXT-PLIST, when provided, is a property list with external
  2396. parameters overriding Org default settings, but still inferior to
  2397. file-local settings.
  2398. Export is done in a buffer named \"*Org LATEX Export*\", which
  2399. will be displayed when `org-export-show-temporary-export-buffer'
  2400. is non-nil."
  2401. (interactive)
  2402. (if async
  2403. (org-export-async-start
  2404. (lambda (output)
  2405. (with-current-buffer (get-buffer-create "*Org LATEX Export*")
  2406. (erase-buffer)
  2407. (insert output)
  2408. (goto-char (point-min))
  2409. (LaTeX-mode)
  2410. (org-export-add-to-stack (current-buffer) 'latex)))
  2411. `(org-export-as 'latex ,subtreep ,visible-only ,body-only
  2412. ',ext-plist))
  2413. (let ((outbuf
  2414. (org-export-to-buffer 'latex "*Org LATEX Export*"
  2415. subtreep visible-only body-only ext-plist)))
  2416. (with-current-buffer outbuf (LaTeX-mode))
  2417. (when org-export-show-temporary-export-buffer
  2418. (switch-to-buffer-other-window outbuf)))))
  2419. ;;;###autoload
  2420. (defun org-latex-convert-region-to-latex ()
  2421. "Assume the current region has org-mode syntax, and convert it to LaTeX.
  2422. This can be used in any buffer. For example, you can write an
  2423. itemized list in org-mode syntax in an LaTeX buffer and use this
  2424. command to convert it."
  2425. (interactive)
  2426. (org-export-replace-region-by 'latex))
  2427. ;;;###autoload
  2428. (defun org-latex-export-to-latex
  2429. (&optional async subtreep visible-only body-only ext-plist)
  2430. "Export current buffer to a LaTeX file.
  2431. If narrowing is active in the current buffer, only export its
  2432. narrowed part.
  2433. If a region is active, export that region.
  2434. A non-nil optional argument ASYNC means the process should happen
  2435. asynchronously. The resulting file should be accessible through
  2436. the `org-export-stack' interface.
  2437. When optional argument SUBTREEP is non-nil, export the sub-tree
  2438. at point, extracting information from the headline properties
  2439. first.
  2440. When optional argument VISIBLE-ONLY is non-nil, don't export
  2441. contents of hidden elements.
  2442. When optional argument BODY-ONLY is non-nil, only write code
  2443. between \"\\begin{document}\" and \"\\end{document}\".
  2444. EXT-PLIST, when provided, is a property list with external
  2445. parameters overriding Org default settings, but still inferior to
  2446. file-local settings.
  2447. Return output file's name."
  2448. (interactive)
  2449. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  2450. (if async
  2451. (org-export-async-start
  2452. (lambda (f) (org-export-add-to-stack f 'latex))
  2453. `(expand-file-name
  2454. (org-export-to-file
  2455. 'latex ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))
  2456. (org-export-to-file
  2457. 'latex outfile subtreep visible-only body-only ext-plist))))
  2458. ;;;###autoload
  2459. (defun org-latex-export-to-pdf
  2460. (&optional async subtreep visible-only body-only ext-plist)
  2461. "Export current buffer to LaTeX then process through to PDF.
  2462. If narrowing is active in the current buffer, only export its
  2463. narrowed part.
  2464. If a region is active, export that region.
  2465. A non-nil optional argument ASYNC means the process should happen
  2466. asynchronously. The resulting file should be accessible through
  2467. the `org-export-stack' interface.
  2468. When optional argument SUBTREEP is non-nil, export the sub-tree
  2469. at point, extracting information from the headline properties
  2470. first.
  2471. When optional argument VISIBLE-ONLY is non-nil, don't export
  2472. contents of hidden elements.
  2473. When optional argument BODY-ONLY is non-nil, only write code
  2474. between \"\\begin{document}\" and \"\\end{document}\".
  2475. EXT-PLIST, when provided, is a property list with external
  2476. parameters overriding Org default settings, but still inferior to
  2477. file-local settings.
  2478. Return PDF file's name."
  2479. (interactive)
  2480. (if async
  2481. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  2482. (org-export-async-start
  2483. (lambda (f) (org-export-add-to-stack f 'latex))
  2484. `(expand-file-name
  2485. (org-latex-compile
  2486. (org-export-to-file
  2487. 'latex ,outfile ,subtreep ,visible-only ,body-only
  2488. ',ext-plist)))))
  2489. (org-latex-compile
  2490. (org-latex-export-to-latex
  2491. nil subtreep visible-only body-only ext-plist))))
  2492. (defun org-latex-compile (texfile &optional snippet)
  2493. "Compile a TeX file.
  2494. TEXFILE is the name of the file being compiled. Processing is
  2495. done through the command specified in `org-latex-pdf-process'.
  2496. When optional argument SNIPPET is non-nil, TEXFILE is a temporary
  2497. file used to preview a LaTeX snippet. In this case, do not
  2498. create a log buffer and do not bother removing log files.
  2499. Return PDF file name or an error if it couldn't be produced."
  2500. (let* ((base-name (file-name-sans-extension (file-name-nondirectory texfile)))
  2501. (full-name (file-truename texfile))
  2502. (out-dir (file-name-directory texfile))
  2503. ;; Properly set working directory for compilation.
  2504. (default-directory (if (file-name-absolute-p texfile)
  2505. (file-name-directory full-name)
  2506. default-directory))
  2507. errors)
  2508. (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
  2509. (save-window-excursion
  2510. (cond
  2511. ;; A function is provided: Apply it.
  2512. ((functionp org-latex-pdf-process)
  2513. (funcall org-latex-pdf-process (shell-quote-argument texfile)))
  2514. ;; A list is provided: Replace %b, %f and %o with appropriate
  2515. ;; values in each command before applying it. Output is
  2516. ;; redirected to "*Org PDF LaTeX Output*" buffer.
  2517. ((consp org-latex-pdf-process)
  2518. (let ((outbuf (and (not snippet)
  2519. (get-buffer-create "*Org PDF LaTeX Output*"))))
  2520. (mapc
  2521. (lambda (command)
  2522. (shell-command
  2523. (replace-regexp-in-string
  2524. "%b" (shell-quote-argument base-name)
  2525. (replace-regexp-in-string
  2526. "%f" (shell-quote-argument full-name)
  2527. (replace-regexp-in-string
  2528. "%o" (shell-quote-argument out-dir) command t t) t t) t t)
  2529. outbuf))
  2530. org-latex-pdf-process)
  2531. ;; Collect standard errors from output buffer.
  2532. (setq errors (and (not snippet) (org-latex--collect-errors outbuf)))))
  2533. (t (error "No valid command to process to PDF")))
  2534. (let ((pdffile (concat out-dir base-name ".pdf")))
  2535. ;; Check for process failure. Provide collected errors if
  2536. ;; possible.
  2537. (if (not (file-exists-p pdffile))
  2538. (error (concat (format "PDF file %s wasn't produced" pdffile)
  2539. (when errors (concat ": " errors))))
  2540. ;; Else remove log files, when specified, and signal end of
  2541. ;; process to user, along with any error encountered.
  2542. (when (and (not snippet) org-latex-remove-logfiles)
  2543. (dolist (ext org-latex-logfiles-extensions)
  2544. (let ((file (concat out-dir base-name "." ext)))
  2545. (when (file-exists-p file) (delete-file file)))))
  2546. (message (concat "Process completed"
  2547. (if (not errors) "."
  2548. (concat " with errors: " errors)))))
  2549. ;; Return output file name.
  2550. pdffile))))
  2551. (defun org-latex--collect-errors (buffer)
  2552. "Collect some kind of errors from \"pdflatex\" command output.
  2553. BUFFER is the buffer containing output.
  2554. Return collected error types as a string, or nil if there was
  2555. none."
  2556. (with-current-buffer buffer
  2557. (save-excursion
  2558. (goto-char (point-max))
  2559. (when (re-search-backward "^[ \t]*This is .*?TeX.*?Version" nil t)
  2560. (let ((case-fold-search t)
  2561. (errors ""))
  2562. (dolist (latex-error org-latex-known-errors)
  2563. (when (save-excursion (re-search-forward (car latex-error) nil t))
  2564. (setq errors (concat errors " " (cdr latex-error)))))
  2565. (and (org-string-nw-p errors) (org-trim errors)))))))
  2566. ;;;###autoload
  2567. (defun org-latex-publish-to-latex (plist filename pub-dir)
  2568. "Publish an Org file to LaTeX.
  2569. FILENAME is the filename of the Org file to be published. PLIST
  2570. is the property list for the given project. PUB-DIR is the
  2571. publishing directory.
  2572. Return output file name."
  2573. (org-publish-org-to 'latex filename ".tex" plist pub-dir))
  2574. ;;;###autoload
  2575. (defun org-latex-publish-to-pdf (plist filename pub-dir)
  2576. "Publish an Org file to PDF (via LaTeX).
  2577. FILENAME is the filename of the Org file to be published. PLIST
  2578. is the property list for the given project. PUB-DIR is the
  2579. publishing directory.
  2580. Return output file name."
  2581. ;; Unlike to `org-latex-publish-to-latex', PDF file is generated
  2582. ;; in working directory and then moved to publishing directory.
  2583. (org-publish-attachment
  2584. plist
  2585. (org-latex-compile (org-publish-org-to 'latex filename ".tex" plist))
  2586. pub-dir))
  2587. (provide 'ox-latex)
  2588. ;; Local variables:
  2589. ;; generated-autoload-file: "org-loaddefs.el"
  2590. ;; End:
  2591. ;;; ox-latex.el ends here