ox-latex.el 108 KB

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