ox-latex.el 106 KB

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