org-e-latex.el 95 KB

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