通过宏对长表行进行着色

通过宏对长表行进行着色

来自这里:前一个线程,我专注于一个问题并提供一个 MVE。我的问题是:我想通过宏为 longtable 中的行着色,使其具有相同的颜色,以区分从外部触发的两个不同构建(内部和公共版本)。通过 rowcolor 执行此操作,只有第一行被着色,其他行仍为默认背景颜色。我可以对每一行使用该命令,但我只想有一个命令。

谢谢。

MWE 是:

\documentclass[a4paper,11pt,index=totoc]{scrreprt}

% --- out of style file

% \def\isRelease{1}

\usepackage{colortbl} 
\usepackage{array}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage[justification=centering,font=scriptsize,labelfont=bf,position=bottom]{caption}
\usepackage{xifthen} 
\usepackage{color}

\newcommand{\headcol}{\rowcolor{black}}
\newcommand{\internCol}{\rowcolor{yellow}}

\newcommand{\startLongTable}%
{%
  \renewcommand{\arraystretch}{1.2}%
  \setlength\LTleft{0pt plus \textwidth}%
  \setlength\LTright{0pt plus \textwidth}%
}

\newcommand{\tableRowEnd}%
{%
\\ \midrule%
}

\newcommand{\lastTableRowEnd}%
{%
\\ \addlinespace%
}

