ox-latex.el 103 KB

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