emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* How to specify column alignment in LaTeX table output?
@ 2025-01-09  9:38 Richard H Stanton
  2025-01-09 10:17 ` Rens Oliemans
  0 siblings, 1 reply; 13+ messages in thread
From: Richard H Stanton @ 2025-01-09  9:38 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1261 bytes --]

Here’s a table in my org document:

#+ATTR_LATEX: :align rrrr :options [htbp]
#+begin_table
|-------+--------+-----------+------------|
|  Tier | Number |     Total | % of Total |
|-------+--------+-----------+------------|
|     1 |      4 |  8,700.00 |      57.39 |
|     2 |     19 |  5,398.00 |      35.61 |
|     3 |     24 |  1,061.40 |       7.00 |
|-------+--------+-----------+------------|
| Total |     47 | 15,159.40 |     100.00 |
|-------+--------+-----------+------------|
#+end_table

I want all the columns to be right-aligned (as they are in the org document), but when exported to LaTeX/PDF, the “Total” column in the output table is left aligned instead. Here’s the relevant section of the .tex document generated during PDF export:

\begin{table}[htbp]
\begin{center}
\begin{tabular}{rrlr}
\hline
Tier & Number & Total & \% of Total\\
\hline
1 & 4 & 8,700.00 & 57.39\\
2 & 19 & 5,398.00 & 35.61\\
3 & 24 & 1,061.40 & 7.00\\
\hline
Total & 47 & 15,159.40 & 100.00\\
\hline
\end{tabular}

\end{center}
\end{table}

What am I missing? If relevant, org-version returns

Org mode version 9.8-pre (release_9.7.18-223-g7ef659 @ /Users/stanton/.emacs.d/straight/build/org/)

Thanks for any suggestions.

[-- Attachment #2: Type: text/html, Size: 2967 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to specify column alignment in LaTeX table output?
  2025-01-09  9:38 Richard H Stanton
@ 2025-01-09 10:17 ` Rens Oliemans
  2025-01-09 15:02   ` Richard H Stanton
  0 siblings, 1 reply; 13+ messages in thread
From: Rens Oliemans @ 2025-01-09 10:17 UTC (permalink / raw)
  To: Richard H Stanton, emacs-orgmode

Richard H Stanton <rhstanton@berkeley.edu> writes:

> Here’s a table in my org document:
>
> #+ATTR_LATEX: :align rrrr :options [htbp]
> #+begin_table
> |-------+--------+-----------+------------|
> |  Tier | Number |     Total | % of Total |
> |-------+--------+-----------+------------|
> |     1 |      4 |  8,700.00 |      57.39 |
> |     2 |     19 |  5,398.00 |      35.61 |
> |     3 |     24 |  1,061.40 |       7.00 |
> |-------+--------+-----------+------------|
> | Total |     47 | 15,159.40 |     100.00 |
> |-------+--------+-----------+------------|
> #+end_table
>
> I want all the columns to be right-aligned (as they are in the org document), but when exported to LaTeX/PDF, the “Total” column in the output table is left aligned instead. Here’s the relevant section of the .tex document generated during PDF export:

Remove the #+begin_table and #+end_table parts: table isn't a valid block type.
'C-C C-,' (org-insert-structure-template) shows you what blocks are valid.

A table is simply defined via lines starting with '|'. This should work:

#+ATTR_LATEX: :align rrrr
|-------+--------+-----------+------------|
|  Tier | Number |     Total | % of Total |
|-------+--------+-----------+------------|
|     1 |      4 |  8,700.00 |      57.39 |
|     2 |     19 |  5,398.00 |      35.61 |
|     3 |     24 |  1,061.40 |       7.00 |
|-------+--------+-----------+------------|
| Total |     47 | 15,159.40 |     100.00 |
|-------+--------+-----------+------------|

Also, I think your :options is a bit weird. Your exported LaTeX code seems fine,
but that's a side effect of you having the #+begin_table blocks, I _think_ Org
creates a special 'table' environment based on your block name ('table'), which
makes your :options work. See "(org) Special blocks in LaTeX export" in the
manual.

To make your desired placement work with a "correct" table, see "(org) Tables in
LaTeX export" and the documentation of ':placement':

    ‘:float’
    ‘:placement’
         The table environments by default are not floats in LaTeX.  To make
         them floating objects use ‘:float’ with one of the following
         options: ‘t’ (for a default ‘table’ environment), ‘sideways’ (for a
         ‘sidewaystable’ environment), ‘multicolumn’ (to span the table
         across multiple columns of a page in a ‘table*’ environment) and
         ‘nil’.  In addition to these three values, ‘:float’ can pass
         through any arbitrary value, for example a user-defined float type
         with the ‘float’ LaTeX package.

         LaTeX floats can also have additional layout ‘:placement’
         attributes.  These are the usual ‘[h t b p ! H]’ permissions
         specified in square brackets.  Note that for ‘:float sideways’
         tables, the LaTeX export backend ignores ‘:placement’ attributes.

I think that this org snippet is what you are looking for:

#+ATTR_LATEX: :align rrrr :float t :placement [htbp]
|-------+--------+-----------+------------|
|  Tier | Number |     Total | % of Total |
|-------+--------+-----------+------------|
|     1 |      4 |  8,700.00 |      57.39 |
|     2 |     19 |  5,398.00 |      35.61 |
|     3 |     24 |  1,061.40 |       7.00 |
|-------+--------+-----------+------------|
| Total |     47 | 15,159.40 |     100.00 |
|-------+--------+-----------+------------|

-- 
Best,
Rens


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to specify column alignment in LaTeX table output?
  2025-01-09 10:17 ` Rens Oliemans
