Export highlighting in html when using bookmark+ in org-mode - org-mode

I use the extension BookMark+ for Emacs in order to highlight permanently section of texts.
Is there anyway to mirror the highlighting when using normal html export ?
And for reveal.js export ?
I have been advised to add the txt file as well, it simplifies the help process : TextFile

It is not easy to integrate this with the org-mode export, but you can probably do this to get an html file with highlighting.
(let* ((html-buffer (htmlize-buffer))
(html (with-current-buffer html-buffer
(buffer-string))))
(with-temp-file "test.html"
(insert html))
(kill-buffer html-buffer))
(browse-url "test.html")

Related

HTML to Text Conversion in Emacs

I have a bunch of org-mode files with snippets containing HTML code and I would like to convert those to plain text.
I don't need any fancy fully automated solution, I can just past my HTML snippet into a scratch buffer if that's easier.
Here's a simple example of desired behavior:
<div><div>First Line<br>Second Line</div></div>
First Line
Second Line
What are the options available to Emacs users for such a task?
Emacs added EWW in Emacs 24.4 (2014), the Emacs Web Wowser, a built-in web browser . The shr.el library is used for rendering HTML, e.g.,
(with-temp-buffer
(insert
"<div><div>First Line<br>Second Line</div></div> ")
(shr-render-region (point-min) (point-max))
(buffer-substring-no-properties (point-min) (point-max)))
;; =>
"First Line
Second Line
"
shr-render-region uses libxml-parse-html-region which requires your Emacs has libxml2 support.
html2org package seems to get the job done
html2org function converts and replaces the HTML code as text.

Disable title in org latex export

Is it possible to completely disable the title, i.e. not even \title{}, when export org file to latex? I am using org mode to write paper the latex template provided by the publisher does not allow \title{} command appears before \begin{document}
I tried many solutions found online but neither of them works with the latex template I am using. Currently, I put #+BIND: org-latex-title-command " in my org file. From the source code in ox-latex, I found the following code inside org-latex-template (contents info):
;; Title and subtitle.
(let* ((subtitle (plist-get info :subtitle))
(formatted-subtitle
(when subtitle
(format (plist-get info :latex-subtitle-format)
(org-export-data subtitle info))))
(separate (plist-get info :latex-subtitle-separate)))
(concat
(format "\\title{%s%s}\n" title
(if separate "" (or formatted-subtitle "")))
(when (and separate subtitle)
(concat formatted-subtitle "\n"))))
Does this mean that there is no way to get rid of the \title{} command in the exported latex file?
Thanks for your assistance!
I came up with this little advice function that removes the \title{...} line from the output:
(defun my-org-latex-remove-title (str)
(replace-regexp-in-string "^\\\\title{.*}$" "" str))
(advice-add 'org-latex-template :filter-return 'my-org-latex-remove-title)

How to export org file to HTML file when save?

