ox-latex.el 118 KB

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