长表格单元格中的文本换行不正确

长表格单元格中的文本换行不正确

我正在尝试为一个拆分为多个页面的表格生成一个 LaTeX jinja 模板,该模板将使用自动生成的 LaTeX 进行填充。为了实现这一点,我使用了环境longtable,但是,当单元格包含字符过多的单词时,它无法正确换行。以下是表格的相关代码片段(带有虚拟内容):

\def\arraystretch{1.2}

\begin{document}


\begin{longtable}{p{0.1\linewidth} p{0.2\linewidth}  p{0.7\linewidth} }
  \# & question & answer \\
  \arrayrulecolor{brand_dark} \hline
  \endhead
  
  \rowcolor{brand_light} \multicolumn{3}{l}{tXRcRDxdKJOJHbSAAZeOCcoEPlpNzymLx} \\
  \rowcolor{white}
  LRenA &
  \begin{minipage}[t]{\linewidth}
    \begin{flushleft}KMtMfYwfHkGLLbaZFAfRtTFj\end{flushleft}
  \end{minipage} & \hspace{0pt}
  \begin{minipage}[t]{\dimexpr \linewidth - 0pt}
    \begin{flushleft}
      udrPJNdJGqOLzgGJjs
    \end{flushleft}
  \end{minipage}

  \\
  
  \rowcolor{light-grey}
  ZCKhv &
  \begin{minipage}[t]{\linewidth}
    \begin{flushleft}hnjtmbuJPwyvBkbGjKJMEojGlUHREvvzwKwSSQeOEU\end{flushleft}
  \end{minipage} & \hspace{0pt}
  \begin{minipage}[t]{\dimexpr \linewidth - 0pt}
    \begin{flushleft}
      NmEwyZowJszdELsNktHfOvSaSNmIqIDHqabyKtNzuUgReEauJpiwjppdQWrHqorhMBgopxsROQEHPs
    \end{flushleft}
  \end{minipage}

  \\
  \rowcolor{white}
  DPzyJ &
  \begin{minipage}[t]{\linewidth}
    \begin{flushleft}ikHnifxdnsPJNGEqvwTWjvHZ\end{flushleft}
  \end{minipage} & \hspace{0pt}
  \begin{minipage}[t]{\dimexpr \linewidth - 0pt}
    \begin{flushleft}
      BRiTGeOBvY
    \end{flushleft}
  \end{minipage}

  \\
  
  \rowcolor{light-grey}
  LYYiI &
  \begin{minipage}[t]{\linewidth}
    \begin{flushleft}ALdgFWrSKqcNpBEeDvtnZX\end{flushleft}
  \end{minipage} & \hspace{0pt}
  \begin{minipage}[t]{\dimexpr \linewidth - 0pt}
    \begin{flushleft}
      UtyYPYhXSSmSUzgnO
    \end{flushleft}
  \end{minipage}

  \\
  \rowcolor{white}
  plBsz &
  \begin{minipage}[t]{\linewidth}
    \begin{flushleft}VckBeukgwVihwVXtYbAPqgzd\end{flushleft}
  \end{minipage} & \hspace{0pt}
  \begin{minipage}[t]{\dimexpr \linewidth - 0pt}
    \begin{flushleft}
      VTEQmsYvTjinhubaqZDqVXSEdtmZVpwuYlLAIWGQbQmcRLnfhISiIdIpmhEEbGPMqoWCroWOHqJuMvCCEgtWqganBlBoJomZLvQQpSArshkAUXscRwLUyWbslXqMmEgUgdLvxnGnNcvMZgnRii
    \end{flushleft}
  \end{minipage}

\end{longtable}
  
\end{document}

结果如下表:

长表单元格包装不正确

并不是说中间和最右边的单元格的内容被封装在一个迷你页面中,因为一般来说,它可以包含换行符以及任意嵌套的itemizeenumerateflushleft和环境以及、和的使用。flushrightcenter\emph\textbf\color

使此类文本正确换行的最佳方法是什么?

答案1

longtable除了换行不太好之外, 还存在一些问题。首先, 的总宽度longtable目前超出\textwidth6\tabcolsep即大约半英寸/一厘米多。

为了强制在长字符串中在每行的末尾出现换行符(并且在此过程中暂停可能生效的普通连字符规则),我建议您加载该seqsplit包并\seqsplit在需要时使用其宏。

长字符串可能会或可能不会出现在、、等环境中,但这minipage并不center意味着flushright您不能\seqsplit在需要时使用指令。

在此处输入图片描述

\documentclass{article}

\usepackage[dvipsnames,table]{xcolor}

\usepackage{alltt}
\usepackage{amssymb}
\usepackage{array}
%%%\usepackage{colortbl} % rendundant
\usepackage{datetime}
\usepackage{enumitem}
\usepackage{everypage} 
\usepackage{fancyhdr}

\usepackage{fontspec}
\setmainfont{Comfortaa-Bold}

\usepackage[landscape, margin=1in]{geometry}
\usepackage{longtable}
\usepackage{pdflscape}
\usepackage{tikzpagenodes}

