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