* 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; 2+ 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] 2+ 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; 2+ 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] 2+ messages in thread
end of thread, other threads:[~2025-01-10 17:06 UTC | newest]
Thread overview: 2+ 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
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).