\usetikzlibrary{calc}
\usetikzlibrary{positioning}

\definecolor{light-grey}{RGB}{243, 243, 243}
\definecolor{brand_dark}{RGB}{28, 69, 135}
\definecolor{brand_light}{RGB}{201, 218, 248}

\newcommand{\Company}{Company}
\newcommand{\CompanyURL}{company.com}
\newcommand{\Index}{QA}

\newcommand{\monthyear}{\ifcase \month \or January\or February\or March\or %
  April\or May \or June\or July\or August\or September\or October\or November\or %
  December\fi, \number \year} 

% Turn on the style
\pagestyle{fancy}
% Clear the header and footer
% \fancyhead{}
\fancyfoot{}
\renewcommand{\headrulewidth}{0pt}
% Set the right side of the footer to be the page number
\fancyfoot[R]{\color{brand_dark}Page \thepage}
\fancyfoot[L]{\color{brand_dark}{\monthyear}}

\AddEverypageHook{\begin{tikzpicture}[remember picture,overlay]
\fill[brand_dark] (current page.north west) rectangle ($ (current page header area.north -| current page.east) + (0,0.5) $);
\node [above right = 0.5cm and -2cm of current page header area.north west] {\color{white} \Company \; | \, \Index \, 2021 };
\node [above left = 0.6cm and -2cm of current page header area.north east] {\color{white} \CompanyURL};
\end{tikzpicture}}

\def\arraystretch{1.2}

\usepackage{hyperref}
\hypersetup{colorlinks=true, linkcolor=blue, filecolor=magenta, urlcolor=cyan}

