ox-latex.el 106 KB

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