@ 2025-01-09 15:02   ` Richard H Stanton
  2025-01-10  9:47     ` Rens Oliemans
  0 siblings, 1 reply; 13+ messages in thread
From: Richard H Stanton @ 2025-01-09 15:02 UTC (permalink / raw)
  To: Rens Oliemans; +Cc: emacs-orgmode

On Jan 9, 2025, at 2:17 AM, Rens Oliemans <hallo@rensoliemans.nl> wrote:
> 
> Richard H Stanton <rhstanton@berkeley.edu> writes:
> 
>> Here’s a table in my org document:
>> 
>> #+ATTR_LATEX: :align rrrr :options [htbp]
>> #+begin_table
>> |-------+--------+-----------+------------|
>> |  Tier | Number |     Total | % of Total |
>> |-------+--------+-----------+------------|
>> |     1 |      4 |  8,700.00 |      57.39 |
>> |     2 |     19 |  5,398.00 |      35.61 |
>> |     3 |     24 |  1,061.40 |       7.00 |
>> |-------+--------+-----------+------------|
>> | Total |     47 | 15,159.40 |     100.00 |
>> |-------+--------+-----------+------------|
>> #+end_table
>> 
>> I want all the columns to be right-aligned (as they are in the org document), but when exported to LaTeX/PDF, the “Total” column in the output table is left aligned instead. Here’s the relevant section of the .tex document generated during PDF export:
> 
> Remove the #+begin_table and #+end_table parts: table isn't a valid block type.
> 'C-C C-,' (org-insert-structure-template) shows you what blocks are valid.
> 
> A table is simply defined via lines starting with '|'. This should work:
> 
> #+ATTR_LATEX: :align rrrr
> |-------+--------+-----------+------------|
> |  Tier | Number |     Total | % of Total |
> |-------+--------+-----------+------------|
> |     1 |      4 |  8,700.00 |      57.39 |
> |     2 |     19 |  5,398.00 |      35.61 |
> |     3 |     24 |  1,061.40 |       7.00 |
> |-------+--------+-----------+------------|
> | Total |     47 | 15,159.40 |     100.00 |
> |-------+--------+-----------+------------|
> 
> Also, I think your :options is a bit weird. Your exported LaTeX code seems fine,
> but that's a side effect of you having the #+begin_table blocks, I _think_ Org
> creates a special 'table' environment based on your block name ('table'), which
> makes your :options work. See "(org) Special blocks in LaTeX export" in the
> manual.
> 
> To make your desired placement work with a "correct" table, see "(org) Tables in
> LaTeX export" and the documentation of ':placement':
> 
>    ‘:float’
>    ‘:placement’
>         The table environments by default are not floats in LaTeX.  To make
>         them floating objects use ‘:float’ with one of the following
>         options: ‘t’ (for a default ‘table’ environment), ‘sideways’ (for a
>         ‘sidewaystable’ environment), ‘multicolumn’ (to span the table
>         across multiple columns of a page in a ‘table*’ environment) and
>         ‘nil’.  In addition to these three values, ‘:float’ can pass
>         through any arbitrary value, for example a user-defined float type
>         with the ‘float’ LaTeX package.
> 
>         LaTeX floats can also have additional layout ‘:placement’
>         attributes.  These are the usual ‘[h t b p ! H]’ permissions
>         specified in square brackets.  Note that for ‘:float sideways’
>         tables, the LaTeX export backend ignores ‘:placement’ attributes.
> 
> I think that this org snippet is what you are looking for:
> 
> #+ATTR_LATEX: :align rrrr :float t :placement [htbp]
> |-------+--------+-----------+------------|
> |  Tier | Number |     Total | % of Total |
> |-------+--------+-----------+------------|
> |     1 |      4 |  8,700.00 |      57.39 |
> |     2 |     19 |  5,398.00 |      35.61 |
> |     3 |     24 |  1,061.40 |       7.00 |
> |-------+--------+-----------+------------|
> | Total |     47 | 15,159.40 |     100.00 |
> |-------+--------+-----------+------------|
> 
> -- 
> Best,
> Rens

