ox-latex.el 104 KB

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