ox-latex.el 110 KB

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