Thanks, Rens. That works and looks a lot neater than my version, which I arrived at through trial and (mostly) error...

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. I found that using :wrap seems to prevent this. I have no idea where “:wrap table” comes from, but org exported this without error, so I stopped thinking about it… Trying it with raw output just now, the table seems to be overwritten just fine when I run the code block multiple times. So maybe I don’t need to worry about this any more, but 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.

Best,

Richard






^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Re: How to specify column alignment in LaTeX table output?
@ 2025-01-10  6:13 Pedro Andres Aranda Gutierrez
  2025-01-10 17:04 ` Richard H Stanton
  0 siblings, 1 reply; 13+ messages in thread
From: Pedro Andres Aranda Gutierrez @ 2025-01-10  6:13 UTC (permalink / raw)
  To: hallo, rhstanton; +Cc: Org Mode List

[-- Attachment #1: Type: text/plain, Size: 1286 bytes --]

Hi,

For org tables generated in Python, I use python-tabulate. Actually, I
forked the original library to include the possibility of generating the
latex attributes from Python too.

Something like:

--- cut here ---
#+BEGIN_SRC python :results value raw :exports results
import math
import pandas as pd
import tabulate

xvals = [math.pi * i / 5 for i in range(10)]
df = pd.DataFrame({
    'x': xvals,
    'sin(x)' : [math.sin(xvals[i]) for i in range (10)],
    'cos(x)' : [math.cos(xvals[i]) for i in range (10)]
})

attrs=':environment longtable :align p{2cm}p{2cm}p{2cm} :placement [h]
:center t'
return tabulate.tabulate(
    df,
    headers=df.columns, tablefmt='orgtbl', floatfmt=".3f",
    caption='Table exported from Python with extended tabulate',
    label='labextend',
    attr_latex=attrs,
    showindex=False)
#+END_SRC
#+RESULTS:
--- cut here ---

Take a look at https://github.com/paaguti/python-tabulate if it sounds
interesting.

CAVEAT: the author of python-tabulate wasn't very excited about this ;-)

Best, /PA
-- 
Fragen sind nicht da, um beantwortet zu werden,
Fragen sind da um gestellt zu werden
Georg Kreisler

Headaches with a Juju log:
unit-basic-16: 09:17:36 WARNING juju.worker.uniter.operation we should run
a leader-deposed hook here, but we can't yet

[-- Attachment #2: Type: text/html, Size: 1948 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to specify column alignment in LaTeX table output?
  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
  0 siblings, 2 replies; 13+ messages in thread
From: Rens Oliemans @ 2025-01-10  9:47 UTC (permalink / raw)
  To: Richard H Stanton; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 2949 bytes --]

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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: test.org --]
[-- Type: text/org, Size: 1438 bytes --]

#+begin_src python
  import random
  from collections import namedtuple

  Tier = namedtuple('Tier', ['number', 'total'])

  def generate_table(tiers):
      total = sum(tier.total for tier in tiers)
      total_numbers = sum(tier.number for tier in tiers)

      # A None in a list will result in a hline in org tables
      return [
          None,  # Header separator
          ('Tier', 'Number', 'Total', '% of Total'),
          None,  # Column header separator
          ,*[
              (i+1, tier.number, f'{tier.total:,.2f}', f'{100 * tier.total / total:.2f}')
              for i, tier in enumerate(tiers)
          ],
          None,  # Footer separator
          ('Total', total_numbers, f'{total:,.2f}', '100.00'),
          None,  # Final separator
      ]

  tiers = [
      Tier(4,  random.uniform(1000, 10000)),
      Tier(19, random.uniform(1000, 10000)),
      Tier(24, random.uniform(1000, 10000)),
  ]
  return generate_table(tiers)
#+end_src

#+RESULTS:
|-------+--------+-----------+------------|
|  Tier | Number | Total     | % of Total |
|-------+--------+-----------+------------|
|     1 |      4 | 8,022.65  |      65.45 |
|     2 |     19 | 1,549.71  |      12.64 |
|     3 |     24 | 2,685.28  |      21.91 |
|-------+--------+-----------+------------|
| Total |     47 | 12,257.65 |     100.00 |
|-------+--------+-----------+------------|

#+ATTR_LATEX: :align rrrr :float t :placement [htbp]
#+RESULTS:

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to specify column alignment in LaTeX table output?
  2025-01-10  9:47     ` Rens Oliemans
