emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Richard H Stanton <rhstanton@berkeley.edu>
To: Rens Oliemans <hallo@rensoliemans.nl>
Cc: emacs-orgmode@gnu.org
Subject: Re: How to specify column alignment in LaTeX table output?
Date: Fri, 10 Jan 2025 08:59:57 -0800	[thread overview]
Message-ID: <DAC01FCA-0A74-47A1-9C55-FBF44B4E06CA@berkeley.edu> (raw)
In-Reply-To: <87wmf3vw3i.fsf@rensoliemans.nl>


> On Jan 10, 2025, at 1:47 AM, Rens Oliemans <hallo@rensoliemans.nl> wrote:
> 
> Richard H Stanton <rhstanton@berkeley.edu> writes:
> 
>> what are the recommended headers for a Python code block that exports a table? For example, ":results output raw” and ":results output drawer” both seem to work (without :wrap), while “:results output” puts everything in an example block, which then seems to get exported verbatim, not as a LaTeX table.
> 
> Take a look at
> https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-python.html. I found
> this link in the Org manual, "(org) Results of Evaluation", and then to
> "Documentation" link of python. From that page:
> 
>    :results {output, value}: Output results come from whatever the python code
>    prints on stdout. Value results are the value of the last expression
>    evaluated in the code block. Value mode is the default (as with other
>    languages). In value mode you can use the following subtypes:
> 
>        verbatim: value is returned as string. In particular, use this to
>        prevent conversion of lists and tuples to tables.
> 
>        table: (Org 9.7+) Try to convert the result to an Org table. Dicts,
>        numpy arrays, and pandas DataFrames/Series can be returned as tables
>        this way (by default, they are printed verbatim). Note that lists and
>        tuples are already converted to table by default (use verbatim to
>        prevent that).
> 
> So, ':results output' will output whatever python prints to stdout. With
> 'print()', I think that this will just be a string, and no conversion will take
> place. If you specify 'output raw', python will still just print the string, but
> *Org mode* will now interpret it as raw Org mode. This will probably work fine,
> but you'll have to add '|' and newlines in your string correctly. Alternative:
> 
> ':results value', the default. In this case ob-python will convert lists and
> tuples to tables by default (and optionally, dicts and DataFrames as well).
> 
> That would be my recommendation: a code block with the default headers, which
> returns a list of lists. I've attached an org file which does this.
> 
>> I actually generate this table from a Python code block. In the past, I’ve often run into problems with raw output (like this), where running the code block multiple times causes the output to appear multiple times, rather than overwriting.
> 
> ':results append' will cause the output to appear multiple times, but I expect
> that it happened due to other circumstances (':results replace' is default and I
> expect you didn't change that). One such circumstance is when you, say, add a
> ':results output' header argument, and then change it back. For reasons unknown
> to me, Org mode will then see the previous #+RESULTS: block as unrelated to the
> current one, and will not replace it.
> 
> This is then kind of annoying, because you'll have to add the #+ATTR_LATEX: line
> to the table again. I'd fix this manually, but if it gets annoying you can use
> the suggestion by PA.


Thanks, Rens. Lots of useful information!

Regarding output appearing multiple times, I realize it’s not tables that are the prime offender. It’s more when I’m outputting text, usually trying to get Python to generate LaTeX code, e.g., after symbolically solving a system of equations in Python.

Here’s an example:

#+begin_src python :results output replace raw 
print("a")
#+end_src

Every time I run this code block, I get another line containing “a”. If I don't use the raw option, e.g.,

#+begin_src python :results output
print("a")
#+end_src

the multiple-output problem goes away, but now it appears as 

#+RESULTS:
: a

The extra “: “ interferes with LaTeX if I’ve just output something like “\begin{equation}”, which is why I’m using raw in the first place.

Wrapping the output in a LaTeX environment helps, e.g.,

#+begin_src python :results output raw :wrap flushleft
print("a")
#+end_src

But is there a “preferred” way to output arbitrary text (e.g., LaTeX equations) from Python code blocks so that they compile fine *and* don’t append?

Thanks for this discussion. This is about where I get to every time I think I want to use org mode to create LaTeX documents with embedded, live calculations, and then after wrestling with the headers for a while I tend to go back again to separate .py and .tex files controlled by GNU Make...







  parent reply	other threads:[~2025-01-10 17:01 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-09  9:38 How to specify column alignment in LaTeX table output? Richard H Stanton
2025-01-09 10:17 ` Rens Oliemans
2025-01-09 15:02   ` Richard H Stanton
2025-01-10  9:47     ` Rens Oliemans
2025-01-10  9:53       ` Rens Oliemans
2025-01-10 16:59       ` Richard H Stanton [this message]
2025-01-10 18:59         ` Rens Oliemans
2025-01-10 19:16           ` Richard H Stanton
2025-01-10 19:38           ` Richard H Stanton
2025-01-10 19:45             ` Ihor Radchenko
2025-01-10 19:04         ` Richard H Stanton
  -- strict thread matches above, loose matches on Subject: below --
2025-01-10  6:13 Pedro Andres Aranda Gutierrez
2025-01-10 17:04 ` Richard H Stanton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DAC01FCA-0A74-47A1-9C55-FBF44B4E06CA@berkeley.edu \
    --to=rhstanton@berkeley.edu \
    --cc=emacs-orgmode@gnu.org \
    --cc=hallo@rensoliemans.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).