io12 / org-fragtog

Automatically toggle Org mode LaTeX fragment previews as the cursor enters and exits them
MIT License
397 stars 14 forks source link

question: preferred SIunitx solution for units #23

Closed japhir closed 3 years ago

japhir commented 3 years ago

In https://github.com/awth13/org-appear/issues/2 I briefly discuss how I would like to work with units in org-mode for latex and pdf export.

Below I wrote a quick test to help illustrate my struggles with how to write nicely in org-mode and have stuff export nicely and tweakable:

* test siunitx for org-mode
#+latex_header: \usepackage{siunitx}
# example extra units
#+latex_header: \DeclareSIUnit\annum{a} % a year, used in years before present
#+latex_header: \DeclareSIUnit\year{yr} % a duration

#+options: toc:nil

#+begin_src emacs-lisp :exports none
  (setq org-entities-user
   '(("celsius" "\\(^{\\circ}\\)C" nil "°C" "°C" "°C" "℃")
     ("us" "\\," nil " " " " " " " ")))
#+end_src

| name               | code                | output              | org-fragtog preview |
|--------------------+---------------------+---------------------+---------------------|
| plain              | ~35 m~                | 35 m                | NA                  |
| manual small space | ~35\(\,\)m~           | 35\(\,\)m           | yes                  |
| ~\us{}~ entity           | ~35\us{}m~                | 35\us{}m                | yes                 |
| inline SI          | ~\SI{35}{\metre}~     | \SI{35}{\metre}     | no                  |
| dollars            | ~$\SI{35}{\metre}$~   | $\SI{35}{\metre}$   | yes                 |
| round              | ~\(\SI{35}{\metre}\)~ | \(\SI{35}{\metre}\) | yes                 |
| square             | ~\[\SI{35}{\metre}\]~ | \[\SI{35}{\metre}\] | yes                 |

which looks as follows in my emacs: 2021-05-10-10:02:17_region (additional minor issue: table outlining fails when previewing LaTeX fragments)

and looks like this in the PDF export: 2021-05-10-10:01:25_region

What are your thoughts on the ideal approach here? Wrapping the \SI{xx}{xx} blocks in either $ or \(...\) seems to work nicely with the preview, but does put it all in math mode in the raw latex file (see details section below). So far I've preferred to work with the inline SI solution. Using the org-entity results in correct spacing but is less flexible than relying on SIunitx.

latex export of above file ``` latex % Created 2021-05-10 Mon 09:37 % Intended LaTeX compiler: pdflatex \documentclass[11pt]{article} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} \usepackage{graphicx} \usepackage{grffile} \usepackage{longtable} \usepackage{wrapfig} \usepackage{rotating} \usepackage[normalem]{ulem} \usepackage{amsmath} \usepackage{textcomp} \usepackage{amssymb} \usepackage{capt-of} \usepackage{hyperref} \usepackage{siunitx} \DeclareSIUnit\annum{a} % a year, used in years before present \DeclareSIUnit\year{yr} % a duration \author{Ilja Kocken} \date{\today} \title{} \begin{document} \section{test siunitx for org-mode} \label{sec:org91a7591} \begin{center} \begin{tabular}{lll} name & code & output\\ \hline plain & \texttt{35 m} & 35 m\\ manual small space & \texttt{35\textbackslash{}(\textbackslash{},\textbackslash{})m} & 35\(\,\)m\\ \texttt{\textbackslash{}us} entity & \texttt{35\textbackslash{}us\{\}m} & 35\,m\\ inline SI & \texttt{\textbackslash{}SI\{35\}\{\textbackslash{}metre\}} & \SI{35}{\metre}\\ dollars & \texttt{\$\textbackslash{}SI\{35\}\{\textbackslash{}metre\}\$} & \(\SI{35}{\metre}\)\\ round & \texttt{\textbackslash{}(\textbackslash{}SI\{35\}\{\textbackslash{}metre\}\textbackslash{})} & \(\SI{35}{\metre}\)\\ square & \texttt{\textbackslash{}[\textbackslash{}SI\{35\}\{\textbackslash{}metre\}\textbackslash{}]} & \[\SI{35}{\metre}\]\\ \end{tabular} \end{center} \end{document} ```
io12 commented 3 years ago

It looks like org-mode doesn't have any awareness of \SI being a LaTeX thing and just inserts the backslash text verbatim into the output. Does something like \begin{flushleft}\SI{35}{\metre}\end{flushleft} work? It should display the fragment and render in text mode. Using macros might make this easier to type. There might be a better solution I'm not thinking of now, though.

japhir commented 3 years ago

Does something like \begin{flushleft}\SI{35}{\metre}\end{flushleft} work?

yes, this works and exports with the \begin{..}..\end{...} parts around it.

io12 commented 3 years ago

I can't think of any easy way to get the export to not have anything around it while still rendering the fragment, sorry. You would probably need to add another extension to org-mode, to treat \SI{}{} as a fragment. I am going to close this issue for now.