ox-latex.el 103 KB

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