@ 2025-01-10  9:53       ` Rens Oliemans
  2025-01-10 16:59       ` Richard H Stanton
  1 sibling, 0 replies; 13+ messages in thread
From: Rens Oliemans @ 2025-01-10  9:53 UTC (permalink / raw)
  To: Richard H Stanton; +Cc: emacs-orgmode

Rens Oliemans <hallo@rensoliemans.nl> writes:
>
> #+RESULTS:
> |-------+--------+-----------+------------|
> |  Tier | Number | Total     | % of Total |
> |-------+--------+-----------+------------|
> |     1 |      4 | 8,022.65  |      65.45 |
> |     2 |     19 | 1,549.71  |      12.64 |
> |     3 |     24 | 2,685.28  |      21.91 |
> |-------+--------+-----------+------------|
> | Total |     47 | 12,257.65 |     100.00 |
> |-------+--------+-----------+------------|
>
> #+ATTR_LATEX: :align rrrr :float t :placement [htbp]
> #+RESULTS:

Ah, here we see the exact problem we've both just attested to! The #+RESULTS: code block
got recreated because I was messing around with ':results' headers, and the #+ATTR_LATEX:
line doesn't belong to the correct table anymore.

You will notice that if you move the #+ATTR_LATEX line above the table, Org mode will keep
replacing that table on later code block evaluations (usually ;))


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to specify column alignment in LaTeX table output?
  2025-01-10  9:47     ` Rens Oliemans
  2025-01-10  9:53       ` Rens Oliemans
@ 2025-01-10 16:59       ` Richard H Stanton
  2025-01-10 18:59         ` Rens Oliemans
  2025-01-10 19:04         ` Richard H Stanton
  1 sibling, 2 replies; 13+ messages in thread
From: Richard H Stanton @ 2025-01-10 16:59 UTC (permalink / raw)
  To: Rens Oliemans; +Cc: emacs-orgmode


> 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...







^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to specify column alignment in LaTeX table output?
  2025-01-10  6:13 Re: How to specify column alignment in LaTeX table output? Pedro Andres Aranda Gutierrez
@ 2025-01-10 17:04 ` Richard H Stanton
  0 siblings, 0 replies; 13+ messages in thread
From: Richard H Stanton @ 2025-01-10 17:04 UTC (permalink / raw)
  To: Pedro Andres Aranda Gutierrez; +Cc: hallo, Org Mode List