% new:
\usepackage{seqsplit}
\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{\dimexpr#1\linewidth-2\tabcolsep}}

\begin{document}

\begin{longtable}{ P{0.1} P{0.2} P{0.7} }

  \# & question & answer \\
  \arrayrulecolor{brand_dark} 
  \hline
  \endhead

  \hline
  \endlastfoot
 
  \rowcolor{brand_light} 
  \multicolumn{3}{l}{tXRcRDxdKJOJHbSAAZeOCcoEPlpNzymLx} \\[0.1ex]

  \rowcolor{white}
  LRenA &
  \seqsplit{KMtMfYwfHkGLLbaZFAfRtTFj} &
  udrPJNdJGqOLzgGJjs \\
  
  \rowcolor{light-grey}
  ZCKhv &
  \seqsplit{hnjtmbuJPwyvBkbGjKJMEojGlUHREvvzwKwSSQeOEU} & 
  \seqsplit{NmEwyZowJszdELsNktHfOvSaSNmIqIDHqabyKtNzuUgReEauJpiwjppdQWrHqorhMBgopxsROQEHPs} \\

  \rowcolor{white}
  DPzyJ &
  \seqsplit{ikHnifxdnsPJNGEqvwTWjvHZ} & 
  BRiTGeOBvY  \\
  
  \rowcolor{light-grey}
  LYYiI &
  \seqsplit{ALdgFWrSKqcNpBEeDvtnZX} & 
  UtyYPYhXSSmSUzgnO \\

  \rowcolor{white}
  plBsz &
  \seqsplit{VckBeukgwVihwVXtYbAPqgzd} & 
  \seqsplit{VTEQmsYvTjinhubaqZDqVXSEdtmZVpwuYlLAIWGQbQmcRLnfhISiIdIpmhEEbGPMqoWCroWOHqJuMvCCEgtWqganBlBoJomZLvQQpSArshkAUXscRwLUyWbslXqMmEgUgdLvxnGnNcvMZgnRii}  

\end{longtable}
  
\end{document}

答案2

作为对 @Mico 的精彩回答 (+1) 的补充。只是为了说明,他的解决方案允许在表格中使用列表。对于列表的格式化,定义用于(任何)具有规定列宽的表格的新列表类型是明智的(正如您在许多关于表格中列表的答案中可以找到的那样):

\documentclass{article}
\usepackage[landscape, margin=1in]{geometry}
\usepackage{pdflscape}

\usepackage{fontspec}
%\setmainfont{Comfortaa-Bold}

\usepackage{alltt}
\usepackage{amssymb}
\usepackage[dvipsnames,table]{xcolor}
    \definecolor{light-grey}{RGB}{243, 243, 243}
    \definecolor{brand_dark}{RGB}{28, 69, 135}
    \definecolor{brand_light}{RGB}{201, 218, 248}
\usepackage{array, longtable}
    \newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{\dimexpr#1\linewidth-2\tabcolsep}}
    \renewcommand\arraystretch{1.2}
\usepackage{enumitem}
    \newlist{tabenum}{enumerate}{1}% <-- defined new enumerate list
    \setlist[tabenum]{nosep,
                      label*=\textbf{\arabic*.},
                      leftmargin=*,
                      before=\begin{minipage}[t]{\hsize}%
                                    \vspace*{-0.6\baselineskip},% <---
                      after=\end{minipage}}

    \newlist{tabitem}{itemize}{1}% <-- defined new itemize list
    \setlist[tabitem]{nosep,
                     leftmargin=*,
                     label=\textbullet,
                     before=\begin{minipage}[t]{\linewidth},    % <---
                     after=\end{minipage}                       % <---
                     }
\usepackage{datetime}
\usepackage{everypage}
\usepackage{fancyhdr}

\usepackage{pdflscape}
\usepackage{tikzpagenodes}
    \usetikzlibrary{calc,
                    positioning}
% new:
\usepackage{seqsplit}

\newcommand{\Company}{Company}
\newcommand{\CompanyURL}{company.com}
\newcommand{\Index}{QA}

\newcommand{\monthyear}{\ifcase \month \or January\or February\or March\or %
  April\or May \or June\or July\or August\or September\or October\or November\or %
  December\fi, \number \year}

% Turn on the style
\pagestyle{fancy}
% Clear the header and footer
% \fancyhead{}
\fancyfoot{}
\renewcommand{\headrulewidth}{0pt}
% Set the right side of the footer to be the page number
\fancyfoot[R]{\color{brand_dark}Page \thepage}
\fancyfoot[L]{\color{brand_dark}{\monthyear}}
\AddEverypageHook{\begin{tikzpicture}[remember picture,overlay]
\fill[brand_dark] (current page.north west) rectangle ($ (current page header area.north -| current page.east) + (0,0.5) $);
\node [above right = 0.5cm and -2cm of current page header area.north west] {\color{white} \Company \; | \, \Index \, 2021 };
\node [above left = 0.6cm and -2cm of current page header area.north east] {\color{white} \CompanyURL};
\end{tikzpicture}}

\usepackage{hyperref}
\hypersetup{colorlinks=true, linkcolor=blue, filecolor=magenta, urlcolor=cyan}
  
\begin{document}
\begin{longtable}{ P{0.1} P{0.2} P{0.7} }
  \# & question & answer \\
  \arrayrulecolor{brand_dark}
  \hline
\endhead
  \hline
\endlastfoot
% table body
  \rowcolor{brand_light}
  \multicolumn{3}{l}{tXRcRDxdKJOJHbSAAZeOCcoEPlpNzymLx} \\[0.1ex]

  \rowcolor{white}
  LRenA &
  \seqsplit{KMtMfYwfHkGLLbaZFAfRtTFj} &
  udrPJNdJGqOLzgGJjs \\

  \rowcolor{light-grey}
  ZCKhv &
  \seqsplit{hnjtmbuJPwyvBkbGjKJMEojGlUHREvvzwKwSSQeOEU} &
  \seqsplit{NmEwyZowJszdELsNktHfOvSaSNmIqIDHqabyKtNzuUgReEauJpiwjppdQWrHqorhMBgopxsROQEHPs} \\

  \rowcolor{white}
  DPzyJ &
  \seqsplit{ikHnifxdnsPJNGEqvwTWjvHZ} &
  BRiTGeOBvY  \\

  \rowcolor{light-grey}
  LYYiI &
  \seqsplit{ALdgFWrSKqcNpBEeDvtnZX} &
  UtyYPYhXSSmSUzgnO \\

  \rowcolor{white}
  plBsz &
  \seqsplit{VckBeukgwVihwVXtYbAPqgzd} &
  \seqsplit{VTEQmsYvTjinhubaqZDqVXSEdtmZVpwuYlLAIWGQbQmcRLnfhISiIdIpmhEEbGPMqoWCroWOHqJuMvCCEgtWqganBlBoJomZLvQQpSArshkAUXscRwLUyWbslXqMmEgUgdLvxnGnNcvMZgnRii}\\

  \rowcolor{light-grey}
  Item itemize  &
  \begin{tabitem}
  \item \seqsplit{ALdgFWrSKqcNpBEeDvtnZX}
  \item \seqsplit{ikHnifxdnsPJNGEqvwTWjvHZ}
  \end{tabitem} &
  UtyYPYhXSSmSUzgnO \\

  \rowcolor{white}
  Item itemize + enumerate  &
  some text before list
  \begin{tabitem}
  \item \seqsplit{ALdgFWrSKqcNpBEeDvtnZX}
  \item \seqsplit{ikHnifxdnsPJNGEqvwTWjvHZ}
  \end{tabitem} &
  \begin{tabenum}
  \item \seqsplit{VTEQmsYvTjinhubaqZDqVXSEdtmZVpwuYlLAIWGQbQmcRLnfhISiIdIpmhEEbGPMqoWCroWOHqJuMvCCEgtWqganBlBoJomZLvQQpSArshkAUXscRwLUyWbslXqMmEgUgdLvxnGnNcvMZgnRii}
  \item \seqsplit{ikHnifxdnsPJNGEqvwTWjvHZALdgFWrSKqcNpBEeDvtnZX}
  \end{tabenum}

\end{longtable}

\end{document}

在此处输入图片描述

相关内容