emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* SymPy equations + displayed equation + label = difficult?
@ 2021-03-23 16:21 autofrettage
  0 siblings, 0 replies; only message in thread
From: autofrettage @ 2021-03-23 16:21 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

Hi all,

I have tried to use SymPy in Org Mode code blocks, export LaTeX
expressions to displayed equations, and refer to those with
labels. My current solution is ugly, which has left me with a
strong feeling that there must be a better way.

Sensitive Org Mode users are warned. Here comes a brief
description of my current solution.

* The equation is defined inside a SymPy block, e.g.:

   a, R, L = symbols('\\alpha R L')
   equ = Eq(R, L * sin(a))

* The LaTeX representation for equ, and the rest of the LaTeX
  code needed for producing a displayed equation, with a label,
  are generated with the Python function expequ:

  #+BEGIN_SRC python :session :results none :exports none
  def expequ(expr, label):
     # will write out code for export of SymPy equation
     expression = latex(expr, mode="plain")
     print("""
     \\begin{equation}
        \\label{%(label)s}
        %(expr)s
     \\end{equation}""" % {'expr': expression, 'label': label})
  #+END_SRC

* At the place where I want to display equ, I call expequ like
  this:

  #+HEADER:  :results output latex overwrite :exports results
  #+BEGIN_SRC python :session
     expequ(equ2,"equ:labeltest")
  #+END_SRC

* ...which yields:

  #+RESULTS:
  #+BEGIN_EXPORT latex
  \begin{equation}
     \label{equ:labeltest}
     R = L \sin{\left(\alpha \right)}
  \end{equation}
  #+END_EXPORT

How many painfully obvious solutions have I missed?

Yours
Rasmus



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-03-23 16:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-23 16:21 SymPy equations + displayed equation + label = difficult? autofrettage

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).