ox-latex.el 115 KB

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