透過宏為 longtable 行著色

透過宏為 longtable 行著色

來自這裡:前執行緒,我專注於一個問題並提供一個MVE。我的問題是:我想透過巨集對長表中的行使用相同的顏色進行著色,以區分從外部觸發的兩個不同的建構(內部版本和公共版本)。透過 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使用 option[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

一個想法是創建一個“幻影額外列”,並在其中給出一個空的多行命令,僅包含行顏色...如果您發現困難...請我幫助您...(我嘗試了一點,但它是進入程式碼有點複雜)。我認為你可以很容易地做到這一點(你已經知道你的程式碼是如何運作的)

相關內容