我想使用 Pandoc 将 Markdown 转换为 PDF 输出中的斑马条纹表格行。要创建 Latex 表格,Pandoc 依赖于包booktabs
和longtable
。因此我尝试重新定义longtable
环境。这是 MWE(Markdown 文件内容):
---
header-includes:
- \usepackage[table]{xcolor}
- \definecolor{lightgray}{gray}{0.95}
- \let\OldLongtable\longtable
- \let\OldEndLongtable\endlongtable
- \renewenvironment{longtable}{\rowcolors{1}{white}{lightgray}\OldLongtable}{\OldEndLongtable}
---
| Right | Left | Default | Center |
|------:|:-----|---------|:------:|
| 12 | 12 | 12 | 12 |
| 123 | 123 | 123 | 123 |
| 1 | 1 | 1 | 1 |
: Demonstration of pipe table syntax.
现在的问题是行颜色比实际表格更宽:
我怎样才能解决这个问题?
编辑:
我偶然发现了一些有关此问题的信息。Pandoc Latex 模板的作者称冰鸟 状态:
不幸的是,彩色单元格超出了表格的边缘,因为 pandoc 使用了 @-expressions(
@{}
) ,如下所示:
\begin{longtable}[]{@{}ll@{}}
\end{longtable}
此外,他还链接到Latex Wikibook 关于 @-expressions 的部分。但对于我这个 Latex 新手来说,这一切都一清二楚……
编辑2:
根据要求扎尔科这与完整的 Latex 文档相同(Pandoc 的 Markdown 到 Latex 转换的输出):
\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\else % if luatex or xelatex
\ifxetex
\usepackage{mathspec}
\else
\usepackage{fontspec}
\fi
\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\PassOptionsToPackage{hyphens}{url} % url is loaded by hyperref
\usepackage[unicode=true]{hyperref}
\hypersetup{
pdfborder={0 0 0},
breaklinks=true}
\urlstyle{same} % don't use monospace font for urls
\usepackage{longtable,booktabs}
% Fix footnotes in tables (requires footnote package)
\IfFileExists{footnote.sty}{\usepackage{footnote}\makesavenoteenv{long table}}{}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi
% set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.95}
\let\OldLongtable\longtable
\let\OldEndLongtable\endlongtable
\renewenvironment{longtable}{\rowcolors{1}{white}{lightgray}\OldLongtable}{\OldEndLongtable}
\date{}
\begin{document}
\begin{longtable}[]{@{}rllc@{}}
\caption{Demonstration of pipe table syntax.}\tabularnewline
\toprule
Right & Left & Default & Center\tabularnewline
\midrule
\endfirsthead
\toprule
Right & Left & Default & Center\tabularnewline
\midrule
\endhead
12 & 12 & 12 & 12\tabularnewline
123 & 123 & 123 & 123\tabularnewline
1 & 1 & 1 & 1\tabularnewline
\bottomrule
\end{longtable}
\end{document}
答案1
像这样:
我从你的代码中提取最小工作示例,我添加的代码可以解决您的问题:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs, longtable}
\begin{document}
\begin{longtable}{@{} >{\columncolor{white}[0pt][\tabcolsep]}r % <---
l
l
>{\columncolor{white}[\tabcolsep][0pt]}c % <---
@{} }
\caption{Demonstration of pipe table syntax.}\\
\toprule
Right & Left & Default & Center \\
\midrule
\endfirsthead
\toprule
Right & Left & Default & Center \\
\midrule
\endhead
\rowcolor{gray!10}
12 & 12 & 12 & 12 \\
123 & 123 & 123 & 123 \\
1 & 1 & 1 & 1 \\
\bottomrule
\end{longtable}
\end{document}
答案2
截至今天,这仍然是一个问题,并且@Zarko 的答案无法通过 pandoc 过滤器解决,因为这是一个编写器问题。下面的脚本是一个(粗略的)解决方案,用于修复可以集成到管道中的表格颜色:
#!/usr/bin/env python
"""
fix-table-color-bleed.py: take a file as argument or input from stdin,
remove @{} from table headers and print the result on stdout
"""
import sys
from re import sub
def fix_table_color_bleed(infile):
"""
Remove @{} from latex table entries and print on stdout
"""
fixedfile = sub("(\\\\begin\{longtable\}\[\]\{)\@\{\}(.*?)\@\{\}",
"\\1\\2", infile)
print(fixedfile)
if __name__ == "__main__":
if len(sys.argv) > 1:
infile = open(sys.argv[1])
filecontens = infile.read()
else:
filecontens = sys.stdin.read()
fix_table_color_bleed(filecontens)
该(\\\\begin\{longtable\}\[\]\{)\@\{\}(.*?)\@\{\}
部分是用于捕获和删除 @{} 序列的正则表达式模式。您可以通过提供 tex 文件作为参数来使用此脚本。要在管道中使用它,您可以使用例如:
pandoc -s --from markdown --to latex <inputfile>.md | fix-table-color-bleed.py <outputfile>.tex