gpt4 book ai didi

latex - 如何控制 pandoc 生成的 latex 中的表?

转载 作者:行者123 更新时间:2023-12-01 13:48:16 25 4
gpt4 key购买 nike

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

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

  • 更改表格标题的文字颜色;
  • 将字幕位置从 table 上方改为 table 下方;
  • 使用单元格边界可见的“网格”表;
  • 将标题更改为灰色背景上的粗体文本。

  • 我能够使用以下方法更改表格标题(第 1 点)的颜色:
    \usepackage{caption}
    \DeclareCaptionFont{myColor}{\color[RGB]{40,70,119}}
    \captionsetup{labelfont=myColor,textfont=myColor,position=bottom}

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

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

    谢谢!

    最佳答案

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

    与其告诉 pandoc 生成最终文档,不如将其 AST 提供给过滤器,以便生成所需的格式而不是默认格式。然后将此修改后的 AST 发送回 pandoc 进行最终转换。

    此 AST 是 JSON 格式。

    过程如下:

    pandoc -t json -s | ./filter.py | pandoc -f json

    在哪里 filter.py是将 JSON 数据作为 stdin 的过滤器并将 JSON 修改为 stdout .

    这可以通过告诉 pandoc 无需管道来完成:
    pandoc --filter ./filter.py -s

    这是我正在使用的过滤器:
    #!/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 安装它:
    pip install pandocfilters

    资料来源:
  • https://groups.google.com/forum/#!msg/pandoc-discuss/RUC-tuu_qf0/h-H3RRVt1coJ
  • https://github.com/jgm/pandocfilters
  • 关于latex - 如何控制 pandoc 生成的 latex 中的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34073615/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com