ox-latex.el 106 KB

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