Giter Club home page Giter Club logo

Comments (8)

stakemori avatar stakemori commented on June 1, 2024

I'm sorry, but I do not fully understand what you want. Of course, you can do the following.

#+BEGIN_SRC sage
  def my_latex(a):
      print r'\(%s\)' % latex(a)
#+END_SRC

#+RESULTS:

#+BEGIN_SRC sage :results raw
  K.<zeta> = CyclotomicField(8)
  h = -36*zeta^3 + 44*zeta^2 + 14*zeta + 28
  my_latex(h)
#+END_SRC

#+RESULTS:
\(-36 \zeta_{8}^{3} + 44 \zeta_{8}^{2} + 14 \zeta_{8} + 28\)

But do you want to do this without modifying the Sage code? If so, I will implement it when I have free time.

from ob-sagemath.

malb avatar malb commented on June 1, 2024

Hi, sorry for not being clear earlier. Indeed, I'd like to have an option such that I don't have to call my_latex myself, i.e. LaTeX output is handled transparently.

from ob-sagemath.

stakemori avatar stakemori commented on June 1, 2024

Hi. I added some support for LaTeX.
Because :results latex has a special meaning and it seems that we cannot use result parameters latex and raw at the same time, I added a new result parameter tolatex. Currently this feature is available only in the dev branch of ob-sagemath.

Here is an example.

#+BEGIN_SRC sage :results tolatex
  K.<zeta> = CyclotomicField(8)
  h = -36*zeta^3 + 44*zeta^2 + 14*zeta + 28; h
#+END_SRC

#+RESULTS:
: -36 \zeta_{8}^{3} + 44 \zeta_{8}^{2} + 14 \zeta_{8} + 28

The result is same as the output of the Sage's function latex. Because I implemented this by changing the display manager of Sage, the following code does not show LaTeX code.

#+BEGIN_SRC sage :results tolatex
  K.<zeta> = CyclotomicField(8)
  h = -36*zeta^3 + 44*zeta^2 + 14*zeta + 28
  print h
#+END_SRC

In your example, LaTeX parentheses \(...\) appeared. But not everyone needs them. So I added a new header argument :latexwrap.
Its value should be a cons and the result will be wrapped with car and cdr of it. Here is an example.

#+BEGIN_SRC sage :results raw tolatex :latexwrap '("\\(" . "\\)")
  K.<zeta> = CyclotomicField(8)
  h = -36*zeta^3 + 44*zeta^2 + 14*zeta + 28; h
#+END_SRC

#+RESULTS:
\(-36 \zeta_{8}^{3} + 44 \zeta_{8}^{2} + 14 \zeta_{8} + 28\)

I have implemented these features in the dev branch. But I'm not sure if it is a right solution to add such a new result parameter or a new header argument. So I will consider it before merging the dev branch into the master branch.

from ob-sagemath.

malb avatar malb commented on June 1, 2024

Thanks, that was quick! After looking at what ob-R.el does, it seems that a header argument is the more canonical solution, e.g. one which allows to do arbitrary post processing using a Sage function or so and the latex one is simply pre-implemented for convenience? However you decide, thanks!

from ob-sagemath.

stakemori avatar stakemori commented on June 1, 2024

Sorry for taking long time. I deleted :results tolatex and added a header argument :tolatex. :tolatex (or :tolatex yes) is same as :results tolatex in the previous version. One can pass a Python function to the header argument :tolatex. The Python function takes a Sage object and should return a LaTeX code (string). Here is an example.

#+BEGIN_SRC sage :tolatex lambda obj: r'\(%s\)' % latex(obj)
  sqrt(x)
#+END_SRC

#+RESULTS:
: \(\sqrt{x}\)

You can also use :latexwrap. If you want to always wrap the results by \(...\), then please try the following configuration:

(push '(:latexwrap ("\\(" . "\\)")) org-babel-default-header-args:sage)

Then the following code returns the same result above.

#+BEGIN_SRC sage :tolatex
  sqrt(x)
#+END_SRC

I also added a new header argument :latexnewline. One can pass an elisp string to it and if the result is a LaTeX code, the newline characters in the result will be replaced by the string. Here is an example of usage:

With the configuration

(push '(:latexnewline . "\\\\\n") org-babel-default-header-args:sage)

the code below

#+BEGIN_SRC sage :results raw :tolatex :latexwrap '("\\begin{gather*}\n" . "\n\\end{gather*}")
  sqrt(x)
  sqrt(x^2 + 1)
#+END_SRC

returns the following.

#+RESULTS:
\begin{gather*}
\sqrt{x}\\
\sqrt{x^{2} + 1}
\end{gather*}

Thanks for the suggestion!

from ob-sagemath.

stakemori avatar stakemori commented on June 1, 2024

I have just merged the dev branch to the master branch. Thanks.

from ob-sagemath.

malb avatar malb commented on June 1, 2024

Thanks! This is great.

from ob-sagemath.

EmmanuelCharpentier avatar EmmanuelCharpentier commented on June 1, 2024

This (closed) issue is the sole documentation of these features, which are, as far as I can tell, unique in the org-mode universe. They should be documented at least in the README.org file.

from ob-sagemath.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.