ox-latex.el 103 KB

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