\newcommand{\preTableHeaderLine}{\arrayrulecolor{black}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
            \arrayrulecolor{black}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
\newcommand{\postTableHeaderLine}{\arrayrulecolor{black}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
            \arrayrulecolor{black}}

\newcommand{\tableIntern}[2]%
{%
  \ifdefined\isRelease%
    #1%
  \else%
    \tableRowEnd%
    \internCol#2%
  \fi%
}

% --- --------------------------

\begin{document}

\startLongTable
\begin{longtable}{ >{\centering}p{0.1\textwidth-2\tabcolsep} 
                                p{0.2\textwidth-2\tabcolsep} 
                                p{0.4\textwidth-2\tabcolsep} 
                   >{\centering}p{0.2\textwidth-2\tabcolsep}
    >{\centering\arraybackslash}p{0.1\textwidth-2\tabcolsep}
                 }
\preTableHeaderLine
\headcol \color{white} Field & \color{white} Name & \color{white} Description & \color{white} Format & \color{white} \# \\
\postTableHeaderLine \addlinespace
\endhead
1 & Field1 & Name1 & A & 2 \tableRowEnd
2 & Field2 & Name2 & B & 3
\tableIntern{\lastTableRowEnd}
{
3 & Field3 & Name3 & C & 3 \tableRowEnd
4 & Field4 & Name4 & C & 4 \lastTableRowEnd
}
\bottomrule
\caption{stackoverflow MWE}
\label{table:stackoverflowMWE}
\end{longtable}


\end{document}

在此处输入图片描述

答案1

您可以xcolor使用选项来加载[table]colortbl它会使用命令来扩展后者,\rowcolors{no of 1st coloured row}{odd rows colour}{even rows colour}如果您为偶数行和奇数行选择相同的颜色,该命令会执行您想要的操作。

顺便提一句:我认为彩色行之间的水平线看起来不太好看。您不妨用 替换它们\addlinespace。此外,如果您想要没有白色条纹的水平线,您可以设置\above/belowrulesep0pt并在包中添加一些(彩色)填充cellspace。最后一点:在我看来,黑色背景上的白色列标题使用粗体字体更易读。

\documentclass[a4paper,11pt,index=totoc]{scrreprt}

% --- out of style file

% \def\isRelease{1}

\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage[justification=centering,font=scriptsize,labelfont=bf,position=bottom]{caption}
\usepackage{xifthen}

\newcommand{\headcol}{\rowcolor{black}}
\newcommand{\internCol}{\rowcolor{yellow}}

\newcommand{\startLongTable}%
{%
  \renewcommand{\arraystretch}{1.2}%
  \setlength\LTleft{0pt plus \textwidth}%
  \setlength\LTright{0pt plus \textwidth}%
}

\newcommand{\tableRowEnd}%
{%
\\ \midrule%
}

\newcommand{\lastTableRowEnd}%
{%
\\ \addlinespace%
}

\newcommand{\preTableHeaderLine}{\arrayrulecolor{black}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
            \arrayrulecolor{black}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
\newcommand{\postTableHeaderLine}{\arrayrulecolor{black}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
            \arrayrulecolor{black}}

\newcommand{\tableIntern}[2]%
{%
  \ifdefined\isRelease%
    #1%
  \else%
    \tableRowEnd%
    \internCol#2%
  \fi%
}

% --- --------------------------

\begin{document}

\startLongTable\rowcolors{2}{yellow}{yellow}
\begin{longtable}{ >{\centering}p{0.1\textwidth-2\tabcolsep}
                                p{0.2\textwidth-2\tabcolsep}
                                p{0.4\textwidth-2\tabcolsep}
                   >{\centering}p{0.2\textwidth-2\tabcolsep}
    >{\centering\arraybackslash}p{0.1\textwidth-2\tabcolsep}
                 }
\preTableHeaderLine
\headcol \color{white}\bfseries Field & \color{white}\bfseries Name & \color{white}\bfseries Description & \color{white}\bfseries Format & \color{white}\bfseries \# \\
\postTableHeaderLine \addlinespace
\endhead
1 & Field1 & Name1 & A & 2 \tableRowEnd
2 & Field2 & Name2 & B & 3
\tableIntern{\lastTableRowEnd}
{
3 & Field3 & Name3 & C & 3 \tableRowEnd
4 & Field4 & Name4 & C & 4 \lastTableRowEnd
}
\hiderowcolors
\bottomrule
\caption{stackoverflow MWE}
\label{table:stackoverflowMWE}
\end{longtable}

\end{document} 

在此处输入图片描述

编辑

另一段代码,使用和\showRowcolors开关\hideRowcolors来激活和停用行着色:

\documentclass[a4paper,11pt,index=totoc]{scrreprt}
    % --- out of style file

    % \def\isRelease{1}

    \usepackage[table]{xcolor}
    \usepackage{array}
    \usepackage{longtable}
    \usepackage{booktabs}
    \usepackage[justification=centering,font=scriptsize,labelfont=bf,position=bottom]{caption}
    \usepackage{xifthen}

    \newcommand{\headcol}{\rowcolor{black}}
    \newcommand{\internCol}{\rowcolor{yellow}}

    \newcommand{\startLongTable}%
    {%
      \renewcommand{\arraystretch}{1.2}%
      \setlength\LTleft{0pt plus \textwidth}%
      \setlength\LTright{0pt plus \textwidth}%
    }

    \newcommand{\tableRowEnd}%
    {%
    \\ \midrule%
    }

    \newcommand{\lastTableRowEnd}%
    {%
    \\ \addlinespace%
    }

    \newcommand{\preTableHeaderLine}{\arrayrulecolor{black}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
\specialrule{\belowrulesep}{0pt}{0pt}%
}
    \newcommand{\postTableHeaderLine}{\arrayrulecolor{black}\specialrule{\aboverulesep}{0pt}{0pt}%
                \specialrule{\lightrulewidth}{0pt}{0pt}%
                }

    \newcommand{\tableIntern}[2]%
    {%
      \ifdefined\isRelease%
        #1%
      \else%
        \tableRowEnd%
        \internCol#2%
      \fi%
    }

    % --- --------------------------

    \begin{document}
    \startLongTable\rowcolors{2}{yellow}{yellow}
    \begin{longtable}{ >{\centering}p{0.1\textwidth-2\tabcolsep}
                                    p{0.2\textwidth-2\tabcolsep}
                                    p{0.4\textwidth-2\tabcolsep}
                       >{\centering}p{0.2\textwidth-2\tabcolsep}
        >{\centering\arraybackslash}p{0.1\textwidth-2\tabcolsep}
                     }
    \preTableHeaderLine
    \headcol \color{white}\bfseries Field & \color{white}\bfseries Name & \color{white}\bfseries Description & \color{white}\bfseries Format & \color{white}\bfseries \# \\
    \postTableHeaderLine% \addlinespace
    \endhead
    \hiderowcolors 1 & Field1 & Name1 & A & 2 \tableRowEnd
    2 & Field2 & Name2 & B & 3
    \tableIntern{\lastTableRowEnd}
    {
    3 & Field3 & Name3 & C & 3 \tableRowEnd
    4 & Field4 & Name4 & C & 4 \tableRowEnd
    }
    \hiderowcolors 5 & Field5 & Name5 & D & 2 \tableRowEnd
    \showrowcolors 6 & Field6 & Name6 & E & 3\lastTableRowEnd
        \hiderowcolors
    \bottomrule
    \caption{stackoverflow MWE}
    \label{table:stackoverflowMWE}
    \end{longtable}

    \end{document} 

在此处输入图片描述

答案2

我不会在参数中添加表格行。这相当脆弱。我会使用类似\startrelease...的语法\stoprelease

除此之外:如果你的\tableIntern部分以不同的行命令结束,那么类似这样的操作可能会有效:

\documentclass[a4paper,11pt,index=totoc]{scrreprt}

% --- out of style file

% \def\isRelease{1}

\usepackage{colortbl}
\usepackage{array}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage[justification=centering,font=scriptsize,labelfont=bf,position=bottom]{caption}
\usepackage{xifthen}
\usepackage{color}

\newcommand{\headcol}{\rowcolor{black}}
\newcommand{\internCol}{\rowcolor{yellow}}

\newcommand{\startLongTable}%
{%
  \renewcommand{\arraystretch}{1.2}%
  \setlength\LTleft{0pt plus \textwidth}%
  \setlength\LTright{0pt plus \textwidth}%
}

\newcommand{\tableRowEnd}%
{%
\\ \midrule%
}

\newcommand{\lastTableRowEnd}%
{%
\\ \addlinespace%
}

\newcommand{\preTableHeaderLine}{\arrayrulecolor{black}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
            \arrayrulecolor{black}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
\newcommand{\postTableHeaderLine}{\arrayrulecolor{black}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
            \arrayrulecolor{black}}

\newcommand{\tableIntern}[2]%
{%
  \ifdefined\isRelease%
    #1%
  \else%
    \gdef \tableRowEnd{\\\midrule\internCol}%
    \gdef \lastTableRowEnd{\gdef\tableRowEnd{\\\midrule}\\\addlinespace}%
    \tableRowEnd%
    \internCol#2%
  \fi%
}

% --- --------------------------

\begin{document}

\startLongTable
\begin{longtable}{ >{\centering}p{0.1\textwidth-2\tabcolsep}
                                p{0.2\textwidth-2\tabcolsep}
                                p{0.4\textwidth-2\tabcolsep}
                   >{\centering}p{0.2\textwidth-2\tabcolsep}
    >{\centering\arraybackslash}p{0.1\textwidth-2\tabcolsep}
                 }
\preTableHeaderLine
\headcol \color{white} Field & \color{white} Name & \color{white} Description & \color{white} Format & \color{white} \# \\
\postTableHeaderLine \addlinespace
\endhead
1 & Field1 & Name1 & A & 2 \tableRowEnd
2 & Field2 & Name2 & B & 3
\tableIntern{\lastTableRowEnd}
{
3 & Field3 & Name3 & C & 3 \tableRowEnd
4 & Field4 & Name4 & C & 4 \lastTableRowEnd
}
5 & text & Name3 & C & 3 \tableRowEnd
6 & test\\
\bottomrule
\caption{stackoverflow MWE}
\label{table:stackoverflowMWE}
\end{longtable}


\end{document}

答案3

一个想法是创建一个“幻影额外列”,并在其中给出一个只有行颜色的空多行命令...如果您遇到困难...请让我帮助您...(我尝试了一下,但进入您的代码有点复杂)。我认为您可以很容易地做到这一点(您已经知道您的代码是如何工作的)

相关内容