* Latex tables using tabular instead of longtable
@ 2014-07-01 1:59 Wagner Macedo
[not found] ` <CAKGY2P=5brJ1YHcGq_G_oc1Zsp6ZCXf6SDVVH9n2wUBG8uLF4A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
0 siblings, 1 reply; 5+ messages in thread
From: Wagner Macedo @ 2014-07-01 1:59 UTC (permalink / raw)
To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw
[-- Attachment #1: Type: text/plain, Size: 3230 bytes --]
Hello guys.
I don't like how *longtable* handle with tables, spanning across pages
without needing, so I took advantage of the filtering mechanism and I did a
filter in python to generate the tables in a more traditional way (in the
sense of Latex) and I'm sharing with you below.
Please make suggestions, I did the things a few hardcoded because I didn't
see other way.
To understand what the filter do, let's say I have a file with the
following table:
*test.mkd*
Code Name
------ ------
1 Pandoc
2 Latex
3 Writer
Table: My *best* tools
The normal generated latex code would be
*test-longtable.tex*
\begin{longtable}[c]{@{}cl@{}}
\toprule\addlinespace
Code & Name
\\\addlinespace
\midrule\endhead
1 & Pandoc
\\\addlinespace
2 & Latex
\\\addlinespace
3 & Writer
\\\addlinespace
\bottomrule
\addlinespace
\caption{My \emph{best} tools}
\end{longtable}
But using the filter, the latex code will be
*test-tabular.tex*
\begin{table}[ht]
\caption{My \emph{best} tools}
\begin{tabular}{@{}cl@{}}
\toprule
Code & Name \\\midrule
1 & Pandoc \\
2 & Latex \\
3 & Writer \\
\bottomrule
\end{tabular}
\end{table}
Note I put \caption above as is more common to my writings.
*filter.py*
import pandocfilters as pf
def latex(s):
return pf.RawBlock('latex', s)
def inlatex(s):
return pf.RawInline('latex', s)
def tbl_caption(s):
return pf.Para([inlatex(r'\caption{')] + s + [inlatex('}')])
def tbl_alignment(s):
aligns = {
"AlignDefault": 'l',
"AlignLeft": 'l',
"AlignCenter": 'c',
"AlignRight": 'r',
}
return ''.join([aligns[e['t']] for e in s])
def tbl_headers(s):
result = s[0][0]['c'][:]
for i in range(1, len(s)):
result.append(inlatex(' & '))
result.extend(s[i][0]['c'])
result.append(inlatex(r' \\\midrule'))
return pf.Para(result)
def tbl_contents(s):
result = []
for row in s:
para = []
for col in row:
para.extend(col[0]['c'])
para.append(inlatex(' & '))
result.extend(para)
result[-1] = inlatex(r' \\' '\n')
return pf.Para(result)
def do_filter(k, v, f, m):
if k == "Table":
return [latex(r'\begin{table}[ht]' '\n' r'\centering' '\n'),
tbl_caption(v[0]),
latex(r'\begin{tabular}{@{}%s@{}}' % tbl_alignment(v[1]) +
('\n' r'\toprule')),
tbl_headers(v[3]),
tbl_contents(v[4]),
latex(r'\bottomrule' '\n' r'\end{tabular}'),
latex(r'\end{table}')]
if __name__ == "__main__":
pf.toJSONFilter(do_filter)
--
Wagner Macedo
--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/CAKGY2P%3D5brJ1YHcGq_G_oc1Zsp6ZCXf6SDVVH9n2wUBG8uLF4A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
[-- Attachment #2: Type: text/html, Size: 9996 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Latex tables using tabular instead of longtable
[not found] ` <CAKGY2P=5brJ1YHcGq_G_oc1Zsp6ZCXf6SDVVH9n2wUBG8uLF4A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2014-07-01 3:39 ` John MacFarlane
[not found] ` <20140701033914.GA75863-bi+AKbBUZKbivNSvqvJHCtPlBySK3R6THiGdP5j34PU@public.gmane.org>
2017-09-14 11:44 ` Jan Hermes
2020-05-13 18:16 ` Leon Bartz
2 siblings, 1 reply; 5+ messages in thread
From: John MacFarlane @ 2014-07-01 3:39 UTC (permalink / raw)
To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw
Thanks for sharing. How does it deal with footnotes in tables?
(That is one thing that longtable does better.)
+++ Wagner Macedo [Jun 30 14 22:59 ]:
> Hello guys.
> I don't like how longtable handle with tables, spanning across pages
> without needing, so I took advantage of the filtering mechanism and I
> did a filter in python to generate the tables in a more traditional way
> (in the sense of Latex) and I'm sharing with you below.
> Please make suggestions, I did the things a few hardcoded because I
> didn't see other way.
> To understand what the filter do, let's say I have a file with the
> following table:
> test.mkd
>Code Name
>------ ------
> 1 Pandoc
> 2 Latex
> 3 Writer
>
>Table: My *best* tools
>
> The normal generated latex code would be
> test-longtable.tex
>\begin{longtable}[c]{@{}cl@{}}
>\toprule\addlinespace
>Code & Name
>\\\addlinespace
>\midrule\endhead
>1 & Pandoc
>\\\addlinespace
>2 & Latex
>\\\addlinespace
>3 & Writer
>\\\addlinespace
>\bottomrule
>\addlinespace
>\caption{My \emph{best} tools}
>\end{longtable}
>But using the filter, the latex code will be
>
>test-tabular.tex
>
>\begin{table}[ht]
>
>\caption{My \emph{best} tools}
>
>\begin{tabular}{@{}cl@{}}
>\toprule
>
>Code & Name \\\midrule
>
>1 & Pandoc \\
>2 & Latex \\
>3 & Writer \\
>
>\bottomrule
>\end{tabular}
>
>\end{table}
>
> Note I put \caption above as is more common to my writings.
> filter.py
>import pandocfilters as pf
>
>def latex(s):
> return pf.RawBlock('latex', s)
>
>def inlatex(s):
> return pf.RawInline('latex', s)
>
>def tbl_caption(s):
> return pf.Para([inlatex(r'\caption{')] + s + [inlatex('}')])
>
>def tbl_alignment(s):
> aligns = {
> "AlignDefault": 'l',
> "AlignLeft": 'l',
> "AlignCenter": 'c',
> "AlignRight": 'r',
> }
> return ''.join([aligns[e['t']] for e in s])
>
>def tbl_headers(s):
> result = s[0][0]['c'][:]
> for i in range(1, len(s)):
> result.append(inlatex(' & '))
> result.extend(s[i][0]['c'])
> result.append(inlatex(r' \\\midrule'))
> return pf.Para(result)
>
>def tbl_contents(s):
> result = []
> for row in s:
> para = []
> for col in row:
> para.extend(col[0]['c'])
> para.append(inlatex(' & '))
> result.extend(para)
> result[-1] = inlatex(r' \\' '\n')
> return pf.Para(result)
>
>def do_filter(k, v, f, m):
> if k == "Table":
> return [latex(r'\begin{table}[ht]' '\n' r'\centering' '\n'),
> tbl_caption(v[0]),
> latex(r'\begin{tabular}{@{}%s@{}}' % tbl_alignment(v[1]) +
> ('\n' r'\toprule')),
> tbl_headers(v[3]),
> tbl_contents(v[4]),
> latex(r'\bottomrule' '\n' r'\end{tabular}'),
> latex(r'\end{table}')]
>
>
>if __name__ == "__main__":
> pf.toJSONFilter(do_filter)
>
> --
> Wagner Macedo
>
> --
> You received this message because you are subscribed to the Google
> Groups "pandoc-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [1]pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To post to this group, send email to
> [2]pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To view this discussion on the web visit
> [3]https://groups.google.com/d/msgid/pandoc-discuss/CAKGY2P%3D5brJ1YHcG
> q_G_oc1Zsp6ZCXf6SDVVH9n2wUBG8uLF4A%40mail.gmail.com.
> For more options, visit [4]https://groups.google.com/d/optout.
>
>References
>
> 1. mailto:pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
> 2. mailto:pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
> 3. https://groups.google.com/d/msgid/pandoc-discuss/CAKGY2P=5brJ1YHcGq_G_oc1Zsp6ZCXf6SDVVH9n2wUBG8uLF4A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org?utm_medium=email&utm_source=footer
> 4. https://groups.google.com/d/optout
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Latex tables using tabular instead of longtable
[not found] ` <20140701033914.GA75863-bi+AKbBUZKbivNSvqvJHCtPlBySK3R6THiGdP5j34PU@public.gmane.org>
@ 2014-07-01 4:37 ` Wagner Macedo
0 siblings, 0 replies; 5+ messages in thread
From: Wagner Macedo @ 2014-07-01 4:37 UTC (permalink / raw)
To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw
[-- Attachment #1: Type: text/plain, Size: 1514 bytes --]
Well, I didn't think in that! Probably because I never needed.
But I remember long time ago something about it. I will search and post so.
--
Wagner Macedo
On 1 July 2014 00:39, John MacFarlane <jgm-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org> wrote:
> Thanks for sharing. How does it deal with footnotes in tables?
> (That is one thing that longtable does better.)
>
> +++ Wagner Macedo [Jun 30 14 22:59 ]:
>
>> Hello guys.
>> I don't like how longtable handle with tables, spanning across pages
>> without needing, so I took advantage of the filtering mechanism and I
>> did a filter in python to generate the tables in a more traditional way
>> (in the sense of Latex) and I'm sharing with you below.
>> Please make suggestions, I did the things a few hardcoded because I
>> didn't see other way.
>> To understand what the filter do, let's say I have a file with the
>> following table:
>> test.mkd
>>
>
--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/CAKGY2PmMvT1SzOOrDv7B_eJmaGz9sxBU6OdwHDnYwx_VD2_8Kw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
[-- Attachment #2: Type: text/html, Size: 2506 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Latex tables using tabular instead of longtable
[not found] ` <CAKGY2P=5brJ1YHcGq_G_oc1Zsp6ZCXf6SDVVH9n2wUBG8uLF4A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-07-01 3:39 ` John MacFarlane
@ 2017-09-14 11:44 ` Jan Hermes
2020-05-13 18:16 ` Leon Bartz
2 siblings, 0 replies; 5+ messages in thread
From: Jan Hermes @ 2017-09-14 11:44 UTC (permalink / raw)
To: pandoc-discuss
[-- Attachment #1.1: Type: text/plain, Size: 4732 bytes --]
Thank you very much for sharing!
Unfortunately this produces an error "IndexError:list index out of range"
in a recent pandoc version: 1.19.2.1
Traceback (most recent call last):
File "filters/tabular.py", line 53, in <module>
pf.toJSONFilter(do_filter)
File "/usr/local/lib/python2.7/dist-packages/pandocfilters.py", line 132,
in toJSONFilter
toJSONFilters([action])
File "/usr/local/lib/python2.7/dist-packages/pandocfilters.py", line 166,
in toJSONFilters
sys.stdout.write(applyJSONFilters(actions, source, format))
File "/usr/local/lib/python2.7/dist-packages/pandocfilters.py", line 197,
in applyJSONFilters
altered = walk(altered, action, format, meta)
File "/usr/local/lib/python2.7/dist-packages/pandocfilters.py", line 124,
in walk
x[k] = walk(x[k], action, format, meta)
File "/usr/local/lib/python2.7/dist-packages/pandocfilters.py", line 111,
in walk
item['c'] if 'c' in item else None, format, meta)
File "filters/tabular.py", line 46, in do_filter
tbl_headers(v[3]),
File "filters/tabular.py", line 22, in tbl_headers
result = s[0][0]['c'][:]
IndexError: list index out of range
pandoc: Error running filter filters/tabular.py
Filter returned error status 1
Am Dienstag, 1. Juli 2014 03:59:26 UTC+2 schrieb Wagner Macedo:
>
> Hello guys.
>
> I don't like how *longtable* handle with tables, spanning across pages
> without needing, so I took advantage of the filtering mechanism and I did a
> filter in python to generate the tables in a more traditional way (in the
> sense of Latex) and I'm sharing with you below.
>
> Please make suggestions, I did the things a few hardcoded because I didn't
> see other way.
>
> To understand what the filter do, let's say I have a file with the
> following table:
>
> *test.mkd*
>
> Code Name
> ------ ------
> 1 Pandoc
> 2 Latex
> 3 Writer
>
> Table: My *best* tools
>
> The normal generated latex code would be
>
> *test-longtable.tex*
>
> \begin{longtable}[c]{@{}cl@{}}
> \toprule\addlinespace
> Code & Name
> \\\addlinespace
> \midrule\endhead
> 1 & Pandoc
> \\\addlinespace
> 2 & Latex
> \\\addlinespace
> 3 & Writer
> \\\addlinespace
> \bottomrule
> \addlinespace
> \caption{My \emph{best} tools}
> \end{longtable}
>
> But using the filter, the latex code will be
>
> *test-tabular.tex*
>
> \begin{table}[ht]
>
> \caption{My \emph{best} tools}
>
> \begin{tabular}{@{}cl@{}}
> \toprule
>
> Code & Name \\\midrule
>
> 1 & Pandoc \\
> 2 & Latex \\
> 3 & Writer \\
>
> \bottomrule
> \end{tabular}
>
> \end{table}
>
> Note I put \caption above as is more common to my writings.
>
> *filter.py*
>
> import pandocfilters as pf
> def latex(s):
> return pf.RawBlock('latex', s)
> def inlatex(s):
> return pf.RawInline('latex', s)
> def tbl_caption(s):
> return pf.Para([inlatex(r'\caption{')] + s + [inlatex('}')])
> def tbl_alignment(s):
> aligns = {
> "AlignDefault": 'l',
> "AlignLeft": 'l',
> "AlignCenter": 'c',
> "AlignRight": 'r',
> }
> return ''.join([aligns[e['t']] for e in s])
> def tbl_headers(s):
> result = s[0][0]['c'][:]
> for i in range(1, len(s)):
> result.append(inlatex(' & '))
> result.extend(s[i][0]['c'])
> result.append(inlatex(r' \\\midrule'))
> return pf.Para(result)
> def tbl_contents(s):
> result = []
> for row in s:
> para = []
> for col in row:
> para.extend(col[0]['c'])
> para.append(inlatex(' & '))
> result.extend(para)
> result[-1] = inlatex(r' \\' '\n')
> return pf.Para(result)
> def do_filter(k, v, f, m):
> if k == "Table":
> return [latex(r'\begin{table}[ht]' '\n' r'\centering' '\n'),
> tbl_caption(v[0]),
> latex(r'\begin{tabular}{@{}%s@{}}' % tbl_alignment(v[1]) +
> ('\n' r'\toprule')),
> tbl_headers(v[3]),
> tbl_contents(v[4]),
> latex(r'\bottomrule' '\n' r'\end{tabular}'),
> latex(r'\end{table}')]
>
> if __name__ == "__main__":
> pf.toJSONFilter(do_filter)
>
> --
> Wagner Macedo
>
--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/1dafcdfb-9bf9-4e82-8768-1b1905fc204a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
[-- Attachment #1.2: Type: text/html, Size: 22854 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Latex tables using tabular instead of longtable
[not found] ` <CAKGY2P=5brJ1YHcGq_G_oc1Zsp6ZCXf6SDVVH9n2wUBG8uLF4A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-07-01 3:39 ` John MacFarlane
2017-09-14 11:44 ` Jan Hermes
@ 2020-05-13 18:16 ` Leon Bartz
2 siblings, 0 replies; 5+ messages in thread
From: Leon Bartz @ 2020-05-13 18:16 UTC (permalink / raw)
To: pandoc-discuss
[-- Attachment #1.1: Type: text/plain, Size: 3818 bytes --]
Hi, the script does not Work with the example below. Can somebody please
help ? Tanks
> <table>
>
> <thead>
>
> <tr>
>
> <th>1</th>
>
> <th>2</th>
>
> <th>3</th>
>
> <th>4</th>
>
> </tr>
>
> </thead>
>
> <tbody>
>
> <tr>
>
> <td>5</td>
>
> <td>6</td>
>
> <td>7</td>
>
> <td></td>
>
> </tr>
>
> <tr>
>
> <td>8</td>
>
> <td>9</td>
>
> <td>1</td>
>
> <td>2</td>
>
> </tr>
>
> <tr>
>
> <td>3</td>
>
> <td>4</td>
>
> <td>5</td>
>
> <td></td>
>
> </tr>
>
> </tbody>
>
> </table>
>
> Am Dienstag, 1. Juli 2014 03:59:26 UTC+2 schrieb Wagner Macedo:
>
> Hello guys.
>
> I don't like how *longtable* handle with tables, spanning across pages
> without needing, so I took advantage of the filtering mechanism and I did a
> filter in python to generate the tables in a more traditional way (in the
> sense of Latex) and I'm sharing with you below.
>
> Please make suggestions, I did the things a few hardcoded because I didn't
> see other way.
>
> To understand what the filter do, let's say I have a file with the
> following table:
>
> *test.mkd*
>
> Code Name
> ------ ------
> 1 Pandoc
> 2 Latex
> 3 Writer
>
> Table: My *best* tools
>
> The normal generated latex code would be
>
> *test-longtable.tex*
>
> \begin{longtable}[c]{@{}cl@{}}
> \toprule\addlinespace
> Code & Name
> \\\addlinespace
> \midrule\endhead
> 1 & Pandoc
> \\\addlinespace
> 2 & Latex
> \\\addlinespace
> 3 & Writer
> \\\addlinespace
> \bottomrule
> \addlinespace
> \caption{My \emph{best} tools}
> \end{longtable}
>
> But using the filter, the latex code will be
>
> *test-tabular.tex*
>
> \begin{table}[ht]
>
> \caption{My \emph{best} tools}
>
> \begin{tabular}{@{}cl@{}}
> \toprule
>
> Code & Name \\\midrule
>
> 1 & Pandoc \\
> 2 & Latex \\
> 3 & Writer \\
>
> \bottomrule
> \end{tabular}
>
> \end{table}
>
> Note I put \caption above as is more common to my writings.
>
> *filter.py*
>
> import pandocfilters as pf
> def latex(s):
> return pf.RawBlock('latex', s)
> def inlatex(s):
> return pf.RawInline('latex', s)
> def tbl_caption(s):
> return pf.Para([inlatex(r'\caption{')] + s + [inlatex('}')])
> def tbl_alignment(s):
> aligns = {
> "AlignDefault": 'l',
> "AlignLeft": 'l',
> "AlignCenter": 'c',
> "AlignRight": 'r',
> }
> return ''.join([aligns[e['t']] for e in s])
> def tbl_headers(s):
> result = s[0][0]['c'][:]
> for i in range(1, len(s)):
> result.append(inlatex(' & '))
> result.extend(s[i][0]['c'])
> result.append(inlatex(r' \\\midrule'))
> return pf.Para(result)
> def tbl_contents(s):
> result = []
> for row in s:
> para = []
> for col in row:
> para.extend(col[0]['c'])
> para.append(inlatex(' & '))
> result.extend(para)
> result[-1] = inlatex(r' \\' '\n')
> return pf.Para(result)
> def do_filter(k, v, f, m):
> if k == "Table":
> return [latex(r'\begin{table}[ht]' '\n' r'\centering' '\n'),
> tbl_caption(v[0]),
> latex(r'\begin{tabular}{@{}%s@{}}' % tbl_alignment(v[1]) +
> ('\n' r'\toprule')),
> tbl_headers(v[3]),
> tbl_contents(v[4]),
> latex(r'\bottomrule' '\n' r'\end{tabular}'),
> latex(r'\end{table}')]
>
> if __name__ == "__main__":
> pf.toJSONFilter(do_filter)
>
> --
> Wagner Macedo
>
--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/112d046a-8566-4d63-8af2-8bb001a5a1ce%40googlegroups.com.
[-- Attachment #1.2: Type: text/html, Size: 14311 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-05-13 18:16 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-01 1:59 Latex tables using tabular instead of longtable Wagner Macedo
[not found] ` <CAKGY2P=5brJ1YHcGq_G_oc1Zsp6ZCXf6SDVVH9n2wUBG8uLF4A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-07-01 3:39 ` John MacFarlane
[not found] ` <20140701033914.GA75863-bi+AKbBUZKbivNSvqvJHCtPlBySK3R6THiGdP5j34PU@public.gmane.org>
2014-07-01 4:37 ` Wagner Macedo
2017-09-14 11:44 ` Jan Hermes
2020-05-13 18:16 ` Leon Bartz
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 NNTP newsgroup(s).