ox-latex.el 106 KB

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