ox-latex.el 107 KB

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