首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何控制pandoc生成的胶乳表?

问如何控制pandoc生成的胶乳表?
EN

Stack Overflow用户
提问于 2015-12-03 18:34:19
回答 1查看 5.8K关注 0票数 4

我使用pandoc将Markdown文档转换为LaTeX。我保存了默认的LaTeX模板(pandoc -D latex),并根据需要对它进行了修改。

现在我需要更改表的外观:

  1. 更改表标题的文本颜色;
  2. 将标题位置从表上方更改为表下;
  3. 使用“网格”表,其中单元格边界可见;
  4. 将标题更改为灰色背景上的粗体文本。

我能够更改表标题的颜色(第1点)。使用:

代码语言:javascript
复制
\usepackage{caption}
\DeclareCaptionFont{myColor}{\color[RGB]{40,70,119}}
\captionsetup{labelfont=myColor,textfont=myColor,position=bottom}

但是忽略了position选项(第2点)。

我希望能够控制pandoc生成的LaTeX表的样式。这有可能吗?有什么建议吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-03 22:16:40

实际上,更改表的生成方式是可能的,而不是在以后尝试修复它们。需要的是一个pandoc过滤器。

它的AST不需要让pandoc生成最终文档,而是需要输入过滤器,这样就可以生成想要的格式,而不是默认的格式。然后将修改后的AST发送回pandoc进行最终转换。

这个AST是JSON格式。

这一过程如下:

代码语言:javascript
复制
pandoc -t json -s | ./filter.py | pandoc -f json

其中,filter.py是一个过滤器,将JSON数据作为stdin,并将修改后的JSON吐出到stdout中。

这不需要管道,只需告诉pandoc:

代码语言:javascript
复制
pandoc --filter ./filter.py -s

下面是我使用的过滤器:

代码语言:javascript
复制
#!/usr/bin/env python2

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'][:]
    # Build the columns. Note how the every column value is bold.
    # We are still missing "\textbf{" for the first column
    # and a "}" for the last column.
    for i in range(1, len(s)):
        result.append(inlatex(r'} & \textbf{'))
        result.extend(s[i][0]['c'])
    # Don't forget to close the last column's "\textbf{" before newline
    result.append(inlatex(r'} \\ \hline'))
    # Put the missing "\textbf{" in front of the list
    result.insert(0, inlatex(r'\textbf{'))
    # Preprend the command to set the row color in front of everything
    result.insert(0, inlatex(r'\rowcolor{grey} '))
    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' \\ \hline' '\n')
    return pf.Para(result)

def do_filter(k, v, f, m):
    if k == "Table":
        # Ensure every alignment characters is surrounded by a pipes.
        # Get the string of the alignment characters
        # and split into an array for every characters.
        split_alignment = [c for c in tbl_alignment(v[1])]
        # Join this list into a single string with pipe symbols
        # between them, plus pipes at start and end.
        # This results in a boxed table.
        new_alignment = "|" + "|".join(split_alignment) + "|"
        return [latex(r'\begin{table}[h]'),
                latex(r'\centering'),
                latex(r'\begin{tabular}{%s} \hline' % new_alignment),
                tbl_headers(v[3]),
                tbl_contents(v[4]),
                latex(r'\end{tabular}'),
                # Put the caption after the tabular so it appears under table.
                tbl_caption(v[0]),
                latex(r'\end{table}')]


if __name__ == "__main__":
    pf.toJSONFilter(do_filter)

注意,它使用的是pandocfilters python模块。使用pip安装

代码语言:javascript
复制
pip install pandocfilters

资料来源:

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34073615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档