org-e-latex.el 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079
  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. ;; This library implements a LaTeX back-end for Org generic exporter.
  17. ;; To test it, run
  18. ;;
  19. ;; M-: (org-export-to-buffer 'e-latex "*Test e-LaTeX*") RET
  20. ;;
  21. ;; in an org-mode buffer then switch to the buffer to see the LaTeX
  22. ;; export. See contrib/lisp/org-export.el for more details on how
  23. ;; this exporter works.
  24. ;; It introduces three new buffer keywords: "LATEX_CLASS",
  25. ;; "LATEX_CLASS_OPTIONS" and "LATEX_HEADER".
  26. ;;; Code:
  27. (eval-when-compile (require 'cl))
  28. (require 'org-element)
  29. (require 'org-export)
  30. ;;; Internal Variables
  31. (defconst org-e-latex-option-alist
  32. '((:date "DATE" nil org-e-latex-date-format t)
  33. (:latex-class "LATEX_CLASS" nil org-e-latex-default-class t)
  34. (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
  35. (:latex-header-extra "LATEX_HEADER" nil nil newline))
  36. "Alist between LaTeX export properties and ways to set them.
  37. See `org-export-option-alist' for more information on the
  38. structure of the value.")
  39. ;;; User Configurable Variables
  40. (defgroup org-export-e-latex nil
  41. "Options for exporting Org mode files to LaTeX."
  42. :tag "Org Export LaTeX"
  43. :group 'org-export)
  44. ;;;; Preamble
  45. (defcustom org-e-latex-default-class "article"
  46. "The default LaTeX class."
  47. :group 'org-export-e-latex
  48. :type '(string :tag "LaTeX class"))
  49. (defcustom org-e-latex-classes
  50. '(("article"
  51. "\\documentclass[11pt]{article}"
  52. ("\\section{%s}" . "\\section*{%s}")
  53. ("\\subsection{%s}" . "\\subsection*{%s}")
  54. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
  55. ("\\paragraph{%s}" . "\\paragraph*{%s}")
  56. ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
  57. ("report"
  58. "\\documentclass[11pt]{report}"
  59. ("\\part{%s}" . "\\part*{%s}")
  60. ("\\chapter{%s}" . "\\chapter*{%s}")
  61. ("\\section{%s}" . "\\section*{%s}")
  62. ("\\subsection{%s}" . "\\subsection*{%s}")
  63. ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
  64. ("book"
  65. "\\documentclass[11pt]{book}"
  66. ("\\part{%s}" . "\\part*{%s}")
  67. ("\\chapter{%s}" . "\\chapter*{%s}")
  68. ("\\section{%s}" . "\\section*{%s}")
  69. ("\\subsection{%s}" . "\\subsection*{%s}")
  70. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
  71. "Alist of LaTeX classes and associated header and structure.
  72. If #+LaTeX_CLASS is set in the buffer, use its value and the
  73. associated information. Here is the structure of each cell:
  74. \(class-name
  75. header-string
  76. \(numbered-section . unnumbered-section\)
  77. ...\)
  78. The header string
  79. -----------------
  80. The HEADER-STRING is the header that will be inserted into the LaTeX file.
  81. It should contain the \\documentclass macro, and anything else that is needed
  82. for this setup. To this header, the following commands will be added:
  83. - Calls to \\usepackage for all packages mentioned in the variables
  84. `org-e-latex-default-packages-alist' and
  85. `org-e-latex-packages-alist'. Thus, your header definitions should
  86. avoid to also request these packages.
  87. - Lines specified via \"#+LaTeX_HEADER:\"
  88. If you need more control about the sequence in which the header is built
  89. up, or if you want to exclude one of these building blocks for a particular
  90. class, you can use the following macro-like placeholders.
  91. [DEFAULT-PACKAGES] \\usepackage statements for default packages
  92. [NO-DEFAULT-PACKAGES] do not include any of the default packages
  93. [PACKAGES] \\usepackage statements for packages
  94. [NO-PACKAGES] do not include the packages
  95. [EXTRA] the stuff from #+LaTeX_HEADER
  96. [NO-EXTRA] do not include #+LaTeX_HEADER stuff
  97. [BEAMER-HEADER-EXTRA] the beamer extra headers
  98. So a header like
  99. \\documentclass{article}
  100. [NO-DEFAULT-PACKAGES]
  101. [EXTRA]
  102. \\providecommand{\\alert}[1]{\\textbf{#1}}
  103. [PACKAGES]
  104. will omit the default packages, and will include the #+LaTeX_HEADER lines,
  105. then have a call to \\providecommand, and then place \\usepackage commands
  106. based on the content of `org-e-latex-packages-alist'.
  107. If your header or `org-e-latex-default-packages-alist' inserts
  108. \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be replaced with
  109. a coding system derived from `buffer-file-coding-system'. See also the
  110. variable `org-e-latex-inputenc-alist' for a way to influence this
  111. mechanism.
  112. The sectioning structure
  113. ------------------------
  114. The sectioning structure of the class is given by the elements following
  115. the header string. For each sectioning level, a number of strings is
  116. specified. A %s formatter is mandatory in each section string and will
  117. be replaced by the title of the section.
  118. Instead of a cons cell \(numbered . unnumbered\), you can also
  119. provide a list of 2 or 4 elements,
  120. \(numbered-open numbered-close\)
  121. or
  122. \(numbered-open numbered-close unnumbered-open unnumbered-close\)
  123. providing opening and closing strings for a LaTeX environment that should
  124. represent the document section. The opening clause should have a %s
  125. to represent the section title.
  126. Instead of a list of sectioning commands, you can also specify a
  127. function name. That function will be called with two parameters,
  128. the (reduced) level of the headline, and a predicate non-nil when
  129. the headline should be numbered. It must return a format string in
  130. which the section title will be added."
  131. :group 'org-export-e-latex
  132. :type '(repeat
  133. (list (string :tag "LaTeX class")
  134. (string :tag "LaTeX header")
  135. (repeat :tag "Levels" :inline t
  136. (choice
  137. (cons :tag "Heading"
  138. (string :tag " numbered")
  139. (string :tag "unnumbered"))
  140. (list :tag "Environment"
  141. (string :tag "Opening (numbered)")
  142. (string :tag "Closing (numbered)")
  143. (string :tag "Opening (unnumbered)")
  144. (string :tag "Closing (unnumbered)"))
  145. (function :tag "Hook computing sectioning"))))))
  146. (defcustom org-e-latex-inputenc-alist nil
  147. "Alist of inputenc coding system names, and what should really be used.
  148. For example, adding an entry
  149. (\"utf8\" . \"utf8x\")
  150. will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
  151. are written as utf8 files."
  152. :group 'org-export-e-latex
  153. :type '(repeat
  154. (cons
  155. (string :tag "Derived from buffer")
  156. (string :tag "Use this instead"))))
  157. (defcustom org-e-latex-date-format
  158. "\\today"
  159. "Format string for \\date{...}."
  160. :group 'org-export-e-latex
  161. :type 'boolean)
  162. (defcustom org-e-latex-title-command "\\maketitle"
  163. "The command used to insert the title just after \\begin{document}.
  164. If this string contains the formatting specification \"%s\" then
  165. it will be used as a formatting string, passing the title as an
  166. argument."
  167. :group 'org-export-e-latex
  168. :type 'string)
  169. ;;;; Headline
  170. (defcustom org-e-latex-format-headline-function nil
  171. "Function to format headline text.
  172. This function will be called with 5 arguments:
  173. TODO the todo keyword \(string or nil\).
  174. TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
  175. PRIORITY the priority of the headline \(integer or nil\)
  176. TEXT the main headline text \(string\).
  177. TAGS the tags string, separated with colons \(string or nil\).
  178. The function result will be used in the section format string.
  179. As an example, one could set the variable to the following, in
  180. order to reproduce the default set-up:
  181. \(defun org-e-latex-format-headline \(todo todo-type priority text tags\)
  182. \"Default format function for an headline.\"
  183. \(concat \(when todo
  184. \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
  185. \(when priority
  186. \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  187. text
  188. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
  189. :group 'org-export-e-latex
  190. :type 'function)
  191. ;;;; Emphasis
  192. (defcustom org-e-latex-emphasis-alist
  193. '(("*" . "\\textbf{%s}")
  194. ("/" . "\\emph{%s}")
  195. ("_" . "\\underline{%s}")
  196. ("+" . "\\st{%s}")
  197. ("=" . protectedtexttt)
  198. ("~" . verb))
  199. "Alist of LaTeX expressions to convert emphasis fontifiers.
  200. The key is the character used as a marker for fontification. The
  201. value is a formatting string to wrap fontified text with.
  202. Value can also be set to the following symbols: `verb' and
  203. `protectedtexttt'. For the former, Org will use \"\\verb\" to
  204. create a format string and select a delimiter character that
  205. isn't in the string. For the latter, Org will use \"\\texttt\"
  206. to typeset and try to protect special characters."
  207. :group 'org-export-e-latex
  208. :type 'alist)
  209. ;;;; Footnotes
  210. (defcustom org-e-latex-footnote-separator "\\textsuperscript{,}\\,"
  211. "Text used to separate footnotes."
  212. :group 'org-export-e-latex
  213. :type 'string)
  214. ;;;; Time-stamps
  215. (defcustom org-e-latex-active-timestamp-format "\\textit{%s}"
  216. "A printf format string to be applied to active time-stamps."
  217. :group 'org-export-e-latex
  218. :type 'string)
  219. (defcustom org-e-latex-inactive-timestamp-format "\\textit{%s}"
  220. "A printf format string to be applied to inactive time-stamps."
  221. :group 'org-export-e-latex
  222. :type 'string)
  223. (defcustom org-e-latex-diary-timestamp-format "\\textit{%s}"
  224. "A printf format string to be applied to diary time-stamps."
  225. :group 'org-export-e-latex
  226. :type 'string)
  227. ;;;; Links
  228. (defcustom org-e-latex-image-default-option "width=.9\\linewidth"
  229. "Default option for images."
  230. :group 'org-export-e-latex
  231. :type 'string)
  232. (defcustom org-e-latex-default-figure-position "htb"
  233. "Default position for latex figures."
  234. :group 'org-export-e-latex
  235. :type 'string)
  236. (defcustom org-e-latex-inline-image-extensions
  237. '("pdf" "jpeg" "jpg" "png" "ps" "eps")
  238. "Extensions of image files that can be inlined into LaTeX.
  239. Note that the image extension *actually* allowed depend on the
  240. way the LaTeX file is processed. When used with pdflatex, pdf,
  241. jpg and png images are OK. When processing through dvi to
  242. Postscript, only ps and eps are allowed. The default we use here
  243. encompasses both."
  244. :group 'org-export-e-latex
  245. :type '(repeat (string :tag "Extension")))
  246. ;;;; Tables
  247. (defcustom org-e-latex-default-table-environment "tabular"
  248. "Default environment used to build tables."
  249. :group 'org-export-e-latex
  250. :type 'string)
  251. (defcustom org-e-latex-tables-centered t
  252. "When non-nil, tables are exported in a center environment."
  253. :group 'org-export-e-latex
  254. :type 'boolean)
  255. (defcustom org-e-latex-tables-verbatim nil
  256. "When non-nil, tables are exported verbatim."
  257. :group 'org-export-e-latex
  258. :type 'boolean)
  259. (defcustom org-e-latex-table-caption-above t
  260. "When non-nil, place caption string at the beginning of the table.
  261. Otherwise, place it near the end."
  262. :group 'org-export-e-latex
  263. :type 'boolean)
  264. ;;;; Drawers
  265. (defcustom org-e-latex-format-drawer-function nil
  266. "Function called to format a drawer in LaTeX code.
  267. The function must accept two parameters:
  268. NAME the drawer name, like \"LOGBOOK\"
  269. CONTENTS the contents of the drawer.
  270. The function should return the string to be exported.
  271. For example, the variable could be set to the following function
  272. in order to mimic default behaviour:
  273. \(defun org-e-latex-format-drawer-default \(name contents\)
  274. \"Format a drawer element for LaTeX export.\"
  275. contents\)"
  276. :group 'org-export-e-latex
  277. :type 'function)
  278. ;;;; Inlinetasks
  279. (defcustom org-e-latex-format-inlinetask-function nil
  280. "Function called to format an inlinetask in LaTeX code.
  281. The function must accept six parameters:
  282. TODO the todo keyword, as a string
  283. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  284. PRIORITY the inlinetask priority, as a string
  285. NAME the inlinetask name, as a string.
  286. TAGS the inlinetask tags, as a string.
  287. CONTENTS the contents of the inlinetask, as a string.
  288. The function should return the string to be exported.
  289. For example, the variable could be set to the following function
  290. in order to mimic default behaviour:
  291. \(defun org-e-latex-format-inlinetask \(todo type priority name tags contents\)
  292. \"Format an inline task element for LaTeX export.\"
  293. \(let \(\(full-title
  294. \(concat
  295. \(when todo
  296. \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
  297. \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  298. title
  299. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
  300. \(format \(concat \"\\\\begin{center}\\n\"
  301. \"\\\\fbox{\\n\"
  302. \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
  303. \"%s\\n\\n\"
  304. \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
  305. \"%s\"
  306. \"\\\\end{minipage}}\"
  307. \"\\\\end{center}\"\)
  308. full-title contents\)\)"
  309. :group 'org-export-e-latex
  310. :type 'function)
  311. ;; Src blocks
  312. (defcustom org-e-latex-listings nil
  313. "Non-nil means export source code using the listings package.
  314. This package will fontify source code, possibly even with color.
  315. If you want to use this, you also need to make LaTeX use the
  316. listings package, and if you want to have color, the color
  317. package. Just add these to `org-e-latex-packages-alist',
  318. for example using customize, or with something like
  319. (require 'org-e-latex)
  320. (add-to-list 'org-e-latex-packages-alist '(\"\" \"listings\"))
  321. (add-to-list 'org-e-latex-packages-alist '(\"\" \"color\"))
  322. Alternatively,
  323. (setq org-e-latex-listings 'minted)
  324. causes source code to be exported using the minted package as
  325. opposed to listings. If you want to use minted, you need to add
  326. the minted package to `org-e-latex-packages-alist', for
  327. example using customize, or with
  328. (require 'org-e-latex)
  329. (add-to-list 'org-e-latex-packages-alist '(\"\" \"minted\"))
  330. In addition, it is necessary to install
  331. pygments (http://pygments.org), and to configure the variable
  332. `org-e-latex-to-pdf-process' so that the -shell-escape option is
  333. passed to pdflatex."
  334. :group 'org-export-e-latex
  335. :type '(choice
  336. (const :tag "Use listings" t)
  337. (const :tag "Use minted" 'minted)
  338. (const :tag "Export verbatim" nil)))
  339. (defcustom org-e-latex-listings-langs
  340. '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
  341. (c "C") (cc "C++")
  342. (fortran "fortran")
  343. (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
  344. (html "HTML") (xml "XML")
  345. (tex "TeX") (latex "TeX")
  346. (shell-script "bash")
  347. (gnuplot "Gnuplot")
  348. (ocaml "Caml") (caml "Caml")
  349. (sql "SQL") (sqlite "sql"))
  350. "Alist mapping languages to their listing language counterpart.
  351. The key is a symbol, the major mode symbol without the \"-mode\".
  352. The value is the string that should be inserted as the language parameter
  353. for the listings package. If the mode name and the listings name are
  354. the same, the language does not need an entry in this list - but it does not
  355. hurt if it is present."
  356. :group 'org-export-e-latex
  357. :type '(repeat
  358. (list
  359. (symbol :tag "Major mode ")
  360. (string :tag "Listings language"))))
  361. (defcustom org-e-latex-listings-options nil
  362. "Association list of options for the latex listings package.
  363. These options are supplied as a comma-separated list to the
  364. \\lstset command. Each element of the association list should be
  365. a list containing two strings: the name of the option, and the
  366. value. For example,
  367. (setq org-e-latex-listings-options
  368. '((\"basicstyle\" \"\\small\")
  369. (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
  370. will typeset the code in a small size font with underlined, bold
  371. black keywords.
  372. Note that the same options will be applied to blocks of all
  373. languages."
  374. :group 'org-export-e-latex
  375. :type '(repeat
  376. (list
  377. (string :tag "Listings option name ")
  378. (string :tag "Listings option value"))))
  379. (defcustom org-e-latex-minted-langs
  380. '((emacs-lisp "common-lisp")
  381. (cc "c++")
  382. (cperl "perl")
  383. (shell-script "bash")
  384. (caml "ocaml"))
  385. "Alist mapping languages to their minted language counterpart.
  386. The key is a symbol, the major mode symbol without the \"-mode\".
  387. The value is the string that should be inserted as the language parameter
  388. for the minted package. If the mode name and the listings name are
  389. the same, the language does not need an entry in this list - but it does not
  390. hurt if it is present.
  391. Note that minted uses all lower case for language identifiers,
  392. and that the full list of language identifiers can be obtained
  393. with:
  394. pygmentize -L lexers"
  395. :group 'org-export-e-latex
  396. :type '(repeat
  397. (list
  398. (symbol :tag "Major mode ")
  399. (string :tag "Minted language"))))
  400. (defcustom org-e-latex-minted-options nil
  401. "Association list of options for the latex minted package.
  402. These options are supplied within square brackets in
  403. \\begin{minted} environments. Each element of the alist should
  404. be a list containing two strings: the name of the option, and the
  405. value. For example,
  406. (setq org-e-latex-minted-options
  407. '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
  408. will result in src blocks being exported with
  409. \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
  410. as the start of the minted environment. Note that the same
  411. options will be applied to blocks of all languages."
  412. :group 'org-export-e-latex
  413. :type '(repeat
  414. (list
  415. (string :tag "Minted option name ")
  416. (string :tag "Minted option value"))))
  417. (defvar org-e-latex-custom-lang-environments nil
  418. "Alist mapping languages to language-specific LaTeX environments.
  419. It is used during export of src blocks by the listings and minted
  420. latex packages. For example,
  421. \(setq org-e-latex-custom-lang-environments
  422. '\(\(python \"pythoncode\"\)\)\)
  423. would have the effect that if org encounters begin_src python
  424. during latex export it will output
  425. \\begin{pythoncode}
  426. <src block body>
  427. \\end{pythoncode}")
  428. ;;;; Plain text
  429. (defcustom org-e-latex-quotes
  430. '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
  431. ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
  432. "Alist for quotes to use when converting english double-quotes.
  433. The CAR of each item in this alist is the language code.
  434. The CDR of each item in this alist is a list of three CONS:
  435. - the first CONS defines the opening quote;
  436. - the second CONS defines the closing quote;
  437. - the last CONS defines single quotes.
  438. For each item in a CONS, the first string is a regexp
  439. for allowed characters before/after the quote, the second
  440. string defines the replacement string for this quote."
  441. :group 'org-export-e-latex
  442. :type '(list
  443. (cons :tag "Opening quote"
  444. (string :tag "Regexp for char before")
  445. (string :tag "Replacement quote "))
  446. (cons :tag "Closing quote"
  447. (string :tag "Regexp for char after ")
  448. (string :tag "Replacement quote "))
  449. (cons :tag "Single quote"
  450. (string :tag "Regexp for char before")
  451. (string :tag "Replacement quote "))))
  452. ;;;; Compilation
  453. (defcustom org-e-latex-pdf-process
  454. '("pdflatex -interaction nonstopmode -output-directory %o %f"
  455. "pdflatex -interaction nonstopmode -output-directory %o %f"
  456. "pdflatex -interaction nonstopmode -output-directory %o %f")
  457. "Commands to process a LaTeX file to a PDF file.
  458. This is a list of strings, each of them will be given to the shell
  459. as a command. %f in the command will be replaced by the full file name, %b
  460. by the file base name (i.e. without extension) and %o by the base directory
  461. of the file.
  462. The reason why this is a list is that it usually takes several runs of
  463. `pdflatex', maybe mixed with a call to `bibtex'. Org does not have a clever
  464. mechanism to detect which of these commands have to be run to get to a stable
  465. result, and it also does not do any error checking.
  466. By default, Org uses 3 runs of `pdflatex' to do the processing. If you
  467. have texi2dvi on your system and if that does not cause the infamous
  468. egrep/locale bug:
  469. http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
  470. then `texi2dvi' is the superior choice. Org does offer it as one
  471. of the customize options.
  472. Alternatively, this may be a Lisp function that does the processing, so you
  473. could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
  474. This function should accept the file name as its single argument."
  475. :group 'org-export-pdf
  476. :type '(choice
  477. (repeat :tag "Shell command sequence"
  478. (string :tag "Shell command"))
  479. (const :tag "2 runs of pdflatex"
  480. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  481. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  482. (const :tag "3 runs of pdflatex"
  483. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  484. "pdflatex -interaction nonstopmode -output-directory %o %f"
  485. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  486. (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
  487. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  488. "bibtex %b"
  489. "pdflatex -interaction nonstopmode -output-directory %o %f"
  490. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  491. (const :tag "texi2dvi"
  492. ("texi2dvi -p -b -c -V %f"))
  493. (const :tag "rubber"
  494. ("rubber -d --into %o %f"))
  495. (function)))
  496. (defcustom org-e-latex-logfiles-extensions
  497. '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
  498. "The list of file extensions to consider as LaTeX logfiles."
  499. :group 'org-export-e-latex
  500. :type '(repeat (string :tag "Extension")))
  501. (defcustom org-e-latex-remove-logfiles t
  502. "Non-nil means remove the logfiles produced by PDF production.
  503. These are the .aux, .log, .out, and .toc files."
  504. :group 'org-export-e-latex
  505. :type 'boolean)
  506. ;;; Internal Functions
  507. (defun org-e-latex--caption/label-string (caption label info)
  508. "Return caption and label LaTeX string for floats.
  509. CAPTION is a secondary string \(a list of strings and Org
  510. objects\) and LABEL a string representing the label. INFO is
  511. a plist holding contextual information.
  512. If there's no caption nor label, return the empty string.
  513. For non-floats, see `org-e-latex--wrap-label'."
  514. (let ((caption-str (and caption
  515. (org-export-secondary-string
  516. caption 'e-latex info)))
  517. (label-str (if label (format "\\label{%s}" label) "")))
  518. (cond
  519. ((and (not caption-str) (not label)) "")
  520. ((not caption-str) (format "\\label{%s}\n" label))
  521. ;; Option caption format with short name.
  522. ((string-match "\\[\\([^][]*\\)\\]{\\([^{}]*\\)}" caption-str)
  523. (format "\\caption[%s]{%s%s}\n"
  524. (org-match-string-no-properties 1 caption-str)
  525. label-str
  526. (org-match-string-no-properties 2 caption-str)))
  527. ;; Standard caption format.
  528. (t (format "\\caption{%s%s}\n" label-str caption-str)))))
  529. (defun org-e-latex--guess-inputenc (header)
  530. "Set the coding system in inputenc to what the buffer is.
  531. HEADER is the LaTeX header string.
  532. Return the new header."
  533. (let* ((cs (or (ignore-errors
  534. (latexenc-coding-system-to-inputenc
  535. buffer-file-coding-system))
  536. "utf8")))
  537. (if (not cs)
  538. header
  539. ;; First translate if that is requested.
  540. (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
  541. ;; Then find the \usepackage statement and replace the option.
  542. (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  543. cs header t nil 1))))
  544. (defun org-e-latex--find-verb-separator (s)
  545. "Return a character not used in string S.
  546. This is used to choose a separator for constructs like \\verb."
  547. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  548. (loop for c across ll
  549. when (not (string-match (regexp-quote (char-to-string c)) s))
  550. return (char-to-string c))))
  551. (defun org-e-latex--make-option-string (options)
  552. "Return a comma separated string of keywords and values.
  553. OPTIONS is an alist where the key is the options keyword as
  554. a string, and the value a list containing the keyword value, or
  555. nil."
  556. (mapconcat (lambda (pair)
  557. (concat (first pair)
  558. (when (> (length (second pair)) 0)
  559. (concat "=" (second pair)))))
  560. options
  561. ","))
  562. (defun org-e-latex--quotation-marks (text info)
  563. "Export quotation marks depending on language conventions.
  564. TEXT is a string containing quotation marks to be replaced. INFO
  565. is a plist used as a communication channel."
  566. (mapc (lambda(l)
  567. (let ((start 0))
  568. (while (setq start (string-match (car l) text start))
  569. (let ((new-quote (concat (match-string 1 text) (cdr l))))
  570. (setq text (replace-match new-quote t t text))))))
  571. (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
  572. ;; Falls back on English.
  573. (assoc "en" org-e-latex-quotes))))
  574. text)
  575. (defun org-e-latex--wrap-label (element output)
  576. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  577. This function shouldn't be used for floats. See
  578. `org-e-latex--caption/label-string'."
  579. (let ((label (org-element-get-property :name element)))
  580. (if (or (not output) (not label) (string= output "") (string= label ""))
  581. output
  582. (concat (format "\\label{%s}\n" label) output))))
  583. ;;; Template
  584. (defun org-e-latex-template (contents info)
  585. "Return complete document string after LaTeX conversion.
  586. CONTENTS is the transcoded contents string. INFO is a plist
  587. holding export options."
  588. (let ((title (org-export-secondary-string
  589. (plist-get info :title) 'e-latex info)))
  590. (concat
  591. ;; 1. Time-stamp.
  592. (and (plist-get info :time-stamp-file)
  593. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  594. ;; 2. Document class and packages.
  595. (let ((class (plist-get info :latex-class))
  596. (class-options (plist-get info :latex-class-options)))
  597. (org-element-normalize-string
  598. (let* ((header (nth 1 (assoc class org-e-latex-classes)))
  599. (document-class-string
  600. (and (stringp header)
  601. (if class-options
  602. (replace-regexp-in-string
  603. "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
  604. class-options header t nil 1)
  605. header))))
  606. (org-e-latex--guess-inputenc
  607. (org-splice-latex-header
  608. document-class-string
  609. org-export-latex-default-packages-alist ; defined in org.el
  610. org-export-latex-packages-alist nil ; defined in org.el
  611. (plist-get info :latex-header-extra))))))
  612. ;; 3. Define alert if not yet defined.
  613. "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
  614. ;; 4. Author.
  615. (let ((author (and (plist-get info :with-author)
  616. (let ((auth (plist-get info :author)))
  617. (and auth (org-export-secondary-string
  618. auth 'e-latex info)))))
  619. (email (and (plist-get info :with-email)
  620. (org-export-secondary-string
  621. (plist-get info :email) 'e-latex info))))
  622. (cond ((and author email (not (string= "" email)))
  623. (format "\\author{%s\\thanks{%s}}\n" author email))
  624. (author (format "\\author{%s}\n" author))
  625. (t "\\author{}\n")))
  626. ;; 5. Date.
  627. (let ((date (plist-get info :date)))
  628. (and date (format "\\date{%s}\n" date)))
  629. ;; 6. Title
  630. (format "\\title{%s}\n" title)
  631. ;; 7. Hyperref options.
  632. (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
  633. (or (plist-get info :keywords) "")
  634. (or (plist-get info :description) "")
  635. (let ((creator-info (plist-get info :with-creator)))
  636. (cond
  637. ((not creator-info) "")
  638. ((eq creator-info 'comment) "")
  639. (t (plist-get info :creator)))))
  640. ;; 7. Document start.
  641. "\\begin{document}\n\n"
  642. ;; 8. Title command.
  643. (org-element-normalize-string
  644. (cond ((string= "" title) nil)
  645. ((not (stringp org-e-latex-title-command)) nil)
  646. ((string-match "\\(?:[^%]\\|^\\)%s"
  647. org-e-latex-title-command)
  648. (format org-e-latex-title-command title))
  649. (t org-e-latex-title-command)))
  650. ;; 9. Table of contents.
  651. (let ((depth (plist-get info :with-toc)))
  652. (when depth
  653. (concat (when (wholenump depth)
  654. (format "\\setcounter{tocdepth}{%d}\n" depth))
  655. "\\tableofcontents\n\\vspace*{1cm}\n\n")))
  656. ;; 10. Document's body.
  657. contents
  658. ;; 11. Creator.
  659. (let ((creator-info (plist-get info :with-creator)))
  660. (cond
  661. ((not creator-info))
  662. ((eq creator-info 'comment)
  663. (format "%% %s\n" (plist-get info :creator)))
  664. (t (concat (plist-get info :creator) "\n"))))
  665. ;; 12. Document end.
  666. "\\end{document}")))
  667. ;;; Transcode Functions
  668. ;;;; Block
  669. (defun org-e-latex-center-block (center-block contents info)
  670. "Transcode a CENTER-BLOCK element from Org to LaTeX.
  671. CONTENTS holds the contents of the block. INFO is a plist
  672. holding contextual information."
  673. (org-e-latex--wrap-label
  674. center-block
  675. (format "\\begin{center}\n%s\\end{center}" contents)))
  676. ;;;; Comment
  677. ;; Comments are ignored.
  678. ;;;; Comment Block
  679. ;; Comment Blocks are ignored.
  680. ;;;; Drawer
  681. (defun org-e-latex-drawer (drawer contents info)
  682. "Transcode a DRAWER element from Org to LaTeX.
  683. CONTENTS holds the contents of the block. INFO is a plist
  684. holding contextual information."
  685. (let* ((name (org-element-get-property :drawer-name drawer))
  686. (output (if (functionp org-e-latex-format-drawer-function)
  687. (funcall org-e-latex-format-drawer-function
  688. name contents)
  689. ;; If there's no user defined function: simply
  690. ;; display contents of the drawer.
  691. contents)))
  692. (org-e-latex--wrap-label drawer output)))
  693. ;;;; Dynamic Block
  694. (defun org-e-latex-dynamic-block (dynamic-block contents info)
  695. "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
  696. CONTENTS holds the contents of the block. INFO is a plist
  697. holding contextual information. See
  698. `org-export-data'."
  699. (org-e-latex--wrap-label dynamic-block contents))
  700. ;;;; Emphasis
  701. (defun org-e-latex-emphasis (emphasis contents info)
  702. "Transcode EMPHASIS from Org to LaTeX.
  703. CONTENTS is the contents of the emphasized text. INFO is a plist
  704. holding contextual information.."
  705. (format (cdr (assoc (org-element-get-property :marker emphasis)
  706. org-e-latex-emphasis-alist))
  707. contents))
  708. ;;;; Entity
  709. (defun org-e-latex-entity (entity contents info)
  710. "Transcode an ENTITY object from Org to LaTeX.
  711. CONTENTS are the definition itself. INFO is a plist holding
  712. contextual information."
  713. (let ((ent (org-element-get-property :latex entity)))
  714. (if (org-element-get-property :latex-math-p entity)
  715. (format "$%s$" ent)
  716. ent)))
  717. ;;;; Example Block
  718. (defun org-e-latex-example-block (example-block contents info)
  719. "Transcode a EXAMPLE-BLOCK element from Org to LaTeX.
  720. CONTENTS is nil. INFO is a plist holding contextual information."
  721. (let* ((options (or (org-element-get-property :options example-block) ""))
  722. (value (org-export-handle-code example-block info)))
  723. (org-e-latex--wrap-label
  724. example-block (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  725. ;;;; Export Snippet
  726. (defun org-e-latex-export-snippet (export-snippet contents info)
  727. "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
  728. CONTENTS is nil. INFO is a plist holding contextual information."
  729. (org-element-get-property :value export-snippet))
  730. ;;;; Export Block
  731. (defun org-e-latex-export-block (export-block contents info)
  732. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  733. CONTENTS is nil. INFO is a plist holding contextual information."
  734. (when (string= (org-element-get-property :type export-block) "latex")
  735. (org-remove-indentation (org-element-get-property :value export-block))))
  736. ;;;; Fixed Width
  737. (defun org-e-latex-fixed-width (fixed-width contents info)
  738. "Transcode a FIXED-WIDTH element from Org to LaTeX.
  739. CONTENTS is nil. INFO is a plist holding contextual information."
  740. (let* ((value (org-element-normalize-string
  741. (replace-regexp-in-string
  742. "^[ \t]*: ?" ""
  743. (org-element-get-property :value fixed-width)))))
  744. (org-e-latex--wrap-label
  745. fixed-width (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  746. ;;;; Footnote Definition
  747. ;; Footnote Definitions are ignored.
  748. ;;;; Footnote Reference
  749. (defun org-e-latex-footnote-reference (footnote-reference contents info)
  750. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  751. CONTENTS is nil. INFO is a plist holding contextual information."
  752. (concat
  753. ;; Insert separator between two footnotes in a row.
  754. (let ((prev (org-export-get-previous-element footnote-reference info)))
  755. (when (and (listp prev) (eq (car prev) 'footnote-reference))
  756. org-e-latex-footnote-separator))
  757. ;; Use \footnotemark if the footnote has already been defined.
  758. ;; Otherwise, define it with \footnote command.
  759. (cond
  760. ((not (org-export-footnote-first-reference-p footnote-reference info))
  761. (format "\\footnotemark[%s]"
  762. (org-export-get-footnote-number footnote-reference info)))
  763. ;; Inline definitions are secondary strings.
  764. ((eq (org-element-get-property :type footnote-reference) 'inline)
  765. (format "\\footnote{%s}"
  766. (org-trim
  767. (org-export-secondary-string
  768. (org-export-get-footnote-definition footnote-reference info)
  769. 'e-latex info))))
  770. ;; Non-inline footnotes definitions are full Org data.
  771. (t
  772. (format "\\footnote{%s}"
  773. (org-trim
  774. (org-export-data
  775. (org-export-get-footnote-definition footnote-reference info)
  776. 'e-latex info)))))))
  777. ;;;; Headline
  778. (defun org-e-latex-headline (headline contents info)
  779. "Transcode an HEADLINE element from Org to LaTeX.
  780. CONTENTS holds the contents of the headline. INFO is a plist
  781. holding contextual information."
  782. (let* ((class (plist-get info :latex-class))
  783. (numberedp (plist-get info :section-numbers))
  784. ;; Get level relative to current parsed data.
  785. (level (org-export-get-relative-level headline info))
  786. (class-sectionning (assoc class org-e-latex-classes))
  787. ;; Section formatting will set two placeholders: one for the
  788. ;; title and the other for the contents.
  789. (section-fmt
  790. (let ((sec (if (and (symbolp (nth 2 class-sectionning))
  791. (fboundp (nth 2 class-sectionning)))
  792. (funcall (nth 2 class-sectionning) level numberedp)
  793. (nth (1+ level) class-sectionning))))
  794. (cond
  795. ;; No section available for that LEVEL.
  796. ((not sec) nil)
  797. ;; Section format directly returned by a function.
  798. ((stringp sec) sec)
  799. ;; (numbered-section . unnumbered-section)
  800. ((not (consp (cdr sec)))
  801. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  802. ;; (numbered-open numbered-close)
  803. ((= (length sec) 2)
  804. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  805. ;; (num-in num-out no-num-in no-num-out)
  806. ((= (length sec) 4)
  807. (if numberedp
  808. (concat (car sec) "\n%s" (nth 1 sec))
  809. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  810. (text (org-export-secondary-string
  811. (org-element-get-property :title headline) 'e-latex info))
  812. (todo (and (plist-get info :with-todo-keywords)
  813. (let ((todo (org-element-get-property
  814. :todo-keyword headline)))
  815. (and todo
  816. (org-export-secondary-string todo 'e-latex info)))))
  817. (todo-type (and todo (org-element-get-property :todo-type headline)))
  818. (tags (and (plist-get info :with-tags)
  819. (org-element-get-property :tags headline)))
  820. (priority (and (plist-get info :with-priority)
  821. (org-element-get-property :priority headline)))
  822. ;; Create the headline text.
  823. (full-text (if (functionp org-e-latex-format-headline-function)
  824. ;; User-defined formatting function.
  825. (funcall org-e-latex-format-headline-function
  826. todo todo-type priority text tags)
  827. ;; Default formatting.
  828. (concat
  829. (when todo
  830. (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  831. (when priority (format "\\framebox{\\#%c} " priority))
  832. text
  833. (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
  834. ;; Associate some \label to the headline for internal links.
  835. (headline-labels (mapconcat
  836. (lambda (p)
  837. (let ((val (org-element-get-property p headline)))
  838. (when val (format "\\label{%s}\n"
  839. (if (eq p :begin)
  840. (format "headline-%s" val)
  841. val)))))
  842. '(:custom-id :id :begin) ""))
  843. (pre-blanks (make-string (org-element-get-property :pre-blank headline)
  844. 10)))
  845. (cond
  846. ;; Case 1: This is a footnote section: ignore it.
  847. ((org-element-get-property :footnote-section-p headline) nil)
  848. ;; Case 2. This is a deep sub-tree: export it as a list item.
  849. ;; Also export as items headlines for which no section
  850. ;; format has been found.
  851. ((or (not section-fmt) (org-export-low-level-p headline info))
  852. ;; Build the real contents of the sub-tree.
  853. (let ((low-level-body
  854. (concat
  855. ;; If the headline is the first sibling, start a list.
  856. (when (org-export-first-sibling-p headline info)
  857. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  858. ;; Itemize headline
  859. "\\item " full-text "\n" headline-labels pre-blanks contents)))
  860. ;; If headline in the last sibling, close the list, before any
  861. ;; blank line. Otherwise, simply return LOW-LEVEL-BODY.
  862. (if (org-export-last-sibling-p headline info)
  863. (replace-regexp-in-string
  864. "[ \t\n]*\\'"
  865. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  866. low-level-body)
  867. low-level-body)))
  868. ;; Case 3. Standard headline. Export it as a section.
  869. (t (format section-fmt full-text
  870. (concat headline-labels pre-blanks contents))))))
  871. ;;;; Horizontal Rule
  872. (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
  873. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  874. CONTENTS is nil. INFO is a plist holding contextual information."
  875. (let ((attr (mapconcat #'identity
  876. (org-element-get-property :attr_latex horizontal-rule)
  877. " ")))
  878. (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
  879. ;;;; Inline Babel Call
  880. ;; Inline Babel Calls are ignored.
  881. ;;;; Inline Src Block
  882. (defun org-e-latex-inline-src-block (inline-src-block contents info)
  883. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  884. CONTENTS holds the contents of the item. INFO is a plist holding
  885. contextual information."
  886. (let* ((code (org-element-get-property :value inline-src-block))
  887. (separator (org-e-latex--find-verb-separator code)))
  888. (cond
  889. ;; Do not use a special package: transcode it verbatim.
  890. ((not org-e-latex-listings)
  891. (concat "\\verb" separator code separator))
  892. ;; Use minted package.
  893. ((eq org-e-latex-listings 'minted)
  894. (let* ((org-lang (org-element-get-property :language inline-src-block))
  895. (mint-lang (or (cadr (assq (intern org-lang)
  896. org-e-latex-minted-langs))
  897. org-lang))
  898. (options (org-e-latex--make-option-string
  899. org-e-latex-minted-options)))
  900. (concat (format "\\mint%s{%s}"
  901. (if (string= options "") "" (format "[%s]" options))
  902. mint-lang)
  903. separator code separator)))
  904. ;; Use listings package.
  905. (t
  906. ;; Maybe translate language's name.
  907. (let* ((org-lang (org-element-get-property :language inline-src-block))
  908. (lst-lang (or (cadr (assq (intern org-lang)
  909. org-e-latex-listings-langs))
  910. org-lang))
  911. (options (org-e-latex--make-option-string
  912. (append org-e-latex-listings-options
  913. `(("language" ,lst-lang))))))
  914. (concat (format "\\lstinline[%s]" options)
  915. separator code separator))))))
  916. ;;;; Inlinetask
  917. (defun org-e-latex-inlinetask (inlinetask contents info)
  918. "Transcode an INLINETASK element from Org to LaTeX.
  919. CONTENTS holds the contents of the block. INFO is a plist
  920. holding contextual information."
  921. (let ((title (org-export-secondary-string
  922. (org-element-get-property :title inlinetask) 'e-latex info))
  923. (todo (and (plist-get info :with-todo-keywords)
  924. (let ((todo (org-element-get-property
  925. :todo-keyword inlinetask)))
  926. (and todo
  927. (org-export-secondary-string todo 'e-latex info)))))
  928. (todo-type (org-element-get-property :todo-type inlinetask))
  929. (tags (and (plist-get info :with-tags)
  930. (org-element-get-property :tags inlinetask)))
  931. (priority (and (plist-get info :with-priority)
  932. (org-element-get-property :priority inlinetask))))
  933. ;; If `org-e-latex-format-inlinetask-function' is provided, call it
  934. ;; with appropriate arguments.
  935. (if (functionp org-e-latex-format-inlinetask-function)
  936. (funcall org-e-latex-format-inlinetask-function
  937. todo todo-type priority title tags contents)
  938. ;; Otherwise, use a default template.
  939. (org-e-latex--wrap-label
  940. inlinetask
  941. (let ((full-title
  942. (concat
  943. (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  944. (when priority (format "\\framebox{\\#%c} " priority))
  945. title
  946. (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
  947. (format (concat "\\begin{center}\n"
  948. "\\fbox{\n"
  949. "\\begin{minipage}[c]{.6\\textwidth}\n"
  950. "%s\n\n"
  951. "\\rule[.8em]{\\textwidth}{2pt}\n\n"
  952. "%s"
  953. "\\end{minipage}\n"
  954. "}\n"
  955. "\\end{center}")
  956. full-title contents))))))
  957. ;;;; Item
  958. (defun org-e-latex-item (item contents info)
  959. "Transcode an ITEM element from Org to LaTeX.
  960. CONTENTS holds the contents of the item. INFO is a plist holding
  961. contextual information."
  962. ;; Grab `:level' from plain-list properties, which is always the
  963. ;; first element above current item.
  964. (let* ((level (org-element-get-property
  965. :level (car (plist-get info :genealogy))))
  966. (counter (let ((count (org-element-get-property :counter item)))
  967. (and count
  968. (< level 4)
  969. (format "\\setcounter{enum%s}{%s}\n"
  970. (nth level '("i" "ii" "iii" "iv"))
  971. (1- count)))))
  972. (checkbox (let ((checkbox (org-element-get-property :checkbox item)))
  973. (cond ((eq checkbox 'on) "$\\boxtimes$ ")
  974. ((eq checkbox 'off) "$\\Box$ ")
  975. ((eq checkbox 'trans) "$\\boxminus$ "))))
  976. (tag (let ((tag (org-element-get-property :tag item)))
  977. (and tag
  978. (format "[%s]" (org-export-secondary-string
  979. tag 'e-latex info))))))
  980. (concat counter "\\item" tag " " checkbox contents)))
  981. ;;;; Keyword
  982. (defun org-e-latex-keyword (keyword contents info)
  983. "Transcode a KEYWORD element from Org to LaTeX.
  984. CONTENTS is nil. INFO is a plist holding contextual information."
  985. (let ((key (downcase (org-element-get-property :key keyword)))
  986. (value (org-element-get-property :value keyword)))
  987. (cond
  988. ((string= key "latex") value)
  989. ((string= key "index") (format "\\index{%s}" value))
  990. ((string= key "target")
  991. (format "\\label{%s}" (org-export-solidify-link-text value)))
  992. ((string= key "toc")
  993. (let ((value (downcase value)))
  994. (cond
  995. ((string-match "\\<headlines\\>" value)
  996. (let ((depth (or (and (string-match "[0-9]+" value)
  997. (string-to-number (match-string 0 value)))
  998. (plist-get info :with-toc))))
  999. (concat
  1000. (when (wholenump depth)
  1001. (format "\\setcounter{tocdepth}{%s}\n" depth))
  1002. "\\tableofcontents")))
  1003. ((string= "tables" value) "\\listoftables")
  1004. ((string= "figures" value) "\\listoffigures")
  1005. ((string= "listings" value) "\\listoflistings"))))
  1006. ((string= key "include")
  1007. (org-export-included-file keyword 'e-latex info)))))
  1008. ;;;; Latex Environment
  1009. (defun org-e-latex-latex-environment (latex-environment contents info)
  1010. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1011. CONTENTS is nil. INFO is a plist holding contextual information."
  1012. (org-e-latex--wrap-label
  1013. latex-environment
  1014. (org-remove-indentation (org-element-get-property :value latex-environment))))
  1015. ;;;; Latex Fragment
  1016. (defun org-e-latex-latex-fragment (latex-fragment contents info)
  1017. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1018. CONTENTS is nil. INFO is a plist holding contextual information."
  1019. (org-element-get-property :value latex-fragment))
  1020. ;;;; Line Break
  1021. (defun org-e-latex-line-break (line-break contents info)
  1022. "Transcode a LINE-BREAK object from Org to LaTeX.
  1023. CONTENTS is nil. INFO is a plist holding contextual information."
  1024. "\\\\")
  1025. ;;;; Link
  1026. (defun org-e-latex-link--inline-image (path info)
  1027. "Return LaTeX code for an image at PATH.
  1028. INFO is a plist containing export options."
  1029. (let* ((parent-props (nth 1 (car (plist-get info :genealogy))))
  1030. (caption (org-e-latex--caption/label-string
  1031. (plist-get parent-props :caption)
  1032. (plist-get parent-props :name)
  1033. info))
  1034. ;; Retrieve latex attributes from the element around.
  1035. (attr (let ((raw-attr
  1036. (mapconcat #'identity
  1037. (plist-get parent-props :attr_latex) " ")))
  1038. (unless (string= raw-attr "") raw-attr)))
  1039. (disposition
  1040. (cond
  1041. ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
  1042. ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
  1043. ((or (and attr (string-match "\\<float\\>" attr))
  1044. (not (string= caption "")))
  1045. 'float)))
  1046. (placement
  1047. (cond
  1048. ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
  1049. (org-match-string-no-properties 1 attr))
  1050. ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
  1051. ((eq disposition 'float)
  1052. (concat "[" org-e-latex-default-figure-position "]"))
  1053. (t ""))))
  1054. ;; Now clear ATTR from any special keyword and set a default
  1055. ;; value if nothing is left.
  1056. (if (not attr)
  1057. (setq attr "")
  1058. (while (string-match "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)"
  1059. attr)
  1060. (replace-match "" nil nil attr))
  1061. (setq attr (org-trim attr)))
  1062. (setq attr (cond ((not (string= attr "")) attr)
  1063. ((eq disposition 'float) "width=0.7\\textwidth")
  1064. ((eq disposition 'wrap) "width=0.48\\textwidth")
  1065. (t (or org-e-latex-image-default-option ""))))
  1066. ;; Return proper string, depending on DISPOSITION.
  1067. (case disposition
  1068. ('wrap (format "\\begin{wrapfigure}%s
  1069. \\centering
  1070. \\includegraphics[%s]{%s}
  1071. %s\\end{wrapfigure}" placement attr path caption))
  1072. ('mulicolumn (format "\\begin{figure*}%s
  1073. \\centering
  1074. \\includegraphics[%s]{%s}
  1075. %s\\end{figure*}" placement attr path caption))
  1076. ('float (format "\\begin{figure}%s
  1077. \\centering
  1078. \\includegraphics[%s]{%s}
  1079. %s\\end{figure}" placement attr path caption))
  1080. (t (format "\\includegraphics[%s]{%s}" attr path)))))
  1081. (defun org-e-latex-link (link desc info)
  1082. "Transcode a LINK object from Org to LaTeX.
  1083. DESC is the description part of the link, or the empty string.
  1084. INFO is a plist holding contextual information. See
  1085. `org-export-data'."
  1086. (let* ((type (org-element-get-property :type link))
  1087. (raw-path (org-element-get-property :path link))
  1088. ;; Ensure DESC really exists, or set it to nil.
  1089. (desc (and (not (string= desc "")) desc))
  1090. (imagep (org-export-inline-image-p
  1091. link org-e-latex-inline-image-extensions))
  1092. (path (cond
  1093. ((member type '("http" "https" "ftp" "mailto"))
  1094. (concat type ":" raw-path))
  1095. ((and (not imagep) (string= type "file"))
  1096. (when (string-match "\\(.+\\)::.+" raw-path)
  1097. (setq raw-path (match-string 1 raw-path)))
  1098. (if (file-name-absolute-p raw-path)
  1099. (concat "file://" (expand-file-name raw-path))
  1100. ;; TODO: Not implemented yet. Concat also:
  1101. ;; (org-export-directory :LaTeX info)
  1102. (concat "file://" raw-path)))
  1103. (t raw-path)))
  1104. protocol)
  1105. (cond
  1106. ;; Image file.
  1107. (imagep (org-e-latex-link--inline-image path info))
  1108. ;; Id: for now, assume it's an internal link. TODO: do something
  1109. ;; to check if it isn't in the current file.
  1110. ((string= type "id")
  1111. (format "\\hyperref[%s]{%s}" path (or desc path)))
  1112. ;; Custom-id, target or radioed target: replace link with the
  1113. ;; normalized custom-id/target name.
  1114. ((member type '("custom-id" "target" "radio"))
  1115. (format "\\hyperref[%s]{%s}"
  1116. (org-export-solidify-link-text path)
  1117. (or desc (org-export-secondary-string path 'e-latex info))))
  1118. ;; Fuzzy: With the help of `org-export-resolve-fuzzy-link', find
  1119. ;; the destination of the link.
  1120. ((string= type "fuzzy")
  1121. (let ((destination (org-export-resolve-fuzzy-link link info)))
  1122. (case (car destination)
  1123. (target
  1124. (format "\\hyperref[%s]{%s}"
  1125. (org-export-solidify-link-text
  1126. (org-element-get-property :raw-value destination))
  1127. (or desc
  1128. (org-export-secondary-string
  1129. (org-element-get-property :raw-link link)
  1130. 'e-latex info))))
  1131. (headline
  1132. (format "\\hyperref[headline-%d]{%s}"
  1133. (org-element-get-property :begin destination)
  1134. (or desc
  1135. (org-export-secondary-string
  1136. (org-element-get-property :raw-link link)
  1137. 'e-latex info))))
  1138. (otherwise
  1139. (format "\\texttt{%s}"
  1140. (or desc
  1141. (org-export-secondary-string
  1142. (org-element-get-property :raw-link link)
  1143. 'e-latex info)))))))
  1144. ;; Coderef: replace link with the reference name or the
  1145. ;; equivalent line number.
  1146. ((string= type "coderef")
  1147. (format (org-export-get-coderef-format path (or desc ""))
  1148. (org-export-resolve-coderef path info)))
  1149. ;; Link type is handled by a special function.
  1150. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  1151. (funcall protocol (org-link-unescape path) desc 'latex))
  1152. ;; External link with a description part.
  1153. ((and path desc) (format "\\href{%s}{%s}" path desc))
  1154. ;; External link without a description part.
  1155. (path (format "\\url{%s}" path))
  1156. ;; No path, only description. Try to do something useful.
  1157. (t (format "\\texttt{%s}" desc)))))
  1158. ;;;; Babel Call
  1159. ;; Babel Calls are ignored.
  1160. ;;;; Macro
  1161. (defun org-e-latex-macro (macro contents info)
  1162. "Transcode a MACRO element from Org to LaTeX.
  1163. CONTENTS is nil. INFO is a plist holding contextual information."
  1164. ;; Use available tools.
  1165. (org-export-expand-macro macro info))
  1166. ;;;; Paragraph
  1167. (defun org-e-latex-paragraph (paragraph contents info)
  1168. "Transcode a PARAGRAPH element from Org to LaTeX.
  1169. CONTENTS is the contents of the paragraph, as a string. INFO is
  1170. the plist used as a communication channel."
  1171. contents)
  1172. ;;;; Plain List
  1173. (defun org-e-latex-plain-list (plain-list contents info)
  1174. "Transcode a PLAIN-LIST element from Org to LaTeX.
  1175. CONTENTS is the contents of the list. INFO is a plist holding
  1176. contextual information."
  1177. (let* ((type (org-element-get-property :type plain-list))
  1178. (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
  1179. "inparadesc" "asparadesc"))
  1180. (paralist-regexp (concat
  1181. "\\("
  1182. (mapconcat 'identity paralist-types "\\|")
  1183. "\\)"))
  1184. (attr (mapconcat #'identity
  1185. (org-element-get-property :attr_latex plain-list)
  1186. " "))
  1187. (latex-type (cond
  1188. ((and attr
  1189. (string-match
  1190. (format "\\<%s\\>" paralist-regexp) attr))
  1191. (match-string 1 attr))
  1192. ((eq type 'ordered) "enumerate")
  1193. ((eq type 'unordered) "itemize")
  1194. ((eq type 'descriptive) "description"))))
  1195. (org-e-latex--wrap-label
  1196. plain-list
  1197. (format "\\begin{%s}%s\n%s\\end{%s}"
  1198. latex-type
  1199. ;; Once special environment, if any, has been removed, the
  1200. ;; rest of the attributes will be optional arguments.
  1201. ;; They will be put inside square brackets if necessary.
  1202. (let ((opt (replace-regexp-in-string
  1203. (format " *%s *" paralist-regexp) "" attr)))
  1204. (cond ((string= opt "") "")
  1205. ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
  1206. (t (format "[%s]" opt))))
  1207. contents
  1208. latex-type))))
  1209. ;;;; Plain Text
  1210. (defun org-e-latex-plain-text (text info)
  1211. "Transcode a TEXT string from Org to LaTeX.
  1212. TEXT is the string to transcode. INFO is a plist holding
  1213. contextual information."
  1214. ;; Protect %, #, &, $, ~, ^, _, { and }.
  1215. (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
  1216. (setq text
  1217. (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
  1218. ;; Protect \
  1219. (setq text (replace-regexp-in-string
  1220. "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
  1221. "$\\backslash$" text nil t 1))
  1222. ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
  1223. (let ((case-fold-search nil)
  1224. (start 0))
  1225. (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
  1226. (setq text (replace-match
  1227. (format "\\%s{}" (match-string 1 text)) nil t text)
  1228. start (match-end 0))))
  1229. ;; Handle quotation marks
  1230. (setq text (org-e-latex--quotation-marks text info))
  1231. ;; Convert special strings.
  1232. (when (plist-get info :with-special-strings)
  1233. (while (string-match (regexp-quote "...") text)
  1234. (setq text (replace-match "\\ldots{}" nil t text))))
  1235. ;; Handle break preservation if required.
  1236. (when (plist-get info :preserve-breaks)
  1237. (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
  1238. text)))
  1239. ;; Return value.
  1240. text)
  1241. ;;;; Property Drawer
  1242. (defun org-e-latex-property-drawer (property-drawer contents info)
  1243. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  1244. CONTENTS is nil. INFO is a plist holding contextual
  1245. information."
  1246. ;; The property drawer isn't exported but we want separating blank
  1247. ;; lines nonetheless.
  1248. "")
  1249. ;;;; Quote Block
  1250. (defun org-e-latex-quote-block (quote-block contents info)
  1251. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  1252. CONTENTS holds the contents of the block. INFO is a plist
  1253. holding contextual information."
  1254. (org-e-latex--wrap-label
  1255. quote-block
  1256. (format "\\begin{quote}\n%s\\end{quote}" contents)))
  1257. ;;;; Quote Section
  1258. (defun org-e-latex-quote-section (quote-section contents info)
  1259. "Transcode a QUOTE-SECTION element from Org to LaTeX.
  1260. CONTENTS is nil. INFO is a plist holding contextual information."
  1261. (let ((value (org-remove-indentation
  1262. (org-element-get-property :value quote-section))))
  1263. (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  1264. ;;;; Section
  1265. (defun org-e-latex-section (section contents info)
  1266. "Transcode a SECTION element from Org to LaTeX.
  1267. CONTENTS holds the contents of the section. INFO is a plist
  1268. holding contextual information."
  1269. contents)
  1270. ;;;; Radio Target
  1271. (defun org-e-latex-radio-target (radio-target text info)
  1272. "Transcode a RADIO-TARGET object from Org to LaTeX.
  1273. TEXT is the text of the target. INFO is a plist holding
  1274. contextual information."
  1275. (format "\\label{%s}%s"
  1276. (org-export-solidify-link-text
  1277. (org-element-get-property :raw-value radio-target))
  1278. text))
  1279. ;;;; Special Block
  1280. (defun org-e-latex-special-block (special-block contents info)
  1281. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  1282. CONTENTS holds the contents of the block. INFO is a plist
  1283. holding contextual information."
  1284. (let ((type (downcase (org-element-get-property :type special-block))))
  1285. (org-e-latex--wrap-label
  1286. special-block
  1287. (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
  1288. ;;;; Src Block
  1289. (defun org-e-latex-src-block (src-block contents info)
  1290. "Transcode a SRC-BLOCK element from Org to LaTeX.
  1291. CONTENTS holds the contents of the item. INFO is a plist holding
  1292. contextual information."
  1293. (let* ((lang (org-element-get-property :language src-block))
  1294. (code (org-export-handle-code src-block info))
  1295. (caption (org-element-get-property :caption src-block))
  1296. (label (org-element-get-property :name src-block))
  1297. (custom-env (and lang
  1298. (cadr (assq (intern lang)
  1299. org-e-latex-custom-lang-environments)))))
  1300. (cond
  1301. ;; No source fontification.
  1302. ((not org-e-latex-listings)
  1303. (let ((caption-str (org-e-latex--caption/label-string
  1304. caption label info))
  1305. (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
  1306. (format (or float-env "%s")
  1307. (concat
  1308. caption-str
  1309. (format "\\begin{verbatim}\n%s\\end{verbatim}" code)))))
  1310. ;; Custom environment.
  1311. (custom-env
  1312. (format "\\begin{%s}\n%s\\end{%s}\n" custom-env code custom-env))
  1313. ;; Use minted package.
  1314. ((eq org-e-latex-listings 'minted)
  1315. (let* ((mint-lang (or (cadr (assq (intern lang) org-e-latex-minted-langs))
  1316. lang))
  1317. (float-env (when (or label caption)
  1318. (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
  1319. (org-e-latex--caption/label-string
  1320. caption label info))))
  1321. (body (format "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  1322. (org-e-latex--make-option-string
  1323. org-e-latex-minted-options)
  1324. mint-lang code)))
  1325. (if float-env (format float-env body) body)))
  1326. ;; Use listings package.
  1327. (t
  1328. (let ((lst-lang (or (cadr (assq (intern lang) org-e-latex-listings-langs))
  1329. lang))
  1330. (caption-str (and caption
  1331. (org-export-secondary-string
  1332. (org-element-get-property :caption src-block)
  1333. 'e-latex info))))
  1334. (concat (format "\\lstset{%s}\n"
  1335. (org-e-latex--make-option-string
  1336. (append org-e-latex-listings-options
  1337. `(("language" ,lst-lang))
  1338. (when label `(("label" ,label)))
  1339. (when caption-str
  1340. `(("caption" ,caption-str))))))
  1341. (format "\\begin{lstlisting}\n%s\\end{lstlisting}" code)))))))
  1342. ;;;; Statistics Cookie
  1343. (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
  1344. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  1345. CONTENTS is nil. INFO is a plist holding contextual information."
  1346. (org-element-get-property :value statistics-cookie))
  1347. ;;;; Subscript
  1348. (defun org-e-latex-subscript (subscript contents info)
  1349. "Transcode a SUBSCRIPT object from Org to LaTeX.
  1350. CONTENTS is the contents of the object. INFO is a plist holding
  1351. contextual information."
  1352. (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
  1353. ;;;; Superscript
  1354. (defun org-e-latex-superscript (superscript contents info)
  1355. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  1356. CONTENTS is the contents of the object. INFO is a plist holding
  1357. contextual information."
  1358. (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
  1359. ;;;; Table
  1360. (defun org-e-latex-table--format-string (table info)
  1361. "Return an appropriate format string for TABLE.
  1362. INFO is the plist containing format info about the table, as
  1363. returned by `org-export-table-format-info'.
  1364. The format string one placeholder for the body of the table."
  1365. (let* ((label (org-element-get-property :name table))
  1366. (caption (org-e-latex--caption/label-string
  1367. (org-element-get-property :caption table) label info))
  1368. (attr (mapconcat #'identity
  1369. (org-element-get-property :attr_latex table)
  1370. " "))
  1371. ;; Determine alignment string.
  1372. (alignment (org-e-latex-table--align-string attr info))
  1373. ;; Determine environment for the table: longtable, tabular...
  1374. (table-env (cond
  1375. ((not attr) org-e-latex-default-table-environment)
  1376. ((string-match "\\<longtable\\>" attr) "longtable")
  1377. ((string-match "\\(tabular.\\)" attr)
  1378. (org-match-string-no-properties 1 attr))
  1379. (t org-e-latex-default-table-environment)))
  1380. ;; If table is a float, determine environment: table or table*.
  1381. (float-env (cond
  1382. ((string= "longtable" table-env) nil)
  1383. ((and attr
  1384. (or (string-match (regexp-quote "table*") attr)
  1385. (string-match "\\<multicolumn\\>" attr)))
  1386. "table*")
  1387. ((or (not (string= caption "")) label) "table")))
  1388. ;; Extract others display options.
  1389. (width (and attr
  1390. (string-match "\\<width=\\(\\S-+\\)" attr)
  1391. (org-match-string-no-properties 1 attr)))
  1392. (placement (if (and attr
  1393. (string-match "\\<placement=\\(\\S-+\\)" attr))
  1394. (org-match-string-no-properties 1 attr)
  1395. (concat "["
  1396. org-e-latex-default-figure-position
  1397. "]"))))
  1398. ;; Prepare the final format string for the table.
  1399. (cond
  1400. ;; Longtable.
  1401. ((string= "longtable" table-env)
  1402. (format "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
  1403. alignment
  1404. (if (or (not org-e-latex-table-caption-above)
  1405. (string= "" caption))
  1406. ""
  1407. (concat (org-trim caption) "\\\\"))
  1408. (if (or org-e-latex-table-caption-above
  1409. (string= "" caption))
  1410. ""
  1411. (concat (org-trim caption) "\\\\\n"))))
  1412. ;; Others.
  1413. (t (concat (when float-env
  1414. (concat
  1415. (format "\\begin{%s}%s\n" float-env placement)
  1416. (if org-e-latex-table-caption-above caption "")))
  1417. (when org-e-latex-tables-centered "\\begin{center}\n")
  1418. (format "\\begin{%s}%s{%s}\n%%s\n\\end{%s}"
  1419. table-env
  1420. (if width (format "{%s}" width) "")
  1421. alignment
  1422. table-env)
  1423. (when org-e-latex-tables-centered "\n\\end{center}")
  1424. (when float-env
  1425. (concat (if org-e-latex-table-caption-above "" caption)
  1426. (format "\n\\end{%s}" float-env))))))))
  1427. (defun org-e-latex-table--align-string (attr info)
  1428. "Return an appropriate LaTeX alignment string.
  1429. ATTR is a string containing table's LaTeX specific attributes.
  1430. INFO is the plist containing format info about the table, as
  1431. returned by `org-export-table-format-info'."
  1432. (or (and attr
  1433. (string-match "\\<align=\\(\\S-+\\)" attr)
  1434. (match-string 1 attr))
  1435. (let* ((align (copy-sequence (plist-get info :alignment)))
  1436. (colgroups (copy-sequence (plist-get info :column-groups)))
  1437. (cols (length align))
  1438. (separators (make-vector (1+ cols) "")))
  1439. ;; Ignore the first column if it's special.
  1440. (when (plist-get info :special-column-p)
  1441. (aset align 0 "") (aset colgroups 0 nil))
  1442. (let ((col 0))
  1443. (mapc (lambda (el)
  1444. (let ((gr (aref colgroups col)))
  1445. (when (memq gr '(start start-end))
  1446. (aset separators col "|"))
  1447. (when (memq gr '(end start-end))
  1448. (aset separators (1+ col) "|")))
  1449. (incf col))
  1450. align))
  1451. ;; Build the LaTeX specific alignment string.
  1452. (loop for al across align
  1453. for sep across separators
  1454. concat (concat sep al) into output
  1455. finally return (concat output (aref separators cols))))))
  1456. (defun org-e-latex-table (table contents info)
  1457. "Transcode a TABLE element from Org to LaTeX.
  1458. CONTENTS is nil. INFO is a plist holding contextual information."
  1459. (let ((attr (mapconcat #'identity
  1460. (org-element-get-property :attr_latex table)
  1461. " "))
  1462. (raw-table (org-element-get-property :raw-table table)))
  1463. (cond
  1464. ;; Case 1: verbatim table.
  1465. ((or org-e-latex-tables-verbatim
  1466. (and attr (string-match "\\<verbatim\\>" attr)))
  1467. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  1468. (org-export-clean-table
  1469. raw-table
  1470. (plist-get (org-export-table-format-info raw-table)
  1471. :special-column-p))))
  1472. ;; Case 2: table.el table. Convert it using appropriate tools.
  1473. ((eq (org-element-get-property :type table) 'table.el)
  1474. (require 'table)
  1475. ;; Ensure "*org-export-table*" buffer is empty.
  1476. (and (get-buffer "*org-export-table*")
  1477. (kill-buffer (get-buffer "*org-export-table*")))
  1478. (let ((output (with-temp-buffer
  1479. (insert raw-table)
  1480. (goto-char 1)
  1481. (re-search-forward "^[ \t]*|[^|]" nil t)
  1482. (table-generate-source 'latex "*org-export-table*")
  1483. (with-current-buffer "*org-export-table*"
  1484. (org-trim (buffer-string))))))
  1485. (kill-buffer (get-buffer "*org-export-table*"))
  1486. ;; Remove left out comments.
  1487. (while (string-match "^%.*\n" output)
  1488. (setq output (replace-match "" t t output)))
  1489. ;; When the "rmlines" attribute is provided, remove all hlines
  1490. ;; but the the one separating heading from the table body.
  1491. (when (and attr (string-match "\\<rmlines\\>" attr))
  1492. (let ((n 0) (pos 0))
  1493. (while (and (< (length output) pos)
  1494. (setq pos (string-match "^\\\\hline\n?" output pos)))
  1495. (incf n)
  1496. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  1497. (if org-e-latex-tables-centered
  1498. (format "\\begin{center}\n%s\n\\end{center}" output)
  1499. output)))
  1500. ;; Case 3: Standard table.
  1501. (t
  1502. (let* ((table-info (org-export-table-format-info raw-table))
  1503. (clean-table (org-export-clean-table
  1504. raw-table (plist-get table-info :special-column-p)))
  1505. (columns-number (length (plist-get table-info :alignment))))
  1506. ;; Convert ROWS to send them to `orgtbl-to-latex'. In
  1507. ;; particular, send each cell to
  1508. ;; `org-element-parse-secondary-string' to expand any Org
  1509. ;; object within. Eventually, flesh the format string out with
  1510. ;; the table.
  1511. (format (org-e-latex-table--format-string table table-info)
  1512. (orgtbl-to-latex
  1513. (mapcar
  1514. (lambda (row)
  1515. (if (string-match org-table-hline-regexp row)
  1516. 'hline
  1517. (mapcar
  1518. (lambda (cell)
  1519. (org-export-secondary-string
  1520. (org-element-parse-secondary-string
  1521. cell
  1522. (cdr (assq 'table org-element-string-restrictions)))
  1523. 'e-latex info))
  1524. (org-split-string row "[ \t]*|[ \t]*"))))
  1525. (org-split-string clean-table "\n"))
  1526. `(:tstart nil :tend nil
  1527. ;; Longtable environment requires specific
  1528. ;; header line end.
  1529. :hlend ,(and attr
  1530. (string-match "\\<longtable\\>" attr)
  1531. (format "\\\\
  1532. \\hline
  1533. \\endhead
  1534. \\hline\\multicolumn{%d}{r}{Continued on next page}\\\\
  1535. \\endfoot
  1536. \\endlastfoot"
  1537. columns-number))))))))))
  1538. ;;;; Target
  1539. (defun org-e-latex-target (target text info)
  1540. "Transcode a TARGET object from Org to LaTeX.
  1541. TEXT is the text of the target. INFO is a plist holding
  1542. contextual information."
  1543. (format "\\label{%s}%s"
  1544. (org-export-solidify-link-text
  1545. (org-element-get-property :raw-value target))
  1546. text))
  1547. ;;;; Time-stamp
  1548. (defun org-e-latex-time-stamp (time-stamp contents info)
  1549. "Transcode a TIME-STAMP object from Org to LaTeX.
  1550. CONTENTS is nil. INFO is a plist holding contextual
  1551. information."
  1552. (let ((value (org-element-get-property :value time-stamp))
  1553. (type (org-element-get-property :type time-stamp))
  1554. (appt-type (org-element-get-property :appt-type time-stamp)))
  1555. (concat (cond ((eq appt-type 'scheduled)
  1556. (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
  1557. ((eq appt-type 'deadline)
  1558. (format "\\textbf{\\textsc{%s}} " org-deadline-string))
  1559. ((eq appt-type 'closed)
  1560. (format "\\textbf{\\textsc{%s}} " org-closed-string)))
  1561. (cond ((memq type '(active active-range))
  1562. (format org-e-latex-active-timestamp-format value))
  1563. ((memq type '(inactive inactive-range))
  1564. (format org-e-latex-inactive-timestamp-format value))
  1565. (t
  1566. (format org-e-latex-diary-timestamp-format value))))))
  1567. ;;;; Verbatim
  1568. (defun org-e-latex-verbatim (verbatim contents info)
  1569. "Transcode a VERBATIM object from Org to LaTeX.
  1570. CONTENTS is nil. INFO is a plist used as a communication
  1571. channel."
  1572. (let ((fmt (cdr (assoc (org-element-get-property :marker verbatim)
  1573. org-e-latex-emphasis-alist)))
  1574. (value (org-element-get-property :value verbatim)))
  1575. (cond
  1576. ;; Handle the `verb' special case.
  1577. ((eq 'verb fmt)
  1578. (let ((separator (org-e-latex--find-verb-separator value)))
  1579. (concat "\\verb" separator value separator)))
  1580. ;; Handle the `protectedtexttt' special case.
  1581. ((eq 'protectedtexttt fmt)
  1582. (let ((start 0)
  1583. (trans '(("\\" . "\\textbackslash{}")
  1584. ("~" . "\\textasciitilde{}")
  1585. ("^" . "\\textasciicircum{}")))
  1586. (rtn "")
  1587. char)
  1588. (while (string-match "[\\{}$%&_#~^]" value)
  1589. (setq char (match-string 0 value))
  1590. (if (> (match-beginning 0) 0)
  1591. (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
  1592. (setq value (substring value (1+ (match-beginning 0))))
  1593. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  1594. rtn (concat rtn char)))
  1595. (setq value (concat rtn value)
  1596. fmt "\\texttt{%s}")
  1597. (while (string-match "--" value)
  1598. (setq value (replace-match "-{}-" t t value)))
  1599. (format fmt value)))
  1600. ;; Else use format string.
  1601. (t (format fmt value)))))
  1602. ;;;; Verse Block
  1603. (defun org-e-latex-verse-block (verse-block contents info)
  1604. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  1605. CONTENTS is nil. INFO is a plist holding contextual information."
  1606. (org-e-latex--wrap-label
  1607. verse-block
  1608. ;; In a verse environment, add a line break to each newline
  1609. ;; character and change each white space at beginning of a line
  1610. ;; into a space of 1 em. Also change each blank line with
  1611. ;; a vertical space of 1 em.
  1612. (progn
  1613. (setq contents (replace-regexp-in-string
  1614. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  1615. (replace-regexp-in-string
  1616. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
  1617. (org-remove-indentation
  1618. (org-export-secondary-string
  1619. (org-element-get-property :value verse-block)
  1620. 'e-latex info)))))
  1621. (while (string-match "^[ \t]+" contents)
  1622. (let ((new-str (format "\\hspace*{%dem}"
  1623. (length (match-string 0 contents)))))
  1624. (setq contents (replace-match new-str nil t contents))))
  1625. (format "\\begin{verse}\n%s\\end{verse}" contents))))
  1626. ;;; Interactive functions
  1627. (defun org-e-latex-export-to-latex
  1628. (&optional subtreep visible-only body-only ext-plist pub-dir)
  1629. "Export current buffer to a LaTeX file.
  1630. If narrowing is active in the current buffer, only export its
  1631. narrowed part.
  1632. If a region is active, export that region.
  1633. When optional argument SUBTREEP is non-nil, export the sub-tree
  1634. at point, extracting information from the headline properties
  1635. first.
  1636. When optional argument VISIBLE-ONLY is non-nil, don't export
  1637. contents of hidden elements.
  1638. When optional argument BODY-ONLY is non-nil, only write code
  1639. between \"\\begin{document}\" and \"\\end{document}\".
  1640. EXT-PLIST, when provided, is a property list with external
  1641. parameters overriding Org default settings, but still inferior to
  1642. file-local settings.
  1643. When optional argument PUB-DIR is set, use it as the publishing
  1644. directory.
  1645. Return output file's name."
  1646. (interactive)
  1647. (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
  1648. (org-export-to-file
  1649. 'e-latex outfile subtreep visible-only body-only ext-plist)))
  1650. (defun org-e-latex-export-to-pdf
  1651. (&optional subtreep visible-only body-only ext-plist pub-dir)
  1652. "Export current buffer to LaTeX then process through to PDF.
  1653. If narrowing is active in the current buffer, only export its
  1654. narrowed part.
  1655. If a region is active, export that region.
  1656. When optional argument SUBTREEP is non-nil, export the sub-tree
  1657. at point, extracting information from the headline properties
  1658. first.
  1659. When optional argument VISIBLE-ONLY is non-nil, don't export
  1660. contents of hidden elements.
  1661. When optional argument BODY-ONLY is non-nil, only write code
  1662. between \"\\begin{document}\" and \"\\end{document}\".
  1663. EXT-PLIST, when provided, is a property list with external
  1664. parameters overriding Org default settings, but still inferior to
  1665. file-local settings.
  1666. When optional argument PUB-DIR is set, use it as the publishing
  1667. directory.
  1668. Return PDF file's name."
  1669. (interactive)
  1670. (org-e-latex-compile
  1671. (org-e-latex-export-to-latex
  1672. subtreep visible-only body-only ext-plist pub-dir)))
  1673. (defun org-e-latex-compile (texfile)
  1674. "Compile a TeX file.
  1675. TEXFILE is the name of the file being compiled. Processing is
  1676. done through the command specified in `org-e-latex-pdf-process'.
  1677. Return PDF file name or an error if it couldn't be produced."
  1678. (let* ((wconfig (current-window-configuration))
  1679. (texfile (file-truename texfile))
  1680. (base (file-name-sans-extension texfile))
  1681. errors)
  1682. (message (format "Processing LaTeX file " texfile "..." texfile))
  1683. (unwind-protect
  1684. (progn
  1685. (cond
  1686. ;; A function is provided: Apply it.
  1687. ((functionp org-latex-to-pdf-process)
  1688. (funcall org-latex-to-pdf-process (shell-quote-argument texfile)))
  1689. ;; A list is provided: Replace %b, %f and %o with appropriate
  1690. ;; values in each command before applying it. Output is
  1691. ;; redirected to "*Org PDF LaTeX Output*" buffer.
  1692. ((consp org-e-latex-pdf-process)
  1693. (let* ((out-dir (or (file-name-directory texfile) "./"))
  1694. (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
  1695. (mapc
  1696. (lambda (command)
  1697. (shell-command
  1698. (replace-regexp-in-string
  1699. "%b" (shell-quote-argument base)
  1700. (replace-regexp-in-string
  1701. "%f" (shell-quote-argument texfile)
  1702. (replace-regexp-in-string
  1703. "%o" (shell-quote-argument out-dir) command)))
  1704. outbuf))
  1705. org-e-latex-pdf-process)
  1706. ;; Collect standard errors from output buffer.
  1707. (setq errors (org-e-latex-collect-errors outbuf))))
  1708. (t (error "No valid command to process to PDF")))
  1709. (let ((pdffile (concat base ".pdf")))
  1710. ;; Check for process failure. Provide collected errors if
  1711. ;; possible.
  1712. (if (not (file-exists-p pdffile))
  1713. (error (concat (format "PDF file %s wasn't produced" pdffile)
  1714. (when errors (concat ": " errors))))
  1715. ;; Else remove log files, when specified, and signal end of
  1716. ;; process to user, along with any error encountered.
  1717. (when org-e-latex-remove-logfiles
  1718. (dolist (ext org-e-latex-logfiles-extensions)
  1719. (let ((file (concat base "." ext)))
  1720. (when (file-exists-p file) (delete-file file)))))
  1721. (message (concat "Process completed"
  1722. (if (not errors) "."
  1723. (concat " with errors: " errors)))))
  1724. ;; Return output file name.
  1725. pdffile))
  1726. (set-window-configuration wconfig))))
  1727. (defun org-e-latex-collect-errors (buffer)
  1728. "Collect some kind of errors from \"pdflatex\" command output.
  1729. BUFFER is the buffer containing output.
  1730. Return collected error types as a string, or nil if there was
  1731. none."
  1732. (with-current-buffer buffer
  1733. (save-excursion
  1734. (goto-char (point-max))
  1735. ;; Find final "pdflatex" run.
  1736. (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
  1737. (let ((case-fold-search t)
  1738. (errors ""))
  1739. (when (save-excursion
  1740. (re-search-forward "Reference.*?undefined" nil t))
  1741. (setq errors (concat errors " [undefined reference]")))
  1742. (when (save-excursion
  1743. (re-search-forward "Citation.*?undefined" nil t))
  1744. (setq errors (concat errors " [undefined citation]")))
  1745. (when (save-excursion
  1746. (re-search-forward "Undefined control sequence" nil t))
  1747. (setq errors (concat errors " [undefined control sequence]")))
  1748. (when (save-excursion
  1749. (re-search-forward "^! LaTeX.*?Error" nil t))
  1750. (setq errors (concat errors " [LaTeX error]")))
  1751. (when (save-excursion
  1752. (re-search-forward "^! Package.*?Error" nil t))
  1753. (setq errors (concat errors " [package error]")))
  1754. (and (org-string-nw-p errors) (org-trim errors)))))))
  1755. (provide 'org-e-latex)
  1756. ;;; org-e-latex.el ends here