[-- Attachment #1: Type: text/plain, Size: 2108 bytes --]

On Jan 9, 2025, at 10:13 PM, Pedro Andres Aranda Gutierrez <paaguti@gmail.com> wrote:
> 
> Hi,
> 
> For org tables generated in Python, I use python-tabulate. Actually, I forked the original library to include the possibility of generating the latex attributes from Python too.
> 
> Something like:
> 
> --- cut here ---
> #+BEGIN_SRC python :results value raw :exports results
> import math
> import pandas as pd
> import tabulate
> 
> xvals = [math.pi * i / 5 for i in range(10)]
> df = pd.DataFrame({
>     'x': xvals,
>     'sin(x)' : [math.sin(xvals[i]) for i in range (10)],
>     'cos(x)' : [math.cos(xvals[i]) for i in range (10)]
> })
> 
> attrs=':environment longtable :align p{2cm}p{2cm}p{2cm} :placement [h] :center t'
> return tabulate.tabulate(
>     df,
>     headers=df.columns, tablefmt='orgtbl', floatfmt=".3f",
>     caption='Table exported from Python with extended tabulate',
>     label='labextend',
>     attr_latex=attrs,
>     showindex=False)
> #+END_SRC
> #+RESULTS:
> --- cut here ---
> 
> Take a look at https://github.com/paaguti/python-tabulate if it sounds interesting. 
> 
> CAVEAT: the author of python-tabulate wasn't very excited about this ;-)
> 
> Best, /PA

Thanks for the suggestion! I was creating the output in a rather similar way, using the pandas to_markdown() function, which calls tabulate to do the work. Using the orgtbl output option, it only prints one horizontal line under the header, so I created a simple wrapper function to add additional horizontal lines at top and bottom (and optionally between the last and penultimate lines, for when there’s a “Total” row):

-----

def add_hlines(table, total_row=False):
    """Add horizontal lines to markdown table."""
    table_markdown = table.to_markdown(index=False, tablefmt="orgtbl", floatfmt=",.2f")
    table_lines = table_markdown.split("\n")
    hline = table_lines[1]
    table_lines.insert(0, hline)
    if total_row:
        table_lines.insert(-1, hline)
    table_lines.append(hline)
    return "\n".join(table_lines)

-----





[-- Attachment #2: Type: text/html, Size: 3034 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to specify column alignment in LaTeX table output?
  2025-01-10 16:59       ` Richard H Stanton
@ 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:04         ` Richard H Stanton
  1 sibling, 2 replies; 13+ messages in thread
From: Rens Oliemans @ 2025-01-10 18:59 UTC (permalink / raw)
  To: Richard H Stanton; +Cc: emacs-orgmode

Richard H Stanton <rhstanton@berkeley.edu> writes:

> 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
>
> ...
>
> 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?

Generally, I don't know. Perhaps someone else can enlighten us on this part.

Since you are using \begin{equation} which is definitively LaTeX-specific, you
can always use the "latex" header option, say

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

    #+RESULTS:
    #+begin_export latex
    a
    #+end_export

This is more or less what you had with your flushleft drawer, so I'm not sure
whether this fixes anything or not.

> 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...

I know how you feel! I'm currently writing my master's thesis, and had written
the first part of it in Org. However, there were some stumbling blocks, and
whenever deadlines loom I tend to choose the quickest option, which was separate
.tex files in this case. This isn't too bad though, AUCTeX and RefTeX are
fantastic, and I'm not really combinining LaTeX with the results of code blocks.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to specify column alignment in LaTeX table output?
  2025-01-10 16:59       ` Richard H Stanton
  2025-01-10 18:59         ` Rens Oliemans
@ 2025-01-10 19:04         ` Richard H Stanton
  1 sibling, 0 replies; 13+ messages in thread
From: Richard H Stanton @ 2025-01-10 19:04 UTC (permalink / raw)
  To: Rens Oliemans; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 6517 bytes --]

> On Jan 10, 2025, at 8:59 AM, Richard H Stanton <rhstanton@berkeley.edu> wrote:
> 
>> 
>> 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…

After a bit of experimentation, I think the solution is to put results in a drawer, e.g.,

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

This produces

#+RESULTS:
:results:
a
:end:

This exports to LaTeX vey nicely and (I assume because it's obvious where the end is) org doesn’t over-write when you rerun the code block.

Just one more thing that might be helpful: I run various packages that fold and/or hide drawers because most of the time I don’t want to see them. But now I *do* want to see :results: drawers. So I created some functions (with the help of ChatGPT) to make sure :results: drawers are always visible:

——

(defun org-open-drawer-if-closed ()
  (interactive)
  "Open the drawer at point if it is closed."
  (when (and (looking-at org-drawer-regexp) (not (org-at-item-p)))
    (save-excursion
      (let ((element (org-element-at-point)))
        (when (eq (org-element-type element) 'drawer)
          (org-flag-drawer nil))))))


(defun my-org-unfold-results-drawers (&optional state)
  "Unfold all :results: drawers in the current buffer.
   STATE is ignored and is only present to match the signature for functions used in `org-cycle-hook`."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "^[ \t]*:results:$" nil t)
      ;; Go to the beginning of the line with :results:
      (beginning-of-line)
      ;; Unfold the drawer using org-cycle
      (org-open-drawer-if-closed)
      ;; Move forward to avoid toggling the same drawer again
      (forward-line))))

(add-hook 'org-mode-hook
          (lambda ()
            (my-org-unfold-results-drawers)
            (add-hook 'org-cycle-hook #'my-org-unfold-results-drawers)))



——




[-- Attachment #2: Type: text/html, Size: 26470 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to specify column alignment in LaTeX table output?
  2025-01-10 18:59         ` Rens Oliemans
@ 2025-01-10 19:16           ` Richard H Stanton
  2025-01-10 19:38           ` Richard H Stanton
  1 sibling, 0 replies; 13+ messages in thread
From: Richard H Stanton @ 2025-01-10 19:16 UTC (permalink / raw)
  To: Rens Oliemans; +Cc: emacs-orgmode

On Jan 10, 2025, at 10:59 AM, Rens Oliemans <hallo@rensoliemans.nl> wrote:
> 
> Richard H Stanton <rhstanton@berkeley.edu> writes:
> 
>> 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
>> 
>> ...
>> 
>> 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?
> 
> Generally, I don't know. Perhaps someone else can enlighten us on this part.
> 
> Since you are using \begin{equation} which is definitively LaTeX-specific, you
> can always use the "latex" header option, say
> 
>    #+begin_src python :results output latex
>      print("a")
>    #+end_src
> 
>    #+RESULTS:
>    #+begin_export latex
>    a
>    #+end_export
> 
> This is more or less what you had with your flushleft drawer, so I'm not sure
> whether this fixes anything or not.
> 
>> 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...
> 
> I know how you feel! I'm currently writing my master's thesis, and had written
> the first part of it in Org. However, there were some stumbling blocks, and
> whenever deadlines loom I tend to choose the quickest option, which was separate
> .tex files in this case. This isn't too bad though, AUCTeX and RefTeX are
> fantastic, and I'm not really combinining LaTeX with the results of code blocks.

Yes, I mostly have Python files that generate the output under the control of GNU Make, and then import the resulting tables/figures as external files into my LaTeX document. And I agree, AUXTeX and reflex are indeed excellent!

A big reason I don’t use org-mode more: pesky coauthors who refuse to use Emacs…! 

By the way, for writing up exam solutions and the like, PythonTeX is another nice solution that allows you to embed Python code into your LaTeX document. Just like using org-mode, if you change the numbers in the question, the solution automatically regenerates without your needing to manually cut and paste numbers from elsewhere. And you can share it with, for example, TAs who don’t use Emacs. Though it’s possible we’re getting a bit off-topic here…





^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to specify column alignment in LaTeX table output?
  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
  1 sibling, 1 reply; 13+ messages in thread
From: Richard H Stanton @ 2025-01-10 19:38 UTC (permalink / raw)
  To: Rens Oliemans; +Cc: emacs-orgmode


> On Jan 10, 2025, at 10:59 AM, Rens Oliemans <hallo@rensoliemans.nl> wrote:
> 
> Richard H Stanton <rhstanton@berkeley.edu> writes:
> 
>> 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
>> 
>> ...
>> 
>> 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?
> 
> Generally, I don't know. Perhaps someone else can enlighten us on this part.
> 
> Since you are using \begin{equation} which is definitively LaTeX-specific, you
> can always use the "latex" header option, say
> 
>    #+begin_src python :results output latex
>      print("a")
>    #+end_src
> 
>    #+RESULTS:
>    #+begin_export latex
>    a
>    #+end_export
> 
> This is more or less what you had with your flushleft drawer, so I'm not sure
> whether this fixes anything or not.

If you do this, the output doesn’t show if you export to HTML. flushleft and the :results: drawer both seem to work fine exporting to both PDF and HTML.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to specify column alignment in LaTeX table output?
  2025-01-10 19:38           ` Richard H Stanton
@ 2025-01-10 19:45             ` Ihor Radchenko
  0 siblings, 0 replies; 13+ messages in thread
From: Ihor Radchenko @ 2025-01-10 19:45 UTC (permalink / raw)
  To: Richard H Stanton; +Cc: Rens Oliemans, emacs-orgmode

Richard H Stanton <rhstanton@berkeley.edu> writes:

>>    #+begin_src python :results output latex
>>      print("a")
>>    #+end_src
>> 
>>    #+RESULTS:
>>    #+begin_export latex
>>    a
>>    #+end_export
>> 
>> This is more or less what you had with your flushleft drawer, so I'm not sure
>> whether this fixes anything or not.
>
> If you do this, the output doesn’t show if you export to HTML. flushleft and the :results: drawer both seem to work fine exporting to both PDF and HTML.

I recommend 16.6 Results of Evaluation section of the manual.
It has helpful examples!

-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2025-01-10 19:43 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-10  6:13 Re: How to specify column alignment in LaTeX table output? Pedro Andres Aranda Gutierrez
2025-01-10 17:04 ` Richard H Stanton
  -- strict thread matches above, loose matches on Subject: below --
2025-01-09  9:38 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
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

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).