LaTeX表格首列无行分隔横线问题求助
解决LaTeX表格首列无水平分隔线问题
问题背景
我正在开发一个基于LaTeX和Python的自动报告生成项目,流程为读取文本文件、正则提取数据存入数组、生成报告表格。目前功能正常,但存在一个问题:表格首列每行之间没有水平分隔线,缺少行分隔横线。
原代码片段
template.tex中的LaTeX模板
% \begin{center} % \begin{tabular}{|m{4cm}|m{2.5cm}|m{2.5cm}|m{2cm}|m{2cm}|} % \hline % \textbf{Meeting Name} & \textbf{Started At} & \textbf{Ended At} & \textbf{Duration (min)} & \textbf{Number of Participants} \\ % \hline % \hline % {% for index, row in df.iterrows() %} % {% if row['Meeting Name'] != '' %} % {{ row['Meeting Name'] }} & {{ row['Started At'].strftime('%Y-%m-%d %H:%M:%S') }} & {{ row['Ended At'].strftime('%Y-%m-%d %H:%M:%S') }} & {{ row['Duration']//60 }} & {{ row['Number of Participants'] }} \\ % \cline{2-5} % {% endif %} % {% endfor %} % \hline % \multicolumn{3}{|r|}{\textbf{Total call duration (min)}} & \multicolumn{2}{l|}{{ total_duration }} \\ % \cline{1-3} \cline{4-5} % \multicolumn{3}{|r|}{\textbf{Total participants}} & \multicolumn{2}{l|}{{ total_participants }} \\ % \hline % \end{tabular} % \end{center} % \begin{center} % \textbf{Total call duration: {{ total_duration }} min} \\ % \textbf{Total participants: {{ total_participants }}} % \end{center} % \end{document}
推送数据的Python代码
# LaTeX template template = r''' \documentclass[a4paper]{report} \usepackage[margin=1in]{geometry} \usepackage{multirow} \usepackage{array} \begin{document} \begin{center} \begin{tabular}{|m{4cm}|m{2.5cm}|m{2.5cm}|m{2.5cm}|m{2cm}|} \hline \textbf{Meeting Name} & \textbf{Started At} & \textbf{Ended At} & \textbf{Duration(min)} & \textbf{Number of Participants} \\ \hline \hline ''' + table_data + r''' \hline \multicolumn{3}{|r|}{\textbf{Total call duration}} & \multicolumn{2}{l|}{''' + str(total_duration) + r'''} \\ \hline \multicolumn{3}{|r|}{\textbf{Total participants}} & \multicolumn{2}{l|}{''' + str(total_participants) + r'''} \\ \hline \end{tabular} \end{center} \end{document} '''
问题原因
原代码中每行末尾使用\cline{2-5},仅对第2到5列添加水平分隔线,首列(第1列)未被包含在内,导致首列无行分隔横线。
修改方案
将循环内的\cline{2-5}替换为\hline,让每行末尾的分隔线覆盖整个表格宽度(包括首列)。
修改后的template.tex循环部分
{% for index, row in df.iterrows() %} {% if row['Meeting Name'] != '' %} {{ row['Meeting Name'] }} & {{ row['Started At'].strftime('%Y-%m-%d %H:%M:%S') }} & {{ row['Ended At'].strftime('%Y-%m-%d %H:%M:%S') }} & {{ row['Duration']//60 }} & {{ row['Number of Participants'] }} \\ \hline {% endif %} {% endfor %}
修改后的Python模板对应部分
确保table_data生成的每行末尾是\hline而非\cline{2-5},如果是通过Python循环生成table_data,需同步修改分隔线代码。
效果说明
修改后,每行末尾的\hline会为表格所有列(包括首列)添加完整的水平分隔线,解决首列无分隔线的问题。
内容的提问来源于stack exchange,提问作者Mr. Dwivedi
相关产品推荐
相关产品推荐

