ox-latex.el 112 KB

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