I want to export my org files to HTML files to certain directory when save.
I can use Emacs and Org-mode but I don't know Elisp.
With Org-Mode 8.3 and Emacs 24.5.1 the accepted answer creates a pseudo-buffer *Org HTML Export* that you have to save manually, while the key C-c C-e h h more conveniently saves the file directly.
To really auto-export in the background try the following code:
# Local variables:
# eval: (add-hook 'after-save-hook 'org-html-export-to-html t t)
# end:
You can combine this solution with the following function in your .emacs:
(defun toggle-html-export-on-save ()
"Enable or disable export HTML when saving current buffer."
(interactive)
(when (not (eq major-mode 'org-mode))
(error "Not an org-mode file!"))
(if (memq 'org-html-export-to-html after-save-hook)
(progn (remove-hook 'after-save-hook 'org-html-export-to-html t)
(message "Disabled org html export on save"))
(add-hook 'after-save-hook 'org-html-export-to-html nil t)
(set-buffer-modified-p t)
(message "Enabled org html export on save")))
Note: The below was written for Emacs 23. Check the answer by #AndreasSpindler for an up-to-date solution.
Emacs has a couple of hooks which are called in certain events. The hook you are looking for is probably the after-save-hook. Just set it to the function you want to run every time you save the file. In your case this would be org-html-export-to-html.
There are many ways to do this, but the following method is probably the fastest and doesn't involve any "real" elisp. Put the following lines somewhere in your org file:
# Local variables:
# after-save-hook: org-html-export-to-html
# end:
The next time you open that file, you'll get a warning and be asked if the local variable should be set (as that's potentially unsafe, but not a problem here). Press y and everything should just work.
The command for this is
C-c C-e h h (org-html-export-to-html)
Export as an HTML file. For an Org file myfile.org, the HTML file will be myfile.html. The file will be overwritten without warning. C-c C-e h o Export as an HTML file and immediately open it with a browser.
Reference

org-mode export to Latex — suppress generation of labels

I'm using org-mode to write a report which I then export to LaTeX.
I have a few different .org files (one per chapter), which I export as "headless" LaTeX and then combine in a master .tex file.
This works nicely, except that the generated .tex files contain labels with conflicting numbers. So both a.tex and b.tex contain \label{sec-1}, for example.
As long as I never actually use these references then it's not much of a problem I think, although the warnings do annoy me. Is there any way to turn off the generation of these labels? It should be simple but I cannot find anything about this in the documentation.
I have written a bit of Lisp which will remove said labels after the export to LaTeX, which looks like this:
(defun remove-orgmode-latex-labels ()
"Remove labels generated by org-mode"
(interactive)
(let ((case-fold-search nil))
(goto-char 1)
(replace-regexp "\\\\label{sec-[0-9][^}]*}" "")
)
)
(add-hook 'org-export-latex-final-hook 'remove-orgmode-latex-labels)
This seems to do the job without removing my own custom labels.
Why not writing your full report as one big Org file?
Anyway, if your prefer having multiple smaller files, I would advice "including" them in one Org master file, as this:
* Chapter 1
#+INCLUDE: "chapter1.org"
* Chapter 2
#+INCLUDE: "chapter2.org"
That way, Org sees only one file (then, I guess that your problem simply disappears), while you edit them as you wish.
As of now (18. 05. 2021) correct solution is this:
(defun my-latex-filter-removeOrgAutoLabels (text backend info)
"Org-mode automatically generates labels for headings despite explicit use of `#+LABEL`. This filter forcibly removes all automatically generated org-labels in headings."
(when (org-export-derived-backend-p backend 'latex)
(replace-regexp-in-string "\\\\label{sec:org[a-f0-9]+}\n" "" text)))
(add-to-list 'org-export-filter-headline-functions
'my-latex-filter-removeOrgAutoLabels)
Only slight modification of previous answer.
This is what worked for me with recent (2020) Org-Mode:
(defun rm-org-latex-labels (text backend _info)
"Remove labels auto-generated by `org-mode' export to LaTeX."
(when (eq backend 'latex)
(replace-regexp-in-string "\\\\label{sec:org[a-f0-9]+}\n" "" text)))
(add-to-list #'org-export-filter-headline-functions
#'rm-org-latex-labels)

Customizing org-mode exports

So, I have been using org-mode for taking my research notes for some time now. I love how I can seamlessly export to both latex (for my papers) and html (for my blog). However, whenever I define macros with \newcommand with #+LATEX_HEADER, these do not show up in the HTML export at all.
I currently handle this by putting the all these commands as
(\newcommand \newcommand etc. etc.)
at the top and then manually removing the "(" and ")" from the tex file.
What I wish I could do was to keep a drawer for these commands and customize html and latex export of org mode to handle this drawer appropriately.
For example, I would add the following in the org file:
:LATEX_MACROS:
\newcommand{\norm}[1]{\lVert{#1}\rVert}
\newcommand{\abs}[1]{\lvert{#1}\rvert}
\newcommand{\half}{\frac{1}{2}}
:END:
And after export, this shows up in the latex file verbatim in header section
and in the html file as
\(
\newcommand{\norm}[1]{\lVert{#1}\rVert}
\newcommand{\abs}[1]{\lvert{#1}\rvert}
\newcommand{\half}{\frac{1}{2}}
\)
Here's a standalone way that works with Org, pdflatex and MathJax at the time of this writing. Make sure the drawers (or at least the LATEXMACROS drawer) are exported (this is the default), then insert near the top of the Org file:
#+OPTIONS: toc:nil
#+DRAWERS: LATEXMACROS ...
:LATEXMACROS:
##html:<div style="display: none">##
\(
\global\def\mymacro{...}
\global\def\mymacrow2args#1#2{...}
...
\)
##html:</div>##
:END:
#+TOC: headlines
This works around the following problems:
We have to use a math environment (here \( ... \)), otherwise Org escapes the TeX syntax.
We cannot use \newcommand, because LaTeX expects them to be in the preamble and MathJaX does not receive it. \newcommand can be used outside of the preamble, but then LaTeX (unlike MathJax) restricts the defined macros to the current math environment. We usually want to use them anywhere in the file.
We cannot use a plain \def because it behaves like \newcommand in terms of scoping (global for MathJax, local for LaTeX). We cannot use \xdef either because MathJax does not know it.
MathJax does not know \global but that won't prevent it from using \def which is global anyway. However, it will print a red warning for each \global in the web page. To get rid of them, we put the math environment inside an undisplayed HTML section.
For MathJax, the macros won't be defined in time for the table of content in its default location. If you use macros in headlines and want a TOC, disable the default with #+OPTIONS: toc:nil and manually add it after the drawer with #+TOC: headlines.
Caveats:
It's not possible to preview latex fragments which use custom macros, because the small TeX file Org build won't include our environment.
Unlike \newcommand, \def silently replaces anything. Also, its syntax is slightly different.
The math environment takes up some vertical space in the output of pdflatex, so we have to put it where that doesn't matter (e.g. before the first headline).
This is probably really version dependent:
Org mode may get better at exporting LaTeX macros (e.g. sending them to MathJax by being smarter when parsing #+LATEX_HEADER, providing a new export option, not escaping \newcommand and putting those into a correctly handled preamble).
MathJax may start accepting \xdef as an alias of \def or ignoring \global (so that the HTML section trick is not needed anymore).
I figured out how to do it myself. Note that this is perhaps not the most elegant solution since it does not place the latex part in the beginning of the latex file (i.e. outside \begin{document}), but it works well enough for me.
(setq org-export-blocks
(cons '(latexmacro org-export-blocks-latexmacro) org-export-blocks))
(defun org-export-blocks-latexmacro (body &rest headers)
(message "exporting latex macros")
(cond
((eq org-export-current-backend 'html) (concat "\\(" body "\\)"))
((eq org-export-current-backend 'latex) body)
(t nil))
)
Alternative solution (not stand-alone), using Org's dynamic blocks.
It has none of the caveats of my original solution (because it generates what Org actually expects for LaTeX or HTML)
It's possible to edit the macros in LaTeX mode (C-c C-')
Callback
Create a file named org-dblock-write:block-macro.el with the following content and add it to Emacs' load path.
(defun org-dblock-write:block-macro (params)
(let ((block-name (or (plist-get params :from) "macros"))
(org-buf (current-buffer)))
(with-temp-buffer
(let ((tmp-buf (current-buffer)))
(set-buffer org-buf)
(save-excursion
(org-babel-goto-named-src-block block-name)
(org-babel-mark-block)
(let ((mblock-begin (region-beginning))
(mblock-end (region-end)))
(set-buffer tmp-buf)
(insert-buffer-substring org-buf mblock-begin mblock-end)))
(set-buffer org-buf)
(insert "#+BEGIN_HTML\n\\(\n")
(insert-buffer-substring tmp-buf)
(insert "\\)\n#+END_HTML\n")
(set-buffer tmp-buf)
(beginning-of-buffer)
(while (re-search-forward "^" nil t)
(replace-match "#+LATEX_HEADER: " nil nil))
(set-buffer org-buf)
(insert-buffer-substring tmp-buf)))))
Org file
Somewhere in the file, create:
A LaTeX source block named "macros" containing your macros
An empty block-macro dynamic block
You can change the name of the source block, and use a :from <custom-name> header argument in the dynamic block. Also, note the :exports none in the source block (usually you don't want to export the LaTeX source).
#+NAME: macros
#+BEGIN_SRC latex :exports none
\newcommand\a{a}
\def\b{b}
\DeclareMathOperator\c{c}
#+END_SRC
#+BEGIN: block-macro
#+END:
Now use C-c C-c with the point on the dynamic block, and it will update to:
#+BEGIN: block-macro
#+BEGIN_HTML
\(
\newcommand\a{a}
\def\b{b}
\DeclareMathOperator\c{c}
\)
#+END_HTML
#+LATEX_HEADER: \newcommand\a{a}
#+LATEX_HEADER: \def\b{b}
#+LATEX_HEADER: \DeclareMathOperator\c{c}
#+LATEX_HEADER:
#+END:
Do this whenever the macros are modified.
I usually have a file with many custom definitions which I reuse for many documents and I want to use it also in my org documents.
The following is a modification of Blout's answer, so please read his answer for more info.
Define macro
(defun org-dblock-write:insert-latex-macros (params)
(let ((text)
(file (plist-get params :file)))
(with-temp-buffer
(insert-file file)
(setq text (split-string (buffer-string) "\n" t)))
(insert (mapconcat (lambda (str) (concat "#+LATEX_HEADER: " str)) text "\n"))
(insert "\n#+BEGIN_HTML\n\\(\n")
(insert (mapconcat 'identity text "\n"))
(insert "\n\\)\n#+END_HTML")))
Usage
File macros.tex:
\newcommand\a{a}
\def\b{b}
\DeclareMathOperator\c{c}
In org file:
#+BEGIN: insert-latex-macros :file "macros.tex"
#+BEGIN_HTML
\(
\newcommand\a{a}
\def\b{b}
\DeclareMathOperator\c{c}
\)
#+END_HTML
#+LATEX_HEADER: \newcommand\a{a}
#+LATEX_HEADER: \def\b{b}
#+LATEX_HEADER: \DeclareMathOperator\c{c}
#+LATEX_HEADER:
#+END: