ox-latex.el 118 KB

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