ox-latex.el 104 KB

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