如何使用带有换行列的替代表格颜色?

如何使用带有换行列的替代表格颜色?

我正在处理表格,其中最后一列用命令包裹\Addbreakable。我还需要为交替行着色。但我可以执行\Addbrekable或为交替行着色。

笔记:

下面是我的示例。

\documentclass{article}
\usepackage{graphicx}
\usepackage[framemethod=TikZ]{mdframed}
\usepackage[margin=1.2cm]{geometry}
\usepackage{framed}
\usepackage{tikz}
\usepackage[T1]{fontenc}
\usepackage{hyphenat}
\usepackage{xstring}
\usepackage{forloop}
\usepackage{collcell}
\usepackage[table]{xcolor}
\definecolor{Gray}{gray}{0.9}

\definecolor{LightCyan}{rgb}{0.88,1,1}
\newsavebox\MyBreakChar%
\sbox\MyBreakChar{}% char to display the break after non char
\newsavebox\MySpaceBreakChar%
\sbox\MySpaceBreakChar{-}% char to display the break after space
\makeatletter%
\newcommand*{\BreakableChar}[1][\MyBreakChar]{%
   \leavevmode%
   \prw@zbreak%
   \discretionary{\usebox#1}{}{}%
   \prw@zbreak%
   }%

   \newcounter{index}%
   \newcommand{\AddBreakableChars}[1]{%
   \StrLen{#1 }[\stringLength]%
   \forloop[1]{index}{1}{\value{index}<\stringLength}{%
   \StrChar{#1}{\value{index}}[\currentLetter]%
   \IfStrEq{\currentLetter}{]}
   {\currentLetter\BreakableChar[\MyBreakChar]}%
        {\currentLetter}%
    }%
  }%

  \begin{document}
  \textbf{Manual Solution:}
   \newline
  \begin{mdframed}[roundcorner=10pt,leftmargin=1, rightmargin=1,    linecolor=yellow,outerlinewidth=.5, innerleftmargin=8, innertopmargin=8,innerbottommargin=8, everyline = true]
   %\begin{tabular}{>{\rule{0pt}{.5cm}}>{\arraybackslash}p{8cm}c|>  {\arraybackslash}p{1cm}c|>{\arraybackslash}p{1cm}c|>{\arraybackslash}p{1cm}c|>{\arraybackslash}p{2cm}c} 

  \rowcolors{2}{gray!25}{white}
  \begin{tabular}{p{2cm}p{2cm}p{6cm}p{2cm}p{2cm}} 
  \rowcolor{yellow}
    Col1 & Col2 & Col3 & Col4 & Col5\\
    test & data & data & data &\AddBreakableChars{[1][7][3][8][5][9][7][8][9][89][100]}\\
    test & data & data & data &\AddBreakableChars{[1][7][3][8][5][9][7][8][9][89][100]}\\

   \end{tabular}
   \end{mdframed}
   \vspace{1mm} 
   \begin{mdframed}[roundcorner=10pt,leftmargin=1.0, rightmargin=1.0, linecolor=red,outerlinewidth=.5, innerleftmargin=8, innertopmargin=8,innerbottommargin=8] 

   \centering 
\rowcolors{2}{green}{white} 
\begin{tabular}{>{\rule{0pt}{.5cm}}>{\arraybackslash}p{2.25 cm}>{\arraybackslash}p{2.25cm}c>{\arraybackslash}p{8cm}c>{\arraybackslash}p{2cm}c} 
\rowcolor{yellow} \textbf{Col1} &\textbf{Col2} &\textbf{Col3} &\textbf{Col4} &\textbf{Col5}\\ 
\hline 
ERBB3  &  TT2292239&  TQWT  &  Grammar. a grammatical unit of one or more words that expresses an independent statement, question, request, command, exclamation, etc.  & \AddBreakableChars{[1][2][3][4][5][6][7][8][9][10][11][12][13][14]} 
\\ 
  &  TT2296336&  CQWG  &  Grammar. a grammatical unit of one or more words that expresses an independent statement, question, request, command, exclamation, etc.  & \AddBreakableChars{[1][2][3][4][5][6][7][8][9][10][11][12][13][14]} 
\\ 
\end{tabular} 
\end{mdframed} 
\end{document}

请帮我解决这个问题。我试过很多方法,但似乎都没有效果。

答案1

上一个版本无法正常工作的原因是您的表格列规范不正确。一旦您设置了列,p{2.0cm}一切就都正常工作了:

在此处输入图片描述

笔记:

  • 我重新格式化了列规范,使其更易于阅读。在您的版本中,您应该注意到有额外的c列类型规范。
  • 我还移动了加载,\usepackage[table]{xcolor}以便不会发生包选项冲突。
  • 两个@{}是针对表格列规范的,目的是消除第一列之前和最后一列之后的额外列间距。这样可以消除警告Overfull \hbox。实际上,您可以只l对第 1、2 和 3 列使用,这样可以生成一个不太宽的表格。
  • 包裹showframe用于显示页边距。

代码:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{showframe}
\usepackage{graphicx}
\usepackage[framemethod=TikZ]{mdframed}
\usepackage[margin=1.2cm]{geometry}
\usepackage{framed}
\usepackage{tikz}
\usepackage[T1]{fontenc}
\usepackage{hyphenat}
\usepackage{xstring}
\usepackage{forloop}
\usepackage{collcell}
\definecolor{Gray}{gray}{0.9}

\definecolor{LightCyan}{rgb}{0.88,1,1}
\newsavebox\MyBreakChar%
\sbox\MyBreakChar{}% char to display the break after non char
\newsavebox\MySpaceBreakChar%
\sbox\MySpaceBreakChar{-}% char to display the break after space
\makeatletter%
\newcommand*{\BreakableChar}[1][\MyBreakChar]{%
   \leavevmode%
   \prw@zbreak%
   \discretionary{\usebox#1}{}{}%
   \prw@zbreak%
   }%

   \newcounter{index}%
   \newcommand{\AddBreakableChars}[1]{%
   \StrLen{#1 }[\stringLength]%
   \forloop[1]{index}{1}{\value{index}<\stringLength}{%
   \StrChar{#1}{\value{index}}[\currentLetter]%
   \IfStrEq{\currentLetter}{]}
   {\currentLetter\BreakableChar[\MyBreakChar]}%
        {\currentLetter}%
    }%
  }%

  \begin{document}
  \begin{mdframed}[roundcorner=10pt,leftmargin=1, rightmargin=1,    linecolor=yellow,outerlinewidth=.5, innerleftmargin=8, innertopmargin=8,innerbottommargin=8, everyline = true]
   %\begin{tabular}{>{\rule{0pt}{.5cm}}>{\arraybackslash}p{8cm}c|>  {\arraybackslash}p{1cm}c|>{\arraybackslash}p{1cm}c|>{\arraybackslash}p{1cm}c|>{\arraybackslash}p{2cm}c} 

  \rowcolors{2}{gray!25}{white}
  \begin{tabular}{p{2cm}p{2cm}p{6cm}p{2cm}p{2cm}} 
  \rowcolor{yellow}
    Col1 & Col2 & Col3 & Col4 & Col5\\
    test & data & data & data &\AddBreakableChars{[1][7][3][8][5][9][7][8][9][89][100]}\\
    test & data & data & data &\AddBreakableChars{[1][7][3][8][5][9][7][8][9][89][100]}\\

   \end{tabular}
   \end{mdframed}
   \vspace{1mm} 
   \begin{mdframed}[roundcorner=10pt,leftmargin=1.0, rightmargin=1.0, linecolor=red,outerlinewidth=.5, innerleftmargin=8, innertopmargin=8,innerbottommargin=8] 

   \centering 
\rowcolors{2}{green}{white} 
\begin{tabular}{@{}
    >{\rule{0pt}{.5cm}}>{\arraybackslash}p{2.25 cm}
    >{\arraybackslash}p{2.25cm}
    >{\arraybackslash}p{2cm}
    >{\arraybackslash}p{8cm}
    >{\arraybackslash}p{2cm}@{}
    } 
\rowcolor{yellow} \textbf{Col1} &\textbf{Col2} &\textbf{Col3} &\textbf{Col4} &\textbf{Col5}\\ 
\hline 
ERBB3  &  TT2292239&  TQWT  &  Grammar. a grammatical unit of one or more words that expresses an independent statement, question, request, command, exclamation, etc.  & \AddBreakableChars{[1][2][3][4][5][6][7][8][9][10][11][12][13][14]} 
\\ 
  &  TT2296336&  CQWG  &  Grammar. a grammatical unit of one or more words that expresses an independent statement, question, request, command, exclamation, etc.  & \AddBreakableChars{[1][2][3][4][5][6][7][8][9][10][11][12][13][14]} 
\\ 
\end{tabular} 
\end{mdframed} 
\end{document}

相关内容